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

PivotedNode   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 96
rs 10
c 0
b 0
f 0
wmc 6
lcom 2
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A pushData() 0 19 3
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\Exceptions\NodeException;
11
use Spiral\ORM\ORMInterface;
12
13
/**
14
 * Provides ability to parse columns of target table and map table all together.
15
 */
16
class PivotedNode extends AbstractNode implements ArrayInterface
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 (empty($this->parent)) {
64
            throw new NodeException("Unable to register data tree, parent is missing");
65
        }
66
67
        if (is_null($data[ORMInterface::PIVOT_DATA][$this->outerPivotKey])) {
68
            //No data was loaded
69
            return;
70
        }
71
72
        //Mounting parsed data into parent under defined container
73
        $this->parent->mountArray(
74
            $this->container,
75
            $this->outerKey,
76
            $data[ORMInterface::PIVOT_DATA][$this->innerPivotKey],
77
            $data
78
        );
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     *
84
     * Method fetches pivot data into sub-array with key "@pivot".
85
     */
86
    protected function fetchData(int $dataOffset, array $line): array
87
    {
88
        $data = parent::fetchData($dataOffset, $line);
89
90
        //Forming pivot data presence
91
        return array_merge(
92
            [ORMInterface::PIVOT_DATA => array_slice($data, 0, $this->countPivot)],
93
            array_slice($data, $this->countPivot)
94
        );
95
    }
96
97
    /**
98
     * De-duplication in pivot tables based on values in pivot table.
99
     *
100
     * @param array $data
101
     *
102
     * @return string
103
     */
104
    protected function duplicateCriteria(array &$data)
105
    {
106
        $pivotData = $data[ORMInterface::PIVOT_DATA];
107
108
        //Unique row criteria
109
        return $pivotData[$this->innerPivotKey] . '.' . $pivotData[$this->outerPivotKey];
110
    }
111
}