Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / zendframework / zend-diactoros / src / CallbackStream.php
1 <?php
2 /**
3  * Zend Framework (http://framework.zend.com/)
4  *
5  * @see       http://github.com/zendframework/zend-diactoros for the canonical source repository
6  * @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (http://www.zend.com)
7  * @license   https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
8  */
9
10 namespace Zend\Diactoros;
11
12 use InvalidArgumentException;
13 use Psr\Http\Message\StreamInterface;
14 use RuntimeException;
15
16 use function array_key_exists;
17
18 use const SEEK_SET;
19
20 /**
21  * Implementation of PSR HTTP streams
22  */
23 class CallbackStream implements StreamInterface
24 {
25     /**
26      * @var callable|null
27      */
28     protected $callback;
29
30     /**
31      * @param callable $callback
32      * @throws InvalidArgumentException
33      */
34     public function __construct(callable $callback)
35     {
36         $this->attach($callback);
37     }
38
39     /**
40      * {@inheritdoc}
41      */
42     public function __toString()
43     {
44         return $this->getContents();
45     }
46
47     /**
48      * {@inheritdoc}
49      */
50     public function close()
51     {
52         $this->callback = null;
53     }
54
55     /**
56      * {@inheritdoc}
57      */
58     public function detach()
59     {
60         $callback = $this->callback;
61         $this->callback = null;
62         return $callback;
63     }
64
65     /**
66      * Attach a new callback to the instance.
67      *
68      * @param callable $callback
69      * @throws InvalidArgumentException for callable callback
70      */
71     public function attach(callable $callback)
72     {
73         $this->callback = $callback;
74     }
75
76     /**
77      * {@inheritdoc}
78      */
79     public function getSize()
80     {
81     }
82
83     /**
84      * {@inheritdoc}
85      */
86     public function tell()
87     {
88         throw new RuntimeException('Callback streams cannot tell position');
89     }
90
91     /**
92      * {@inheritdoc}
93      */
94     public function eof()
95     {
96         return empty($this->callback);
97     }
98
99     /**
100      * {@inheritdoc}
101      */
102     public function isSeekable()
103     {
104         return false;
105     }
106
107     /**
108      * {@inheritdoc}
109      */
110     public function seek($offset, $whence = SEEK_SET)
111     {
112         throw new RuntimeException('Callback streams cannot seek position');
113     }
114
115     /**
116      * {@inheritdoc}
117      */
118     public function rewind()
119     {
120         throw new RuntimeException('Callback streams cannot rewind position');
121     }
122
123     /**
124      * {@inheritdoc}
125      */
126     public function isWritable()
127     {
128         return false;
129     }
130
131     /**
132      * {@inheritdoc}
133      */
134     public function write($string)
135     {
136         throw new RuntimeException('Callback streams cannot write');
137     }
138
139     /**
140      * {@inheritdoc}
141      */
142     public function isReadable()
143     {
144         return false;
145     }
146
147     /**
148      * {@inheritdoc}
149      */
150     public function read($length)
151     {
152         throw new RuntimeException('Callback streams cannot read');
153     }
154
155     /**
156      * {@inheritdoc}
157      */
158     public function getContents()
159     {
160         $callback = $this->detach();
161         return $callback ? $callback() : '';
162     }
163
164     /**
165      * {@inheritdoc}
166      */
167     public function getMetadata($key = null)
168     {
169         $metadata = [
170             'eof' => $this->eof(),
171             'stream_type' => 'callback',
172             'seekable' => false
173         ];
174
175         if (null === $key) {
176             return $metadata;
177         }
178
179         if (! array_key_exists($key, $metadata)) {
180             return null;
181         }
182
183         return $metadata[$key];
184     }
185 }