|
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 Inversed|null */ |
|
39
|
|
|
protected $inversed; |
|
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
|
|
|
return static::OPTIONS + [ |
|
62
|
|
|
'target' => Parser::STRING, |
|
63
|
|
|
'inversed' => Inversed::class |
|
64
|
|
|
]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Set node attribute value. |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $name |
|
71
|
|
|
* @param mixed $value |
|
72
|
|
|
*/ |
|
73
|
|
|
public function setAttribute(string $name, $value) |
|
74
|
|
|
{ |
|
75
|
|
|
if (in_array($name, ['target', 'inversed'])) { |
|
76
|
|
|
$this->{$name} = $value; |
|
77
|
|
|
return; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$this->options[$name] = $value; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return string|null |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getTarget(): ?string |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->target; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return array |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getOptions(): array |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->options; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return bool |
|
101
|
|
|
*/ |
|
102
|
|
|
public function isInversed(): bool |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->inversed !== null && $this->inversed->isValid(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getInverseType(): string |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->inversed->getType(); |
|
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getInverseName(): string |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->inversed->getRelation(); |
|
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
} |