ArrayNode   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 51
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A pushData() 0 19 3
1
<?php
2
/**
3
 * Spiral, Core Components
4
 *
5
 * @author Wolfy-J
6
 */
7
8
namespace Spiral\ORM\Entities\Nodes;
9
10
use Spiral\ORM\Entities\Nodes\Traits\DuplicateTrait;
11
use Spiral\ORM\Exceptions\NodeException;
12
13
/**
14
 * Parses multiple sub children and mount them under parent node.
15
 */
16
class ArrayNode extends AbstractNode implements ArrayInterface
17
{
18
    use DuplicateTrait;
19
20
    /**
21
     * @var string
22
     */
23
    protected $innerKey;
24
25
    /**
26
     * @param array       $columns
27
     * @param string      $innerKey Inner relation key (for example user_id)
28
     * @param string|null $outerKey Outer (parent) relation key (for example id = parent.id)
29
     * @param string      $primaryKey
30
     */
31
    public function __construct(
32
        array $columns = [],
33
        string $innerKey,
34
        string $outerKey,
35
        string $primaryKey
36
    ) {
37
        parent::__construct($columns, $outerKey);
38
        $this->innerKey = $innerKey;
39
40
        //Using primary keys (if any) to de-duplicate results
41
        $this->primaryKey = $primaryKey;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    protected function pushData(array &$data)
48
    {
49
        if (empty($this->parent)) {
50
            throw new NodeException("Unable to register data tree, parent is missing");
51
        }
52
53
        if (is_null($data[$this->innerKey])) {
54
            //No data was loaded
55
            return;
56
        }
57
58
        //Mounting parsed data into parent under defined container
59
        $this->parent->mountArray(
60
            $this->container,
61
            $this->outerKey,
62
            $data[$this->innerKey],
63
            $data
64
        );
65
    }
66
}