Passed
Push — master ( 37810d...c6a7a1 )
by Chris
03:03
created

SafeNodeFactory::createNodeFromValue()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 12
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 4
nop 2
crap 20
1
<?php declare(strict_types=1);
2
3
namespace DaveRandom\Jom;
4
5
use DaveRandom\Jom\Exceptions\InvalidNodeValueException;
6
7
final class SafeNodeFactory extends NodeFactory
8
{
9
    /**
10
     * @inheritdoc
11
     */
12
    public function createNodeFromValue(Document $doc, $value): Node
13
    {
14
        if (null !== $node = $this->createScalarOrNullNodeFromValue($doc, $value)) {
15
            return $node;
16
        }
17
18
        if ($value instanceof \stdClass) {
19
            return $this->createObjectNodeFromStdClass($doc, $value);
20
        }
21
22
        if (\is_array($value)) {
23
            return $this->createArrayNodeFromPackedArray($doc, $value);
24
        }
25
26
        throw new InvalidNodeValueException("Failed to create node from value of type '" . \gettype($value) . "'");
27
    }
28
}
29