Completed
Pull Request — master (#7)
by Sandro
02:14
created

JsonStream::eof()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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