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 / BufferStream.php

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

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