|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace TheCodingMachine\TDBM\Utils; |
|
5
|
|
|
|
|
6
|
|
|
use Doctrine\DBAL\Schema\Table; |
|
7
|
|
|
use Zend\Code\Generator\DocBlock\Tag\ParamTag; |
|
8
|
|
|
use Zend\Code\Generator\MethodGenerator; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* This class represent a property in a bean (a property has a getter, a setter, etc...). |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract class AbstractBeanPropertyDescriptor implements MethodDescriptorInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var Table |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $table; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Whether to use the more complex name in case of conflict. |
|
22
|
|
|
* |
|
23
|
|
|
* @var bool |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $alternativeName = false; |
|
26
|
|
|
/** |
|
27
|
|
|
* @var NamingStrategyInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $namingStrategy; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param Table $table |
|
33
|
|
|
* @param NamingStrategyInterface $namingStrategy |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(Table $table, NamingStrategyInterface $namingStrategy) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->table = $table; |
|
38
|
|
|
$this->namingStrategy = $namingStrategy; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Use the more complex name in case of conflict. |
|
43
|
|
|
*/ |
|
44
|
|
|
public function useAlternativeName(): void |
|
45
|
|
|
{ |
|
46
|
|
|
$this->alternativeName = true; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Returns the name of the class linked to this property or null if this is not a foreign key. |
|
51
|
|
|
* |
|
52
|
|
|
* @return null|string |
|
53
|
|
|
*/ |
|
54
|
|
|
abstract public function getClassName(): ?string; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Returns the PHP type for the property (it can be a scalar like int, bool, or class names, like \DateTimeInterface, App\Bean\User....) |
|
58
|
|
|
* |
|
59
|
|
|
* @return string |
|
60
|
|
|
*/ |
|
61
|
|
|
abstract public function getPhpType(): string; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Returns the param annotation for this property (useful for constructor). |
|
65
|
|
|
* |
|
66
|
|
|
* @return ParamTag |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getParamAnnotation(): ParamTag |
|
69
|
|
|
{ |
|
70
|
|
|
return new ParamTag($this->getVariableName(), [ $this->getPhpType() ]); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function getVariableName(): string |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->namingStrategy->getVariableName($this); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function getSetterName(): string |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->namingStrategy->getSetterName($this); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Alias of the method getGetterName(). Used to validate MethodDescriptorInterface |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getName(): string |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->getGetterName(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function getGetterName(): string |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->namingStrategy->getGetterName($this); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Returns the PHP code used in the ben constructor for this property. |
|
98
|
|
|
* |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getConstructorAssignCode(): string |
|
102
|
|
|
{ |
|
103
|
|
|
$str = '$this->%s(%s);'; |
|
104
|
|
|
|
|
105
|
|
|
return sprintf($str, $this->getSetterName(), $this->getVariableName()); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Returns true if the property is compulsory (and therefore should be fetched in the constructor). |
|
110
|
|
|
* |
|
111
|
|
|
* @return bool |
|
112
|
|
|
*/ |
|
113
|
|
|
abstract public function isCompulsory(): bool; |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Returns true if the property has a default value. |
|
117
|
|
|
* |
|
118
|
|
|
* @return bool |
|
119
|
|
|
*/ |
|
120
|
|
|
abstract public function hasDefault(): bool; |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Returns the code that assigns a value to its default value. |
|
124
|
|
|
* |
|
125
|
|
|
* @return string |
|
126
|
|
|
*/ |
|
127
|
|
|
abstract public function assignToDefaultCode(): string; |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Returns true if the property is the primary key. |
|
131
|
|
|
* |
|
132
|
|
|
* @return bool |
|
133
|
|
|
*/ |
|
134
|
|
|
abstract public function isPrimaryKey(): bool; |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @return Table |
|
138
|
|
|
*/ |
|
139
|
|
|
public function getTable(): Table |
|
140
|
|
|
{ |
|
141
|
|
|
return $this->table; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Returns the PHP code for getters and setters. |
|
146
|
|
|
* |
|
147
|
|
|
* @return MethodGenerator[] |
|
148
|
|
|
*/ |
|
149
|
|
|
abstract public function getGetterSetterCode(): array; |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Returns the part of code useful when doing json serialization. |
|
153
|
|
|
* |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
|
|
abstract public function getJsonSerializeCode(): string; |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @return bool |
|
160
|
|
|
*/ |
|
161
|
|
|
public function isAlternativeName(): bool |
|
162
|
|
|
{ |
|
163
|
|
|
return $this->alternativeName; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* The code to past in the __clone method. |
|
168
|
|
|
* @return null|string |
|
169
|
|
|
*/ |
|
170
|
|
|
abstract public function getCloneRule(): ?string; |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Tells if this property is a type-hintable in PHP (resource isn't for example) |
|
174
|
|
|
* |
|
175
|
|
|
* @return bool |
|
176
|
|
|
*/ |
|
177
|
|
|
abstract public function isTypeHintable() : bool; |
|
178
|
|
|
} |
|
179
|
|
|
|