1 | <?php |
||
22 | class Parser |
||
23 | { |
||
24 | const METHOD_ALL = 'ALL'; |
||
25 | |||
26 | /** |
||
27 | * Content negotiator. |
||
28 | * |
||
29 | * @var Negotiator |
||
30 | */ |
||
31 | protected $negotiator; |
||
32 | |||
33 | /** |
||
34 | * Registered decoders. |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $decoders = []; |
||
39 | |||
40 | /** |
||
41 | * Parser constructor. |
||
42 | * |
||
43 | * @param Negotiator $negotiator |
||
44 | */ |
||
45 | public function __construct(Negotiator $negotiator) |
||
49 | |||
50 | /** |
||
51 | * Add body decoder. |
||
52 | * |
||
53 | * @param Decoder $decoder |
||
54 | * @param string|array $methods |
||
55 | * |
||
56 | * @throws \InvalidArgumentException |
||
57 | * @throws \RuntimeException |
||
58 | */ |
||
59 | public function addDecoder(Decoder $decoder, $methods = self::METHOD_ALL) |
||
84 | |||
85 | /** |
||
86 | * Add decoder for POST requests. |
||
87 | * |
||
88 | * @param Decoder $decoder |
||
89 | */ |
||
90 | public function addPostDecoder(Decoder $decoder) |
||
94 | |||
95 | /** |
||
96 | * Add decoder for PUT requests. |
||
97 | * |
||
98 | * @param Decoder $decoder |
||
99 | */ |
||
100 | public function addPutDecoder(Decoder $decoder) |
||
104 | |||
105 | /** |
||
106 | * Add decoder for PATCH requests. |
||
107 | * |
||
108 | * @param Decoder $decoder |
||
109 | */ |
||
110 | public function addPatchDecoder(Decoder $decoder) |
||
114 | |||
115 | /** |
||
116 | * Get decoders for a method. |
||
117 | * |
||
118 | * @param string $method |
||
119 | * |
||
120 | * @return Decoder[] |
||
121 | */ |
||
122 | public function getDecoders($method = self::METHOD_ALL) |
||
144 | |||
145 | /** |
||
146 | * Flatten array. |
||
147 | * |
||
148 | * @param array $array |
||
149 | * |
||
150 | * @return array |
||
151 | */ |
||
152 | private function flattenArray(array $array) |
||
153 | { |
||
154 | return array_reduce( |
||
155 | $array, |
||
156 | function ($carry, $item) { |
||
157 | return is_array($item) ? array_merge($carry, $this->flattenArray($item)) : array_merge($carry, [$item]); |
||
158 | }, |
||
159 | [] |
||
160 | ); |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Invoke middleware. |
||
165 | * |
||
166 | * @param ServerRequestInterface $request |
||
167 | * @param ResponseInterface $response |
||
168 | * @param callable $next |
||
169 | * |
||
170 | * @return ResponseInterface |
||
171 | */ |
||
172 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
||
190 | |||
191 | /** |
||
192 | * Negotiate content type. |
||
193 | * |
||
194 | * @param string $contentType |
||
195 | * @param array $priorities |
||
196 | * |
||
197 | * @return string |
||
198 | */ |
||
199 | protected function negotiate($contentType, array $priorities) |
||
218 | |||
219 | /** |
||
220 | * Does HTTP method carry body content. |
||
221 | * |
||
222 | * @param string $method |
||
223 | * |
||
224 | * @return bool |
||
225 | */ |
||
226 | private function methodCarriesBody($method) |
||
230 | } |
||
231 |