PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.20
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.20
1.10.20 1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 All 164 releases
woocommerce-pos / vendor_prefixed / guzzlehttp / psr7 / src / AppendStream.php

AppendStream.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.20, at vendor_prefixed/guzzlehttp/psr7/src/AppendStream.php

197 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare (strict_types=1);
4 namespace WCPOS\Vendor\GuzzleHttp\Psr7;
5
6 use WCPOS\Vendor\Psr\Http\Message\StreamInterface;
7 /**
8 * Reads from multiple streams, one after the other.
9 *
10 * This is a read-only stream decorator.
11 */
12 final class AppendStream implements StreamInterface
13 {
14 use NonSerializableStreamTrait;
15 /** @var StreamInterface[] Streams being decorated */
16 private array $streams = [];
17 private bool $seekable = \true;
18 private int $current = 0;
19 private int $pos = 0;
20 /**
21 * @param StreamInterface[] $streams Streams to decorate. Each stream must
22 * be readable.
23 */
24 public function __construct(array $streams = [])
25 {
26 foreach ($streams as $stream) {
27 $this->addStream($stream);
28 }
29 }
30 public function __toString() : string
31 {
32 $this->rewind();
33 return $this->getContents();
34 }
35 /**
36 * Add a stream to the AppendStream
37 *
38 * @param StreamInterface $stream Stream to append. Must be readable.
39 *
40 * @throws \InvalidArgumentException if the stream is not readable
41 */
42 public function addStream(StreamInterface $stream) : void
43 {
44 if (!$stream->isReadable()) {
45 throw new \InvalidArgumentException('Each stream must be readable');
46 }
47 // The stream is only seekable if all streams are seekable
48 if (!$stream->isSeekable()) {
49 $this->seekable = \false;
50 }
51 $this->streams[] = $stream;
52 }
53 public function getContents() : string
54 {
55 return Utils::copyToString($this);
56 }
57 /**
58 * Closes each attached stream.
59 */
60 public function close() : void
61 {
62 $this->pos = $this->current = 0;
63 $this->seekable = \true;
64 foreach ($this->streams as $stream) {
65 $stream->close();
66 }
67 $this->streams = [];
68 }
69 /**
70 * Detaches each attached stream.
71 *
72 * Returns null as it's not clear which underlying stream resource to return.
73 */
74 public function detach()
75 {
76 $this->pos = $this->current = 0;
77 $this->seekable = \true;
78 foreach ($this->streams as $stream) {
79 $stream->detach();
80 }
81 $this->streams = [];
82 return null;
83 }
84 public function tell() : int
85 {
86 return $this->pos;
87 }
88 /**
89 * Tries to calculate the size by adding the size of each stream.
90 *
91 * If any of the streams do not return a valid number, then the size of the
92 * append stream cannot be determined and null is returned.
93 */
94 public function getSize() : ?int
95 {
96 $size = 0;
97 foreach ($this->streams as $stream) {
98 $s = $stream->getSize();
99 if ($s === null) {
100 return null;
101 }
102 $size = Integers::add($size, $s);
103 }
104 return $size;
105 }
106 public function eof() : bool
107 {
108 return !$this->streams || $this->current >= \count($this->streams) - 1 && $this->streams[$this->current]->eof();
109 }
110 public function rewind() : void
111 {
112 $this->seek(0);
113 }
114 /**
115 * Attempts to seek to the given position. Only supports SEEK_SET.
116 */
117 public function seek(int $offset, int $whence = \SEEK_SET) : void
118 {
119 if (!$this->seekable) {
120 throw new \RuntimeException('This AppendStream is not seekable');
121 } elseif ($whence !== \SEEK_SET) {
122 throw new \RuntimeException('The AppendStream can only seek with SEEK_SET');
123 }
124 $this->pos = $this->current = 0;
125 // Rewind each stream
126 foreach ($this->streams as $i => $stream) {
127 try {
128 $stream->rewind();
129 } catch (\Exception $e) {
130 throw new \RuntimeException('Unable to seek stream ' . $i . ' of the AppendStream', 0, $e);
131 }
132 }
133 // Seek to the actual position by reading from each stream
134 while ($this->pos < $offset && !$this->eof()) {
135 $result = $this->read(\min(8096, $offset - $this->pos));
136 if ($result === '') {
137 break;
138 }
139 }
140 }
141 /**
142 * Reads from all of the appended streams until the length is met or EOF.
143 */
144 public function read(int $length) : string
145 {
146 if ($length < 0) {
147 throw new \RuntimeException('Length parameter cannot be negative');
148 }
149 if ($this->streams === []) {
150 return '';
151 }
152 $buffer = '';
153 $total = \count($this->streams) - 1;
154 $remaining = $length;
155 $progressToNext = \false;
156 while ($remaining > 0) {
157 // Progress to the next stream if needed.
158 if ($progressToNext || $this->streams[$this->current]->eof()) {
159 $progressToNext = \false;
160 if ($this->current === $total) {
161 break;
162 }
163 ++$this->current;
164 }
165 $result = StreamTimeout::read($this->streams[$this->current], $remaining, 'Unable to read from stream: timed out');
166 if ($result === '') {
167 $progressToNext = \true;
168 continue;
169 }
170 $buffer .= $result;
171 $remaining = $length - \strlen($buffer);
172 }
173 $this->pos = Integers::add($this->pos, \strlen($buffer));
174 return $buffer;
175 }
176 public function isReadable() : bool
177 {
178 return \true;
179 }
180 public function isWritable() : bool
181 {
182 return \false;
183 }
184 public function isSeekable() : bool
185 {
186 return $this->seekable;
187 }
188 public function write(string $string) : int
189 {
190 throw new \RuntimeException('Cannot write to an AppendStream');
191 }
192 public function getMetadata(?string $key = null) : ?array
193 {
194 return $key === null ? [] : null;
195 }
196 }
197