PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.5
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / vendor_prefixed / guzzlehttp / psr7 / src / LimitStream.php

LimitStream.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.5, at vendor_prefixed/guzzlehttp/psr7/src/LimitStream.php

139 lines 4.9 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 YoastSEO_Vendor\GuzzleHttp\Psr7;
5
6 use YoastSEO_Vendor\Psr\Http\Message\StreamInterface;
7 /**
8 * Decorator used to return only a subset of a stream.
9 */
10 final class LimitStream implements \YoastSEO_Vendor\Psr\Http\Message\StreamInterface
11 {
12 use StreamDecoratorTrait;
13 /** @var int Offset to start reading from */
14 private $offset;
15 /** @var int Limit the number of bytes that can be read */
16 private $limit;
17 /** @var StreamInterface */
18 private $stream;
19 /**
20 * @param StreamInterface $stream Stream to wrap
21 * @param int $limit Total number of bytes to allow to be read
22 * from the stream. Pass -1 for no limit.
23 * @param int $offset Position to seek to before reading (only
24 * works on seekable streams).
25 */
26 public function __construct(\YoastSEO_Vendor\Psr\Http\Message\StreamInterface $stream, int $limit = -1, int $offset = 0)
27 {
28 $this->stream = $stream;
29 $this->setLimit($limit);
30 $this->setOffset($offset);
31 }
32 public function eof() : bool
33 {
34 // Always return true if the underlying stream is EOF
35 if ($this->stream->eof()) {
36 return \true;
37 }
38 // No limit and the underlying stream is not at EOF
39 if ($this->limit === -1) {
40 return \false;
41 }
42 return $this->stream->tell() >= $this->offset + $this->limit;
43 }
44 /**
45 * Returns the size of the limited subset of data
46 */
47 public function getSize() : ?int
48 {
49 if (null === ($length = $this->stream->getSize())) {
50 return null;
51 }
52 $size = $length - $this->offset;
53 if ($this->limit !== -1) {
54 $size = \min($this->limit, $size);
55 }
56 return \max(0, $size);
57 }
58 /**
59 * Allow for a bounded seek on the read limited stream
60 */
61 public function seek($offset, $whence = \SEEK_SET) : void
62 {
63 if (!\is_int($offset)) {
64 \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.11', 'Passing %s to StreamInterface::seek() is deprecated; guzzlehttp/psr7 3.0 requires int for $offset.', \get_debug_type($offset));
65 }
66 if (!\is_int($whence)) {
67 \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.11', 'Passing %s to StreamInterface::seek() is deprecated; guzzlehttp/psr7 3.0 requires int for $whence.', \get_debug_type($whence));
68 }
69 if ($whence !== \SEEK_SET || $offset < 0) {
70 throw new \RuntimeException(\sprintf('Cannot seek to offset %s with whence %s', $offset, $whence));
71 }
72 $offset += $this->offset;
73 if ($this->limit !== -1) {
74 if ($offset > $this->offset + $this->limit) {
75 $offset = $this->offset + $this->limit;
76 }
77 }
78 $this->stream->seek($offset);
79 }
80 /**
81 * Give a relative tell()
82 */
83 public function tell() : int
84 {
85 return $this->stream->tell() - $this->offset;
86 }
87 /**
88 * Set the offset to start limiting from
89 *
90 * @param int $offset Offset to seek to and begin byte limiting from
91 *
92 * @throws \RuntimeException if the stream cannot be seeked.
93 */
94 public function setOffset(int $offset) : void
95 {
96 $current = $this->stream->tell();
97 if ($current !== $offset) {
98 // If the stream cannot seek to the offset position, then read to it
99 if ($this->stream->isSeekable()) {
100 $this->stream->seek($offset);
101 } elseif ($current > $offset) {
102 throw new \RuntimeException("Could not seek to stream offset {$offset}");
103 } else {
104 $this->stream->read($offset - $current);
105 }
106 }
107 $this->offset = $offset;
108 }
109 /**
110 * Set the limit of bytes that the decorator allows to be read from the
111 * stream.
112 *
113 * @param int $limit Number of bytes to allow to be read from the stream.
114 * Use -1 for no limit.
115 */
116 public function setLimit(int $limit) : void
117 {
118 $this->limit = $limit;
119 }
120 public function read($length) : string
121 {
122 if (!\is_int($length)) {
123 \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.11', 'Passing %s to StreamInterface::read() is deprecated; guzzlehttp/psr7 3.0 requires int for $length.', \get_debug_type($length));
124 }
125 if ($this->limit === -1) {
126 return $this->stream->read($length);
127 }
128 // Check if the current position is less than the total allowed
129 // bytes + original offset
130 $remaining = $this->offset + $this->limit - $this->stream->tell();
131 if ($remaining > 0) {
132 // Only return the amount of requested data, ensuring that the byte
133 // limit is not exceeded
134 return $this->stream->read(\min($remaining, $length));
135 }
136 return '';
137 }
138 }
139