PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.9
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.9
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 27.9, at vendor_prefixed/guzzlehttp/psr7/src/LimitStream.php

129 lines 4.2 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 } elseif ($this->limit === -1) {
52 return $length - $this->offset;
53 } else {
54 return \min($this->limit, $length - $this->offset);
55 }
56 }
57 /**
58 * Allow for a bounded seek on the read limited stream
59 */
60 public function seek($offset, $whence = \SEEK_SET) : void
61 {
62 if ($whence !== \SEEK_SET || $offset < 0) {
63 throw new \RuntimeException(\sprintf('Cannot seek to offset %s with whence %s', $offset, $whence));
64 }
65 $offset += $this->offset;
66 if ($this->limit !== -1) {
67 if ($offset > $this->offset + $this->limit) {
68 $offset = $this->offset + $this->limit;
69 }
70 }
71 $this->stream->seek($offset);
72 }
73 /**
74 * Give a relative tell()
75 */
76 public function tell() : int
77 {
78 return $this->stream->tell() - $this->offset;
79 }
80 /**
81 * Set the offset to start limiting from
82 *
83 * @param int $offset Offset to seek to and begin byte limiting from
84 *
85 * @throws \RuntimeException if the stream cannot be seeked.
86 */
87 public function setOffset(int $offset) : void
88 {
89 $current = $this->stream->tell();
90 if ($current !== $offset) {
91 // If the stream cannot seek to the offset position, then read to it
92 if ($this->stream->isSeekable()) {
93 $this->stream->seek($offset);
94 } elseif ($current > $offset) {
95 throw new \RuntimeException("Could not seek to stream offset {$offset}");
96 } else {
97 $this->stream->read($offset - $current);
98 }
99 }
100 $this->offset = $offset;
101 }
102 /**
103 * Set the limit of bytes that the decorator allows to be read from the
104 * stream.
105 *
106 * @param int $limit Number of bytes to allow to be read from the stream.
107 * Use -1 for no limit.
108 */
109 public function setLimit(int $limit) : void
110 {
111 $this->limit = $limit;
112 }
113 public function read($length) : string
114 {
115 if ($this->limit === -1) {
116 return $this->stream->read($length);
117 }
118 // Check if the current position is less than the total allowed
119 // bytes + original offset
120 $remaining = $this->offset + $this->limit - $this->stream->tell();
121 if ($remaining > 0) {
122 // Only return the amount of requested data, ensuring that the byte
123 // limit is not exceeded
124 return $this->stream->read(\min($remaining, $length));
125 }
126 return '';
127 }
128 }
129