NodeToArray::getArray()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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