|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* components |
|
4
|
|
|
* |
|
5
|
|
|
* @author Wolfy-J |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace Spiral\ORM\Entities; |
|
8
|
|
|
|
|
9
|
|
|
use Spiral\ORM\CommandInterface; |
|
10
|
|
|
use Spiral\ORM\ORMInterface; |
|
11
|
|
|
use Spiral\ORM\RecordInterface; |
|
12
|
|
|
use Spiral\ORM\RelationInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Represent set of entity relations. |
|
16
|
|
|
*/ |
|
17
|
|
|
class RelationBucket |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var array|RelationInterface[] |
|
21
|
|
|
*/ |
|
22
|
|
|
private $relations = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Relations schema. |
|
26
|
|
|
* |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
private $schema = []; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Associates ORM manager. |
|
33
|
|
|
* |
|
34
|
|
|
* @var ORMInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $orm; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param RecordInterface $record |
|
40
|
|
|
* @param ORMInterface $orm |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(RecordInterface $record, ORMInterface $orm) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->orm = $orm; |
|
45
|
|
|
$this->schema = $orm->define(get_class($record), ORMInterface::R_RELATIONS); |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Extract relations data from given entity fields. |
|
50
|
|
|
* |
|
51
|
|
|
* @param array $data |
|
52
|
|
|
*/ |
|
53
|
|
|
public function extractRelations(array &$data) |
|
54
|
|
|
{ |
|
55
|
|
|
//Fetch all relations |
|
56
|
|
|
$relations = array_intersect_key($data, $this->schema); |
|
57
|
|
|
|
|
58
|
|
|
foreach ($relations as $name => $relation) { |
|
59
|
|
|
$this->relations[$name] = $relation; |
|
60
|
|
|
unset($data[$name]); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function queueRelations(CommandInterface $parent): CommandInterface |
|
65
|
|
|
{ |
|
66
|
|
|
return $parent; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Check if parent entity has associated relation. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $relation |
|
73
|
|
|
* |
|
74
|
|
|
* @return bool |
|
75
|
|
|
*/ |
|
76
|
|
|
public function has(string $relation): bool |
|
77
|
|
|
{ |
|
78
|
|
|
return isset($this->schema[$relation]); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get associated relation instance. |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $relation |
|
85
|
|
|
* |
|
86
|
|
|
* @return RelationInterface |
|
87
|
|
|
*/ |
|
88
|
|
|
public function get(string $relation): RelationInterface |
|
|
|
|
|
|
89
|
|
|
{ |
|
90
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function set(string $relation, $value) |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function flush(string $relation) |
|
|
|
|
|
|
99
|
|
|
{ |
|
100
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Information about loaded relations. |
|
105
|
|
|
* |
|
106
|
|
|
* @return array |
|
107
|
|
|
*/ |
|
108
|
|
|
public function __debugInfo() |
|
109
|
|
|
{ |
|
110
|
|
|
$relations = []; |
|
111
|
|
|
|
|
112
|
|
|
foreach ($this->schema as $name => $content) { |
|
113
|
|
|
if (!array_key_exists($name, $this->relations)) { |
|
114
|
|
|
$relations[$name] = 'none'; |
|
115
|
|
|
continue; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$relations[$name] = empty($this->relations[$name]) ? 'empty' : 'loaded'; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $relations; |
|
122
|
|
|
} |
|
123
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..