1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheCodingMachine\TDBM\Utils; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Schema\Column; |
6
|
|
|
use Doctrine\DBAL\Schema\Table; |
7
|
|
|
use Doctrine\DBAL\Schema\ForeignKeyConstraint; |
8
|
|
|
use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* This class represent a property in a bean that points to another table. |
12
|
|
|
*/ |
13
|
|
|
class ObjectBeanPropertyDescriptor extends AbstractBeanPropertyDescriptor |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var ForeignKeyConstraint |
17
|
|
|
*/ |
18
|
|
|
private $foreignKey; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var SchemaAnalyzer |
22
|
|
|
*/ |
23
|
|
|
private $schemaAnalyzer; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* ObjectBeanPropertyDescriptor constructor. |
27
|
|
|
* @param Table $table |
28
|
|
|
* @param ForeignKeyConstraint $foreignKey |
29
|
|
|
* @param SchemaAnalyzer $schemaAnalyzer |
30
|
|
|
* @param NamingStrategyInterface $namingStrategy |
31
|
|
|
*/ |
32
|
|
|
public function __construct(Table $table, ForeignKeyConstraint $foreignKey, SchemaAnalyzer $schemaAnalyzer, NamingStrategyInterface $namingStrategy) |
33
|
|
|
{ |
34
|
|
|
parent::__construct($table, $namingStrategy); |
35
|
|
|
$this->foreignKey = $foreignKey; |
36
|
|
|
$this->schemaAnalyzer = $schemaAnalyzer; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Returns the foreignkey the column is part of, if any. null otherwise. |
41
|
|
|
* |
42
|
|
|
* @return ForeignKeyConstraint|null |
43
|
|
|
*/ |
44
|
|
|
public function getForeignKey() |
45
|
|
|
{ |
46
|
|
|
return $this->foreignKey; |
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
|
|
|
public function getClassName() |
55
|
|
|
{ |
56
|
|
|
return $this->namingStrategy->getBeanClassName($this->foreignKey->getForeignTableName()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns the param annotation for this property (useful for constructor). |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public function getParamAnnotation() |
65
|
|
|
{ |
66
|
|
|
$str = ' * @param %s %s'; |
67
|
|
|
|
68
|
|
|
return sprintf($str, $this->getClassName(), $this->getVariableName()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Returns true if the property is compulsory (and therefore should be fetched in the constructor). |
73
|
|
|
* |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
|
public function isCompulsory() |
77
|
|
|
{ |
78
|
|
|
// Are all columns nullable? |
79
|
|
|
$localColumnNames = $this->foreignKey->getLocalColumns(); |
80
|
|
|
|
81
|
|
|
foreach ($localColumnNames as $name) { |
82
|
|
|
$column = $this->table->getColumn($name); |
83
|
|
|
if ($column->getNotnull()) { |
84
|
|
|
return true; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Returns true if the property has a default value. |
93
|
|
|
* |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
|
|
public function hasDefault() |
97
|
|
|
{ |
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns the code that assigns a value to its default value. |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
* |
106
|
|
|
* @throws \TDBMException |
107
|
|
|
*/ |
108
|
|
|
public function assignToDefaultCode() |
109
|
|
|
{ |
110
|
|
|
throw new \TDBMException('Foreign key based properties cannot be assigned a default value.'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Returns true if the property is the primary key. |
115
|
|
|
* |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
|
|
public function isPrimaryKey() |
119
|
|
|
{ |
120
|
|
|
$fkColumns = $this->foreignKey->getLocalColumns(); |
121
|
|
|
sort($fkColumns); |
122
|
|
|
|
123
|
|
|
$pkColumns = $this->table->getPrimaryKeyColumns(); |
124
|
|
|
sort($pkColumns); |
125
|
|
|
|
126
|
|
|
return $fkColumns == $pkColumns; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Returns the PHP code for getters and setters. |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
public function getGetterSetterCode() |
135
|
|
|
{ |
136
|
|
|
$tableName = $this->table->getName(); |
137
|
|
|
$getterName = $this->getGetterName(); |
138
|
|
|
$setterName = $this->getSetterName(); |
139
|
|
|
$isNullable = !$this->isCompulsory(); |
140
|
|
|
|
141
|
|
|
$referencedBeanName = $this->namingStrategy->getBeanClassName($this->foreignKey->getForeignTableName()); |
142
|
|
|
|
143
|
|
|
$str = ' /** |
144
|
|
|
* Returns the '.$referencedBeanName.' object bound to this object via the '.implode(' and ', $this->foreignKey->getLocalColumns()).' column. |
145
|
|
|
* |
146
|
|
|
* @return '.$referencedBeanName.($isNullable?'|null':'').' |
147
|
|
|
*/ |
148
|
|
|
public function '.$getterName.'(): '.($isNullable?'?':'').$referencedBeanName.' |
149
|
|
|
{ |
150
|
|
|
return $this->getRef('.var_export($this->foreignKey->getName(), true).', '.var_export($tableName, true).'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* The setter for the '.$referencedBeanName.' object bound to this object via the '.implode(' and ', $this->foreignKey->getLocalColumns()).' column. |
155
|
|
|
* |
156
|
|
|
* @param '.$referencedBeanName.($isNullable?'|null':'').' $object |
157
|
|
|
*/ |
158
|
|
|
public function '.$setterName.'('.($isNullable?'?':'').$referencedBeanName.' $object) : void |
159
|
|
|
{ |
160
|
|
|
$this->setRef('.var_export($this->foreignKey->getName(), true).', $object, '.var_export($tableName, true).'); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
'; |
164
|
|
|
|
165
|
|
|
return $str; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Returns the part of code useful when doing json serialization. |
170
|
|
|
* |
171
|
|
|
* @return string |
172
|
|
|
*/ |
173
|
|
|
public function getJsonSerializeCode() |
174
|
|
|
{ |
175
|
|
|
return ' if (!$stopRecursion) { |
176
|
|
|
$object = $this->'.$this->getGetterName().'(); |
177
|
|
|
$array['.var_export($this->namingStrategy->getJsonProperty($this), true).'] = $object ? $object->jsonSerialize(true) : null; |
178
|
|
|
} |
179
|
|
|
'; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
} |
183
|
|
|
|