1 | <?php |
||
2 | /** |
||
3 | * Spiral, Core Components |
||
4 | * |
||
5 | * @author Wolfy-J |
||
6 | */ |
||
7 | declare(strict_types=1); |
||
8 | |||
9 | namespace Cycle\ORM\Parser; |
||
10 | |||
11 | use Cycle\ORM\Exception\ParserException; |
||
12 | |||
13 | /** |
||
14 | * Node with ability to push it's data into referenced tree location. |
||
15 | */ |
||
16 | final class SingularNode extends AbstractNode |
||
17 | { |
||
18 | /** @var string */ |
||
19 | protected $innerKey; |
||
20 | |||
21 | /** |
||
22 | * @param array $columns |
||
23 | * @param string $primaryKey |
||
24 | * @param string $innerKey Inner relation key (for example user_id) |
||
25 | * @param string|null $outerKey Outer (parent) relation key (for example id = parent.id) |
||
26 | */ |
||
27 | public function __construct(array $columns, string $primaryKey, string $innerKey, string $outerKey) |
||
28 | { |
||
29 | parent::__construct($columns, $outerKey); |
||
30 | $this->setDuplicateCriteria($primaryKey); |
||
31 | |||
32 | $this->innerKey = $innerKey; |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * {@inheritdoc} |
||
37 | */ |
||
38 | protected function push(array &$data) |
||
39 | { |
||
40 | if (empty($this->parent)) { |
||
41 | throw new ParserException("Unable to register data tree, parent is missing"); |
||
42 | } |
||
43 | |||
44 | if (is_null($data[$this->innerKey])) { |
||
45 | //No data was loaded |
||
46 | return; |
||
47 | } |
||
48 | |||
49 | $this->parent->mount( |
||
50 | $this->container, |
||
51 | $this->outerKey, |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
52 | $data[$this->innerKey], |
||
53 | $data |
||
54 | ); |
||
55 | } |
||
56 | } |