Completed
Pull Request — master (#7)
by Sandro
05:30 queued 03:27
created

JsonStream::isSeekable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * Sandro Keil (https://sandro-keil.de)
4
 *
5
 * @link      http://github.com/sandrokeil/arangodb-php-client for the canonical source repository
6
 * @copyright Copyright (c) 2018-2019 Sandro Keil
7
 * @license   http://github.com/sandrokeil/arangodb-php-client/blob/master/LICENSE.md New BSD License
8
 */
9
10
namespace ArangoDb\Http;
11
12
use ArangoDb\Exception\RuntimeException;
13
use ArangoDb\Util\Json;
14
use Psr\Http\Message\StreamInterface;
15
16
final class JsonStream implements StreamInterface
17
{
18
    /**
19
     * Original data
20
     *
21
     * @var string|array
22
     */
23
    private $data;
24
25
    /**
26
     * Buffered json data if needed
27
     *
28
     * @var string|null
29
     */
30
    private $buffer;
31
32
    /**
33
     * @var int
34
     */
35
    private $size;
36
37
    /**
38
     * @param string|array $data
39
     */
40 7
    public function __construct($data)
41
    {
42 7
        $this->data = $data;
43 7
    }
44
45 1
    public function toJson(): string
46
    {
47 1
        return $this->getContents();
48
    }
49
50 9
    public function toArray(): array
51
    {
52 9
        if (is_string($this->data)) {
53
            return Json::decode($this->data);
54
        }
55 9
        return $this->data;
56
    }
57
58 2
    public function __toString()
59
    {
60 2
        return $this->getContents();
61
    }
62
63
    public function close(): void
64
    {
65
        $this->data = '';
66
        $this->buffer = null;
67
    }
68
69
    public function detach()
70
    {
71
        $this->close();
72
        return null;
73
    }
74
75 2
    public function getSize(): int
76
    {
77 2
        if ($this->size === null) {
78 2
            $this->size = strlen($this->getContents());
79
        }
80
81 2
        return $this->size;
82
    }
83
84
    public function tell(): int
85
    {
86
        throw new RuntimeException('Cannot determine the position of a JsonStream');
87
    }
88
89
    public function eof(): bool
90
    {
91
        if ($this->buffer === null) {
92
            return false;
93
        }
94
        return $this->buffer === '';
95
    }
96
97
    public function isSeekable(): bool
98
    {
99
        return false;
100
    }
101
102
    /**
103
     * @param int $offset
104
     * @param int $whence
105
     */
106
    public function seek($offset, $whence = SEEK_SET): void
107
    {
108
        throw new RuntimeException('Cannot seek a JsonStream');
109
    }
110
111 2
    public function rewind(): void
112
    {
113 2
        $this->buffer = null;
114 2
    }
115
116
    public function isWritable(): bool
117
    {
118
        return false;
119
    }
120
121
    public function write($string): int
122
    {
123
        throw new RuntimeException('Cannot write a JsonStream');
124
    }
125
126
    public function isReadable(): bool
127
    {
128
        return true;
129
    }
130
131
    /**
132
     * @param int $length
133
     * @return string
134
     */
135 2
    public function read($length): string
136
    {
137 2
        if ($this->buffer === null) {
138 2
            $this->buffer = $this->getContents();
139
        }
140 2
        $currentLength = strlen($this->buffer);
141
142 2
        if ($length >= $currentLength) {
143
            // No need to slice the data because we don't have enough data.
144 2
            $result = $this->buffer;
145 2
            $this->buffer = '';
146
        } else {
147
            // Slice up the result to provide a subset of the data.
148 2
            $result = substr($this->buffer, 0, $length);
149 2
            $this->buffer = substr($this->buffer, $length);
150
        }
151
152 2
        return $result;
153
    }
154
155 5
    public function getContents(): string
156
    {
157 5
        if (is_string($this->data)) {
158 4
            return $this->data;
159
        }
160 1
        return Json::encode($this->data);
161
    }
162
163
    /**
164
     * @param string|null $key
165
     * @return array|mixed|null
166
     */
167
    public function getMetadata($key = null)
168
    {
169
        $metadata = [
170
            'wrapper_data' => ['string'],
171
            'wrapper_type' => 'string',
172
            'stream_type' => 'string',
173
            'mode' => 'r',
174
            'unread_bytes' => $this->getSize() - strlen($this->buffer ?? ''),
175
            'seekable' => $this->isSeekable(),
176
            'timeout' => false,
177
            'blocked' => false,
178
            'eof' => $this->eof()
179
        ];
180
181
        if ($key === null) {
182
            return $metadata;
183
        }
184
        return $metadata[$key] ?? null;
185
    }
186
}
187