Completed
Push — master ( 9afc27...035c9e )
by Chris
04:26
created

NodeFactory   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 82.61%

Importance

Changes 0
Metric Value
wmc 13
dl 0
loc 76
ccs 19
cts 23
cp 0.8261
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B createArrayNodeFromPackedArray() 0 17 5
A createScalarOrNullNodeFromValue() 0 13 3
B createObjectNodeFromPropertyMap() 0 17 5
1
<?php declare(strict_types=1);
2
3
namespace DaveRandom\Jom;
4
5
use DaveRandom\Jom\Exceptions\InvalidNodeValueException;
6
7
abstract class NodeFactory
8
{
9
    private const SCALAR_VALUE_NODE_CLASSES = [
10
        'boolean' => BooleanNode::class,
11
        'integer' => NumberNode::class,
12
        'double' => NumberNode::class,
13
        'string' => StringNode::class,
14
    ];
15
16
    /**
17
     * @throws InvalidNodeValueException
18
     */
19 94
    final protected function createArrayNodeFromPackedArray(array $values, ?Document $doc, int $flags): ArrayNode
20
    {
21
        try {
22 94
            $node = new ArrayNode([], $doc);
23
24 94
            foreach ($values as $value) {
25 89
                if (null !== $valueNode = $this->createNodeFromValue($value, $doc, $flags)) {
26 89
                    $node->push($valueNode);
27
                }
28
            }
29
30 94
            return $node;
31
        } catch (InvalidNodeValueException $e) {
32
            throw $e;
33
        //@codeCoverageIgnoreStart
34
        } catch (\Exception $e) {
35
            throw unexpected($e);
36
        }
37
        //@codeCoverageIgnoreEnd
38
    }
39
40
    /**
41
     * @throws InvalidNodeValueException
42
     */
43 64
    final protected function createObjectNodeFromPropertyMap($properties, ?Document $doc, int $flags): ObjectNode
44
    {
45
        try {
46 64
            $node = new ObjectNode([], $doc);
47
48 64
            foreach ($properties as $name => $value) {
49 64
                if (null !== $valueNode = $this->createNodeFromValue($value, $doc, $flags)) {
50 64
                    $node->setProperty($name, $valueNode);
51
                }
52
            }
53
54 64
            return $node;
55
        } catch (InvalidNodeValueException $e) {
56
            throw $e;
57
        //@codeCoverageIgnoreStart
58
        } catch (\Exception $e) {
59
            throw unexpected($e);
60
        }
61
        //@codeCoverageIgnoreEnd
62
    }
63
64 101
    final protected function createScalarOrNullNodeFromValue($value, ?Document $doc): ?Node
65
    {
66 101
        if ($value === null) {
67 35
            return new NullNode($doc);
68
        }
69
70 94
        $className = self::SCALAR_VALUE_NODE_CLASSES[\gettype($value)] ?? null;
71
72 94
        if ($className !== null) {
73 64
            return new $className($value, $doc);
74
        }
75
76 94
        return null;
77
    }
78
79
    /**
80
     * @throws InvalidNodeValueException
81
     */
82
    abstract public function createNodeFromValue($value, ?Document $doc, int $flags): ?Node;
83
}
84