1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mouf\Database\TDBM\Utils; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Schema\Column; |
6
|
|
|
use Doctrine\DBAL\Schema\Table; |
7
|
|
|
use Doctrine\DBAL\Schema\ForeignKeyConstraint; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This class represent a property in a bean (a property has a getter, a setter, etc...). |
11
|
|
|
*/ |
12
|
|
|
class ScalarBeanPropertyDescriptor extends AbstractBeanPropertyDescriptor |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var Column |
16
|
|
|
*/ |
17
|
|
|
private $column; |
18
|
|
|
|
19
|
|
|
public function __construct(Table $table, Column $column) |
20
|
|
|
{ |
21
|
|
|
parent::__construct($table); |
22
|
|
|
$this->table = $table; |
23
|
|
|
$this->column = $column; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Returns the foreign-key the column is part of, if any. null otherwise. |
28
|
|
|
* |
29
|
|
|
* @return ForeignKeyConstraint|null |
30
|
|
|
*/ |
31
|
|
|
public function getForeignKey() |
32
|
|
|
{ |
33
|
|
|
return false; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Returns the param annotation for this property (useful for constructor). |
38
|
|
|
* |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
|
|
public function getParamAnnotation() |
42
|
|
|
{ |
43
|
|
|
$className = $this->getClassName(); |
44
|
|
|
$paramType = $className ?: TDBMDaoGenerator::dbalTypeToPhpType($this->column->getType()); |
45
|
|
|
|
46
|
|
|
$str = ' * @param %s %s'; |
47
|
|
|
|
48
|
|
|
return sprintf($str, $paramType, $this->getVariableName()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getUpperCamelCaseName() |
52
|
|
|
{ |
53
|
|
|
return TDBMDaoGenerator::toCamelCase($this->column->getName()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns the name of the class linked to this property or null if this is not a foreign key. |
58
|
|
|
* |
59
|
|
|
* @return null|string |
60
|
|
|
*/ |
61
|
|
|
public function getClassName() |
62
|
|
|
{ |
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Returns true if the property is compulsory (and therefore should be fetched in the constructor). |
68
|
|
|
* |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
public function isCompulsory() |
72
|
|
|
{ |
73
|
|
|
return $this->column->getNotnull() && !$this->column->getAutoincrement() && $this->column->getDefault() === null; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns true if the property has a default value. |
78
|
|
|
* |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
public function hasDefault() |
82
|
|
|
{ |
83
|
|
|
return $this->column->getDefault() !== null; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns the code that assigns a value to its default value. |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function assignToDefaultCode() |
92
|
|
|
{ |
93
|
|
|
$str = ' $this->%s(%s);'; |
94
|
|
|
|
95
|
|
|
$default = $this->column->getDefault(); |
96
|
|
|
|
97
|
|
|
if (strtoupper($default) === 'CURRENT_TIMESTAMP') { |
98
|
|
|
$defaultCode = 'new \DateTimeImmutable()'; |
99
|
|
|
} else { |
100
|
|
|
$defaultCode = var_export($this->column->getDefault(), true); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return sprintf($str, $this->getSetterName(), $defaultCode); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns true if the property is the primary key. |
108
|
|
|
* |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
|
|
public function isPrimaryKey() |
112
|
|
|
{ |
113
|
|
|
return in_array($this->column->getName(), $this->table->getPrimaryKeyColumns()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Returns the PHP code for getters and setters. |
118
|
|
|
* |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
public function getGetterSetterCode() |
122
|
|
|
{ |
123
|
|
|
$type = $this->column->getType(); |
124
|
|
|
$normalizedType = TDBMDaoGenerator::dbalTypeToPhpType($type); |
125
|
|
|
|
126
|
|
|
$columnGetterName = $this->getGetterName(); |
127
|
|
|
$columnSetterName = $this->getSetterName(); |
128
|
|
|
|
129
|
|
|
if ($normalizedType == '\\DateTimeInterface') { |
130
|
|
|
$castTo = '\\DateTimeInterface '; |
131
|
|
|
} else { |
132
|
|
|
$castTo = ''; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$getterAndSetterCode = ' /** |
136
|
|
|
* The getter for the "%s" column. |
137
|
|
|
* |
138
|
|
|
* @return %s |
139
|
|
|
*/ |
140
|
|
|
public function %s() { |
141
|
|
|
return $this->get(%s, %s); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* The setter for the "%s" column. |
146
|
|
|
* |
147
|
|
|
* @param %s $%s |
148
|
|
|
*/ |
149
|
|
|
public function %s(%s$%s) { |
150
|
|
|
$this->set(%s, $%s, %s); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
'; |
154
|
|
|
|
155
|
|
|
return sprintf($getterAndSetterCode, |
156
|
|
|
// Getter |
157
|
|
|
$this->column->getName(), |
158
|
|
|
$normalizedType, |
159
|
|
|
$columnGetterName, |
160
|
|
|
var_export($this->column->getName(), true), |
161
|
|
|
var_export($this->table->getName(), true), |
162
|
|
|
// Setter |
163
|
|
|
$this->column->getName(), |
164
|
|
|
$normalizedType, |
165
|
|
|
$this->column->getName(), |
166
|
|
|
$columnSetterName, |
167
|
|
|
$castTo, |
168
|
|
|
$this->column->getName().($castTo ? ($this->column->getNotnull() ? '' : ' = null') : ''), |
169
|
|
|
var_export($this->column->getName(), true), |
170
|
|
|
$this->column->getName(), |
171
|
|
|
var_export($this->table->getName(), true) |
172
|
|
|
); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Returns the part of code useful when doing json serialization. |
177
|
|
|
* |
178
|
|
|
* @return string |
179
|
|
|
*/ |
180
|
|
|
public function getJsonSerializeCode() |
181
|
|
|
{ |
182
|
|
|
$type = $this->column->getType(); |
183
|
|
|
$normalizedType = TDBMDaoGenerator::dbalTypeToPhpType($type); |
184
|
|
|
|
185
|
|
|
if ($normalizedType == '\\DateTimeInterface') { |
186
|
|
|
return ' $array['.var_export($this->getLowerCamelCaseName(), true).'] = ($this->'.$this->getGetterName().'() === null)?null:$this->'.$this->getGetterName()."()->format('c');\n"; |
187
|
|
|
} else { |
188
|
|
|
return ' $array['.var_export($this->getLowerCamelCaseName(), true).'] = $this->'.$this->getGetterName()."();\n"; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Returns the column name. |
194
|
|
|
* |
195
|
|
|
* @return string |
196
|
|
|
*/ |
197
|
|
|
public function getColumnName() |
198
|
|
|
{ |
199
|
|
|
return $this->column->getName(); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|