Completed
Push — master ( f400d6...2675c1 )
by Chris
02:17
created

SafeNodeFactory::createNodeFromValue()   B

Complexity

Conditions 6
Paths 14

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 21
c 0
b 0
f 0
ccs 0
cts 18
cp 0
rs 8.7624
cc 6
eloc 12
nc 14
nop 2
crap 42
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($value, ?Document $doc = null): Node
13
    {
14
        try {
15
            if (null !== $node = $this->createScalarOrNullNodeFromValue($value, $doc)) {
16
                return $node;
17
            }
18
19
            if (\is_object($value)) {
20
                return $this->createObjectNodeFromPropertyMap($value, $doc);
21
            }
22
23
            if (\is_array($value)) {
24
                return $this->createArrayNodeFromPackedArray($value, $doc);
25
            }
26
        } catch (InvalidNodeValueException $e) {
27
            throw $e;
28
        } catch (\Exception $e) {
29
            throw new \Error('Unexpected ' . \get_class($e) . ": {$e->getMessage()}", 0, $e);
30
        }
31
32
        throw new InvalidNodeValueException("Failed to create node from value of type '" . \gettype($value) . "'");
33
    }
34
}
35