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.2.3 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 3.9.1 3.9.10 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9
tutor / ecommerce / PaymentGateways / Paypal / vendor / guzzlehttp / psr7 / src / LimitStream.php
tutor / ecommerce / PaymentGateways / Paypal / vendor / guzzlehttp / psr7 / src Last commit date
Exception 1 year ago AppendStream.php 2 months ago BufferStream.php 2 months ago CachingStream.php 2 months ago DroppingStream.php 2 months ago FnStream.php 2 months ago Header.php 2 months ago HttpFactory.php 1 year ago InflateStream.php 1 year ago LazyOpenStream.php 1 year ago LimitStream.php 2 months ago Message.php 2 months ago MessageTrait.php 2 months ago MimeType.php 2 months ago MultipartStream.php 2 months ago NoSeekStream.php 2 months ago PumpStream.php 2 months ago Query.php 1 year ago Request.php 2 months ago Response.php 2 months ago Rfc3986.php 2 months ago Rfc7230.php 2 months ago ServerRequest.php 2 months ago Stream.php 2 months ago StreamDecoratorTrait.php 2 months ago StreamWrapper.php 2 months ago UploadedFile.php 2 months ago Uri.php 2 months ago UriComparator.php 2 months ago UriNormalizer.php 2 months ago UriResolver.php 2 months ago Utils.php 2 months ago
LimitStream.php
189 lines
1 <?php
2
3 declare(strict_types=1);
4
5 namespace GuzzleHttp\Psr7;
6
7 use Psr\Http\Message\StreamInterface;
8
9 /**
10 * Decorator used to return only a subset of a stream.
11 */
12 final class LimitStream implements StreamInterface
13 {
14 use StreamDecoratorTrait;
15
16 /** @var int Offset to start reading from */
17 private $offset;
18
19 /** @var int Limit the number of bytes that can be read */
20 private $limit;
21
22 /** @var StreamInterface */
23 private $stream;
24
25 /**
26 * @param StreamInterface $stream Stream to wrap
27 * @param int $limit Total number of bytes to allow to be read
28 * from the stream. Pass -1 for no limit.
29 * @param int $offset Position to seek to before reading (only
30 * works on seekable streams).
31 */
32 public function __construct(
33 StreamInterface $stream,
34 int $limit = -1,
35 int $offset = 0
36 ) {
37 $this->stream = $stream;
38 $this->setLimit($limit);
39 $this->setOffset($offset);
40 }
41
42 public function eof(): bool
43 {
44 // Always return true if the underlying stream is EOF
45 if ($this->stream->eof()) {
46 return true;
47 }
48
49 // No limit and the underlying stream is not at EOF
50 if ($this->limit === -1) {
51 return false;
52 }
53
54 return $this->stream->tell() >= $this->offset + $this->limit;
55 }
56
57 /**
58 * Returns the size of the limited subset of data
59 */
60 public function getSize(): ?int
61 {
62 if (null === ($length = $this->stream->getSize())) {
63 return null;
64 }
65
66 $size = $length - $this->offset;
67
68 if ($this->limit !== -1) {
69 $size = min($this->limit, $size);
70 }
71
72 return max(0, $size);
73 }
74
75 /**
76 * Allow for a bounded seek on the read limited stream
77 */
78 public function seek($offset, $whence = SEEK_SET): void
79 {
80 if (!\is_int($offset)) {
81 \trigger_deprecation(
82 'guzzlehttp/psr7',
83 '2.11',
84 'Passing %s to StreamInterface::seek() is deprecated; guzzlehttp/psr7 3.0 requires int for $offset.',
85 \get_debug_type($offset)
86 );
87 }
88
89 if (!\is_int($whence)) {
90 \trigger_deprecation(
91 'guzzlehttp/psr7',
92 '2.11',
93 'Passing %s to StreamInterface::seek() is deprecated; guzzlehttp/psr7 3.0 requires int for $whence.',
94 \get_debug_type($whence)
95 );
96 }
97
98 if ($whence !== SEEK_SET || $offset < 0) {
99 throw new \RuntimeException(sprintf(
100 'Cannot seek to offset %s with whence %s',
101 $offset,
102 $whence
103 ));
104 }
105
106 $offset += $this->offset;
107
108 if ($this->limit !== -1) {
109 if ($offset > $this->offset + $this->limit) {
110 $offset = $this->offset + $this->limit;
111 }
112 }
113
114 $this->stream->seek($offset);
115 }
116
117 /**
118 * Give a relative tell()
119 */
120 public function tell(): int
121 {
122 return $this->stream->tell() - $this->offset;
123 }
124
125 /**
126 * Set the offset to start limiting from
127 *
128 * @param int $offset Offset to seek to and begin byte limiting from
129 *
130 * @throws \RuntimeException if the stream cannot be seeked.
131 */
132 public function setOffset(int $offset): void
133 {
134 $current = $this->stream->tell();
135
136 if ($current !== $offset) {
137 // If the stream cannot seek to the offset position, then read to it
138 if ($this->stream->isSeekable()) {
139 $this->stream->seek($offset);
140 } elseif ($current > $offset) {
141 throw new \RuntimeException("Could not seek to stream offset $offset");
142 } else {
143 $this->stream->read($offset - $current);
144 }
145 }
146
147 $this->offset = $offset;
148 }
149
150 /**
151 * Set the limit of bytes that the decorator allows to be read from the
152 * stream.
153 *
154 * @param int $limit Number of bytes to allow to be read from the stream.
155 * Use -1 for no limit.
156 */
157 public function setLimit(int $limit): void
158 {
159 $this->limit = $limit;
160 }
161
162 public function read($length): string
163 {
164 if (!\is_int($length)) {
165 \trigger_deprecation(
166 'guzzlehttp/psr7',
167 '2.11',
168 'Passing %s to StreamInterface::read() is deprecated; guzzlehttp/psr7 3.0 requires int for $length.',
169 \get_debug_type($length)
170 );
171 }
172
173 if ($this->limit === -1) {
174 return $this->stream->read($length);
175 }
176
177 // Check if the current position is less than the total allowed
178 // bytes + original offset
179 $remaining = ($this->offset + $this->limit) - $this->stream->tell();
180 if ($remaining > 0) {
181 // Only return the amount of requested data, ensuring that the byte
182 // limit is not exceeded
183 return $this->stream->read(min($remaining, $length));
184 }
185
186 return '';
187 }
188 }
189