Completed
Push — master ( 3607c7...59cd47 )
by Chris
03:04
created

UnsafeNodeFactory::createNodeFromArrayValue()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.016

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 9
cts 10
cp 0.9
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 6
nop 3
crap 4.016
1
<?php declare(strict_types=1);
2
3
namespace DaveRandom\Jom;
4
5
final class UnsafeNodeFactory extends NodeFactory
6
{
7
    /**
8
     * @inheritdoc
9
     */
10 30
    protected function createNodeFromArrayValue(array $array, ?Document $ownerDoc, int $flags): VectorNode
11
    {
12 30
        $i = 0;
13 30
        $packed = true;
14
15 30
        foreach ($array as $key => $value) {
16 25
            if ($key !== $i++) {
17
                $packed = false;
18 25
                break;
19
            }
20
        }
21
22 30
        return $packed
23 30
            ? $this->createArrayNodeFromPackedArray($array, $ownerDoc, $flags)
24 30
            : $this->createObjectNodeFromPropertyMap($array, $ownerDoc, $flags);
25
    }
26
27
    /**
28
     * @inheritdoc
29
     */
30
    protected function createNodeFromObjectValue(object $object, ?Document $ownerDoc, int $flags): ?Node
31
    {
32
        return $object instanceof \JsonSerializable
33
            ? $this->tryCreateNodeFromValue($object->jsonSerialize(), $ownerDoc, $flags)
34
            : $this->createObjectNodeFromPropertyMap($object, $ownerDoc, $flags);
35
    }
36
}
37