|
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(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Returns true if the property is the primary key. |
|
78
|
|
|
* |
|
79
|
|
|
* @return bool |
|
80
|
|
|
*/ |
|
81
|
|
|
public function isPrimaryKey() |
|
82
|
|
|
{ |
|
83
|
|
|
return in_array($this->column->getName(), $this->table->getPrimaryKeyColumns()); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Returns the PHP code for getters and setters. |
|
88
|
|
|
* |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getGetterSetterCode() |
|
92
|
|
|
{ |
|
93
|
|
|
$type = $this->column->getType(); |
|
94
|
|
|
$normalizedType = TDBMDaoGenerator::dbalTypeToPhpType($type); |
|
95
|
|
|
|
|
96
|
|
|
$columnGetterName = $this->getGetterName(); |
|
97
|
|
|
$columnSetterName = $this->getSetterName(); |
|
98
|
|
|
|
|
99
|
|
|
if ($normalizedType == '\\DateTimeInterface') { |
|
100
|
|
|
$castTo = '\\DateTimeInterface '; |
|
101
|
|
|
} else { |
|
102
|
|
|
$castTo = ''; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$getterAndSetterCode = ' /** |
|
106
|
|
|
* The getter for the "%s" column. |
|
107
|
|
|
* |
|
108
|
|
|
* @return %s |
|
109
|
|
|
*/ |
|
110
|
|
|
public function %s() { |
|
111
|
|
|
return $this->get(%s, %s); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* The setter for the "%s" column. |
|
116
|
|
|
* |
|
117
|
|
|
* @param %s $%s |
|
118
|
|
|
*/ |
|
119
|
|
|
public function %s(%s$%s) { |
|
120
|
|
|
$this->set(%s, $%s, %s); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
'; |
|
124
|
|
|
|
|
125
|
|
|
return sprintf($getterAndSetterCode, |
|
126
|
|
|
// Getter |
|
127
|
|
|
$this->column->getName(), |
|
128
|
|
|
$normalizedType, |
|
129
|
|
|
$columnGetterName, |
|
130
|
|
|
var_export($this->column->getName(), true), |
|
131
|
|
|
var_export($this->table->getName(), true), |
|
132
|
|
|
// Setter |
|
133
|
|
|
$this->column->getName(), |
|
134
|
|
|
$normalizedType, |
|
135
|
|
|
$this->column->getName(), |
|
136
|
|
|
$columnSetterName, |
|
137
|
|
|
$castTo, |
|
138
|
|
|
$this->column->getName(), |
|
139
|
|
|
var_export($this->column->getName(), true), |
|
140
|
|
|
$this->column->getName(), |
|
141
|
|
|
var_export($this->table->getName(), true) |
|
142
|
|
|
); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Returns the part of code useful when doing json serialization. |
|
147
|
|
|
* |
|
148
|
|
|
* @return string |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getJsonSerializeCode() |
|
151
|
|
|
{ |
|
152
|
|
|
$type = $this->column->getType(); |
|
153
|
|
|
$normalizedType = TDBMDaoGenerator::dbalTypeToPhpType($type); |
|
154
|
|
|
|
|
155
|
|
|
if ($normalizedType == '\\DateTimeInterface') { |
|
156
|
|
|
return ' $array['.var_export($this->getLowerCamelCaseName(), true).'] = $this->'.$this->getGetterName()."()->format('c');\n"; |
|
157
|
|
|
} else { |
|
158
|
|
|
return ' $array['.var_export($this->getLowerCamelCaseName(), true).'] = $this->'.$this->getGetterName()."();\n"; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Returns the column name. |
|
164
|
|
|
* |
|
165
|
|
|
* @return string |
|
166
|
|
|
*/ |
|
167
|
|
|
public function getColumnName() { |
|
168
|
|
|
return $this->column->getName(); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|