Completed
Branch feature/pre-split (4c50c1)
by Anton
03:17
created

SinguralNode::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 4
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
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\LoaderException;
11
12
/**
13
 * Node with ability to push it's data into referenced tree location.
14
 */
15
class SinguralNode extends AbstractNode
16
{
17
    use DuplicateTrait;
18
19
    /**
20
     * @var string
21
     */
22
    protected $localKey;
23
24
    /**
25
     * @param array       $columns
26
     * @param string      $localKey  Inner relation key (for example user_id)
27
     * @param string|null $parentKey Outer (parent) relation key (for example id = parent.id)
28
     * @param array       $primaryKeys
29
     */
30
    public function __construct(
31
        array $columns = [],
32
        string $localKey,
33
        string $parentKey,
34
        array $primaryKeys = []
35
    ) {
36
        parent::__construct($columns, $parentKey);
37
        $this->localKey = $localKey;
38
39
        //Using primary keys (if any) to de-duplicate results
40
        $this->duplicateCriteria = $primaryKeys;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    protected function registerData(array &$data)
47
    {
48
        if (empty($this->parent)) {
49
            throw new LoaderException("Unable to register data tree, parent is missing");
50
        }
51
52
        //Mounting parsed data into parent under defined container
53
        $this->parent->mount(
54
            $this->container,
55
            $this->referenceKey,
56
            $data[$this->localKey],
57
            $data
58
        );
59
    }
60
}