PivotedRootNode   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

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

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