2016-11-24 00:02:36 -06:00
|
|
|
<?php namespace Config;
|
|
|
|
|
|
|
|
use CodeIgniter\Config\BaseConfig;
|
|
|
|
|
2017-04-02 21:57:04 -05:00
|
|
|
class Format extends BaseConfig
|
2016-11-24 00:02:36 -06:00
|
|
|
{
|
2017-02-11 19:19:05 +09: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.
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
public $supportedResponseFormats = [
|
|
|
|
'application/json',
|
2018-11-10 02:57:39 -02:00
|
|
|
'application/xml', // machine-readable XML
|
|
|
|
'text/xml', // human-readable XML
|
2017-02-11 19:19:05 +09:00
|
|
|
];
|
2016-11-24 00:02:36 -06:00
|
|
|
|
2017-02-11 19:19:05 +09: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.
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
public $formatters = [
|
2017-04-02 21:57:04 -05:00
|
|
|
'application/json' => \CodeIgniter\Format\JSONFormatter::class,
|
2017-07-05 14:42:45 -07:00
|
|
|
'application/xml' => \CodeIgniter\Format\XMLFormatter::class,
|
2018-11-10 02:57:39 -02:00
|
|
|
'text/xml' => \CodeIgniter\Format\XMLFormatter::class,
|
2017-02-11 19:19:05 +09:00
|
|
|
];
|
2020-07-12 20:10:45 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Formatters Options
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
| Additional Options to adjust default formatters behaviour.
|
|
|
|
| For each mime type, list the additional options that should be used.
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
public $formatterOptions = [
|
2020-07-13 19:27:55 +02:00
|
|
|
'application/json' => JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES,
|
|
|
|
'application/xml' => 0,
|
|
|
|
'text/xml' => 0,
|
2020-07-12 20:10:45 +02:00
|
|
|
];
|
2017-02-11 19:19:05 +09:00
|
|
|
//--------------------------------------------------------------------
|
2016-11-24 00:02:36 -06:00
|
|
|
|
2017-02-11 19:19:05 +09:00
|
|
|
/**
|
|
|
|
* A Factory method to return the appropriate formatter for the given mime type.
|
|
|
|
*
|
|
|
|
* @param string $mime
|
|
|
|
*
|
2017-08-04 08:26:15 -03:00
|
|
|
* @return \CodeIgniter\Format\FormatterInterface
|
2017-02-11 19:19:05 +09:00
|
|
|
*/
|
|
|
|
public function getFormatter(string $mime)
|
|
|
|
{
|
|
|
|
if (! array_key_exists($mime, $this->formatters))
|
|
|
|
{
|
2018-11-10 02:57:39 -02:00
|
|
|
throw new \InvalidArgumentException('No Formatter defined for mime type: ' . $mime);
|
2017-02-11 19:19:05 +09:00
|
|
|
}
|
2016-11-24 00:02:36 -06:00
|
|
|
|
2017-02-11 19:19:05 +09:00
|
|
|
$class = $this->formatters[$mime];
|
2016-11-24 00:02:36 -06:00
|
|
|
|
2017-02-11 19:19:05 +09:00
|
|
|
if (! class_exists($class))
|
|
|
|
{
|
2018-11-10 02:57:39 -02:00
|
|
|
throw new \BadMethodCallException($class . ' is not a valid Formatter.');
|
2017-02-11 19:19:05 +09:00
|
|
|
}
|
2016-11-24 00:02:36 -06:00
|
|
|
|
2017-02-11 19:19:05 +09:00
|
|
|
return new $class();
|
|
|
|
}
|
2016-11-24 00:02:36 -06:00
|
|
|
|
2017-02-11 19:19:05 +09:00
|
|
|
//--------------------------------------------------------------------
|
2016-11-24 00:02:36 -06:00
|
|
|
|
|
|
|
}
|