Json::matches()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 1
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 3
rs 10
1
<?php
2
3
namespace Cerbero\JsonParser\Sources;
4
5
use Cerbero\JsonParser\Concerns\DetectsEndpoints;
6
use Traversable;
7
8
use function is_string;
9
use function strlen;
10
11
/**
12
 * The JSON source.
13
 *
14
 * @property-read string $source
15
 */
16
class Json extends Source
17
{
18
    use DetectsEndpoints;
19
20
    /**
21
     * Retrieve the JSON fragments
22
     *
23
     * @return Traversable<int, string>
24
     */
25 331
    public function getIterator(): Traversable
26
    {
27 331
        for ($i = 0; $i < $this->size(); $i += $this->config->bytes) {
28 331
            yield substr($this->source, $i, $this->config->bytes);
29
        }
30
    }
31
32
    /**
33
     * Determine whether the JSON source can be handled
34
     *
35
     * @return bool
36
     */
37 330
    public function matches(): bool
38
    {
39 330
        return is_string($this->source) && !is_file($this->source) && !$this->isEndpoint($this->source);
40
    }
41
42
    /**
43
     * Retrieve the calculated size of the JSON source
44
     *
45
     * @return int|null
46
     */
47 331
    protected function calculateSize(): ?int
48
    {
49 331
        return strlen($this->source);
50
    }
51
}
52