Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php namespace Config;
use CodeIgniter\Config\BaseConfig;
class Format extends BaseConfig
{
/*
|--------------------------------------------------------------------------
| 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',
'application/xml', // machine-readable XML
'text/xml', // human-readable XML
];
/*
|--------------------------------------------------------------------------
| 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 = [
'application/json' => \CodeIgniter\Format\JSONFormatter::class,
'application/xml' => \CodeIgniter\Format\XMLFormatter::class,
'text/xml' => \CodeIgniter\Format\XMLFormatter::class,
];
//--------------------------------------------------------------------
/**
* 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);
}
$class = $this->formatters[$mime];
if (! class_exists($class))
{
throw new \BadMethodCallException($class . ' is not a valid Formatter.');
}
return new $class();
}
//--------------------------------------------------------------------
}