Format.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php namespace Config;
  2. use CodeIgniter\Config\BaseConfig;
  3. class Format extends BaseConfig
  4. {
  5. /*
  6. |--------------------------------------------------------------------------
  7. | Available Response Formats
  8. |--------------------------------------------------------------------------
  9. |
  10. | When you perform content negotiation with the request, these are the
  11. | available formats that your application supports. This is currently
  12. | only used with the API\ResponseTrait. A valid Formatter must exist
  13. | for the specified format.
  14. |
  15. | These formats are only checked when the data passed to the respond()
  16. | method is an array.
  17. |
  18. */
  19. public $supportedResponseFormats = [
  20. 'application/json',
  21. 'application/xml', // machine-readable XML
  22. 'text/xml', // human-readable XML
  23. ];
  24. /*
  25. |--------------------------------------------------------------------------
  26. | Formatters
  27. |--------------------------------------------------------------------------
  28. |
  29. | Lists the class to use to format responses with of a particular type.
  30. | For each mime type, list the class that should be used. Formatters
  31. | can be retrieved through the getFormatter() method.
  32. |
  33. */
  34. public $formatters = [
  35. 'application/json' => \CodeIgniter\Format\JSONFormatter::class,
  36. 'application/xml' => \CodeIgniter\Format\XMLFormatter::class,
  37. 'text/xml' => \CodeIgniter\Format\XMLFormatter::class,
  38. ];
  39. /*
  40. |--------------------------------------------------------------------------
  41. | Formatters Options
  42. |--------------------------------------------------------------------------
  43. |
  44. | Additional Options to adjust default formatters behaviour.
  45. | For each mime type, list the additional options that should be used.
  46. |
  47. */
  48. public $formatterOptions = [
  49. 'application/json' => JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES,
  50. 'application/xml' => 0,
  51. 'text/xml' => 0,
  52. ];
  53. //--------------------------------------------------------------------
  54. /**
  55. * A Factory method to return the appropriate formatter for the given mime type.
  56. *
  57. * @param string $mime
  58. *
  59. * @return \CodeIgniter\Format\FormatterInterface
  60. */
  61. public function getFormatter(string $mime)
  62. {
  63. if (! array_key_exists($mime, $this->formatters))
  64. {
  65. throw new \InvalidArgumentException('No Formatter defined for mime type: ' . $mime);
  66. }
  67. $class = $this->formatters[$mime];
  68. if (! class_exists($class))
  69. {
  70. throw new \BadMethodCallException($class . ' is not a valid Formatter.');
  71. }
  72. return new $class();
  73. }
  74. //--------------------------------------------------------------------
  75. }