~jlosito/wordpress/wp-plugin-yoast

« back to all changes in this revision

Viewing changes to vendor_prefixed/guzzlehttp/psr7/src/BufferStream.php

  • Committer: John Losito
  • Date: 2019-11-08 15:58:32 UTC
  • Revision ID: john.losito@canonical.com-20191108155832-bjb8eep3l9naaf8f
Updated to 12.4

Show diffs side-by-side

added added

removed removed

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