PivotedNode::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 5
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\Exceptions\NodeException;
12
use Spiral\ORM\ORMInterface;
13
14
/**
15
 * Provides ability to parse columns of target table and map table all together.
16
 */
17
class PivotedNode extends AbstractNode implements ArrayInterface
18
{
19
    use DuplicateTrait;
20
21
    /**
22
     * @invisible
23
     * @var int
24
     */
25
    private $countPivot = 0;
26
27
    /**
28
     * @var string
29
     */
30
    protected $innerPivotKey;
31
32
    /**
33
     * @var string
34
     */
35
    protected $outerPivotKey;
36
37
    /**
38
     * @param array  $columns
39
     * @param array  $pivotColumns
40
     * @param string $outerKey
41
     * @param string $innerPivotKey
42
     * @param string $outerPivotKey
43
     */
44
    public function __construct(
45
        array $columns = [],
46
        array $pivotColumns = [],
47
        string $outerKey,
48
        string $innerPivotKey,
49
        string $outerPivotKey
50
    ) {
51
        //Pivot columns are always prior to table columns
52
        parent::__construct(array_merge($pivotColumns, $columns), $outerKey);
53
        $this->countPivot = count($pivotColumns);
54
55
        $this->innerPivotKey = $innerPivotKey;
56
        $this->outerPivotKey = $outerPivotKey;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    protected function pushData(array &$data)
63
    {
64
        if (empty($this->parent)) {
65
            throw new NodeException("Unable to register data tree, parent is missing");
66
        }
67
68
        if (is_null($data[$this->outerKey])) {
69
            //No data was loaded
70
            return;
71
        }
72
73
        //Mounting parsed data into parent under defined container
74
        $this->parent->mountArray(
75
            $this->container,
76
            $this->outerKey,
77
            $data[ORMInterface::PIVOT_DATA][$this->innerPivotKey],
78
            $data
79
        );
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     *
85
     * Method fetches pivot data into sub-array with key "@pivot".
86
     */
87
    protected function fetchData(int $dataOffset, array $line): array
88
    {
89
        $data = parent::fetchData($dataOffset, $line);
90
91
        //Forming pivot data presence
92
        return array_merge(
93
            [ORMInterface::PIVOT_DATA => array_slice($data, 0, $this->countPivot)],
94
            array_slice($data, $this->countPivot)
95
        );
96
    }
97
98
    /**
99
     * De-duplication in pivot tables based on values in pivot table.
100
     *
101
     * @param array $data
102
     *
103
     * @return string
104
     */
105
    protected function duplicateCriteria(array &$data)
106
    {
107
        $pivotData = $data[ORMInterface::PIVOT_DATA];
108
109
        //Unique row criteria
110
        return $pivotData[$this->innerPivotKey] . '.' . $pivotData[$this->outerPivotKey];
111
    }
112
}