1 | <?php |
||
21 | final class DecoderPlugin implements Plugin |
||
22 | { |
||
23 | /** |
||
24 | * @var bool Whether this plugin decode stream with value in the Content-Encoding header (default to true). |
||
25 | * |
||
26 | * If set to false only the Transfer-Encoding header will be used |
||
27 | */ |
||
28 | private $useContentEncoding; |
||
29 | |||
30 | /** |
||
31 | * @param array $config { |
||
32 | * |
||
33 | * @var bool $use_content_encoding Whether this plugin should look at the Content-Encoding header first or only at the Transfer-Encoding (defaults to true). |
||
34 | * } |
||
35 | */ |
||
36 | 6 | public function __construct(array $config = []) |
|
37 | { |
||
38 | 6 | $resolver = new OptionsResolver(); |
|
39 | 6 | $resolver->setDefaults([ |
|
40 | 6 | 'use_content_encoding' => true, |
|
41 | 6 | ]); |
|
42 | 6 | $resolver->setAllowedTypes('use_content_encoding', 'bool'); |
|
43 | 6 | $options = $resolver->resolve($config); |
|
44 | |||
45 | 6 | $this->useContentEncoding = $options['use_content_encoding']; |
|
46 | 6 | } |
|
47 | |||
48 | /** |
||
49 | * {@inheritdoc} |
||
50 | */ |
||
51 | 4 | public function handleRequest(RequestInterface $request, callable $next, callable $first) |
|
65 | |||
66 | /** |
||
67 | * Decode a response body given its Transfer-Encoding or Content-Encoding value. |
||
68 | * |
||
69 | * @param ResponseInterface $response Response to decode |
||
70 | * |
||
71 | * @return ResponseInterface New response decoded |
||
72 | */ |
||
73 | 4 | private function decodeResponse(ResponseInterface $response) |
|
83 | |||
84 | /** |
||
85 | * Decode a response on a specific header (content encoding or transfer encoding mainly). |
||
86 | * |
||
87 | * @param string $headerName Name of the header |
||
88 | * @param ResponseInterface $response Response |
||
89 | * |
||
90 | * @return ResponseInterface A new instance of the response decoded |
||
91 | */ |
||
92 | 4 | private function decodeOnEncodingHeader($headerName, ResponseInterface $response) |
|
115 | |||
116 | /** |
||
117 | * Decorate a stream given an encoding. |
||
118 | * |
||
119 | * @param string $encoding |
||
120 | * @param StreamInterface $stream |
||
121 | * |
||
122 | * @return StreamInterface|false A new stream interface or false if encoding is not supported |
||
123 | */ |
||
124 | 3 | private function decorateStream($encoding, StreamInterface $stream) |
|
140 | } |
||
141 |