1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* Spiral Framework. |
4
|
|
|
* |
5
|
|
|
* @license MIT |
6
|
|
|
* @author Anton Titov (Wolfy-J) |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Cycle\Annotated\Annotation\Relation; |
10
|
|
|
|
11
|
|
|
use Spiral\Annotations\AnnotationInterface; |
12
|
|
|
use Spiral\Annotations\Parser; |
13
|
|
|
|
14
|
|
|
abstract class Relation implements RelationInterface, AnnotationInterface |
15
|
|
|
{ |
16
|
|
|
protected const NAME = ''; |
17
|
|
|
protected const OPTIONS = [ |
18
|
|
|
'cascade' => Parser::BOOL, |
19
|
|
|
'nullable' => Parser::BOOL, |
20
|
|
|
'innerKey' => Parser::STRING, |
21
|
|
|
'outerKey' => Parser::STRING, |
22
|
|
|
'morphKey' => Parser::STRING, |
23
|
|
|
'morphKeyLength' => Parser::INTEGER, |
24
|
|
|
'though' => Parser::STRING, |
25
|
|
|
'thoughInnerKey' => Parser::STRING, |
26
|
|
|
'thoughOuterKey' => Parser::STRING, |
27
|
|
|
'thoughConstrain' => Parser::STRING, |
28
|
|
|
'thoughWhere' => [Parser::MIXED], |
29
|
|
|
'where' => [Parser::MIXED], |
30
|
|
|
'fkCreate' => Parser::BOOL, |
31
|
|
|
'fkAction' => Parser::STRING, |
32
|
|
|
'indexCreate' => Parser::BOOL, |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** @var string|null */ |
36
|
|
|
protected $target; |
37
|
|
|
|
38
|
|
|
/** @var Inverse|null */ |
39
|
|
|
protected $inverse; |
40
|
|
|
|
41
|
|
|
/** @var array */ |
42
|
|
|
protected $options = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Public and unique node name. |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function getName(): string |
50
|
|
|
{ |
51
|
|
|
return static::NAME; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Return Node schema in a form of [name => Node|SCALAR|[Node]]. |
56
|
|
|
* |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
public function getSchema(): array |
60
|
|
|
{ |
61
|
|
|
$schema = static::OPTIONS + [ |
62
|
|
|
'target' => Parser::STRING, |
63
|
|
|
'inverse' => Inverse::class, |
64
|
|
|
'load' => Parser::STRING, |
65
|
|
|
'fetch' => Parser::STRING // alias to `load` |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
array_walk_recursive($schema, function (&$v) { |
69
|
|
|
if (is_string($v) && class_exists($v)) { |
70
|
|
|
$v = new $v; |
71
|
|
|
} |
72
|
|
|
}); |
73
|
|
|
|
74
|
|
|
return $schema; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Set node attribute value. |
79
|
|
|
* |
80
|
|
|
* @param string $name |
81
|
|
|
* @param mixed $value |
82
|
|
|
*/ |
83
|
|
|
public function setAttribute(string $name, $value) |
84
|
|
|
{ |
85
|
|
|
if (in_array($name, ['target', 'inverse'])) { |
86
|
|
|
$this->{$name} = $value; |
87
|
|
|
return; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (in_array($name, ['load', 'fetch'])) { |
91
|
|
|
$name = 'load'; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$this->options[$name] = $value; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return string|null |
99
|
|
|
*/ |
100
|
|
|
public function getTarget(): ?string |
101
|
|
|
{ |
102
|
|
|
return $this->target; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
public function getOptions(): array |
109
|
|
|
{ |
110
|
|
|
return $this->options; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return string|null |
115
|
|
|
*/ |
116
|
|
|
public function getLoadMethod(): ?string |
117
|
|
|
{ |
118
|
|
|
return $this->options['load'] ?? null; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
|
public function isInversed(): bool |
125
|
|
|
{ |
126
|
|
|
return $this->inverse !== null && $this->inverse->isValid(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
public function getInverseType(): string |
133
|
|
|
{ |
134
|
|
|
return $this->inverse->getType(); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
public function getInverseName(): string |
141
|
|
|
{ |
142
|
|
|
return $this->inverse->getRelation(); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return int|null |
147
|
|
|
*/ |
148
|
|
|
public function getInverseLoadMethod(): ?int |
149
|
|
|
{ |
150
|
|
|
return $this->inverse->getLoadMethod(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
} |