Completed
Push — master ( dee7d0...3607c7 )
by Chris
02:30
created

UnsafeNodeFactory::createVectorNodeFromValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 4
cts 6
cp 0.6667
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 3
crap 3.3332
1
<?php declare(strict_types=1);
2
3
namespace DaveRandom\Jom;
4
5
use DaveRandom\Jom\Exceptions\Exception;
6
7
final class UnsafeNodeFactory extends NodeFactory
8
{
9
    /**
10
     * @throws Exception
11
     */
12 30
    private function createVectorNodeFromArray(array $values, ?Document $doc, int $flags): VectorNode
13
    {
14 30
        $i = 0;
15 30
        $packed = true;
16
17 30
        foreach ($values as $key => $value) {
18 25
            if ($key !== $i++) {
19
                $packed = false;
20 25
                break;
21
            }
22
        }
23
24 30
        return $packed
25 30
            ? $this->createArrayNodeFromPackedArray($values, $doc, $flags)
26 30
            : $this->createObjectNodeFromPropertyMap($values, $doc, $flags);
27
    }
28
29
    /**
30
     * @throws Exception
31
     */
32
    private function createNodeFromObject(object $object, ?Document $doc, int $flags): ?Node
33
    {
34
        return $object instanceof \JsonSerializable
35
            ? $this->createNodeFromValue($object->jsonSerialize(), $doc, $flags)
36
            : $this->createObjectNodeFromPropertyMap($object, $doc, $flags);
37
    }
38
39
    /**
40
     * @inheritdoc
41
     */
42 30
    protected function createVectorNodeFromValue($value, ?Document $doc, int $flags): ?Node
43
    {
44 30
        if (\is_object($value)) {
45
            return $this->createNodeFromObject($value, $doc, $flags);
46
        }
47
48 30
        if (\is_array($value)) {
49 30
            return $this->createVectorNodeFromArray($value, $doc, $flags);
50
        }
51
52
        return null;
53
    }
54
}
55