ObjectNodeToArrayHandler::getArray()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 5
nop 2
1
<?php
2
3
namespace Saxulum\JsonDocument\NodeToArray;
4
5
use Saxulum\JsonDocument\AbstractNode;
6
use Saxulum\JsonDocument\ObjectNode;
7
8
class ObjectNodeToArrayHandler extends AbstractNodeToArrayHandler
9
{
10
    /**
11
     * @param  AbstractNode $node
12
     * @param  bool         $embedded
13
     * @return array
14
     */
15
    public function getArray(AbstractNode $node, $embedded = false)
16
    {
17
        if (!$node instanceof ObjectNode) {
18
            throw new \InvalidArgumentException("Invalid node type!");
19
        }
20
21
        $array = array();
22
23
        foreach ($node->getAttributes() as $attribute) {
24
            $array[$attribute->getFormattedName()] =$this->getMainHandler()->getArray($attribute, true);
25
        }
26
27
        foreach ($node->getNodes() as $name => $childNode) {
28
            $array[$name] = $this->getMainHandler()->getArray($childNode, true);
29
        }
30
31
        return $array;
32
    }
33
34
    /**
35
     * @param  AbstractNode $node
36
     * @return bool
37
     */
38
    public function isResponsible(AbstractNode $node)
39
    {
40
        return $node instanceof ObjectNode;
41
    }
42
}
43