PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.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 / BufferStream.php

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

122 lines 3.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 * Provides a buffer stream that can be written to to fill a buffer, and read
9 * from to remove bytes from the buffer.
10 *
11 * This stream returns a "hwm" metadata value that tells upstream consumers
12 * what the configured high water mark of the stream is, or the maximum
13 * preferred size of the buffer.
14 */
15 final class BufferStream implements \YoastSEO_Vendor\Psr\Http\Message\StreamInterface
16 {
17 /** @var int */
18 private $hwm;
19 /** @var string */
20 private $buffer = '';
21 /**
22 * @param int $hwm High water mark, representing the preferred maximum
23 * buffer size. If the size of the buffer exceeds the high
24 * water mark, then calls to write will continue to succeed
25 * but will return 0 to inform writers to slow down
26 * until the buffer has been drained by reading from it.
27 */
28 public function __construct(int $hwm = 16384)
29 {
30 $this->hwm = $hwm;
31 }
32 public function __toString() : string
33 {
34 return $this->getContents();
35 }
36 public function getContents() : string
37 {
38 $buffer = $this->buffer;
39 $this->buffer = '';
40 return $buffer;
41 }
42 public function close() : void
43 {
44 $this->buffer = '';
45 }
46 public function detach()
47 {
48 $this->close();
49 return null;
50 }
51 public function getSize() : ?int
52 {
53 return \strlen($this->buffer);
54 }
55 public function isReadable() : bool
56 {
57 return \true;
58 }
59 public function isWritable() : bool
60 {
61 return \true;
62 }
63 public function isSeekable() : bool
64 {
65 return \false;
66 }
67 public function rewind() : void
68 {
69 $this->seek(0);
70 }
71 public function seek($offset, $whence = \SEEK_SET) : void
72 {
73 throw new \RuntimeException('Cannot seek a BufferStream');
74 }
75 public function eof() : bool
76 {
77 return \strlen($this->buffer) === 0;
78 }
79 public function tell() : int
80 {
81 throw new \RuntimeException('Cannot determine the position of a BufferStream');
82 }
83 /**
84 * Reads data from the buffer.
85 */
86 public function read($length) : string
87 {
88 $currentLength = \strlen($this->buffer);
89 if ($length >= $currentLength) {
90 // No need to slice the buffer because we don't have enough data.
91 $result = $this->buffer;
92 $this->buffer = '';
93 } else {
94 // Slice up the result to provide a subset of the buffer.
95 $result = \substr($this->buffer, 0, $length);
96 $this->buffer = \substr($this->buffer, $length);
97 }
98 return $result;
99 }
100 /**
101 * Writes data to the buffer.
102 */
103 public function write($string) : int
104 {
105 $this->buffer .= $string;
106 if (\strlen($this->buffer) >= $this->hwm) {
107 return 0;
108 }
109 return \strlen($string);
110 }
111 /**
112 * @return mixed
113 */
114 public function getMetadata($key = null)
115 {
116 if ($key === 'hwm') {
117 return $this->hwm;
118 }
119 return $key ? null : [];
120 }
121 }
122