2020-08-04 11:25:22 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Config;
|
2020-05-27 18:46:16 +02:00
|
|
|
|
|
|
|
use CodeIgniter\Config\BaseConfig;
|
|
|
|
|
|
|
|
class Format extends BaseConfig
|
|
|
|
{
|
2020-06-10 15:00:12 +00:00
|
|
|
/*
|
2020-08-04 11:25:22 +00:00
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Available Response Formats
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
| When you perform content negotiation with the request, these are the
|
|
|
|
| available formats that your application supports. This is currently
|
|
|
|
| only used with the API\ResponseTrait. A valid Formatter must exist
|
|
|
|
| for the specified format.
|
|
|
|
|
|
|
|
|
| These formats are only checked when the data passed to the respond()
|
|
|
|
| method is an array.
|
|
|
|
|
|
|
|
|
*/
|
2020-06-10 15:00:12 +00:00
|
|
|
public $supportedResponseFormats = [
|
|
|
|
'application/json',
|
|
|
|
'application/xml', // machine-readable XML
|
|
|
|
'text/xml', // human-readable XML
|
|
|
|
];
|
2020-05-27 18:46:16 +02:00
|
|
|
|
2020-06-10 15:00:12 +00:00
|
|
|
/*
|
2020-08-04 11:25:22 +00:00
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Formatters
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
| Lists the class to use to format responses with of a particular type.
|
|
|
|
| For each mime type, list the class that should be used. Formatters
|
|
|
|
| can be retrieved through the getFormatter() method.
|
|
|
|
|
|
|
|
|
*/
|
2020-06-10 15:00:12 +00:00
|
|
|
public $formatters = [
|
|
|
|
'application/json' => \CodeIgniter\Format\JSONFormatter::class,
|
|
|
|
'application/xml' => \CodeIgniter\Format\XMLFormatter::class,
|
|
|
|
'text/xml' => \CodeIgniter\Format\XMLFormatter::class,
|
|
|
|
];
|
2020-05-27 18:46:16 +02:00
|
|
|
|
2020-06-10 15:00:12 +00:00
|
|
|
//--------------------------------------------------------------------
|
2020-05-27 18:46:16 +02:00
|
|
|
|
2020-06-10 15:00:12 +00:00
|
|
|
/**
|
|
|
|
* A Factory method to return the appropriate formatter for the given mime type.
|
|
|
|
*
|
|
|
|
* @param string $mime
|
|
|
|
*
|
|
|
|
* @return \CodeIgniter\Format\FormatterInterface
|
|
|
|
*/
|
|
|
|
public function getFormatter(string $mime)
|
|
|
|
{
|
|
|
|
if (!array_key_exists($mime, $this->formatters)) {
|
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
'No Formatter defined for mime type: ' . $mime
|
|
|
|
);
|
|
|
|
}
|
2020-05-27 18:46:16 +02:00
|
|
|
|
2020-06-10 15:00:12 +00:00
|
|
|
$class = $this->formatters[$mime];
|
2020-05-27 18:46:16 +02:00
|
|
|
|
2020-06-10 15:00:12 +00:00
|
|
|
if (!class_exists($class)) {
|
|
|
|
throw new \BadMethodCallException(
|
|
|
|
$class . ' is not a valid Formatter.'
|
|
|
|
);
|
|
|
|
}
|
2020-05-27 18:46:16 +02:00
|
|
|
|
2020-06-10 15:00:12 +00:00
|
|
|
return new $class();
|
|
|
|
}
|
2020-05-27 18:46:16 +02:00
|
|
|
|
2020-06-10 15:00:12 +00:00
|
|
|
//--------------------------------------------------------------------
|
2020-05-27 18:46:16 +02:00
|
|
|
}
|