Completed
Branch feature/pre-split (c69968)
by Anton
21:25
created

PivotedRootNode   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 87
rs 10
c 0
b 0
f 0
wmc 5
lcom 2
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A pushData() 0 9 2
A fetchData() 0 10 1
A duplicateCriteria() 0 7 1
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\ORMInterface;
11
12
/**
13
 * Similar to normal pivot node but does not require parent!
14
 */
15
class PivotedRootNode extends OutputNode
16
{
17
    use DuplicateTrait;
18
19
    /**
20
     * @invisible
21
     * @var int
22
     */
23
    private $countPivot = 0;
24
25
    /**
26
     * @var string
27
     */
28
    protected $innerPivotKey;
29
30
    /**
31
     * @var string
32
     */
33
    protected $outerPivotKey;
34
35
    /**
36
     * @param array  $columns
37
     * @param array  $pivotColumns
38
     * @param string $outerKey
39
     * @param string $innerPivotKey
40
     * @param string $outerPivotKey
41
     */
42
    public function __construct(
43
        array $columns = [],
44
        array $pivotColumns = [],
45
        string $outerKey,
46
        string $innerPivotKey,
47
        string $outerPivotKey
48
    ) {
49
        //Pivot columns are always prior to table columns
50
        parent::__construct(array_merge($pivotColumns, $columns), $outerKey);
51
        $this->countPivot = count($pivotColumns);
52
53
        $this->innerPivotKey = $innerPivotKey;
54
        $this->outerPivotKey = $outerPivotKey;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    protected function pushData(array &$data)
61
    {
62
        if (is_null($data[ORMInterface::PIVOT_DATA][$this->outerPivotKey])) {
63
            //No data was loaded
64
            return;
65
        }
66
67
        $this->result[] = &$data;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     *
73
     * Method fetches pivot data into sub-array with key "@pivot".
74
     */
75
    protected function fetchData(int $dataOffset, array $line): array
76
    {
77
        $data = parent::fetchData($dataOffset, $line);
78
79
        //Forming pivot data presence
80
        return array_merge(
81
            [ORMInterface::PIVOT_DATA => array_slice($data, 0, $this->countPivot)],
82
            array_slice($data, $this->countPivot)
83
        );
84
    }
85
86
    /**
87
     * De-duplication in pivot tables based on values in pivot table.
88
     *
89
     * @param array $data
90
     *
91
     * @return string
92
     */
93
    protected function duplicateCriteria(array &$data)
94
    {
95
        $pivotData = $data[ORMInterface::PIVOT_DATA];
96
97
        //Unique row criteria
98
        return $pivotData[$this->innerPivotKey] . '.' . $pivotData[$this->outerPivotKey];
99
    }
100
101
}