Completed
Branch feature/pre-split (669609)
by Anton
03:30
created

SingularNode   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

2 Methods

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