@@ -12,169 +12,169 @@ discard block |
||
12 | 12 | */ |
13 | 13 | class ObjectBeanPropertyDescriptor extends AbstractBeanPropertyDescriptor |
14 | 14 | { |
15 | - /** |
|
16 | - * @var ForeignKeyConstraint |
|
17 | - */ |
|
18 | - private $foreignKey; |
|
19 | - |
|
20 | - /** |
|
21 | - * @var SchemaAnalyzer |
|
22 | - */ |
|
23 | - private $schemaAnalyzer; |
|
24 | - /** |
|
25 | - * @var NamingStrategyInterface |
|
26 | - */ |
|
27 | - private $namingStrategy; |
|
28 | - |
|
29 | - /** |
|
30 | - * ObjectBeanPropertyDescriptor constructor. |
|
31 | - * @param Table $table |
|
32 | - * @param ForeignKeyConstraint $foreignKey |
|
33 | - * @param SchemaAnalyzer $schemaAnalyzer |
|
34 | - * @param NamingStrategyInterface $namingStrategy |
|
35 | - */ |
|
36 | - public function __construct(Table $table, ForeignKeyConstraint $foreignKey, SchemaAnalyzer $schemaAnalyzer, NamingStrategyInterface $namingStrategy) |
|
37 | - { |
|
38 | - parent::__construct($table); |
|
39 | - $this->foreignKey = $foreignKey; |
|
40 | - $this->schemaAnalyzer = $schemaAnalyzer; |
|
41 | - $this->namingStrategy = $namingStrategy; |
|
42 | - } |
|
43 | - |
|
44 | - /** |
|
45 | - * Returns the foreignkey the column is part of, if any. null otherwise. |
|
46 | - * |
|
47 | - * @return ForeignKeyConstraint|null |
|
48 | - */ |
|
49 | - public function getForeignKey() |
|
50 | - { |
|
51 | - return $this->foreignKey; |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * Returns the name of the class linked to this property or null if this is not a foreign key. |
|
56 | - * |
|
57 | - * @return null|string |
|
58 | - */ |
|
59 | - public function getClassName() |
|
60 | - { |
|
61 | - return $this->namingStrategy->getBeanClassName($this->foreignKey->getForeignTableName()); |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * Returns the param annotation for this property (useful for constructor). |
|
66 | - * |
|
67 | - * @return string |
|
68 | - */ |
|
69 | - public function getParamAnnotation() |
|
70 | - { |
|
71 | - $str = ' * @param %s %s'; |
|
72 | - |
|
73 | - return sprintf($str, $this->getClassName(), $this->getVariableName()); |
|
74 | - } |
|
75 | - |
|
76 | - public function getUpperCamelCaseName() |
|
77 | - { |
|
78 | - // First, are there many column or only one? |
|
79 | - // If one column, we name the setter after it. Otherwise, we name the setter after the table name |
|
80 | - if (count($this->foreignKey->getLocalColumns()) > 1) { |
|
81 | - $name = TDBMDaoGenerator::toSingular(TDBMDaoGenerator::toCamelCase($this->foreignKey->getForeignTableName())); |
|
82 | - if ($this->alternativeName) { |
|
83 | - $camelizedColumns = array_map(['Mouf\\Database\\TDBM\\Utils\\TDBMDaoGenerator', 'toCamelCase'], $this->foreignKey->getLocalColumns()); |
|
84 | - |
|
85 | - $name .= 'By'.implode('And', $camelizedColumns); |
|
86 | - } |
|
87 | - } else { |
|
88 | - $column = $this->foreignKey->getLocalColumns()[0]; |
|
89 | - // Let's remove any _id or id_. |
|
90 | - if (strpos(strtolower($column), 'id_') === 0) { |
|
91 | - $column = substr($column, 3); |
|
92 | - } |
|
93 | - if (strrpos(strtolower($column), '_id') === strlen($column) - 3) { |
|
94 | - $column = substr($column, 0, strlen($column) - 3); |
|
95 | - } |
|
96 | - $name = TDBMDaoGenerator::toCamelCase($column); |
|
97 | - if ($this->alternativeName) { |
|
98 | - $name .= 'Object'; |
|
99 | - } |
|
100 | - } |
|
101 | - |
|
102 | - return $name; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * Returns true if the property is compulsory (and therefore should be fetched in the constructor). |
|
107 | - * |
|
108 | - * @return bool |
|
109 | - */ |
|
110 | - public function isCompulsory() |
|
111 | - { |
|
112 | - // Are all columns nullable? |
|
113 | - $localColumnNames = $this->foreignKey->getLocalColumns(); |
|
114 | - |
|
115 | - foreach ($localColumnNames as $name) { |
|
116 | - $column = $this->table->getColumn($name); |
|
117 | - if ($column->getNotnull()) { |
|
118 | - return true; |
|
119 | - } |
|
120 | - } |
|
121 | - |
|
122 | - return false; |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Returns true if the property has a default value. |
|
127 | - * |
|
128 | - * @return bool |
|
129 | - */ |
|
130 | - public function hasDefault() |
|
131 | - { |
|
132 | - return false; |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * Returns the code that assigns a value to its default value. |
|
137 | - * |
|
138 | - * @return string |
|
139 | - * |
|
140 | - * @throws \TDBMException |
|
141 | - */ |
|
142 | - public function assignToDefaultCode() |
|
143 | - { |
|
144 | - throw new \TDBMException('Foreign key based properties cannot be assigned a default value.'); |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * Returns true if the property is the primary key. |
|
149 | - * |
|
150 | - * @return bool |
|
151 | - */ |
|
152 | - public function isPrimaryKey() |
|
153 | - { |
|
154 | - $fkColumns = $this->foreignKey->getLocalColumns(); |
|
155 | - sort($fkColumns); |
|
156 | - |
|
157 | - $pkColumns = $this->table->getPrimaryKeyColumns(); |
|
158 | - sort($pkColumns); |
|
159 | - |
|
160 | - return $fkColumns == $pkColumns; |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * Returns the PHP code for getters and setters. |
|
165 | - * |
|
166 | - * @return string |
|
167 | - */ |
|
168 | - public function getGetterSetterCode() |
|
169 | - { |
|
170 | - $tableName = $this->table->getName(); |
|
171 | - $getterName = $this->getGetterName(); |
|
172 | - $setterName = $this->getSetterName(); |
|
173 | - $isNullable = !$this->isCompulsory(); |
|
174 | - |
|
175 | - $referencedBeanName = $this->namingStrategy->getBeanClassName($this->foreignKey->getForeignTableName()); |
|
176 | - |
|
177 | - $str = ' /** |
|
15 | + /** |
|
16 | + * @var ForeignKeyConstraint |
|
17 | + */ |
|
18 | + private $foreignKey; |
|
19 | + |
|
20 | + /** |
|
21 | + * @var SchemaAnalyzer |
|
22 | + */ |
|
23 | + private $schemaAnalyzer; |
|
24 | + /** |
|
25 | + * @var NamingStrategyInterface |
|
26 | + */ |
|
27 | + private $namingStrategy; |
|
28 | + |
|
29 | + /** |
|
30 | + * ObjectBeanPropertyDescriptor constructor. |
|
31 | + * @param Table $table |
|
32 | + * @param ForeignKeyConstraint $foreignKey |
|
33 | + * @param SchemaAnalyzer $schemaAnalyzer |
|
34 | + * @param NamingStrategyInterface $namingStrategy |
|
35 | + */ |
|
36 | + public function __construct(Table $table, ForeignKeyConstraint $foreignKey, SchemaAnalyzer $schemaAnalyzer, NamingStrategyInterface $namingStrategy) |
|
37 | + { |
|
38 | + parent::__construct($table); |
|
39 | + $this->foreignKey = $foreignKey; |
|
40 | + $this->schemaAnalyzer = $schemaAnalyzer; |
|
41 | + $this->namingStrategy = $namingStrategy; |
|
42 | + } |
|
43 | + |
|
44 | + /** |
|
45 | + * Returns the foreignkey the column is part of, if any. null otherwise. |
|
46 | + * |
|
47 | + * @return ForeignKeyConstraint|null |
|
48 | + */ |
|
49 | + public function getForeignKey() |
|
50 | + { |
|
51 | + return $this->foreignKey; |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * Returns the name of the class linked to this property or null if this is not a foreign key. |
|
56 | + * |
|
57 | + * @return null|string |
|
58 | + */ |
|
59 | + public function getClassName() |
|
60 | + { |
|
61 | + return $this->namingStrategy->getBeanClassName($this->foreignKey->getForeignTableName()); |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * Returns the param annotation for this property (useful for constructor). |
|
66 | + * |
|
67 | + * @return string |
|
68 | + */ |
|
69 | + public function getParamAnnotation() |
|
70 | + { |
|
71 | + $str = ' * @param %s %s'; |
|
72 | + |
|
73 | + return sprintf($str, $this->getClassName(), $this->getVariableName()); |
|
74 | + } |
|
75 | + |
|
76 | + public function getUpperCamelCaseName() |
|
77 | + { |
|
78 | + // First, are there many column or only one? |
|
79 | + // If one column, we name the setter after it. Otherwise, we name the setter after the table name |
|
80 | + if (count($this->foreignKey->getLocalColumns()) > 1) { |
|
81 | + $name = TDBMDaoGenerator::toSingular(TDBMDaoGenerator::toCamelCase($this->foreignKey->getForeignTableName())); |
|
82 | + if ($this->alternativeName) { |
|
83 | + $camelizedColumns = array_map(['Mouf\\Database\\TDBM\\Utils\\TDBMDaoGenerator', 'toCamelCase'], $this->foreignKey->getLocalColumns()); |
|
84 | + |
|
85 | + $name .= 'By'.implode('And', $camelizedColumns); |
|
86 | + } |
|
87 | + } else { |
|
88 | + $column = $this->foreignKey->getLocalColumns()[0]; |
|
89 | + // Let's remove any _id or id_. |
|
90 | + if (strpos(strtolower($column), 'id_') === 0) { |
|
91 | + $column = substr($column, 3); |
|
92 | + } |
|
93 | + if (strrpos(strtolower($column), '_id') === strlen($column) - 3) { |
|
94 | + $column = substr($column, 0, strlen($column) - 3); |
|
95 | + } |
|
96 | + $name = TDBMDaoGenerator::toCamelCase($column); |
|
97 | + if ($this->alternativeName) { |
|
98 | + $name .= 'Object'; |
|
99 | + } |
|
100 | + } |
|
101 | + |
|
102 | + return $name; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * Returns true if the property is compulsory (and therefore should be fetched in the constructor). |
|
107 | + * |
|
108 | + * @return bool |
|
109 | + */ |
|
110 | + public function isCompulsory() |
|
111 | + { |
|
112 | + // Are all columns nullable? |
|
113 | + $localColumnNames = $this->foreignKey->getLocalColumns(); |
|
114 | + |
|
115 | + foreach ($localColumnNames as $name) { |
|
116 | + $column = $this->table->getColumn($name); |
|
117 | + if ($column->getNotnull()) { |
|
118 | + return true; |
|
119 | + } |
|
120 | + } |
|
121 | + |
|
122 | + return false; |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Returns true if the property has a default value. |
|
127 | + * |
|
128 | + * @return bool |
|
129 | + */ |
|
130 | + public function hasDefault() |
|
131 | + { |
|
132 | + return false; |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * Returns the code that assigns a value to its default value. |
|
137 | + * |
|
138 | + * @return string |
|
139 | + * |
|
140 | + * @throws \TDBMException |
|
141 | + */ |
|
142 | + public function assignToDefaultCode() |
|
143 | + { |
|
144 | + throw new \TDBMException('Foreign key based properties cannot be assigned a default value.'); |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * Returns true if the property is the primary key. |
|
149 | + * |
|
150 | + * @return bool |
|
151 | + */ |
|
152 | + public function isPrimaryKey() |
|
153 | + { |
|
154 | + $fkColumns = $this->foreignKey->getLocalColumns(); |
|
155 | + sort($fkColumns); |
|
156 | + |
|
157 | + $pkColumns = $this->table->getPrimaryKeyColumns(); |
|
158 | + sort($pkColumns); |
|
159 | + |
|
160 | + return $fkColumns == $pkColumns; |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * Returns the PHP code for getters and setters. |
|
165 | + * |
|
166 | + * @return string |
|
167 | + */ |
|
168 | + public function getGetterSetterCode() |
|
169 | + { |
|
170 | + $tableName = $this->table->getName(); |
|
171 | + $getterName = $this->getGetterName(); |
|
172 | + $setterName = $this->getSetterName(); |
|
173 | + $isNullable = !$this->isCompulsory(); |
|
174 | + |
|
175 | + $referencedBeanName = $this->namingStrategy->getBeanClassName($this->foreignKey->getForeignTableName()); |
|
176 | + |
|
177 | + $str = ' /** |
|
178 | 178 | * Returns the '.$referencedBeanName.' object bound to this object via the '.implode(' and ', $this->foreignKey->getLocalColumns()).' column. |
179 | 179 | * |
180 | 180 | * @return '.$referencedBeanName.($isNullable?'|null':'').' |
@@ -196,20 +196,20 @@ discard block |
||
196 | 196 | |
197 | 197 | '; |
198 | 198 | |
199 | - return $str; |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * Returns the part of code useful when doing json serialization. |
|
204 | - * |
|
205 | - * @return string |
|
206 | - */ |
|
207 | - public function getJsonSerializeCode() |
|
208 | - { |
|
209 | - return ' if (!$stopRecursion) { |
|
199 | + return $str; |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * Returns the part of code useful when doing json serialization. |
|
204 | + * |
|
205 | + * @return string |
|
206 | + */ |
|
207 | + public function getJsonSerializeCode() |
|
208 | + { |
|
209 | + return ' if (!$stopRecursion) { |
|
210 | 210 | $object = $this->'.$this->getGetterName().'(); |
211 | 211 | $array['.var_export($this->getLowerCamelCaseName(), true).'] = $object ? $object->jsonSerialize(true) : null; |
212 | 212 | } |
213 | 213 | '; |
214 | - } |
|
214 | + } |
|
215 | 215 | } |
@@ -77,7 +77,7 @@ discard block |
||
77 | 77 | { |
78 | 78 | // First, are there many column or only one? |
79 | 79 | // If one column, we name the setter after it. Otherwise, we name the setter after the table name |
80 | - if (count($this->foreignKey->getLocalColumns()) > 1) { |
|
80 | + if (count($this->foreignKey->getLocalColumns())>1) { |
|
81 | 81 | $name = TDBMDaoGenerator::toSingular(TDBMDaoGenerator::toCamelCase($this->foreignKey->getForeignTableName())); |
82 | 82 | if ($this->alternativeName) { |
83 | 83 | $camelizedColumns = array_map(['Mouf\\Database\\TDBM\\Utils\\TDBMDaoGenerator', 'toCamelCase'], $this->foreignKey->getLocalColumns()); |
@@ -90,8 +90,8 @@ discard block |
||
90 | 90 | if (strpos(strtolower($column), 'id_') === 0) { |
91 | 91 | $column = substr($column, 3); |
92 | 92 | } |
93 | - if (strrpos(strtolower($column), '_id') === strlen($column) - 3) { |
|
94 | - $column = substr($column, 0, strlen($column) - 3); |
|
93 | + if (strrpos(strtolower($column), '_id') === strlen($column)-3) { |
|
94 | + $column = substr($column, 0, strlen($column)-3); |
|
95 | 95 | } |
96 | 96 | $name = TDBMDaoGenerator::toCamelCase($column); |
97 | 97 | if ($this->alternativeName) { |
@@ -177,9 +177,9 @@ discard block |
||
177 | 177 | $str = ' /** |
178 | 178 | * Returns the '.$referencedBeanName.' object bound to this object via the '.implode(' and ', $this->foreignKey->getLocalColumns()).' column. |
179 | 179 | * |
180 | - * @return '.$referencedBeanName.($isNullable?'|null':'').' |
|
180 | + * @return '.$referencedBeanName.($isNullable ? '|null' : '').' |
|
181 | 181 | */ |
182 | - public function '.$getterName.'(): '.($isNullable?'?':'').$referencedBeanName.' |
|
182 | + public function '.$getterName.'(): '.($isNullable ? '?' : '').$referencedBeanName.' |
|
183 | 183 | { |
184 | 184 | return $this->getRef('.var_export($this->foreignKey->getName(), true).', '.var_export($tableName, true).'); |
185 | 185 | } |
@@ -187,9 +187,9 @@ discard block |
||
187 | 187 | /** |
188 | 188 | * The setter for the '.$referencedBeanName.' object bound to this object via the '.implode(' and ', $this->foreignKey->getLocalColumns()).' column. |
189 | 189 | * |
190 | - * @param '.$referencedBeanName.($isNullable?'|null':'').' $object |
|
190 | + * @param '.$referencedBeanName.($isNullable ? '|null' : '').' $object |
|
191 | 191 | */ |
192 | - public function '.$setterName.'('.($isNullable?'?':'').$referencedBeanName.' $object) : void |
|
192 | + public function '.$setterName.'('.($isNullable ? '?' : '').$referencedBeanName.' $object) : void |
|
193 | 193 | { |
194 | 194 | $this->setRef('.var_export($this->foreignKey->getName(), true).', $object, '.var_export($tableName, true).'); |
195 | 195 | } |
@@ -16,241 +16,241 @@ discard block |
||
16 | 16 | */ |
17 | 17 | class BeanDescriptor implements BeanDescriptorInterface |
18 | 18 | { |
19 | - /** |
|
20 | - * @var Table |
|
21 | - */ |
|
22 | - private $table; |
|
23 | - |
|
24 | - /** |
|
25 | - * @var SchemaAnalyzer |
|
26 | - */ |
|
27 | - private $schemaAnalyzer; |
|
28 | - |
|
29 | - /** |
|
30 | - * @var Schema |
|
31 | - */ |
|
32 | - private $schema; |
|
33 | - |
|
34 | - /** |
|
35 | - * @var AbstractBeanPropertyDescriptor[] |
|
36 | - */ |
|
37 | - private $beanPropertyDescriptors = []; |
|
38 | - |
|
39 | - /** |
|
40 | - * @var TDBMSchemaAnalyzer |
|
41 | - */ |
|
42 | - private $tdbmSchemaAnalyzer; |
|
43 | - |
|
44 | - /** |
|
45 | - * @var NamingStrategyInterface |
|
46 | - */ |
|
47 | - private $namingStrategy; |
|
48 | - |
|
49 | - /** |
|
50 | - * @param Table $table |
|
51 | - * @param SchemaAnalyzer $schemaAnalyzer |
|
52 | - * @param Schema $schema |
|
53 | - * @param TDBMSchemaAnalyzer $tdbmSchemaAnalyzer |
|
54 | - * @param NamingStrategyInterface $namingStrategy |
|
55 | - */ |
|
56 | - public function __construct(Table $table, SchemaAnalyzer $schemaAnalyzer, Schema $schema, TDBMSchemaAnalyzer $tdbmSchemaAnalyzer, NamingStrategyInterface $namingStrategy) |
|
57 | - { |
|
58 | - $this->table = $table; |
|
59 | - $this->schemaAnalyzer = $schemaAnalyzer; |
|
60 | - $this->schema = $schema; |
|
61 | - $this->tdbmSchemaAnalyzer = $tdbmSchemaAnalyzer; |
|
62 | - $this->namingStrategy = $namingStrategy; |
|
63 | - $this->initBeanPropertyDescriptors(); |
|
64 | - } |
|
65 | - |
|
66 | - private function initBeanPropertyDescriptors() |
|
67 | - { |
|
68 | - $this->beanPropertyDescriptors = $this->getProperties($this->table); |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * Returns the foreign-key the column is part of, if any. null otherwise. |
|
73 | - * |
|
74 | - * @param Table $table |
|
75 | - * @param Column $column |
|
76 | - * |
|
77 | - * @return ForeignKeyConstraint|null |
|
78 | - */ |
|
79 | - private function isPartOfForeignKey(Table $table, Column $column) |
|
80 | - { |
|
81 | - $localColumnName = $column->getName(); |
|
82 | - foreach ($table->getForeignKeys() as $foreignKey) { |
|
83 | - foreach ($foreignKey->getColumns() as $columnName) { |
|
84 | - if ($columnName === $localColumnName) { |
|
85 | - return $foreignKey; |
|
86 | - } |
|
87 | - } |
|
88 | - } |
|
89 | - |
|
90 | - return; |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * @return AbstractBeanPropertyDescriptor[] |
|
95 | - */ |
|
96 | - public function getBeanPropertyDescriptors() |
|
97 | - { |
|
98 | - return $this->beanPropertyDescriptors; |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * Returns the list of columns that are not nullable and not autogenerated for a given table and its parent. |
|
103 | - * |
|
104 | - * @return AbstractBeanPropertyDescriptor[] |
|
105 | - */ |
|
106 | - public function getConstructorProperties() |
|
107 | - { |
|
108 | - $constructorProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) { |
|
109 | - return $property->isCompulsory(); |
|
110 | - }); |
|
111 | - |
|
112 | - return $constructorProperties; |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Returns the list of columns that have default values for a given table. |
|
117 | - * |
|
118 | - * @return AbstractBeanPropertyDescriptor[] |
|
119 | - */ |
|
120 | - public function getPropertiesWithDefault() |
|
121 | - { |
|
122 | - $properties = $this->getPropertiesForTable($this->table); |
|
123 | - $defaultProperties = array_filter($properties, function (AbstractBeanPropertyDescriptor $property) { |
|
124 | - return $property->hasDefault(); |
|
125 | - }); |
|
126 | - |
|
127 | - return $defaultProperties; |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * Returns the list of properties exposed as getters and setters in this class. |
|
132 | - * |
|
133 | - * @return AbstractBeanPropertyDescriptor[] |
|
134 | - */ |
|
135 | - public function getExposedProperties(): array |
|
136 | - { |
|
137 | - $exposedProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) { |
|
138 | - return $property->getTable()->getName() == $this->table->getName(); |
|
139 | - }); |
|
140 | - |
|
141 | - return $exposedProperties; |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * Returns the list of properties for this table (including parent tables). |
|
146 | - * |
|
147 | - * @param Table $table |
|
148 | - * |
|
149 | - * @return AbstractBeanPropertyDescriptor[] |
|
150 | - */ |
|
151 | - private function getProperties(Table $table) |
|
152 | - { |
|
153 | - $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
|
154 | - if ($parentRelationship) { |
|
155 | - $parentTable = $this->schema->getTable($parentRelationship->getForeignTableName()); |
|
156 | - $properties = $this->getProperties($parentTable); |
|
157 | - // we merge properties by overriding property names. |
|
158 | - $localProperties = $this->getPropertiesForTable($table); |
|
159 | - foreach ($localProperties as $name => $property) { |
|
160 | - // We do not override properties if this is a primary key! |
|
161 | - if ($property->isPrimaryKey()) { |
|
162 | - continue; |
|
163 | - } |
|
164 | - $properties[$name] = $property; |
|
165 | - } |
|
166 | - } else { |
|
167 | - $properties = $this->getPropertiesForTable($table); |
|
168 | - } |
|
169 | - |
|
170 | - return $properties; |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * Returns the list of properties for this table (ignoring parent tables). |
|
175 | - * |
|
176 | - * @param Table $table |
|
177 | - * |
|
178 | - * @return AbstractBeanPropertyDescriptor[] |
|
179 | - */ |
|
180 | - private function getPropertiesForTable(Table $table) |
|
181 | - { |
|
182 | - $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
|
183 | - if ($parentRelationship) { |
|
184 | - $ignoreColumns = $parentRelationship->getLocalColumns(); |
|
185 | - } else { |
|
186 | - $ignoreColumns = []; |
|
187 | - } |
|
188 | - |
|
189 | - $beanPropertyDescriptors = []; |
|
190 | - |
|
191 | - foreach ($table->getColumns() as $column) { |
|
192 | - if (array_search($column->getName(), $ignoreColumns) !== false) { |
|
193 | - continue; |
|
194 | - } |
|
195 | - |
|
196 | - $fk = $this->isPartOfForeignKey($table, $column); |
|
197 | - if ($fk !== null) { |
|
198 | - // Check that previously added descriptors are not added on same FK (can happen with multi key FK). |
|
199 | - foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
200 | - if ($beanDescriptor instanceof ObjectBeanPropertyDescriptor && $beanDescriptor->getForeignKey() === $fk) { |
|
201 | - continue 2; |
|
202 | - } |
|
203 | - } |
|
204 | - // Check that this property is not an inheritance relationship |
|
205 | - $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
|
206 | - if ($parentRelationship === $fk) { |
|
207 | - continue; |
|
208 | - } |
|
209 | - |
|
210 | - $beanPropertyDescriptors[] = new ObjectBeanPropertyDescriptor($table, $fk, $this->schemaAnalyzer, $this->namingStrategy); |
|
211 | - } else { |
|
212 | - $beanPropertyDescriptors[] = new ScalarBeanPropertyDescriptor($table, $column, $this->namingStrategy); |
|
213 | - } |
|
214 | - } |
|
215 | - |
|
216 | - // Now, let's get the name of all properties and let's check there is no duplicate. |
|
217 | - /** @var $names AbstractBeanPropertyDescriptor[] */ |
|
218 | - $names = []; |
|
219 | - foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
220 | - $name = $beanDescriptor->getUpperCamelCaseName(); |
|
221 | - if (isset($names[$name])) { |
|
222 | - $names[$name]->useAlternativeName(); |
|
223 | - $beanDescriptor->useAlternativeName(); |
|
224 | - } else { |
|
225 | - $names[$name] = $beanDescriptor; |
|
226 | - } |
|
227 | - } |
|
228 | - |
|
229 | - // Final check (throw exceptions if problem arises) |
|
230 | - $names = []; |
|
231 | - foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
232 | - $name = $beanDescriptor->getUpperCamelCaseName(); |
|
233 | - if (isset($names[$name])) { |
|
234 | - throw new TDBMException('Unsolvable name conflict while generating method name'); |
|
235 | - } else { |
|
236 | - $names[$name] = $beanDescriptor; |
|
237 | - } |
|
238 | - } |
|
239 | - |
|
240 | - // Last step, let's rebuild the list with a map: |
|
241 | - $beanPropertyDescriptorsMap = []; |
|
242 | - foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
243 | - $beanPropertyDescriptorsMap[$beanDescriptor->getLowerCamelCaseName()] = $beanDescriptor; |
|
244 | - } |
|
245 | - |
|
246 | - return $beanPropertyDescriptorsMap; |
|
247 | - } |
|
248 | - |
|
249 | - private function generateBeanConstructor() : string |
|
250 | - { |
|
251 | - $constructorProperties = $this->getConstructorProperties(); |
|
252 | - |
|
253 | - $constructorCode = ' /** |
|
19 | + /** |
|
20 | + * @var Table |
|
21 | + */ |
|
22 | + private $table; |
|
23 | + |
|
24 | + /** |
|
25 | + * @var SchemaAnalyzer |
|
26 | + */ |
|
27 | + private $schemaAnalyzer; |
|
28 | + |
|
29 | + /** |
|
30 | + * @var Schema |
|
31 | + */ |
|
32 | + private $schema; |
|
33 | + |
|
34 | + /** |
|
35 | + * @var AbstractBeanPropertyDescriptor[] |
|
36 | + */ |
|
37 | + private $beanPropertyDescriptors = []; |
|
38 | + |
|
39 | + /** |
|
40 | + * @var TDBMSchemaAnalyzer |
|
41 | + */ |
|
42 | + private $tdbmSchemaAnalyzer; |
|
43 | + |
|
44 | + /** |
|
45 | + * @var NamingStrategyInterface |
|
46 | + */ |
|
47 | + private $namingStrategy; |
|
48 | + |
|
49 | + /** |
|
50 | + * @param Table $table |
|
51 | + * @param SchemaAnalyzer $schemaAnalyzer |
|
52 | + * @param Schema $schema |
|
53 | + * @param TDBMSchemaAnalyzer $tdbmSchemaAnalyzer |
|
54 | + * @param NamingStrategyInterface $namingStrategy |
|
55 | + */ |
|
56 | + public function __construct(Table $table, SchemaAnalyzer $schemaAnalyzer, Schema $schema, TDBMSchemaAnalyzer $tdbmSchemaAnalyzer, NamingStrategyInterface $namingStrategy) |
|
57 | + { |
|
58 | + $this->table = $table; |
|
59 | + $this->schemaAnalyzer = $schemaAnalyzer; |
|
60 | + $this->schema = $schema; |
|
61 | + $this->tdbmSchemaAnalyzer = $tdbmSchemaAnalyzer; |
|
62 | + $this->namingStrategy = $namingStrategy; |
|
63 | + $this->initBeanPropertyDescriptors(); |
|
64 | + } |
|
65 | + |
|
66 | + private function initBeanPropertyDescriptors() |
|
67 | + { |
|
68 | + $this->beanPropertyDescriptors = $this->getProperties($this->table); |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * Returns the foreign-key the column is part of, if any. null otherwise. |
|
73 | + * |
|
74 | + * @param Table $table |
|
75 | + * @param Column $column |
|
76 | + * |
|
77 | + * @return ForeignKeyConstraint|null |
|
78 | + */ |
|
79 | + private function isPartOfForeignKey(Table $table, Column $column) |
|
80 | + { |
|
81 | + $localColumnName = $column->getName(); |
|
82 | + foreach ($table->getForeignKeys() as $foreignKey) { |
|
83 | + foreach ($foreignKey->getColumns() as $columnName) { |
|
84 | + if ($columnName === $localColumnName) { |
|
85 | + return $foreignKey; |
|
86 | + } |
|
87 | + } |
|
88 | + } |
|
89 | + |
|
90 | + return; |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * @return AbstractBeanPropertyDescriptor[] |
|
95 | + */ |
|
96 | + public function getBeanPropertyDescriptors() |
|
97 | + { |
|
98 | + return $this->beanPropertyDescriptors; |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * Returns the list of columns that are not nullable and not autogenerated for a given table and its parent. |
|
103 | + * |
|
104 | + * @return AbstractBeanPropertyDescriptor[] |
|
105 | + */ |
|
106 | + public function getConstructorProperties() |
|
107 | + { |
|
108 | + $constructorProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) { |
|
109 | + return $property->isCompulsory(); |
|
110 | + }); |
|
111 | + |
|
112 | + return $constructorProperties; |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Returns the list of columns that have default values for a given table. |
|
117 | + * |
|
118 | + * @return AbstractBeanPropertyDescriptor[] |
|
119 | + */ |
|
120 | + public function getPropertiesWithDefault() |
|
121 | + { |
|
122 | + $properties = $this->getPropertiesForTable($this->table); |
|
123 | + $defaultProperties = array_filter($properties, function (AbstractBeanPropertyDescriptor $property) { |
|
124 | + return $property->hasDefault(); |
|
125 | + }); |
|
126 | + |
|
127 | + return $defaultProperties; |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * Returns the list of properties exposed as getters and setters in this class. |
|
132 | + * |
|
133 | + * @return AbstractBeanPropertyDescriptor[] |
|
134 | + */ |
|
135 | + public function getExposedProperties(): array |
|
136 | + { |
|
137 | + $exposedProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) { |
|
138 | + return $property->getTable()->getName() == $this->table->getName(); |
|
139 | + }); |
|
140 | + |
|
141 | + return $exposedProperties; |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * Returns the list of properties for this table (including parent tables). |
|
146 | + * |
|
147 | + * @param Table $table |
|
148 | + * |
|
149 | + * @return AbstractBeanPropertyDescriptor[] |
|
150 | + */ |
|
151 | + private function getProperties(Table $table) |
|
152 | + { |
|
153 | + $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
|
154 | + if ($parentRelationship) { |
|
155 | + $parentTable = $this->schema->getTable($parentRelationship->getForeignTableName()); |
|
156 | + $properties = $this->getProperties($parentTable); |
|
157 | + // we merge properties by overriding property names. |
|
158 | + $localProperties = $this->getPropertiesForTable($table); |
|
159 | + foreach ($localProperties as $name => $property) { |
|
160 | + // We do not override properties if this is a primary key! |
|
161 | + if ($property->isPrimaryKey()) { |
|
162 | + continue; |
|
163 | + } |
|
164 | + $properties[$name] = $property; |
|
165 | + } |
|
166 | + } else { |
|
167 | + $properties = $this->getPropertiesForTable($table); |
|
168 | + } |
|
169 | + |
|
170 | + return $properties; |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * Returns the list of properties for this table (ignoring parent tables). |
|
175 | + * |
|
176 | + * @param Table $table |
|
177 | + * |
|
178 | + * @return AbstractBeanPropertyDescriptor[] |
|
179 | + */ |
|
180 | + private function getPropertiesForTable(Table $table) |
|
181 | + { |
|
182 | + $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
|
183 | + if ($parentRelationship) { |
|
184 | + $ignoreColumns = $parentRelationship->getLocalColumns(); |
|
185 | + } else { |
|
186 | + $ignoreColumns = []; |
|
187 | + } |
|
188 | + |
|
189 | + $beanPropertyDescriptors = []; |
|
190 | + |
|
191 | + foreach ($table->getColumns() as $column) { |
|
192 | + if (array_search($column->getName(), $ignoreColumns) !== false) { |
|
193 | + continue; |
|
194 | + } |
|
195 | + |
|
196 | + $fk = $this->isPartOfForeignKey($table, $column); |
|
197 | + if ($fk !== null) { |
|
198 | + // Check that previously added descriptors are not added on same FK (can happen with multi key FK). |
|
199 | + foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
200 | + if ($beanDescriptor instanceof ObjectBeanPropertyDescriptor && $beanDescriptor->getForeignKey() === $fk) { |
|
201 | + continue 2; |
|
202 | + } |
|
203 | + } |
|
204 | + // Check that this property is not an inheritance relationship |
|
205 | + $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
|
206 | + if ($parentRelationship === $fk) { |
|
207 | + continue; |
|
208 | + } |
|
209 | + |
|
210 | + $beanPropertyDescriptors[] = new ObjectBeanPropertyDescriptor($table, $fk, $this->schemaAnalyzer, $this->namingStrategy); |
|
211 | + } else { |
|
212 | + $beanPropertyDescriptors[] = new ScalarBeanPropertyDescriptor($table, $column, $this->namingStrategy); |
|
213 | + } |
|
214 | + } |
|
215 | + |
|
216 | + // Now, let's get the name of all properties and let's check there is no duplicate. |
|
217 | + /** @var $names AbstractBeanPropertyDescriptor[] */ |
|
218 | + $names = []; |
|
219 | + foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
220 | + $name = $beanDescriptor->getUpperCamelCaseName(); |
|
221 | + if (isset($names[$name])) { |
|
222 | + $names[$name]->useAlternativeName(); |
|
223 | + $beanDescriptor->useAlternativeName(); |
|
224 | + } else { |
|
225 | + $names[$name] = $beanDescriptor; |
|
226 | + } |
|
227 | + } |
|
228 | + |
|
229 | + // Final check (throw exceptions if problem arises) |
|
230 | + $names = []; |
|
231 | + foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
232 | + $name = $beanDescriptor->getUpperCamelCaseName(); |
|
233 | + if (isset($names[$name])) { |
|
234 | + throw new TDBMException('Unsolvable name conflict while generating method name'); |
|
235 | + } else { |
|
236 | + $names[$name] = $beanDescriptor; |
|
237 | + } |
|
238 | + } |
|
239 | + |
|
240 | + // Last step, let's rebuild the list with a map: |
|
241 | + $beanPropertyDescriptorsMap = []; |
|
242 | + foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
243 | + $beanPropertyDescriptorsMap[$beanDescriptor->getLowerCamelCaseName()] = $beanDescriptor; |
|
244 | + } |
|
245 | + |
|
246 | + return $beanPropertyDescriptorsMap; |
|
247 | + } |
|
248 | + |
|
249 | + private function generateBeanConstructor() : string |
|
250 | + { |
|
251 | + $constructorProperties = $this->getConstructorProperties(); |
|
252 | + |
|
253 | + $constructorCode = ' /** |
|
254 | 254 | * The constructor takes all compulsory arguments. |
255 | 255 | * |
256 | 256 | %s |
@@ -260,110 +260,110 @@ discard block |
||
260 | 260 | %s%s } |
261 | 261 | '; |
262 | 262 | |
263 | - $paramAnnotations = []; |
|
264 | - $arguments = []; |
|
265 | - $assigns = []; |
|
266 | - $parentConstructorArguments = []; |
|
267 | - |
|
268 | - foreach ($constructorProperties as $property) { |
|
269 | - $className = $property->getClassName(); |
|
270 | - if ($className) { |
|
271 | - $arguments[] = $className.' '.$property->getVariableName(); |
|
272 | - } else { |
|
273 | - $arguments[] = $property->getVariableName(); |
|
274 | - } |
|
275 | - $paramAnnotations[] = $property->getParamAnnotation(); |
|
276 | - if ($property->getTable()->getName() === $this->table->getName()) { |
|
277 | - $assigns[] = $property->getConstructorAssignCode()."\n"; |
|
278 | - } else { |
|
279 | - $parentConstructorArguments[] = $property->getVariableName(); |
|
280 | - } |
|
281 | - } |
|
282 | - |
|
283 | - $parentConstructorCode = sprintf(" parent::__construct(%s);\n", implode(', ', $parentConstructorArguments)); |
|
284 | - |
|
285 | - foreach ($this->getPropertiesWithDefault() as $property) { |
|
286 | - $assigns[] = $property->assignToDefaultCode()."\n"; |
|
287 | - } |
|
288 | - |
|
289 | - return sprintf($constructorCode, implode("\n", $paramAnnotations), implode(', ', $arguments), $parentConstructorCode, implode('', $assigns)); |
|
290 | - } |
|
291 | - |
|
292 | - public function getDirectForeignKeysDescriptors(): array |
|
293 | - { |
|
294 | - $fks = $this->tdbmSchemaAnalyzer->getIncomingForeignKeys($this->table->getName()); |
|
295 | - |
|
296 | - $descriptors = []; |
|
297 | - |
|
298 | - foreach ($fks as $fk) { |
|
299 | - $descriptors[] = new DirectForeignKeyMethodDescriptor($fk, $this->table, $this->namingStrategy); |
|
300 | - } |
|
301 | - |
|
302 | - return $descriptors; |
|
303 | - } |
|
304 | - |
|
305 | - private function getPivotTableDescriptors(): array |
|
306 | - { |
|
307 | - $descs = []; |
|
308 | - foreach ($this->schemaAnalyzer->detectJunctionTables(true) as $table) { |
|
309 | - // There are exactly 2 FKs since this is a pivot table. |
|
310 | - $fks = array_values($table->getForeignKeys()); |
|
311 | - |
|
312 | - if ($fks[0]->getForeignTableName() === $this->table->getName()) { |
|
313 | - list($localFk, $remoteFk) = $fks; |
|
314 | - } elseif ($fks[1]->getForeignTableName() === $this->table->getName()) { |
|
315 | - list($remoteFk, $localFk) = $fks; |
|
316 | - } else { |
|
317 | - continue; |
|
318 | - } |
|
319 | - |
|
320 | - $descs[] = new PivotTableMethodsDescriptor($table, $localFk, $remoteFk, $this->namingStrategy); |
|
321 | - } |
|
322 | - |
|
323 | - return $descs; |
|
324 | - } |
|
325 | - |
|
326 | - /** |
|
327 | - * Returns the list of method descriptors (and applies the alternative name if needed). |
|
328 | - * |
|
329 | - * @return MethodDescriptorInterface[] |
|
330 | - */ |
|
331 | - private function getMethodDescriptors(): array |
|
332 | - { |
|
333 | - $directForeignKeyDescriptors = $this->getDirectForeignKeysDescriptors(); |
|
334 | - $pivotTableDescriptors = $this->getPivotTableDescriptors(); |
|
335 | - |
|
336 | - $descriptors = array_merge($directForeignKeyDescriptors, $pivotTableDescriptors); |
|
337 | - |
|
338 | - // Descriptors by method names |
|
339 | - $descriptorsByMethodName = []; |
|
340 | - |
|
341 | - foreach ($descriptors as $descriptor) { |
|
342 | - $descriptorsByMethodName[$descriptor->getName()][] = $descriptor; |
|
343 | - } |
|
344 | - |
|
345 | - foreach ($descriptorsByMethodName as $descriptorsForMethodName) { |
|
346 | - if (count($descriptorsForMethodName) > 1) { |
|
347 | - foreach ($descriptorsForMethodName as $descriptor) { |
|
348 | - $descriptor->useAlternativeName(); |
|
349 | - } |
|
350 | - } |
|
351 | - } |
|
352 | - |
|
353 | - return $descriptors; |
|
354 | - } |
|
355 | - |
|
356 | - public function generateJsonSerialize(): string |
|
357 | - { |
|
358 | - $tableName = $this->table->getName(); |
|
359 | - $parentFk = $this->schemaAnalyzer->getParentRelationship($tableName); |
|
360 | - if ($parentFk !== null) { |
|
361 | - $initializer = '$array = parent::jsonSerialize($stopRecursion);'; |
|
362 | - } else { |
|
363 | - $initializer = '$array = [];'; |
|
364 | - } |
|
365 | - |
|
366 | - $str = ' |
|
263 | + $paramAnnotations = []; |
|
264 | + $arguments = []; |
|
265 | + $assigns = []; |
|
266 | + $parentConstructorArguments = []; |
|
267 | + |
|
268 | + foreach ($constructorProperties as $property) { |
|
269 | + $className = $property->getClassName(); |
|
270 | + if ($className) { |
|
271 | + $arguments[] = $className.' '.$property->getVariableName(); |
|
272 | + } else { |
|
273 | + $arguments[] = $property->getVariableName(); |
|
274 | + } |
|
275 | + $paramAnnotations[] = $property->getParamAnnotation(); |
|
276 | + if ($property->getTable()->getName() === $this->table->getName()) { |
|
277 | + $assigns[] = $property->getConstructorAssignCode()."\n"; |
|
278 | + } else { |
|
279 | + $parentConstructorArguments[] = $property->getVariableName(); |
|
280 | + } |
|
281 | + } |
|
282 | + |
|
283 | + $parentConstructorCode = sprintf(" parent::__construct(%s);\n", implode(', ', $parentConstructorArguments)); |
|
284 | + |
|
285 | + foreach ($this->getPropertiesWithDefault() as $property) { |
|
286 | + $assigns[] = $property->assignToDefaultCode()."\n"; |
|
287 | + } |
|
288 | + |
|
289 | + return sprintf($constructorCode, implode("\n", $paramAnnotations), implode(', ', $arguments), $parentConstructorCode, implode('', $assigns)); |
|
290 | + } |
|
291 | + |
|
292 | + public function getDirectForeignKeysDescriptors(): array |
|
293 | + { |
|
294 | + $fks = $this->tdbmSchemaAnalyzer->getIncomingForeignKeys($this->table->getName()); |
|
295 | + |
|
296 | + $descriptors = []; |
|
297 | + |
|
298 | + foreach ($fks as $fk) { |
|
299 | + $descriptors[] = new DirectForeignKeyMethodDescriptor($fk, $this->table, $this->namingStrategy); |
|
300 | + } |
|
301 | + |
|
302 | + return $descriptors; |
|
303 | + } |
|
304 | + |
|
305 | + private function getPivotTableDescriptors(): array |
|
306 | + { |
|
307 | + $descs = []; |
|
308 | + foreach ($this->schemaAnalyzer->detectJunctionTables(true) as $table) { |
|
309 | + // There are exactly 2 FKs since this is a pivot table. |
|
310 | + $fks = array_values($table->getForeignKeys()); |
|
311 | + |
|
312 | + if ($fks[0]->getForeignTableName() === $this->table->getName()) { |
|
313 | + list($localFk, $remoteFk) = $fks; |
|
314 | + } elseif ($fks[1]->getForeignTableName() === $this->table->getName()) { |
|
315 | + list($remoteFk, $localFk) = $fks; |
|
316 | + } else { |
|
317 | + continue; |
|
318 | + } |
|
319 | + |
|
320 | + $descs[] = new PivotTableMethodsDescriptor($table, $localFk, $remoteFk, $this->namingStrategy); |
|
321 | + } |
|
322 | + |
|
323 | + return $descs; |
|
324 | + } |
|
325 | + |
|
326 | + /** |
|
327 | + * Returns the list of method descriptors (and applies the alternative name if needed). |
|
328 | + * |
|
329 | + * @return MethodDescriptorInterface[] |
|
330 | + */ |
|
331 | + private function getMethodDescriptors(): array |
|
332 | + { |
|
333 | + $directForeignKeyDescriptors = $this->getDirectForeignKeysDescriptors(); |
|
334 | + $pivotTableDescriptors = $this->getPivotTableDescriptors(); |
|
335 | + |
|
336 | + $descriptors = array_merge($directForeignKeyDescriptors, $pivotTableDescriptors); |
|
337 | + |
|
338 | + // Descriptors by method names |
|
339 | + $descriptorsByMethodName = []; |
|
340 | + |
|
341 | + foreach ($descriptors as $descriptor) { |
|
342 | + $descriptorsByMethodName[$descriptor->getName()][] = $descriptor; |
|
343 | + } |
|
344 | + |
|
345 | + foreach ($descriptorsByMethodName as $descriptorsForMethodName) { |
|
346 | + if (count($descriptorsForMethodName) > 1) { |
|
347 | + foreach ($descriptorsForMethodName as $descriptor) { |
|
348 | + $descriptor->useAlternativeName(); |
|
349 | + } |
|
350 | + } |
|
351 | + } |
|
352 | + |
|
353 | + return $descriptors; |
|
354 | + } |
|
355 | + |
|
356 | + public function generateJsonSerialize(): string |
|
357 | + { |
|
358 | + $tableName = $this->table->getName(); |
|
359 | + $parentFk = $this->schemaAnalyzer->getParentRelationship($tableName); |
|
360 | + if ($parentFk !== null) { |
|
361 | + $initializer = '$array = parent::jsonSerialize($stopRecursion);'; |
|
362 | + } else { |
|
363 | + $initializer = '$array = [];'; |
|
364 | + } |
|
365 | + |
|
366 | + $str = ' |
|
367 | 367 | /** |
368 | 368 | * Serializes the object for JSON encoding. |
369 | 369 | * |
@@ -379,77 +379,77 @@ discard block |
||
379 | 379 | } |
380 | 380 | '; |
381 | 381 | |
382 | - $propertiesCode = ''; |
|
383 | - foreach ($this->beanPropertyDescriptors as $beanPropertyDescriptor) { |
|
384 | - $propertiesCode .= $beanPropertyDescriptor->getJsonSerializeCode(); |
|
385 | - } |
|
386 | - |
|
387 | - // Many2many relationships |
|
388 | - $methodsCode = ''; |
|
389 | - foreach ($this->getMethodDescriptors() as $methodDescriptor) { |
|
390 | - $methodsCode .= $methodDescriptor->getJsonSerializeCode(); |
|
391 | - } |
|
392 | - |
|
393 | - return sprintf($str, $initializer, $propertiesCode, $methodsCode); |
|
394 | - } |
|
395 | - |
|
396 | - /** |
|
397 | - * Returns as an array the class we need to extend from and the list of use statements. |
|
398 | - * |
|
399 | - * @param ForeignKeyConstraint|null $parentFk |
|
400 | - * @return array |
|
401 | - */ |
|
402 | - private function generateExtendsAndUseStatements(ForeignKeyConstraint $parentFk = null): array |
|
403 | - { |
|
404 | - $classes = []; |
|
405 | - if ($parentFk !== null) { |
|
406 | - $extends = $this->namingStrategy->getBeanClassName($parentFk->getForeignTableName()); |
|
407 | - $classes[] = $extends; |
|
408 | - } |
|
409 | - |
|
410 | - foreach ($this->getBeanPropertyDescriptors() as $beanPropertyDescriptor) { |
|
411 | - $className = $beanPropertyDescriptor->getClassName(); |
|
412 | - if (null !== $className) { |
|
413 | - $classes[] = $beanPropertyDescriptor->getClassName(); |
|
414 | - } |
|
415 | - } |
|
416 | - |
|
417 | - foreach ($this->getMethodDescriptors() as $descriptor) { |
|
418 | - $classes = array_merge($classes, $descriptor->getUsedClasses()); |
|
419 | - } |
|
420 | - |
|
421 | - $classes = array_unique($classes); |
|
422 | - |
|
423 | - return $classes; |
|
424 | - } |
|
425 | - |
|
426 | - /** |
|
427 | - * Writes the PHP bean file with all getters and setters from the table passed in parameter. |
|
428 | - * |
|
429 | - * @param string $beannamespace The namespace of the bean |
|
430 | - * @return string |
|
431 | - */ |
|
432 | - public function generatePhpCode($beannamespace): string |
|
433 | - { |
|
434 | - $tableName = $this->table->getName(); |
|
435 | - $baseClassName = $this->namingStrategy->getBaseBeanClassName($tableName); |
|
436 | - $className = $this->namingStrategy->getBeanClassName($tableName); |
|
437 | - $parentFk = $this->schemaAnalyzer->getParentRelationship($this->table->getName()); |
|
438 | - |
|
439 | - $classes = $this->generateExtendsAndUseStatements($parentFk); |
|
440 | - |
|
441 | - $uses = array_map(function ($className) use ($beannamespace) { |
|
442 | - return 'use '.$beannamespace.'\\'.$className.";\n"; |
|
443 | - }, $classes); |
|
444 | - $use = implode('', $uses); |
|
445 | - |
|
446 | - $extends = $this->getExtendedBeanClassName(); |
|
447 | - if ($extends === null) { |
|
448 | - $extends = 'AbstractTDBMObject'; |
|
449 | - $use .= "use Mouf\\Database\\TDBM\\AbstractTDBMObject;\n"; |
|
450 | - } |
|
451 | - |
|
452 | - $str = "<?php |
|
382 | + $propertiesCode = ''; |
|
383 | + foreach ($this->beanPropertyDescriptors as $beanPropertyDescriptor) { |
|
384 | + $propertiesCode .= $beanPropertyDescriptor->getJsonSerializeCode(); |
|
385 | + } |
|
386 | + |
|
387 | + // Many2many relationships |
|
388 | + $methodsCode = ''; |
|
389 | + foreach ($this->getMethodDescriptors() as $methodDescriptor) { |
|
390 | + $methodsCode .= $methodDescriptor->getJsonSerializeCode(); |
|
391 | + } |
|
392 | + |
|
393 | + return sprintf($str, $initializer, $propertiesCode, $methodsCode); |
|
394 | + } |
|
395 | + |
|
396 | + /** |
|
397 | + * Returns as an array the class we need to extend from and the list of use statements. |
|
398 | + * |
|
399 | + * @param ForeignKeyConstraint|null $parentFk |
|
400 | + * @return array |
|
401 | + */ |
|
402 | + private function generateExtendsAndUseStatements(ForeignKeyConstraint $parentFk = null): array |
|
403 | + { |
|
404 | + $classes = []; |
|
405 | + if ($parentFk !== null) { |
|
406 | + $extends = $this->namingStrategy->getBeanClassName($parentFk->getForeignTableName()); |
|
407 | + $classes[] = $extends; |
|
408 | + } |
|
409 | + |
|
410 | + foreach ($this->getBeanPropertyDescriptors() as $beanPropertyDescriptor) { |
|
411 | + $className = $beanPropertyDescriptor->getClassName(); |
|
412 | + if (null !== $className) { |
|
413 | + $classes[] = $beanPropertyDescriptor->getClassName(); |
|
414 | + } |
|
415 | + } |
|
416 | + |
|
417 | + foreach ($this->getMethodDescriptors() as $descriptor) { |
|
418 | + $classes = array_merge($classes, $descriptor->getUsedClasses()); |
|
419 | + } |
|
420 | + |
|
421 | + $classes = array_unique($classes); |
|
422 | + |
|
423 | + return $classes; |
|
424 | + } |
|
425 | + |
|
426 | + /** |
|
427 | + * Writes the PHP bean file with all getters and setters from the table passed in parameter. |
|
428 | + * |
|
429 | + * @param string $beannamespace The namespace of the bean |
|
430 | + * @return string |
|
431 | + */ |
|
432 | + public function generatePhpCode($beannamespace): string |
|
433 | + { |
|
434 | + $tableName = $this->table->getName(); |
|
435 | + $baseClassName = $this->namingStrategy->getBaseBeanClassName($tableName); |
|
436 | + $className = $this->namingStrategy->getBeanClassName($tableName); |
|
437 | + $parentFk = $this->schemaAnalyzer->getParentRelationship($this->table->getName()); |
|
438 | + |
|
439 | + $classes = $this->generateExtendsAndUseStatements($parentFk); |
|
440 | + |
|
441 | + $uses = array_map(function ($className) use ($beannamespace) { |
|
442 | + return 'use '.$beannamespace.'\\'.$className.";\n"; |
|
443 | + }, $classes); |
|
444 | + $use = implode('', $uses); |
|
445 | + |
|
446 | + $extends = $this->getExtendedBeanClassName(); |
|
447 | + if ($extends === null) { |
|
448 | + $extends = 'AbstractTDBMObject'; |
|
449 | + $use .= "use Mouf\\Database\\TDBM\\AbstractTDBMObject;\n"; |
|
450 | + } |
|
451 | + |
|
452 | + $str = "<?php |
|
453 | 453 | namespace {$beannamespace}\\Generated; |
454 | 454 | |
455 | 455 | use Mouf\\Database\\TDBM\\ResultIterator; |
@@ -469,125 +469,125 @@ discard block |
||
469 | 469 | { |
470 | 470 | "; |
471 | 471 | |
472 | - $str .= $this->generateBeanConstructor(); |
|
472 | + $str .= $this->generateBeanConstructor(); |
|
473 | 473 | |
474 | - foreach ($this->getExposedProperties() as $property) { |
|
475 | - $str .= $property->getGetterSetterCode(); |
|
476 | - } |
|
474 | + foreach ($this->getExposedProperties() as $property) { |
|
475 | + $str .= $property->getGetterSetterCode(); |
|
476 | + } |
|
477 | 477 | |
478 | - foreach ($this->getMethodDescriptors() as $methodDescriptor) { |
|
479 | - $str .= $methodDescriptor->getCode(); |
|
480 | - } |
|
481 | - $str .= $this->generateJsonSerialize(); |
|
478 | + foreach ($this->getMethodDescriptors() as $methodDescriptor) { |
|
479 | + $str .= $methodDescriptor->getCode(); |
|
480 | + } |
|
481 | + $str .= $this->generateJsonSerialize(); |
|
482 | 482 | |
483 | - $str .= $this->generateGetUsedTablesCode(); |
|
483 | + $str .= $this->generateGetUsedTablesCode(); |
|
484 | 484 | |
485 | - $str .= $this->generateOnDeleteCode(); |
|
485 | + $str .= $this->generateOnDeleteCode(); |
|
486 | 486 | |
487 | - $str .= '} |
|
487 | + $str .= '} |
|
488 | 488 | '; |
489 | 489 | |
490 | - return $str; |
|
491 | - } |
|
492 | - |
|
493 | - /** |
|
494 | - * @param string $beanNamespace |
|
495 | - * @param string $beanClassName |
|
496 | - * |
|
497 | - * @return array first element: list of used beans, second item: PHP code as a string |
|
498 | - */ |
|
499 | - public function generateFindByDaoCode($beanNamespace, $beanClassName) |
|
500 | - { |
|
501 | - $code = ''; |
|
502 | - $usedBeans = []; |
|
503 | - foreach ($this->table->getIndexes() as $index) { |
|
504 | - if (!$index->isPrimary()) { |
|
505 | - list($usedBeansForIndex, $codeForIndex) = $this->generateFindByDaoCodeForIndex($index, $beanNamespace, $beanClassName); |
|
506 | - $code .= $codeForIndex; |
|
507 | - $usedBeans = array_merge($usedBeans, $usedBeansForIndex); |
|
508 | - } |
|
509 | - } |
|
510 | - |
|
511 | - return [$usedBeans, $code]; |
|
512 | - } |
|
513 | - |
|
514 | - /** |
|
515 | - * @param Index $index |
|
516 | - * @param string $beanNamespace |
|
517 | - * @param string $beanClassName |
|
518 | - * |
|
519 | - * @return array first element: list of used beans, second item: PHP code as a string |
|
520 | - */ |
|
521 | - private function generateFindByDaoCodeForIndex(Index $index, $beanNamespace, $beanClassName) |
|
522 | - { |
|
523 | - $columns = $index->getColumns(); |
|
524 | - $usedBeans = []; |
|
525 | - |
|
526 | - /* |
|
490 | + return $str; |
|
491 | + } |
|
492 | + |
|
493 | + /** |
|
494 | + * @param string $beanNamespace |
|
495 | + * @param string $beanClassName |
|
496 | + * |
|
497 | + * @return array first element: list of used beans, second item: PHP code as a string |
|
498 | + */ |
|
499 | + public function generateFindByDaoCode($beanNamespace, $beanClassName) |
|
500 | + { |
|
501 | + $code = ''; |
|
502 | + $usedBeans = []; |
|
503 | + foreach ($this->table->getIndexes() as $index) { |
|
504 | + if (!$index->isPrimary()) { |
|
505 | + list($usedBeansForIndex, $codeForIndex) = $this->generateFindByDaoCodeForIndex($index, $beanNamespace, $beanClassName); |
|
506 | + $code .= $codeForIndex; |
|
507 | + $usedBeans = array_merge($usedBeans, $usedBeansForIndex); |
|
508 | + } |
|
509 | + } |
|
510 | + |
|
511 | + return [$usedBeans, $code]; |
|
512 | + } |
|
513 | + |
|
514 | + /** |
|
515 | + * @param Index $index |
|
516 | + * @param string $beanNamespace |
|
517 | + * @param string $beanClassName |
|
518 | + * |
|
519 | + * @return array first element: list of used beans, second item: PHP code as a string |
|
520 | + */ |
|
521 | + private function generateFindByDaoCodeForIndex(Index $index, $beanNamespace, $beanClassName) |
|
522 | + { |
|
523 | + $columns = $index->getColumns(); |
|
524 | + $usedBeans = []; |
|
525 | + |
|
526 | + /* |
|
527 | 527 | * The list of elements building this index (expressed as columns or foreign keys) |
528 | 528 | * @var AbstractBeanPropertyDescriptor[] |
529 | 529 | */ |
530 | - $elements = []; |
|
531 | - |
|
532 | - foreach ($columns as $column) { |
|
533 | - $fk = $this->isPartOfForeignKey($this->table, $this->table->getColumn($column)); |
|
534 | - if ($fk !== null) { |
|
535 | - if (!in_array($fk, $elements)) { |
|
536 | - $elements[] = new ObjectBeanPropertyDescriptor($this->table, $fk, $this->schemaAnalyzer, $this->namingStrategy); |
|
537 | - } |
|
538 | - } else { |
|
539 | - $elements[] = new ScalarBeanPropertyDescriptor($this->table, $this->table->getColumn($column), $this->namingStrategy); |
|
540 | - } |
|
541 | - } |
|
542 | - |
|
543 | - // If the index is actually only a foreign key, let's bypass it entirely. |
|
544 | - if (count($elements) === 1 && $elements[0] instanceof ObjectBeanPropertyDescriptor) { |
|
545 | - return [[], '']; |
|
546 | - } |
|
547 | - |
|
548 | - $methodNameComponent = []; |
|
549 | - $functionParameters = []; |
|
550 | - $first = true; |
|
551 | - foreach ($elements as $element) { |
|
552 | - $methodNameComponent[] = $element->getUpperCamelCaseName(); |
|
553 | - $functionParameter = $element->getClassName(); |
|
554 | - if ($functionParameter) { |
|
555 | - $usedBeans[] = $beanNamespace.'\\'.$functionParameter; |
|
556 | - $functionParameter .= ' '; |
|
557 | - } |
|
558 | - $functionParameter .= $element->getVariableName(); |
|
559 | - if ($first) { |
|
560 | - $first = false; |
|
561 | - } else { |
|
562 | - $functionParameter .= ' = null'; |
|
563 | - } |
|
564 | - $functionParameters[] = $functionParameter; |
|
565 | - } |
|
566 | - |
|
567 | - $functionParametersString = implode(', ', $functionParameters); |
|
568 | - |
|
569 | - $count = 0; |
|
570 | - |
|
571 | - $params = []; |
|
572 | - $filterArrayCode = ''; |
|
573 | - $commentArguments = []; |
|
574 | - foreach ($elements as $element) { |
|
575 | - $params[] = $element->getParamAnnotation(); |
|
576 | - if ($element instanceof ScalarBeanPropertyDescriptor) { |
|
577 | - $filterArrayCode .= ' '.var_export($element->getColumnName(), true).' => '.$element->getVariableName().",\n"; |
|
578 | - } else { |
|
579 | - ++$count; |
|
580 | - $filterArrayCode .= ' '.$count.' => '.$element->getVariableName().",\n"; |
|
581 | - } |
|
582 | - $commentArguments[] = substr($element->getVariableName(), 1); |
|
583 | - } |
|
584 | - $paramsString = implode("\n", $params); |
|
585 | - |
|
586 | - if ($index->isUnique()) { |
|
587 | - $methodName = 'findOneBy'.implode('And', $methodNameComponent); |
|
588 | - $returnType = "{$beanClassName}"; |
|
589 | - |
|
590 | - $code = " |
|
530 | + $elements = []; |
|
531 | + |
|
532 | + foreach ($columns as $column) { |
|
533 | + $fk = $this->isPartOfForeignKey($this->table, $this->table->getColumn($column)); |
|
534 | + if ($fk !== null) { |
|
535 | + if (!in_array($fk, $elements)) { |
|
536 | + $elements[] = new ObjectBeanPropertyDescriptor($this->table, $fk, $this->schemaAnalyzer, $this->namingStrategy); |
|
537 | + } |
|
538 | + } else { |
|
539 | + $elements[] = new ScalarBeanPropertyDescriptor($this->table, $this->table->getColumn($column), $this->namingStrategy); |
|
540 | + } |
|
541 | + } |
|
542 | + |
|
543 | + // If the index is actually only a foreign key, let's bypass it entirely. |
|
544 | + if (count($elements) === 1 && $elements[0] instanceof ObjectBeanPropertyDescriptor) { |
|
545 | + return [[], '']; |
|
546 | + } |
|
547 | + |
|
548 | + $methodNameComponent = []; |
|
549 | + $functionParameters = []; |
|
550 | + $first = true; |
|
551 | + foreach ($elements as $element) { |
|
552 | + $methodNameComponent[] = $element->getUpperCamelCaseName(); |
|
553 | + $functionParameter = $element->getClassName(); |
|
554 | + if ($functionParameter) { |
|
555 | + $usedBeans[] = $beanNamespace.'\\'.$functionParameter; |
|
556 | + $functionParameter .= ' '; |
|
557 | + } |
|
558 | + $functionParameter .= $element->getVariableName(); |
|
559 | + if ($first) { |
|
560 | + $first = false; |
|
561 | + } else { |
|
562 | + $functionParameter .= ' = null'; |
|
563 | + } |
|
564 | + $functionParameters[] = $functionParameter; |
|
565 | + } |
|
566 | + |
|
567 | + $functionParametersString = implode(', ', $functionParameters); |
|
568 | + |
|
569 | + $count = 0; |
|
570 | + |
|
571 | + $params = []; |
|
572 | + $filterArrayCode = ''; |
|
573 | + $commentArguments = []; |
|
574 | + foreach ($elements as $element) { |
|
575 | + $params[] = $element->getParamAnnotation(); |
|
576 | + if ($element instanceof ScalarBeanPropertyDescriptor) { |
|
577 | + $filterArrayCode .= ' '.var_export($element->getColumnName(), true).' => '.$element->getVariableName().",\n"; |
|
578 | + } else { |
|
579 | + ++$count; |
|
580 | + $filterArrayCode .= ' '.$count.' => '.$element->getVariableName().",\n"; |
|
581 | + } |
|
582 | + $commentArguments[] = substr($element->getVariableName(), 1); |
|
583 | + } |
|
584 | + $paramsString = implode("\n", $params); |
|
585 | + |
|
586 | + if ($index->isUnique()) { |
|
587 | + $methodName = 'findOneBy'.implode('And', $methodNameComponent); |
|
588 | + $returnType = "{$beanClassName}"; |
|
589 | + |
|
590 | + $code = " |
|
591 | 591 | /** |
592 | 592 | * Get a $beanClassName filtered by ".implode(', ', $commentArguments).". |
593 | 593 | * |
@@ -602,11 +602,11 @@ discard block |
||
602 | 602 | return \$this->findOne(\$filter, [], \$additionalTablesFetch); |
603 | 603 | } |
604 | 604 | "; |
605 | - } else { |
|
606 | - $methodName = 'findBy'.implode('And', $methodNameComponent); |
|
607 | - $returnType = "{$beanClassName}[]|ResultIterator|ResultArray"; |
|
605 | + } else { |
|
606 | + $methodName = 'findBy'.implode('And', $methodNameComponent); |
|
607 | + $returnType = "{$beanClassName}[]|ResultIterator|ResultArray"; |
|
608 | 608 | |
609 | - $code = " |
|
609 | + $code = " |
|
610 | 610 | /** |
611 | 611 | * Get a list of $beanClassName filtered by ".implode(', ', $commentArguments).". |
612 | 612 | * |
@@ -623,29 +623,29 @@ discard block |
||
623 | 623 | return \$this->find(\$filter, [], \$orderBy, \$additionalTablesFetch, \$mode); |
624 | 624 | } |
625 | 625 | "; |
626 | - } |
|
627 | - |
|
628 | - return [$usedBeans, $code]; |
|
629 | - } |
|
630 | - |
|
631 | - /** |
|
632 | - * Generates the code for the getUsedTable protected method. |
|
633 | - * |
|
634 | - * @return string |
|
635 | - */ |
|
636 | - private function generateGetUsedTablesCode() |
|
637 | - { |
|
638 | - $hasParentRelationship = $this->schemaAnalyzer->getParentRelationship($this->table->getName()) !== null; |
|
639 | - if ($hasParentRelationship) { |
|
640 | - $code = sprintf(' $tables = parent::getUsedTables(); |
|
626 | + } |
|
627 | + |
|
628 | + return [$usedBeans, $code]; |
|
629 | + } |
|
630 | + |
|
631 | + /** |
|
632 | + * Generates the code for the getUsedTable protected method. |
|
633 | + * |
|
634 | + * @return string |
|
635 | + */ |
|
636 | + private function generateGetUsedTablesCode() |
|
637 | + { |
|
638 | + $hasParentRelationship = $this->schemaAnalyzer->getParentRelationship($this->table->getName()) !== null; |
|
639 | + if ($hasParentRelationship) { |
|
640 | + $code = sprintf(' $tables = parent::getUsedTables(); |
|
641 | 641 | $tables[] = %s; |
642 | 642 | |
643 | 643 | return $tables;', var_export($this->table->getName(), true)); |
644 | - } else { |
|
645 | - $code = sprintf(' return [ %s ];', var_export($this->table->getName(), true)); |
|
646 | - } |
|
644 | + } else { |
|
645 | + $code = sprintf(' return [ %s ];', var_export($this->table->getName(), true)); |
|
646 | + } |
|
647 | 647 | |
648 | - return sprintf(' |
|
648 | + return sprintf(' |
|
649 | 649 | /** |
650 | 650 | * Returns an array of used tables by this bean (from parent to child relationship). |
651 | 651 | * |
@@ -656,20 +656,20 @@ discard block |
||
656 | 656 | %s |
657 | 657 | } |
658 | 658 | ', $code); |
659 | - } |
|
660 | - |
|
661 | - private function generateOnDeleteCode() |
|
662 | - { |
|
663 | - $code = ''; |
|
664 | - $relationships = $this->getPropertiesForTable($this->table); |
|
665 | - foreach ($relationships as $relationship) { |
|
666 | - if ($relationship instanceof ObjectBeanPropertyDescriptor) { |
|
667 | - $code .= sprintf(' $this->setRef('.var_export($relationship->getForeignKey()->getName(), true).', null, '.var_export($this->table->getName(), true).");\n"); |
|
668 | - } |
|
669 | - } |
|
670 | - |
|
671 | - if ($code) { |
|
672 | - return sprintf(' |
|
659 | + } |
|
660 | + |
|
661 | + private function generateOnDeleteCode() |
|
662 | + { |
|
663 | + $code = ''; |
|
664 | + $relationships = $this->getPropertiesForTable($this->table); |
|
665 | + foreach ($relationships as $relationship) { |
|
666 | + if ($relationship instanceof ObjectBeanPropertyDescriptor) { |
|
667 | + $code .= sprintf(' $this->setRef('.var_export($relationship->getForeignKey()->getName(), true).', null, '.var_export($this->table->getName(), true).");\n"); |
|
668 | + } |
|
669 | + } |
|
670 | + |
|
671 | + if ($code) { |
|
672 | + return sprintf(' |
|
673 | 673 | /** |
674 | 674 | * Method called when the bean is removed from database. |
675 | 675 | * |
@@ -679,73 +679,73 @@ discard block |
||
679 | 679 | parent::onDelete(); |
680 | 680 | %s } |
681 | 681 | ', $code); |
682 | - } |
|
683 | - |
|
684 | - return ''; |
|
685 | - } |
|
686 | - |
|
687 | - /** |
|
688 | - * Returns the bean class name (without the namespace). |
|
689 | - * |
|
690 | - * @return string |
|
691 | - */ |
|
692 | - public function getBeanClassName() : string |
|
693 | - { |
|
694 | - return $this->namingStrategy->getBeanClassName($this->table->getName()); |
|
695 | - } |
|
696 | - |
|
697 | - /** |
|
698 | - * Returns the base bean class name (without the namespace). |
|
699 | - * |
|
700 | - * @return string |
|
701 | - */ |
|
702 | - public function getBaseBeanClassName() : string |
|
703 | - { |
|
704 | - return $this->namingStrategy->getBaseBeanClassName($this->table->getName()); |
|
705 | - } |
|
706 | - |
|
707 | - /** |
|
708 | - * Returns the DAO class name (without the namespace). |
|
709 | - * |
|
710 | - * @return string |
|
711 | - */ |
|
712 | - public function getDaoClassName() : string |
|
713 | - { |
|
714 | - return $this->namingStrategy->getDaoClassName($this->table->getName()); |
|
715 | - } |
|
716 | - |
|
717 | - /** |
|
718 | - * Returns the base DAO class name (without the namespace). |
|
719 | - * |
|
720 | - * @return string |
|
721 | - */ |
|
722 | - public function getBaseDaoClassName() : string |
|
723 | - { |
|
724 | - return $this->namingStrategy->getBaseDaoClassName($this->table->getName()); |
|
725 | - } |
|
726 | - |
|
727 | - /** |
|
728 | - * Returns the table used to build this bean. |
|
729 | - * |
|
730 | - * @return Table |
|
731 | - */ |
|
732 | - public function getTable(): Table |
|
733 | - { |
|
734 | - return $this->table; |
|
735 | - } |
|
736 | - |
|
737 | - /** |
|
738 | - * Returns the extended bean class name (without the namespace), or null if the bean is not extended. |
|
739 | - * |
|
740 | - * @return string |
|
741 | - */ |
|
742 | - public function getExtendedBeanClassName(): ?string |
|
743 | - { |
|
744 | - $parentFk = $this->schemaAnalyzer->getParentRelationship($this->table->getName()); |
|
745 | - if ($parentFk !== null) { |
|
746 | - return $this->namingStrategy->getBeanClassName($parentFk->getForeignTableName()); |
|
747 | - } else { |
|
748 | - return null; |
|
749 | - } |
|
750 | - } |
|
682 | + } |
|
683 | + |
|
684 | + return ''; |
|
685 | + } |
|
686 | + |
|
687 | + /** |
|
688 | + * Returns the bean class name (without the namespace). |
|
689 | + * |
|
690 | + * @return string |
|
691 | + */ |
|
692 | + public function getBeanClassName() : string |
|
693 | + { |
|
694 | + return $this->namingStrategy->getBeanClassName($this->table->getName()); |
|
695 | + } |
|
696 | + |
|
697 | + /** |
|
698 | + * Returns the base bean class name (without the namespace). |
|
699 | + * |
|
700 | + * @return string |
|
701 | + */ |
|
702 | + public function getBaseBeanClassName() : string |
|
703 | + { |
|
704 | + return $this->namingStrategy->getBaseBeanClassName($this->table->getName()); |
|
705 | + } |
|
706 | + |
|
707 | + /** |
|
708 | + * Returns the DAO class name (without the namespace). |
|
709 | + * |
|
710 | + * @return string |
|
711 | + */ |
|
712 | + public function getDaoClassName() : string |
|
713 | + { |
|
714 | + return $this->namingStrategy->getDaoClassName($this->table->getName()); |
|
715 | + } |
|
716 | + |
|
717 | + /** |
|
718 | + * Returns the base DAO class name (without the namespace). |
|
719 | + * |
|
720 | + * @return string |
|
721 | + */ |
|
722 | + public function getBaseDaoClassName() : string |
|
723 | + { |
|
724 | + return $this->namingStrategy->getBaseDaoClassName($this->table->getName()); |
|
725 | + } |
|
726 | + |
|
727 | + /** |
|
728 | + * Returns the table used to build this bean. |
|
729 | + * |
|
730 | + * @return Table |
|
731 | + */ |
|
732 | + public function getTable(): Table |
|
733 | + { |
|
734 | + return $this->table; |
|
735 | + } |
|
736 | + |
|
737 | + /** |
|
738 | + * Returns the extended bean class name (without the namespace), or null if the bean is not extended. |
|
739 | + * |
|
740 | + * @return string |
|
741 | + */ |
|
742 | + public function getExtendedBeanClassName(): ?string |
|
743 | + { |
|
744 | + $parentFk = $this->schemaAnalyzer->getParentRelationship($this->table->getName()); |
|
745 | + if ($parentFk !== null) { |
|
746 | + return $this->namingStrategy->getBeanClassName($parentFk->getForeignTableName()); |
|
747 | + } else { |
|
748 | + return null; |
|
749 | + } |
|
750 | + } |
|
751 | 751 | } |
@@ -105,7 +105,7 @@ discard block |
||
105 | 105 | */ |
106 | 106 | public function getConstructorProperties() |
107 | 107 | { |
108 | - $constructorProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) { |
|
108 | + $constructorProperties = array_filter($this->beanPropertyDescriptors, function(AbstractBeanPropertyDescriptor $property) { |
|
109 | 109 | return $property->isCompulsory(); |
110 | 110 | }); |
111 | 111 | |
@@ -120,7 +120,7 @@ discard block |
||
120 | 120 | public function getPropertiesWithDefault() |
121 | 121 | { |
122 | 122 | $properties = $this->getPropertiesForTable($this->table); |
123 | - $defaultProperties = array_filter($properties, function (AbstractBeanPropertyDescriptor $property) { |
|
123 | + $defaultProperties = array_filter($properties, function(AbstractBeanPropertyDescriptor $property) { |
|
124 | 124 | return $property->hasDefault(); |
125 | 125 | }); |
126 | 126 | |
@@ -134,7 +134,7 @@ discard block |
||
134 | 134 | */ |
135 | 135 | public function getExposedProperties(): array |
136 | 136 | { |
137 | - $exposedProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) { |
|
137 | + $exposedProperties = array_filter($this->beanPropertyDescriptors, function(AbstractBeanPropertyDescriptor $property) { |
|
138 | 138 | return $property->getTable()->getName() == $this->table->getName(); |
139 | 139 | }); |
140 | 140 | |
@@ -343,7 +343,7 @@ discard block |
||
343 | 343 | } |
344 | 344 | |
345 | 345 | foreach ($descriptorsByMethodName as $descriptorsForMethodName) { |
346 | - if (count($descriptorsForMethodName) > 1) { |
|
346 | + if (count($descriptorsForMethodName)>1) { |
|
347 | 347 | foreach ($descriptorsForMethodName as $descriptor) { |
348 | 348 | $descriptor->useAlternativeName(); |
349 | 349 | } |
@@ -438,7 +438,7 @@ discard block |
||
438 | 438 | |
439 | 439 | $classes = $this->generateExtendsAndUseStatements($parentFk); |
440 | 440 | |
441 | - $uses = array_map(function ($className) use ($beannamespace) { |
|
441 | + $uses = array_map(function($className) use ($beannamespace) { |
|
442 | 442 | return 'use '.$beannamespace.'\\'.$className.";\n"; |
443 | 443 | }, $classes); |
444 | 444 | $use = implode('', $uses); |
@@ -739,7 +739,7 @@ discard block |
||
739 | 739 | * |
740 | 740 | * @return string |
741 | 741 | */ |
742 | - public function getExtendedBeanClassName(): ?string |
|
742 | + public function getExtendedBeanClassName(): ? string |
|
743 | 743 | { |
744 | 744 | $parentFk = $this->schemaAnalyzer->getParentRelationship($this->table->getName()); |
745 | 745 | if ($parentFk !== null) { |
@@ -9,52 +9,52 @@ |
||
9 | 9 | */ |
10 | 10 | interface BeanDescriptorInterface |
11 | 11 | { |
12 | - /** |
|
13 | - * Returns the table used to build this bean. |
|
14 | - * |
|
15 | - * @return Table |
|
16 | - */ |
|
17 | - public function getTable() : Table; |
|
18 | - |
|
19 | - /** |
|
20 | - * Returns the bean class name (without the namespace). |
|
21 | - * |
|
22 | - * @return string |
|
23 | - */ |
|
24 | - public function getBeanClassName(): string; |
|
25 | - |
|
26 | - /** |
|
27 | - * Returns the base bean class name (without the namespace). |
|
28 | - * |
|
29 | - * @return string |
|
30 | - */ |
|
31 | - public function getBaseBeanClassName(): string; |
|
32 | - |
|
33 | - /** |
|
34 | - * Returns the extended bean class name (without the namespace), or null if the bean is not extended. |
|
35 | - * |
|
36 | - * @return null|string |
|
37 | - */ |
|
38 | - public function getExtendedBeanClassName(): ?string; |
|
39 | - |
|
40 | - /** |
|
41 | - * Returns the DAO class name (without the namespace). |
|
42 | - * |
|
43 | - * @return string |
|
44 | - */ |
|
45 | - public function getDaoClassName(): string; |
|
46 | - |
|
47 | - /** |
|
48 | - * Returns the base DAO class name (without the namespace). |
|
49 | - * |
|
50 | - * @return string |
|
51 | - */ |
|
52 | - public function getBaseDaoClassName(): string; |
|
53 | - |
|
54 | - /** |
|
55 | - * Returns the list of properties exposed as getters and setters in this class. |
|
56 | - * |
|
57 | - * @return AbstractBeanPropertyDescriptor[] |
|
58 | - */ |
|
59 | - public function getExposedProperties(): array; |
|
12 | + /** |
|
13 | + * Returns the table used to build this bean. |
|
14 | + * |
|
15 | + * @return Table |
|
16 | + */ |
|
17 | + public function getTable() : Table; |
|
18 | + |
|
19 | + /** |
|
20 | + * Returns the bean class name (without the namespace). |
|
21 | + * |
|
22 | + * @return string |
|
23 | + */ |
|
24 | + public function getBeanClassName(): string; |
|
25 | + |
|
26 | + /** |
|
27 | + * Returns the base bean class name (without the namespace). |
|
28 | + * |
|
29 | + * @return string |
|
30 | + */ |
|
31 | + public function getBaseBeanClassName(): string; |
|
32 | + |
|
33 | + /** |
|
34 | + * Returns the extended bean class name (without the namespace), or null if the bean is not extended. |
|
35 | + * |
|
36 | + * @return null|string |
|
37 | + */ |
|
38 | + public function getExtendedBeanClassName(): ?string; |
|
39 | + |
|
40 | + /** |
|
41 | + * Returns the DAO class name (without the namespace). |
|
42 | + * |
|
43 | + * @return string |
|
44 | + */ |
|
45 | + public function getDaoClassName(): string; |
|
46 | + |
|
47 | + /** |
|
48 | + * Returns the base DAO class name (without the namespace). |
|
49 | + * |
|
50 | + * @return string |
|
51 | + */ |
|
52 | + public function getBaseDaoClassName(): string; |
|
53 | + |
|
54 | + /** |
|
55 | + * Returns the list of properties exposed as getters and setters in this class. |
|
56 | + * |
|
57 | + * @return AbstractBeanPropertyDescriptor[] |
|
58 | + */ |
|
59 | + public function getExposedProperties(): array; |
|
60 | 60 | } |
@@ -35,7 +35,7 @@ |
||
35 | 35 | * |
36 | 36 | * @return null|string |
37 | 37 | */ |
38 | - public function getExtendedBeanClassName(): ?string; |
|
38 | + public function getExtendedBeanClassName(): ? string; |
|
39 | 39 | |
40 | 40 | /** |
41 | 41 | * Returns the DAO class name (without the namespace). |
@@ -8,42 +8,42 @@ |
||
8 | 8 | */ |
9 | 9 | interface NamingStrategyInterface |
10 | 10 | { |
11 | - /** |
|
12 | - * Returns the bean class name from the table name (excluding the namespace). |
|
13 | - * |
|
14 | - * @param string $tableName |
|
15 | - * @return string |
|
16 | - */ |
|
17 | - public function getBeanClassName(string $tableName) : string; |
|
11 | + /** |
|
12 | + * Returns the bean class name from the table name (excluding the namespace). |
|
13 | + * |
|
14 | + * @param string $tableName |
|
15 | + * @return string |
|
16 | + */ |
|
17 | + public function getBeanClassName(string $tableName) : string; |
|
18 | 18 | |
19 | - /** |
|
20 | - * Returns the base bean class name from the table name (excluding the namespace). |
|
21 | - * |
|
22 | - * @param string $tableName |
|
23 | - * @return string |
|
24 | - */ |
|
25 | - public function getBaseBeanClassName(string $tableName) : string; |
|
19 | + /** |
|
20 | + * Returns the base bean class name from the table name (excluding the namespace). |
|
21 | + * |
|
22 | + * @param string $tableName |
|
23 | + * @return string |
|
24 | + */ |
|
25 | + public function getBaseBeanClassName(string $tableName) : string; |
|
26 | 26 | |
27 | - /** |
|
28 | - * Returns the name of the DAO class from the table name (excluding the namespace). |
|
29 | - * |
|
30 | - * @param string $tableName |
|
31 | - * @return string |
|
32 | - */ |
|
33 | - public function getDaoClassName(string $tableName) : string; |
|
27 | + /** |
|
28 | + * Returns the name of the DAO class from the table name (excluding the namespace). |
|
29 | + * |
|
30 | + * @param string $tableName |
|
31 | + * @return string |
|
32 | + */ |
|
33 | + public function getDaoClassName(string $tableName) : string; |
|
34 | 34 | |
35 | - /** |
|
36 | - * Returns the name of the base DAO class from the table name (excluding the namespace). |
|
37 | - * |
|
38 | - * @param string $tableName |
|
39 | - * @return string |
|
40 | - */ |
|
41 | - public function getBaseDaoClassName(string $tableName) : string; |
|
35 | + /** |
|
36 | + * Returns the name of the base DAO class from the table name (excluding the namespace). |
|
37 | + * |
|
38 | + * @param string $tableName |
|
39 | + * @return string |
|
40 | + */ |
|
41 | + public function getBaseDaoClassName(string $tableName) : string; |
|
42 | 42 | |
43 | - /** |
|
44 | - * Returns the class name for the DAO factory. |
|
45 | - * |
|
46 | - * @return string |
|
47 | - */ |
|
48 | - public function getDaoFactoryClassName() : string; |
|
43 | + /** |
|
44 | + * Returns the class name for the DAO factory. |
|
45 | + * |
|
46 | + * @return string |
|
47 | + */ |
|
48 | + public function getDaoFactoryClassName() : string; |
|
49 | 49 | } |
@@ -7,181 +7,181 @@ |
||
7 | 7 | |
8 | 8 | class DefaultNamingStrategy implements NamingStrategyInterface |
9 | 9 | { |
10 | - private $beanPrefix = ''; |
|
11 | - private $beanSuffix = ''; |
|
12 | - private $baseBeanPrefix = 'Abstract'; |
|
13 | - private $baseBeanSuffix = ''; |
|
14 | - private $daoPrefix = ''; |
|
15 | - private $daoSuffix = 'Dao'; |
|
16 | - private $baseDaoPrefix = 'Abstract'; |
|
17 | - private $baseDaoSuffix = 'Dao'; |
|
18 | - |
|
19 | - /** |
|
20 | - * Sets the string prefix to any bean class name. |
|
21 | - * |
|
22 | - * @param string $beanPrefix |
|
23 | - */ |
|
24 | - public function setBeanPrefix(string $beanPrefix) |
|
25 | - { |
|
26 | - $this->beanPrefix = $beanPrefix; |
|
27 | - } |
|
28 | - |
|
29 | - /** |
|
30 | - * Sets the string suffix to any bean class name. |
|
31 | - * |
|
32 | - * @param string $beanSuffix |
|
33 | - */ |
|
34 | - public function setBeanSuffix(string $beanSuffix) |
|
35 | - { |
|
36 | - $this->beanSuffix = $beanSuffix; |
|
37 | - } |
|
38 | - |
|
39 | - /** |
|
40 | - * Sets the string prefix to any base bean class name. |
|
41 | - * |
|
42 | - * @param string $baseBeanPrefix |
|
43 | - */ |
|
44 | - public function setBaseBeanPrefix(string $baseBeanPrefix) |
|
45 | - { |
|
46 | - $this->baseBeanPrefix = $baseBeanPrefix; |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * Sets the string suffix to any base bean class name. |
|
51 | - * |
|
52 | - * @param string $baseBeanSuffix |
|
53 | - */ |
|
54 | - public function setBaseBeanSuffix(string $baseBeanSuffix) |
|
55 | - { |
|
56 | - $this->baseBeanSuffix = $baseBeanSuffix; |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * Sets the string prefix to any DAO class name. |
|
61 | - * |
|
62 | - * @param string $daoPrefix |
|
63 | - */ |
|
64 | - public function setDaoPrefix(string $daoPrefix) |
|
65 | - { |
|
66 | - $this->daoPrefix = $daoPrefix; |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * Sets the string suffix to any DAO class name. |
|
71 | - * |
|
72 | - * @param string $daoSuffix |
|
73 | - */ |
|
74 | - public function setDaoSuffix(string $daoSuffix) |
|
75 | - { |
|
76 | - $this->daoSuffix = $daoSuffix; |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * Sets the string prefix to any base DAO class name. |
|
81 | - * |
|
82 | - * @param string $baseDaoPrefix |
|
83 | - */ |
|
84 | - public function setBaseDaoPrefix(string $baseDaoPrefix) |
|
85 | - { |
|
86 | - $this->baseDaoPrefix = $baseDaoPrefix; |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * Sets the string suffix to any base DAO class name. |
|
91 | - * |
|
92 | - * @param string $baseDaoSuffix |
|
93 | - */ |
|
94 | - public function setBaseDaoSuffix(string $baseDaoSuffix) |
|
95 | - { |
|
96 | - $this->baseDaoSuffix = $baseDaoSuffix; |
|
97 | - } |
|
98 | - |
|
99 | - |
|
100 | - /** |
|
101 | - * Returns the bean class name from the table name (excluding the namespace). |
|
102 | - * |
|
103 | - * @param string $tableName |
|
104 | - * @return string |
|
105 | - */ |
|
106 | - public function getBeanClassName(string $tableName): string |
|
107 | - { |
|
108 | - return $this->beanPrefix.self::toSingularCamelCase($tableName).$this->beanSuffix; |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * Returns the base bean class name from the table name (excluding the namespace). |
|
113 | - * |
|
114 | - * @param string $tableName |
|
115 | - * @return string |
|
116 | - */ |
|
117 | - public function getBaseBeanClassName(string $tableName): string |
|
118 | - { |
|
119 | - return $this->baseBeanPrefix.self::toSingularCamelCase($tableName).$this->baseBeanSuffix; |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Returns the name of the DAO class from the table name (excluding the namespace). |
|
124 | - * |
|
125 | - * @param string $tableName |
|
126 | - * @return string |
|
127 | - */ |
|
128 | - public function getDaoClassName(string $tableName): string |
|
129 | - { |
|
130 | - return $this->daoPrefix.self::toSingularCamelCase($tableName).$this->daoSuffix; |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * Returns the name of the base DAO class from the table name (excluding the namespace). |
|
135 | - * |
|
136 | - * @param string $tableName |
|
137 | - * @return string |
|
138 | - */ |
|
139 | - public function getBaseDaoClassName(string $tableName): string |
|
140 | - { |
|
141 | - return $this->baseDaoPrefix.self::toSingularCamelCase($tableName).$this->baseDaoSuffix; |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * Tries to put string to the singular form (if it is plural) and camel case form. |
|
146 | - * We assume the table names are in english. |
|
147 | - * |
|
148 | - * @param $str string |
|
149 | - * |
|
150 | - * @return string |
|
151 | - */ |
|
152 | - private static function toSingularCamelCase(string $str): string |
|
153 | - { |
|
154 | - $tokens = preg_split("/[_ ]+/", $str); |
|
155 | - $tokens = array_map([Inflector::class, 'singularize'], $tokens); |
|
156 | - |
|
157 | - $str = ''; |
|
158 | - foreach ($tokens as $token) { |
|
159 | - $str .= ucfirst(Inflector::singularize($token)); |
|
160 | - } |
|
161 | - |
|
162 | - return $str; |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * Put the first letter of the string in lower case. |
|
167 | - * Very useful to transform a class name into a variable name. |
|
168 | - * |
|
169 | - * @param $str string |
|
170 | - * |
|
171 | - * @return string |
|
172 | - */ |
|
173 | - private static function toVariableName($str) |
|
174 | - { |
|
175 | - return strtolower(substr($str, 0, 1)).substr($str, 1); |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * Returns the class name for the DAO factory. |
|
180 | - * |
|
181 | - * @return string |
|
182 | - */ |
|
183 | - public function getDaoFactoryClassName(): string |
|
184 | - { |
|
185 | - return 'DaoFactory'; |
|
186 | - } |
|
10 | + private $beanPrefix = ''; |
|
11 | + private $beanSuffix = ''; |
|
12 | + private $baseBeanPrefix = 'Abstract'; |
|
13 | + private $baseBeanSuffix = ''; |
|
14 | + private $daoPrefix = ''; |
|
15 | + private $daoSuffix = 'Dao'; |
|
16 | + private $baseDaoPrefix = 'Abstract'; |
|
17 | + private $baseDaoSuffix = 'Dao'; |
|
18 | + |
|
19 | + /** |
|
20 | + * Sets the string prefix to any bean class name. |
|
21 | + * |
|
22 | + * @param string $beanPrefix |
|
23 | + */ |
|
24 | + public function setBeanPrefix(string $beanPrefix) |
|
25 | + { |
|
26 | + $this->beanPrefix = $beanPrefix; |
|
27 | + } |
|
28 | + |
|
29 | + /** |
|
30 | + * Sets the string suffix to any bean class name. |
|
31 | + * |
|
32 | + * @param string $beanSuffix |
|
33 | + */ |
|
34 | + public function setBeanSuffix(string $beanSuffix) |
|
35 | + { |
|
36 | + $this->beanSuffix = $beanSuffix; |
|
37 | + } |
|
38 | + |
|
39 | + /** |
|
40 | + * Sets the string prefix to any base bean class name. |
|
41 | + * |
|
42 | + * @param string $baseBeanPrefix |
|
43 | + */ |
|
44 | + public function setBaseBeanPrefix(string $baseBeanPrefix) |
|
45 | + { |
|
46 | + $this->baseBeanPrefix = $baseBeanPrefix; |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * Sets the string suffix to any base bean class name. |
|
51 | + * |
|
52 | + * @param string $baseBeanSuffix |
|
53 | + */ |
|
54 | + public function setBaseBeanSuffix(string $baseBeanSuffix) |
|
55 | + { |
|
56 | + $this->baseBeanSuffix = $baseBeanSuffix; |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * Sets the string prefix to any DAO class name. |
|
61 | + * |
|
62 | + * @param string $daoPrefix |
|
63 | + */ |
|
64 | + public function setDaoPrefix(string $daoPrefix) |
|
65 | + { |
|
66 | + $this->daoPrefix = $daoPrefix; |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * Sets the string suffix to any DAO class name. |
|
71 | + * |
|
72 | + * @param string $daoSuffix |
|
73 | + */ |
|
74 | + public function setDaoSuffix(string $daoSuffix) |
|
75 | + { |
|
76 | + $this->daoSuffix = $daoSuffix; |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * Sets the string prefix to any base DAO class name. |
|
81 | + * |
|
82 | + * @param string $baseDaoPrefix |
|
83 | + */ |
|
84 | + public function setBaseDaoPrefix(string $baseDaoPrefix) |
|
85 | + { |
|
86 | + $this->baseDaoPrefix = $baseDaoPrefix; |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * Sets the string suffix to any base DAO class name. |
|
91 | + * |
|
92 | + * @param string $baseDaoSuffix |
|
93 | + */ |
|
94 | + public function setBaseDaoSuffix(string $baseDaoSuffix) |
|
95 | + { |
|
96 | + $this->baseDaoSuffix = $baseDaoSuffix; |
|
97 | + } |
|
98 | + |
|
99 | + |
|
100 | + /** |
|
101 | + * Returns the bean class name from the table name (excluding the namespace). |
|
102 | + * |
|
103 | + * @param string $tableName |
|
104 | + * @return string |
|
105 | + */ |
|
106 | + public function getBeanClassName(string $tableName): string |
|
107 | + { |
|
108 | + return $this->beanPrefix.self::toSingularCamelCase($tableName).$this->beanSuffix; |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * Returns the base bean class name from the table name (excluding the namespace). |
|
113 | + * |
|
114 | + * @param string $tableName |
|
115 | + * @return string |
|
116 | + */ |
|
117 | + public function getBaseBeanClassName(string $tableName): string |
|
118 | + { |
|
119 | + return $this->baseBeanPrefix.self::toSingularCamelCase($tableName).$this->baseBeanSuffix; |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Returns the name of the DAO class from the table name (excluding the namespace). |
|
124 | + * |
|
125 | + * @param string $tableName |
|
126 | + * @return string |
|
127 | + */ |
|
128 | + public function getDaoClassName(string $tableName): string |
|
129 | + { |
|
130 | + return $this->daoPrefix.self::toSingularCamelCase($tableName).$this->daoSuffix; |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * Returns the name of the base DAO class from the table name (excluding the namespace). |
|
135 | + * |
|
136 | + * @param string $tableName |
|
137 | + * @return string |
|
138 | + */ |
|
139 | + public function getBaseDaoClassName(string $tableName): string |
|
140 | + { |
|
141 | + return $this->baseDaoPrefix.self::toSingularCamelCase($tableName).$this->baseDaoSuffix; |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * Tries to put string to the singular form (if it is plural) and camel case form. |
|
146 | + * We assume the table names are in english. |
|
147 | + * |
|
148 | + * @param $str string |
|
149 | + * |
|
150 | + * @return string |
|
151 | + */ |
|
152 | + private static function toSingularCamelCase(string $str): string |
|
153 | + { |
|
154 | + $tokens = preg_split("/[_ ]+/", $str); |
|
155 | + $tokens = array_map([Inflector::class, 'singularize'], $tokens); |
|
156 | + |
|
157 | + $str = ''; |
|
158 | + foreach ($tokens as $token) { |
|
159 | + $str .= ucfirst(Inflector::singularize($token)); |
|
160 | + } |
|
161 | + |
|
162 | + return $str; |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * Put the first letter of the string in lower case. |
|
167 | + * Very useful to transform a class name into a variable name. |
|
168 | + * |
|
169 | + * @param $str string |
|
170 | + * |
|
171 | + * @return string |
|
172 | + */ |
|
173 | + private static function toVariableName($str) |
|
174 | + { |
|
175 | + return strtolower(substr($str, 0, 1)).substr($str, 1); |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * Returns the class name for the DAO factory. |
|
180 | + * |
|
181 | + * @return string |
|
182 | + */ |
|
183 | + public function getDaoFactoryClassName(): string |
|
184 | + { |
|
185 | + return 'DaoFactory'; |
|
186 | + } |
|
187 | 187 | } |
@@ -10,9 +10,9 @@ |
||
10 | 10 | */ |
11 | 11 | interface GeneratorListenerInterface |
12 | 12 | { |
13 | - /** |
|
14 | - * @param ConfigurationInterface $configuration |
|
15 | - * @param BeanDescriptorInterface[] $beanDescriptors |
|
16 | - */ |
|
17 | - public function onGenerate(ConfigurationInterface $configuration, array $beanDescriptors) : void; |
|
13 | + /** |
|
14 | + * @param ConfigurationInterface $configuration |
|
15 | + * @param BeanDescriptorInterface[] $beanDescriptors |
|
16 | + */ |
|
17 | + public function onGenerate(ConfigurationInterface $configuration, array $beanDescriptors) : void; |
|
18 | 18 | } |
@@ -17,21 +17,21 @@ |
||
17 | 17 | */ |
18 | 18 | class MoufConfiguration extends Configuration |
19 | 19 | { |
20 | - private $daoFactoryInstanceName = 'daoFactory'; |
|
20 | + private $daoFactoryInstanceName = 'daoFactory'; |
|
21 | 21 | |
22 | - /** |
|
23 | - * @return string |
|
24 | - */ |
|
25 | - public function getDaoFactoryInstanceName() : string |
|
26 | - { |
|
27 | - return $this->daoFactoryInstanceName; |
|
28 | - } |
|
22 | + /** |
|
23 | + * @return string |
|
24 | + */ |
|
25 | + public function getDaoFactoryInstanceName() : string |
|
26 | + { |
|
27 | + return $this->daoFactoryInstanceName; |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * @param string $daoFactoryInstanceName |
|
32 | - */ |
|
33 | - public function setDaoFactoryInstanceName(string $daoFactoryInstanceName) |
|
34 | - { |
|
35 | - $this->daoFactoryInstanceName = $daoFactoryInstanceName; |
|
36 | - } |
|
30 | + /** |
|
31 | + * @param string $daoFactoryInstanceName |
|
32 | + */ |
|
33 | + public function setDaoFactoryInstanceName(string $daoFactoryInstanceName) |
|
34 | + { |
|
35 | + $this->daoFactoryInstanceName = $daoFactoryInstanceName; |
|
36 | + } |
|
37 | 37 | } |
@@ -33,42 +33,42 @@ |
||
33 | 33 | */ |
34 | 34 | class TDBMObject extends AbstractTDBMObject |
35 | 35 | { |
36 | - public function getProperty($var, $tableName = null) |
|
37 | - { |
|
38 | - return $this->get($var, $tableName); |
|
39 | - } |
|
36 | + public function getProperty($var, $tableName = null) |
|
37 | + { |
|
38 | + return $this->get($var, $tableName); |
|
39 | + } |
|
40 | 40 | |
41 | - public function setProperty($var, $value, $tableName = null) |
|
42 | - { |
|
43 | - $this->set($var, $value, $tableName); |
|
44 | - } |
|
41 | + public function setProperty($var, $value, $tableName = null) |
|
42 | + { |
|
43 | + $this->set($var, $value, $tableName); |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * Specify data which should be serialized to JSON. |
|
48 | - * |
|
49 | - * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
|
50 | - * |
|
51 | - * @return mixed data which can be serialized by <b>json_encode</b>, |
|
52 | - * which is a value of any type other than a resource |
|
53 | - * |
|
54 | - * @since 5.4.0 |
|
55 | - */ |
|
56 | - public function jsonSerialize() |
|
57 | - { |
|
58 | - throw new TDBMException('Json serialization is only implemented for generated beans.'); |
|
59 | - } |
|
46 | + /** |
|
47 | + * Specify data which should be serialized to JSON. |
|
48 | + * |
|
49 | + * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
|
50 | + * |
|
51 | + * @return mixed data which can be serialized by <b>json_encode</b>, |
|
52 | + * which is a value of any type other than a resource |
|
53 | + * |
|
54 | + * @since 5.4.0 |
|
55 | + */ |
|
56 | + public function jsonSerialize() |
|
57 | + { |
|
58 | + throw new TDBMException('Json serialization is only implemented for generated beans.'); |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * Returns an array of used tables by this bean (from parent to child relationship). |
|
63 | - * |
|
64 | - * @return string[] |
|
65 | - */ |
|
66 | - protected function getUsedTables() : array |
|
67 | - { |
|
68 | - $tableNames = array_keys($this->dbRows); |
|
69 | - $tableNames = $this->tdbmService->_getLinkBetweenInheritedTables($tableNames); |
|
70 | - $tableNames = array_reverse($tableNames); |
|
61 | + /** |
|
62 | + * Returns an array of used tables by this bean (from parent to child relationship). |
|
63 | + * |
|
64 | + * @return string[] |
|
65 | + */ |
|
66 | + protected function getUsedTables() : array |
|
67 | + { |
|
68 | + $tableNames = array_keys($this->dbRows); |
|
69 | + $tableNames = $this->tdbmService->_getLinkBetweenInheritedTables($tableNames); |
|
70 | + $tableNames = array_reverse($tableNames); |
|
71 | 71 | |
72 | - return $tableNames; |
|
73 | - } |
|
72 | + return $tableNames; |
|
73 | + } |
|
74 | 74 | } |
@@ -31,615 +31,615 @@ |
||
31 | 31 | */ |
32 | 32 | abstract class AbstractTDBMObject implements JsonSerializable |
33 | 33 | { |
34 | - /** |
|
35 | - * The service this object is bound to. |
|
36 | - * |
|
37 | - * @var TDBMService |
|
38 | - */ |
|
39 | - protected $tdbmService; |
|
40 | - |
|
41 | - /** |
|
42 | - * An array of DbRow, indexed by table name. |
|
43 | - * |
|
44 | - * @var DbRow[] |
|
45 | - */ |
|
46 | - protected $dbRows = []; |
|
47 | - |
|
48 | - /** |
|
49 | - * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
50 | - * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
51 | - * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
52 | - * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
53 | - * |
|
54 | - * @var string |
|
55 | - */ |
|
56 | - private $status; |
|
57 | - |
|
58 | - /** |
|
59 | - * Array storing beans related via many to many relationships (pivot tables). |
|
60 | - * |
|
61 | - * @var \SplObjectStorage[] Key: pivot table name, value: SplObjectStorage |
|
62 | - */ |
|
63 | - private $relationships = []; |
|
64 | - |
|
65 | - /** |
|
66 | - * @var bool[] Key: pivot table name, value: whether a query was performed to load the data |
|
67 | - */ |
|
68 | - private $loadedRelationships = []; |
|
69 | - |
|
70 | - /** |
|
71 | - * Array storing beans related via many to one relationships (this bean is pointed by external beans). |
|
72 | - * |
|
73 | - * @var AlterableResultIterator[] Key: [external_table]___[external_column], value: SplObjectStorage |
|
74 | - */ |
|
75 | - private $manyToOneRelationships = []; |
|
76 | - |
|
77 | - /** |
|
78 | - * Used with $primaryKeys when we want to retrieve an existing object |
|
79 | - * and $primaryKeys=[] if we want a new object. |
|
80 | - * |
|
81 | - * @param string $tableName |
|
82 | - * @param array $primaryKeys |
|
83 | - * @param TDBMService $tdbmService |
|
84 | - * |
|
85 | - * @throws TDBMException |
|
86 | - * @throws TDBMInvalidOperationException |
|
87 | - */ |
|
88 | - public function __construct($tableName = null, array $primaryKeys = [], TDBMService $tdbmService = null) |
|
89 | - { |
|
90 | - // FIXME: lazy loading should be forbidden on tables with inheritance and dynamic type assignation... |
|
91 | - if (!empty($tableName)) { |
|
92 | - $this->dbRows[$tableName] = new DbRow($this, $tableName, $primaryKeys, $tdbmService); |
|
93 | - } |
|
94 | - |
|
95 | - if ($tdbmService === null) { |
|
96 | - $this->_setStatus(TDBMObjectStateEnum::STATE_DETACHED); |
|
97 | - } else { |
|
98 | - $this->_attach($tdbmService); |
|
99 | - if (!empty($primaryKeys)) { |
|
100 | - $this->_setStatus(TDBMObjectStateEnum::STATE_NOT_LOADED); |
|
101 | - } else { |
|
102 | - $this->_setStatus(TDBMObjectStateEnum::STATE_NEW); |
|
103 | - } |
|
104 | - } |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * Alternative constructor called when data is fetched from database via a SELECT. |
|
109 | - * |
|
110 | - * @param array $beanData array<table, array<column, value>> |
|
111 | - * @param TDBMService $tdbmService |
|
112 | - */ |
|
113 | - public function _constructFromData(array $beanData, TDBMService $tdbmService) |
|
114 | - { |
|
115 | - $this->tdbmService = $tdbmService; |
|
116 | - |
|
117 | - foreach ($beanData as $table => $columns) { |
|
118 | - $this->dbRows[$table] = new DbRow($this, $table, $tdbmService->_getPrimaryKeysFromObjectData($table, $columns), $tdbmService, $columns); |
|
119 | - } |
|
120 | - |
|
121 | - $this->status = TDBMObjectStateEnum::STATE_LOADED; |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * Alternative constructor called when bean is lazily loaded. |
|
126 | - * |
|
127 | - * @param string $tableName |
|
128 | - * @param array $primaryKeys |
|
129 | - * @param TDBMService $tdbmService |
|
130 | - */ |
|
131 | - public function _constructLazy($tableName, array $primaryKeys, TDBMService $tdbmService) |
|
132 | - { |
|
133 | - $this->tdbmService = $tdbmService; |
|
134 | - |
|
135 | - $this->dbRows[$tableName] = new DbRow($this, $tableName, $primaryKeys, $tdbmService); |
|
136 | - |
|
137 | - $this->status = TDBMObjectStateEnum::STATE_NOT_LOADED; |
|
138 | - } |
|
139 | - |
|
140 | - public function _attach(TDBMService $tdbmService) |
|
141 | - { |
|
142 | - if ($this->status !== TDBMObjectStateEnum::STATE_DETACHED) { |
|
143 | - throw new TDBMInvalidOperationException('Cannot attach an object that is already attached to TDBM.'); |
|
144 | - } |
|
145 | - $this->tdbmService = $tdbmService; |
|
146 | - |
|
147 | - // If we attach this object, we must work to make sure the tables are in ascending order (from low level to top level) |
|
148 | - $tableNames = $this->getUsedTables(); |
|
149 | - |
|
150 | - $newDbRows = []; |
|
151 | - |
|
152 | - foreach ($tableNames as $table) { |
|
153 | - if (!isset($this->dbRows[$table])) { |
|
154 | - $this->registerTable($table); |
|
155 | - } |
|
156 | - $newDbRows[$table] = $this->dbRows[$table]; |
|
157 | - } |
|
158 | - $this->dbRows = $newDbRows; |
|
159 | - |
|
160 | - $this->status = TDBMObjectStateEnum::STATE_NEW; |
|
161 | - foreach ($this->dbRows as $dbRow) { |
|
162 | - $dbRow->_attach($tdbmService); |
|
163 | - } |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * Sets the state of the TDBM Object |
|
168 | - * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
169 | - * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
170 | - * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
171 | - * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
172 | - * |
|
173 | - * @param string $state |
|
174 | - */ |
|
175 | - public function _setStatus($state) |
|
176 | - { |
|
177 | - $this->status = $state; |
|
178 | - |
|
179 | - // TODO: we might ignore the loaded => dirty state here! dirty status comes from the db_row itself. |
|
180 | - foreach ($this->dbRows as $dbRow) { |
|
181 | - $dbRow->_setStatus($state); |
|
182 | - } |
|
183 | - |
|
184 | - if ($state === TDBMObjectStateEnum::STATE_DELETED) { |
|
185 | - $this->onDelete(); |
|
186 | - } |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * Checks that $tableName is ok, or returns the only possible table name if "$tableName = null" |
|
191 | - * or throws an error. |
|
192 | - * |
|
193 | - * @param string $tableName |
|
194 | - * |
|
195 | - * @return string |
|
196 | - */ |
|
197 | - private function checkTableName($tableName = null) |
|
198 | - { |
|
199 | - if ($tableName === null) { |
|
200 | - if (count($this->dbRows) > 1) { |
|
201 | - throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
|
202 | - } elseif (count($this->dbRows) === 1) { |
|
203 | - $tableName = array_keys($this->dbRows)[0]; |
|
204 | - } |
|
205 | - } |
|
206 | - |
|
207 | - return $tableName; |
|
208 | - } |
|
209 | - |
|
210 | - protected function get($var, $tableName = null) |
|
211 | - { |
|
212 | - $tableName = $this->checkTableName($tableName); |
|
213 | - |
|
214 | - if (!isset($this->dbRows[$tableName])) { |
|
215 | - return; |
|
216 | - } |
|
217 | - |
|
218 | - return $this->dbRows[$tableName]->get($var); |
|
219 | - } |
|
220 | - |
|
221 | - protected function set($var, $value, $tableName = null) |
|
222 | - { |
|
223 | - if ($tableName === null) { |
|
224 | - if (count($this->dbRows) > 1) { |
|
225 | - throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
|
226 | - } elseif (count($this->dbRows) === 1) { |
|
227 | - $tableName = array_keys($this->dbRows)[0]; |
|
228 | - } else { |
|
229 | - throw new TDBMException('Please specify a table for this object.'); |
|
230 | - } |
|
231 | - } |
|
232 | - |
|
233 | - if (!isset($this->dbRows[$tableName])) { |
|
234 | - $this->registerTable($tableName); |
|
235 | - } |
|
236 | - |
|
237 | - $this->dbRows[$tableName]->set($var, $value); |
|
238 | - if ($this->dbRows[$tableName]->_getStatus() === TDBMObjectStateEnum::STATE_DIRTY) { |
|
239 | - $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
240 | - } |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * @param string $foreignKeyName |
|
245 | - * @param AbstractTDBMObject $bean |
|
246 | - */ |
|
247 | - protected function setRef($foreignKeyName, AbstractTDBMObject $bean = null, $tableName = null) |
|
248 | - { |
|
249 | - if ($tableName === null) { |
|
250 | - if (count($this->dbRows) > 1) { |
|
251 | - throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
|
252 | - } elseif (count($this->dbRows) === 1) { |
|
253 | - $tableName = array_keys($this->dbRows)[0]; |
|
254 | - } else { |
|
255 | - throw new TDBMException('Please specify a table for this object.'); |
|
256 | - } |
|
257 | - } |
|
258 | - |
|
259 | - if (!isset($this->dbRows[$tableName])) { |
|
260 | - $this->registerTable($tableName); |
|
261 | - } |
|
262 | - |
|
263 | - $oldLinkedBean = $this->dbRows[$tableName]->getRef($foreignKeyName); |
|
264 | - if ($oldLinkedBean !== null) { |
|
265 | - $oldLinkedBean->removeManyToOneRelationship($tableName, $foreignKeyName, $this); |
|
266 | - } |
|
267 | - |
|
268 | - $this->dbRows[$tableName]->setRef($foreignKeyName, $bean); |
|
269 | - if ($this->dbRows[$tableName]->_getStatus() === TDBMObjectStateEnum::STATE_DIRTY) { |
|
270 | - $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
271 | - } |
|
272 | - |
|
273 | - if ($bean !== null) { |
|
274 | - $bean->setManyToOneRelationship($tableName, $foreignKeyName, $this); |
|
275 | - } |
|
276 | - } |
|
277 | - |
|
278 | - /** |
|
279 | - * @param string $foreignKeyName A unique name for this reference |
|
280 | - * |
|
281 | - * @return AbstractTDBMObject|null |
|
282 | - */ |
|
283 | - protected function getRef($foreignKeyName, $tableName = null) |
|
284 | - { |
|
285 | - $tableName = $this->checkTableName($tableName); |
|
286 | - |
|
287 | - if (!isset($this->dbRows[$tableName])) { |
|
288 | - return; |
|
289 | - } |
|
290 | - |
|
291 | - return $this->dbRows[$tableName]->getRef($foreignKeyName); |
|
292 | - } |
|
293 | - |
|
294 | - /** |
|
295 | - * Adds a many to many relationship to this bean. |
|
296 | - * |
|
297 | - * @param string $pivotTableName |
|
298 | - * @param AbstractTDBMObject $remoteBean |
|
299 | - */ |
|
300 | - protected function addRelationship($pivotTableName, AbstractTDBMObject $remoteBean) |
|
301 | - { |
|
302 | - $this->setRelationship($pivotTableName, $remoteBean, 'new'); |
|
303 | - } |
|
304 | - |
|
305 | - /** |
|
306 | - * Returns true if there is a relationship to this bean. |
|
307 | - * |
|
308 | - * @param string $pivotTableName |
|
309 | - * @param AbstractTDBMObject $remoteBean |
|
310 | - * |
|
311 | - * @return bool |
|
312 | - */ |
|
313 | - protected function hasRelationship($pivotTableName, AbstractTDBMObject $remoteBean) |
|
314 | - { |
|
315 | - $storage = $this->retrieveRelationshipsStorage($pivotTableName); |
|
316 | - |
|
317 | - if ($storage->contains($remoteBean)) { |
|
318 | - if ($storage[$remoteBean]['status'] !== 'delete') { |
|
319 | - return true; |
|
320 | - } |
|
321 | - } |
|
322 | - |
|
323 | - return false; |
|
324 | - } |
|
325 | - |
|
326 | - /** |
|
327 | - * Internal TDBM method. Removes a many to many relationship from this bean. |
|
328 | - * |
|
329 | - * @param string $pivotTableName |
|
330 | - * @param AbstractTDBMObject $remoteBean |
|
331 | - */ |
|
332 | - public function _removeRelationship($pivotTableName, AbstractTDBMObject $remoteBean) |
|
333 | - { |
|
334 | - if (isset($this->relationships[$pivotTableName][$remoteBean]) && $this->relationships[$pivotTableName][$remoteBean]['status'] === 'new') { |
|
335 | - unset($this->relationships[$pivotTableName][$remoteBean]); |
|
336 | - unset($remoteBean->relationships[$pivotTableName][$this]); |
|
337 | - } else { |
|
338 | - $this->setRelationship($pivotTableName, $remoteBean, 'delete'); |
|
339 | - } |
|
340 | - } |
|
341 | - |
|
342 | - /** |
|
343 | - * Sets many to many relationships for this bean. |
|
344 | - * Adds new relationships and removes unused ones. |
|
345 | - * |
|
346 | - * @param $pivotTableName |
|
347 | - * @param array $remoteBeans |
|
348 | - */ |
|
349 | - protected function setRelationships($pivotTableName, array $remoteBeans) |
|
350 | - { |
|
351 | - $storage = $this->retrieveRelationshipsStorage($pivotTableName); |
|
352 | - |
|
353 | - foreach ($storage as $oldRemoteBean) { |
|
354 | - if (!in_array($oldRemoteBean, $remoteBeans, true)) { |
|
355 | - // $oldRemoteBean must be removed |
|
356 | - $this->_removeRelationship($pivotTableName, $oldRemoteBean); |
|
357 | - } |
|
358 | - } |
|
359 | - |
|
360 | - foreach ($remoteBeans as $remoteBean) { |
|
361 | - if (!$storage->contains($remoteBean) || $storage[$remoteBean]['status'] === 'delete') { |
|
362 | - // $remoteBean must be added |
|
363 | - $this->addRelationship($pivotTableName, $remoteBean); |
|
364 | - } |
|
365 | - } |
|
366 | - } |
|
367 | - |
|
368 | - /** |
|
369 | - * Returns the list of objects linked to this bean via $pivotTableName. |
|
370 | - * |
|
371 | - * @param $pivotTableName |
|
372 | - * |
|
373 | - * @return \SplObjectStorage |
|
374 | - */ |
|
375 | - private function retrieveRelationshipsStorage($pivotTableName) |
|
376 | - { |
|
377 | - $storage = $this->getRelationshipStorage($pivotTableName); |
|
378 | - if ($this->status === TDBMObjectStateEnum::STATE_DETACHED || $this->status === TDBMObjectStateEnum::STATE_NEW || (isset($this->loadedRelationships[$pivotTableName]) && $this->loadedRelationships[$pivotTableName])) { |
|
379 | - return $storage; |
|
380 | - } |
|
381 | - |
|
382 | - $beans = $this->tdbmService->_getRelatedBeans($pivotTableName, $this); |
|
383 | - $this->loadedRelationships[$pivotTableName] = true; |
|
384 | - |
|
385 | - foreach ($beans as $bean) { |
|
386 | - if (isset($storage[$bean])) { |
|
387 | - $oldStatus = $storage[$bean]['status']; |
|
388 | - if ($oldStatus === 'delete') { |
|
389 | - // Keep deleted things deleted |
|
390 | - continue; |
|
391 | - } |
|
392 | - } |
|
393 | - $this->setRelationship($pivotTableName, $bean, 'loaded'); |
|
394 | - } |
|
395 | - |
|
396 | - return $storage; |
|
397 | - } |
|
398 | - |
|
399 | - /** |
|
400 | - * Internal TDBM method. Returns the list of objects linked to this bean via $pivotTableName. |
|
401 | - * |
|
402 | - * @param $pivotTableName |
|
403 | - * |
|
404 | - * @return AbstractTDBMObject[] |
|
405 | - */ |
|
406 | - public function _getRelationships($pivotTableName) |
|
407 | - { |
|
408 | - return $this->relationshipStorageToArray($this->retrieveRelationshipsStorage($pivotTableName)); |
|
409 | - } |
|
410 | - |
|
411 | - private function relationshipStorageToArray(\SplObjectStorage $storage) |
|
412 | - { |
|
413 | - $beans = []; |
|
414 | - foreach ($storage as $bean) { |
|
415 | - $statusArr = $storage[$bean]; |
|
416 | - if ($statusArr['status'] !== 'delete') { |
|
417 | - $beans[] = $bean; |
|
418 | - } |
|
419 | - } |
|
420 | - |
|
421 | - return $beans; |
|
422 | - } |
|
423 | - |
|
424 | - /** |
|
425 | - * Declares a relationship between. |
|
426 | - * |
|
427 | - * @param string $pivotTableName |
|
428 | - * @param AbstractTDBMObject $remoteBean |
|
429 | - * @param string $status |
|
430 | - */ |
|
431 | - private function setRelationship($pivotTableName, AbstractTDBMObject $remoteBean, $status) |
|
432 | - { |
|
433 | - $storage = $this->getRelationshipStorage($pivotTableName); |
|
434 | - $storage->attach($remoteBean, ['status' => $status, 'reverse' => false]); |
|
435 | - if ($this->status === TDBMObjectStateEnum::STATE_LOADED) { |
|
436 | - $this->_setStatus(TDBMObjectStateEnum::STATE_DIRTY); |
|
437 | - } |
|
438 | - |
|
439 | - $remoteStorage = $remoteBean->getRelationshipStorage($pivotTableName); |
|
440 | - $remoteStorage->attach($this, ['status' => $status, 'reverse' => true]); |
|
441 | - } |
|
442 | - |
|
443 | - /** |
|
444 | - * Returns the SplObjectStorage associated to this relationship (creates it if it does not exists). |
|
445 | - * |
|
446 | - * @param string $pivotTableName |
|
447 | - * |
|
448 | - * @return \SplObjectStorage |
|
449 | - */ |
|
450 | - private function getRelationshipStorage(string $pivotTableName) : \SplObjectStorage |
|
451 | - { |
|
452 | - return $this->relationships[$pivotTableName] ?? $this->relationships[$pivotTableName] = new \SplObjectStorage(); |
|
453 | - } |
|
454 | - |
|
455 | - /** |
|
456 | - * Returns the SplObjectStorage associated to this relationship (creates it if it does not exists). |
|
457 | - * |
|
458 | - * @param string $tableName |
|
459 | - * @param string $foreignKeyName |
|
460 | - * |
|
461 | - * @return AlterableResultIterator |
|
462 | - */ |
|
463 | - private function getManyToOneAlterableResultIterator(string $tableName, string $foreignKeyName) : AlterableResultIterator |
|
464 | - { |
|
465 | - $key = $tableName.'___'.$foreignKeyName; |
|
466 | - |
|
467 | - return $this->manyToOneRelationships[$key] ?? $this->manyToOneRelationships[$key] = new AlterableResultIterator(); |
|
468 | - } |
|
469 | - |
|
470 | - /** |
|
471 | - * Declares a relationship between this bean and the bean pointing to it. |
|
472 | - * |
|
473 | - * @param string $tableName |
|
474 | - * @param string $foreignKeyName |
|
475 | - * @param AbstractTDBMObject $remoteBean |
|
476 | - */ |
|
477 | - private function setManyToOneRelationship(string $tableName, string $foreignKeyName, AbstractTDBMObject $remoteBean) |
|
478 | - { |
|
479 | - $alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName); |
|
480 | - $alterableResultIterator->add($remoteBean); |
|
481 | - } |
|
482 | - |
|
483 | - /** |
|
484 | - * Declares a relationship between this bean and the bean pointing to it. |
|
485 | - * |
|
486 | - * @param string $tableName |
|
487 | - * @param string $foreignKeyName |
|
488 | - * @param AbstractTDBMObject $remoteBean |
|
489 | - */ |
|
490 | - private function removeManyToOneRelationship(string $tableName, string $foreignKeyName, AbstractTDBMObject $remoteBean) |
|
491 | - { |
|
492 | - $alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName); |
|
493 | - $alterableResultIterator->remove($remoteBean); |
|
494 | - } |
|
495 | - |
|
496 | - /** |
|
497 | - * Returns the list of objects linked to this bean via a given foreign key. |
|
498 | - * |
|
499 | - * @param string $tableName |
|
500 | - * @param string $foreignKeyName |
|
501 | - * @param string $searchTableName |
|
502 | - * @param array $searchFilter |
|
503 | - * @param string $orderString The ORDER BY part of the query. All columns must be prefixed by the table name (in the form: table.column). WARNING : This parameter is not kept when there is an additionnal or removal object ! |
|
504 | - * |
|
505 | - * @return AlterableResultIterator |
|
506 | - */ |
|
507 | - protected function retrieveManyToOneRelationshipsStorage(string $tableName, string $foreignKeyName, string $searchTableName, array $searchFilter, $orderString = null) : AlterableResultIterator |
|
508 | - { |
|
509 | - $key = $tableName.'___'.$foreignKeyName; |
|
510 | - $alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName); |
|
511 | - if ($this->status === TDBMObjectStateEnum::STATE_DETACHED || $this->status === TDBMObjectStateEnum::STATE_NEW || (isset($this->manyToOneRelationships[$key]) && $this->manyToOneRelationships[$key]->getUnderlyingResultIterator() !== null)) { |
|
512 | - return $alterableResultIterator; |
|
513 | - } |
|
514 | - |
|
515 | - $unalteredResultIterator = $this->tdbmService->findObjects($searchTableName, $searchFilter, [], $orderString); |
|
516 | - |
|
517 | - $alterableResultIterator->setResultIterator($unalteredResultIterator->getIterator()); |
|
518 | - |
|
519 | - return $alterableResultIterator; |
|
520 | - } |
|
521 | - |
|
522 | - /** |
|
523 | - * Reverts any changes made to the object and resumes it to its DB state. |
|
524 | - * This can only be called on objects that come from database and that have not been deleted. |
|
525 | - * Otherwise, this will throw an exception. |
|
526 | - * |
|
527 | - * @throws TDBMException |
|
528 | - */ |
|
529 | - public function discardChanges() |
|
530 | - { |
|
531 | - if ($this->status === TDBMObjectStateEnum::STATE_NEW || $this->status === TDBMObjectStateEnum::STATE_DETACHED) { |
|
532 | - throw new TDBMException("You cannot call discardChanges() on an object that has been created with the 'new' keyword and that has not yet been saved."); |
|
533 | - } |
|
534 | - |
|
535 | - if ($this->status === TDBMObjectStateEnum::STATE_DELETED) { |
|
536 | - throw new TDBMException('You cannot call discardChanges() on an object that has been deleted.'); |
|
537 | - } |
|
538 | - |
|
539 | - $this->_setStatus(TDBMObjectStateEnum::STATE_NOT_LOADED); |
|
540 | - } |
|
541 | - |
|
542 | - /** |
|
543 | - * Method used internally by TDBM. You should not use it directly. |
|
544 | - * This method returns the status of the TDBMObject. |
|
545 | - * This is one of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
546 | - * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
547 | - * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
548 | - * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
549 | - * |
|
550 | - * @return string |
|
551 | - */ |
|
552 | - public function _getStatus() |
|
553 | - { |
|
554 | - return $this->status; |
|
555 | - } |
|
556 | - |
|
557 | - /** |
|
558 | - * Override the native php clone function for TDBMObjects. |
|
559 | - */ |
|
560 | - public function __clone() |
|
561 | - { |
|
562 | - // Let's clone the many to many relationships |
|
563 | - if ($this->status === TDBMObjectStateEnum::STATE_DETACHED) { |
|
564 | - $pivotTableList = array_keys($this->relationships); |
|
565 | - } else { |
|
566 | - $pivotTableList = $this->tdbmService->_getPivotTablesLinkedToBean($this); |
|
567 | - } |
|
568 | - |
|
569 | - foreach ($pivotTableList as $pivotTable) { |
|
570 | - $storage = $this->retrieveRelationshipsStorage($pivotTable); |
|
571 | - |
|
572 | - // Let's duplicate the reverse side of the relationship // This is useless: already done by "retrieveRelationshipsStorage"!!! |
|
573 | - /*foreach ($storage as $remoteBean) { |
|
34 | + /** |
|
35 | + * The service this object is bound to. |
|
36 | + * |
|
37 | + * @var TDBMService |
|
38 | + */ |
|
39 | + protected $tdbmService; |
|
40 | + |
|
41 | + /** |
|
42 | + * An array of DbRow, indexed by table name. |
|
43 | + * |
|
44 | + * @var DbRow[] |
|
45 | + */ |
|
46 | + protected $dbRows = []; |
|
47 | + |
|
48 | + /** |
|
49 | + * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
50 | + * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
51 | + * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
52 | + * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
53 | + * |
|
54 | + * @var string |
|
55 | + */ |
|
56 | + private $status; |
|
57 | + |
|
58 | + /** |
|
59 | + * Array storing beans related via many to many relationships (pivot tables). |
|
60 | + * |
|
61 | + * @var \SplObjectStorage[] Key: pivot table name, value: SplObjectStorage |
|
62 | + */ |
|
63 | + private $relationships = []; |
|
64 | + |
|
65 | + /** |
|
66 | + * @var bool[] Key: pivot table name, value: whether a query was performed to load the data |
|
67 | + */ |
|
68 | + private $loadedRelationships = []; |
|
69 | + |
|
70 | + /** |
|
71 | + * Array storing beans related via many to one relationships (this bean is pointed by external beans). |
|
72 | + * |
|
73 | + * @var AlterableResultIterator[] Key: [external_table]___[external_column], value: SplObjectStorage |
|
74 | + */ |
|
75 | + private $manyToOneRelationships = []; |
|
76 | + |
|
77 | + /** |
|
78 | + * Used with $primaryKeys when we want to retrieve an existing object |
|
79 | + * and $primaryKeys=[] if we want a new object. |
|
80 | + * |
|
81 | + * @param string $tableName |
|
82 | + * @param array $primaryKeys |
|
83 | + * @param TDBMService $tdbmService |
|
84 | + * |
|
85 | + * @throws TDBMException |
|
86 | + * @throws TDBMInvalidOperationException |
|
87 | + */ |
|
88 | + public function __construct($tableName = null, array $primaryKeys = [], TDBMService $tdbmService = null) |
|
89 | + { |
|
90 | + // FIXME: lazy loading should be forbidden on tables with inheritance and dynamic type assignation... |
|
91 | + if (!empty($tableName)) { |
|
92 | + $this->dbRows[$tableName] = new DbRow($this, $tableName, $primaryKeys, $tdbmService); |
|
93 | + } |
|
94 | + |
|
95 | + if ($tdbmService === null) { |
|
96 | + $this->_setStatus(TDBMObjectStateEnum::STATE_DETACHED); |
|
97 | + } else { |
|
98 | + $this->_attach($tdbmService); |
|
99 | + if (!empty($primaryKeys)) { |
|
100 | + $this->_setStatus(TDBMObjectStateEnum::STATE_NOT_LOADED); |
|
101 | + } else { |
|
102 | + $this->_setStatus(TDBMObjectStateEnum::STATE_NEW); |
|
103 | + } |
|
104 | + } |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * Alternative constructor called when data is fetched from database via a SELECT. |
|
109 | + * |
|
110 | + * @param array $beanData array<table, array<column, value>> |
|
111 | + * @param TDBMService $tdbmService |
|
112 | + */ |
|
113 | + public function _constructFromData(array $beanData, TDBMService $tdbmService) |
|
114 | + { |
|
115 | + $this->tdbmService = $tdbmService; |
|
116 | + |
|
117 | + foreach ($beanData as $table => $columns) { |
|
118 | + $this->dbRows[$table] = new DbRow($this, $table, $tdbmService->_getPrimaryKeysFromObjectData($table, $columns), $tdbmService, $columns); |
|
119 | + } |
|
120 | + |
|
121 | + $this->status = TDBMObjectStateEnum::STATE_LOADED; |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * Alternative constructor called when bean is lazily loaded. |
|
126 | + * |
|
127 | + * @param string $tableName |
|
128 | + * @param array $primaryKeys |
|
129 | + * @param TDBMService $tdbmService |
|
130 | + */ |
|
131 | + public function _constructLazy($tableName, array $primaryKeys, TDBMService $tdbmService) |
|
132 | + { |
|
133 | + $this->tdbmService = $tdbmService; |
|
134 | + |
|
135 | + $this->dbRows[$tableName] = new DbRow($this, $tableName, $primaryKeys, $tdbmService); |
|
136 | + |
|
137 | + $this->status = TDBMObjectStateEnum::STATE_NOT_LOADED; |
|
138 | + } |
|
139 | + |
|
140 | + public function _attach(TDBMService $tdbmService) |
|
141 | + { |
|
142 | + if ($this->status !== TDBMObjectStateEnum::STATE_DETACHED) { |
|
143 | + throw new TDBMInvalidOperationException('Cannot attach an object that is already attached to TDBM.'); |
|
144 | + } |
|
145 | + $this->tdbmService = $tdbmService; |
|
146 | + |
|
147 | + // If we attach this object, we must work to make sure the tables are in ascending order (from low level to top level) |
|
148 | + $tableNames = $this->getUsedTables(); |
|
149 | + |
|
150 | + $newDbRows = []; |
|
151 | + |
|
152 | + foreach ($tableNames as $table) { |
|
153 | + if (!isset($this->dbRows[$table])) { |
|
154 | + $this->registerTable($table); |
|
155 | + } |
|
156 | + $newDbRows[$table] = $this->dbRows[$table]; |
|
157 | + } |
|
158 | + $this->dbRows = $newDbRows; |
|
159 | + |
|
160 | + $this->status = TDBMObjectStateEnum::STATE_NEW; |
|
161 | + foreach ($this->dbRows as $dbRow) { |
|
162 | + $dbRow->_attach($tdbmService); |
|
163 | + } |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * Sets the state of the TDBM Object |
|
168 | + * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
169 | + * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
170 | + * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
171 | + * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
172 | + * |
|
173 | + * @param string $state |
|
174 | + */ |
|
175 | + public function _setStatus($state) |
|
176 | + { |
|
177 | + $this->status = $state; |
|
178 | + |
|
179 | + // TODO: we might ignore the loaded => dirty state here! dirty status comes from the db_row itself. |
|
180 | + foreach ($this->dbRows as $dbRow) { |
|
181 | + $dbRow->_setStatus($state); |
|
182 | + } |
|
183 | + |
|
184 | + if ($state === TDBMObjectStateEnum::STATE_DELETED) { |
|
185 | + $this->onDelete(); |
|
186 | + } |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * Checks that $tableName is ok, or returns the only possible table name if "$tableName = null" |
|
191 | + * or throws an error. |
|
192 | + * |
|
193 | + * @param string $tableName |
|
194 | + * |
|
195 | + * @return string |
|
196 | + */ |
|
197 | + private function checkTableName($tableName = null) |
|
198 | + { |
|
199 | + if ($tableName === null) { |
|
200 | + if (count($this->dbRows) > 1) { |
|
201 | + throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
|
202 | + } elseif (count($this->dbRows) === 1) { |
|
203 | + $tableName = array_keys($this->dbRows)[0]; |
|
204 | + } |
|
205 | + } |
|
206 | + |
|
207 | + return $tableName; |
|
208 | + } |
|
209 | + |
|
210 | + protected function get($var, $tableName = null) |
|
211 | + { |
|
212 | + $tableName = $this->checkTableName($tableName); |
|
213 | + |
|
214 | + if (!isset($this->dbRows[$tableName])) { |
|
215 | + return; |
|
216 | + } |
|
217 | + |
|
218 | + return $this->dbRows[$tableName]->get($var); |
|
219 | + } |
|
220 | + |
|
221 | + protected function set($var, $value, $tableName = null) |
|
222 | + { |
|
223 | + if ($tableName === null) { |
|
224 | + if (count($this->dbRows) > 1) { |
|
225 | + throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
|
226 | + } elseif (count($this->dbRows) === 1) { |
|
227 | + $tableName = array_keys($this->dbRows)[0]; |
|
228 | + } else { |
|
229 | + throw new TDBMException('Please specify a table for this object.'); |
|
230 | + } |
|
231 | + } |
|
232 | + |
|
233 | + if (!isset($this->dbRows[$tableName])) { |
|
234 | + $this->registerTable($tableName); |
|
235 | + } |
|
236 | + |
|
237 | + $this->dbRows[$tableName]->set($var, $value); |
|
238 | + if ($this->dbRows[$tableName]->_getStatus() === TDBMObjectStateEnum::STATE_DIRTY) { |
|
239 | + $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
240 | + } |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * @param string $foreignKeyName |
|
245 | + * @param AbstractTDBMObject $bean |
|
246 | + */ |
|
247 | + protected function setRef($foreignKeyName, AbstractTDBMObject $bean = null, $tableName = null) |
|
248 | + { |
|
249 | + if ($tableName === null) { |
|
250 | + if (count($this->dbRows) > 1) { |
|
251 | + throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
|
252 | + } elseif (count($this->dbRows) === 1) { |
|
253 | + $tableName = array_keys($this->dbRows)[0]; |
|
254 | + } else { |
|
255 | + throw new TDBMException('Please specify a table for this object.'); |
|
256 | + } |
|
257 | + } |
|
258 | + |
|
259 | + if (!isset($this->dbRows[$tableName])) { |
|
260 | + $this->registerTable($tableName); |
|
261 | + } |
|
262 | + |
|
263 | + $oldLinkedBean = $this->dbRows[$tableName]->getRef($foreignKeyName); |
|
264 | + if ($oldLinkedBean !== null) { |
|
265 | + $oldLinkedBean->removeManyToOneRelationship($tableName, $foreignKeyName, $this); |
|
266 | + } |
|
267 | + |
|
268 | + $this->dbRows[$tableName]->setRef($foreignKeyName, $bean); |
|
269 | + if ($this->dbRows[$tableName]->_getStatus() === TDBMObjectStateEnum::STATE_DIRTY) { |
|
270 | + $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
271 | + } |
|
272 | + |
|
273 | + if ($bean !== null) { |
|
274 | + $bean->setManyToOneRelationship($tableName, $foreignKeyName, $this); |
|
275 | + } |
|
276 | + } |
|
277 | + |
|
278 | + /** |
|
279 | + * @param string $foreignKeyName A unique name for this reference |
|
280 | + * |
|
281 | + * @return AbstractTDBMObject|null |
|
282 | + */ |
|
283 | + protected function getRef($foreignKeyName, $tableName = null) |
|
284 | + { |
|
285 | + $tableName = $this->checkTableName($tableName); |
|
286 | + |
|
287 | + if (!isset($this->dbRows[$tableName])) { |
|
288 | + return; |
|
289 | + } |
|
290 | + |
|
291 | + return $this->dbRows[$tableName]->getRef($foreignKeyName); |
|
292 | + } |
|
293 | + |
|
294 | + /** |
|
295 | + * Adds a many to many relationship to this bean. |
|
296 | + * |
|
297 | + * @param string $pivotTableName |
|
298 | + * @param AbstractTDBMObject $remoteBean |
|
299 | + */ |
|
300 | + protected function addRelationship($pivotTableName, AbstractTDBMObject $remoteBean) |
|
301 | + { |
|
302 | + $this->setRelationship($pivotTableName, $remoteBean, 'new'); |
|
303 | + } |
|
304 | + |
|
305 | + /** |
|
306 | + * Returns true if there is a relationship to this bean. |
|
307 | + * |
|
308 | + * @param string $pivotTableName |
|
309 | + * @param AbstractTDBMObject $remoteBean |
|
310 | + * |
|
311 | + * @return bool |
|
312 | + */ |
|
313 | + protected function hasRelationship($pivotTableName, AbstractTDBMObject $remoteBean) |
|
314 | + { |
|
315 | + $storage = $this->retrieveRelationshipsStorage($pivotTableName); |
|
316 | + |
|
317 | + if ($storage->contains($remoteBean)) { |
|
318 | + if ($storage[$remoteBean]['status'] !== 'delete') { |
|
319 | + return true; |
|
320 | + } |
|
321 | + } |
|
322 | + |
|
323 | + return false; |
|
324 | + } |
|
325 | + |
|
326 | + /** |
|
327 | + * Internal TDBM method. Removes a many to many relationship from this bean. |
|
328 | + * |
|
329 | + * @param string $pivotTableName |
|
330 | + * @param AbstractTDBMObject $remoteBean |
|
331 | + */ |
|
332 | + public function _removeRelationship($pivotTableName, AbstractTDBMObject $remoteBean) |
|
333 | + { |
|
334 | + if (isset($this->relationships[$pivotTableName][$remoteBean]) && $this->relationships[$pivotTableName][$remoteBean]['status'] === 'new') { |
|
335 | + unset($this->relationships[$pivotTableName][$remoteBean]); |
|
336 | + unset($remoteBean->relationships[$pivotTableName][$this]); |
|
337 | + } else { |
|
338 | + $this->setRelationship($pivotTableName, $remoteBean, 'delete'); |
|
339 | + } |
|
340 | + } |
|
341 | + |
|
342 | + /** |
|
343 | + * Sets many to many relationships for this bean. |
|
344 | + * Adds new relationships and removes unused ones. |
|
345 | + * |
|
346 | + * @param $pivotTableName |
|
347 | + * @param array $remoteBeans |
|
348 | + */ |
|
349 | + protected function setRelationships($pivotTableName, array $remoteBeans) |
|
350 | + { |
|
351 | + $storage = $this->retrieveRelationshipsStorage($pivotTableName); |
|
352 | + |
|
353 | + foreach ($storage as $oldRemoteBean) { |
|
354 | + if (!in_array($oldRemoteBean, $remoteBeans, true)) { |
|
355 | + // $oldRemoteBean must be removed |
|
356 | + $this->_removeRelationship($pivotTableName, $oldRemoteBean); |
|
357 | + } |
|
358 | + } |
|
359 | + |
|
360 | + foreach ($remoteBeans as $remoteBean) { |
|
361 | + if (!$storage->contains($remoteBean) || $storage[$remoteBean]['status'] === 'delete') { |
|
362 | + // $remoteBean must be added |
|
363 | + $this->addRelationship($pivotTableName, $remoteBean); |
|
364 | + } |
|
365 | + } |
|
366 | + } |
|
367 | + |
|
368 | + /** |
|
369 | + * Returns the list of objects linked to this bean via $pivotTableName. |
|
370 | + * |
|
371 | + * @param $pivotTableName |
|
372 | + * |
|
373 | + * @return \SplObjectStorage |
|
374 | + */ |
|
375 | + private function retrieveRelationshipsStorage($pivotTableName) |
|
376 | + { |
|
377 | + $storage = $this->getRelationshipStorage($pivotTableName); |
|
378 | + if ($this->status === TDBMObjectStateEnum::STATE_DETACHED || $this->status === TDBMObjectStateEnum::STATE_NEW || (isset($this->loadedRelationships[$pivotTableName]) && $this->loadedRelationships[$pivotTableName])) { |
|
379 | + return $storage; |
|
380 | + } |
|
381 | + |
|
382 | + $beans = $this->tdbmService->_getRelatedBeans($pivotTableName, $this); |
|
383 | + $this->loadedRelationships[$pivotTableName] = true; |
|
384 | + |
|
385 | + foreach ($beans as $bean) { |
|
386 | + if (isset($storage[$bean])) { |
|
387 | + $oldStatus = $storage[$bean]['status']; |
|
388 | + if ($oldStatus === 'delete') { |
|
389 | + // Keep deleted things deleted |
|
390 | + continue; |
|
391 | + } |
|
392 | + } |
|
393 | + $this->setRelationship($pivotTableName, $bean, 'loaded'); |
|
394 | + } |
|
395 | + |
|
396 | + return $storage; |
|
397 | + } |
|
398 | + |
|
399 | + /** |
|
400 | + * Internal TDBM method. Returns the list of objects linked to this bean via $pivotTableName. |
|
401 | + * |
|
402 | + * @param $pivotTableName |
|
403 | + * |
|
404 | + * @return AbstractTDBMObject[] |
|
405 | + */ |
|
406 | + public function _getRelationships($pivotTableName) |
|
407 | + { |
|
408 | + return $this->relationshipStorageToArray($this->retrieveRelationshipsStorage($pivotTableName)); |
|
409 | + } |
|
410 | + |
|
411 | + private function relationshipStorageToArray(\SplObjectStorage $storage) |
|
412 | + { |
|
413 | + $beans = []; |
|
414 | + foreach ($storage as $bean) { |
|
415 | + $statusArr = $storage[$bean]; |
|
416 | + if ($statusArr['status'] !== 'delete') { |
|
417 | + $beans[] = $bean; |
|
418 | + } |
|
419 | + } |
|
420 | + |
|
421 | + return $beans; |
|
422 | + } |
|
423 | + |
|
424 | + /** |
|
425 | + * Declares a relationship between. |
|
426 | + * |
|
427 | + * @param string $pivotTableName |
|
428 | + * @param AbstractTDBMObject $remoteBean |
|
429 | + * @param string $status |
|
430 | + */ |
|
431 | + private function setRelationship($pivotTableName, AbstractTDBMObject $remoteBean, $status) |
|
432 | + { |
|
433 | + $storage = $this->getRelationshipStorage($pivotTableName); |
|
434 | + $storage->attach($remoteBean, ['status' => $status, 'reverse' => false]); |
|
435 | + if ($this->status === TDBMObjectStateEnum::STATE_LOADED) { |
|
436 | + $this->_setStatus(TDBMObjectStateEnum::STATE_DIRTY); |
|
437 | + } |
|
438 | + |
|
439 | + $remoteStorage = $remoteBean->getRelationshipStorage($pivotTableName); |
|
440 | + $remoteStorage->attach($this, ['status' => $status, 'reverse' => true]); |
|
441 | + } |
|
442 | + |
|
443 | + /** |
|
444 | + * Returns the SplObjectStorage associated to this relationship (creates it if it does not exists). |
|
445 | + * |
|
446 | + * @param string $pivotTableName |
|
447 | + * |
|
448 | + * @return \SplObjectStorage |
|
449 | + */ |
|
450 | + private function getRelationshipStorage(string $pivotTableName) : \SplObjectStorage |
|
451 | + { |
|
452 | + return $this->relationships[$pivotTableName] ?? $this->relationships[$pivotTableName] = new \SplObjectStorage(); |
|
453 | + } |
|
454 | + |
|
455 | + /** |
|
456 | + * Returns the SplObjectStorage associated to this relationship (creates it if it does not exists). |
|
457 | + * |
|
458 | + * @param string $tableName |
|
459 | + * @param string $foreignKeyName |
|
460 | + * |
|
461 | + * @return AlterableResultIterator |
|
462 | + */ |
|
463 | + private function getManyToOneAlterableResultIterator(string $tableName, string $foreignKeyName) : AlterableResultIterator |
|
464 | + { |
|
465 | + $key = $tableName.'___'.$foreignKeyName; |
|
466 | + |
|
467 | + return $this->manyToOneRelationships[$key] ?? $this->manyToOneRelationships[$key] = new AlterableResultIterator(); |
|
468 | + } |
|
469 | + |
|
470 | + /** |
|
471 | + * Declares a relationship between this bean and the bean pointing to it. |
|
472 | + * |
|
473 | + * @param string $tableName |
|
474 | + * @param string $foreignKeyName |
|
475 | + * @param AbstractTDBMObject $remoteBean |
|
476 | + */ |
|
477 | + private function setManyToOneRelationship(string $tableName, string $foreignKeyName, AbstractTDBMObject $remoteBean) |
|
478 | + { |
|
479 | + $alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName); |
|
480 | + $alterableResultIterator->add($remoteBean); |
|
481 | + } |
|
482 | + |
|
483 | + /** |
|
484 | + * Declares a relationship between this bean and the bean pointing to it. |
|
485 | + * |
|
486 | + * @param string $tableName |
|
487 | + * @param string $foreignKeyName |
|
488 | + * @param AbstractTDBMObject $remoteBean |
|
489 | + */ |
|
490 | + private function removeManyToOneRelationship(string $tableName, string $foreignKeyName, AbstractTDBMObject $remoteBean) |
|
491 | + { |
|
492 | + $alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName); |
|
493 | + $alterableResultIterator->remove($remoteBean); |
|
494 | + } |
|
495 | + |
|
496 | + /** |
|
497 | + * Returns the list of objects linked to this bean via a given foreign key. |
|
498 | + * |
|
499 | + * @param string $tableName |
|
500 | + * @param string $foreignKeyName |
|
501 | + * @param string $searchTableName |
|
502 | + * @param array $searchFilter |
|
503 | + * @param string $orderString The ORDER BY part of the query. All columns must be prefixed by the table name (in the form: table.column). WARNING : This parameter is not kept when there is an additionnal or removal object ! |
|
504 | + * |
|
505 | + * @return AlterableResultIterator |
|
506 | + */ |
|
507 | + protected function retrieveManyToOneRelationshipsStorage(string $tableName, string $foreignKeyName, string $searchTableName, array $searchFilter, $orderString = null) : AlterableResultIterator |
|
508 | + { |
|
509 | + $key = $tableName.'___'.$foreignKeyName; |
|
510 | + $alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName); |
|
511 | + if ($this->status === TDBMObjectStateEnum::STATE_DETACHED || $this->status === TDBMObjectStateEnum::STATE_NEW || (isset($this->manyToOneRelationships[$key]) && $this->manyToOneRelationships[$key]->getUnderlyingResultIterator() !== null)) { |
|
512 | + return $alterableResultIterator; |
|
513 | + } |
|
514 | + |
|
515 | + $unalteredResultIterator = $this->tdbmService->findObjects($searchTableName, $searchFilter, [], $orderString); |
|
516 | + |
|
517 | + $alterableResultIterator->setResultIterator($unalteredResultIterator->getIterator()); |
|
518 | + |
|
519 | + return $alterableResultIterator; |
|
520 | + } |
|
521 | + |
|
522 | + /** |
|
523 | + * Reverts any changes made to the object and resumes it to its DB state. |
|
524 | + * This can only be called on objects that come from database and that have not been deleted. |
|
525 | + * Otherwise, this will throw an exception. |
|
526 | + * |
|
527 | + * @throws TDBMException |
|
528 | + */ |
|
529 | + public function discardChanges() |
|
530 | + { |
|
531 | + if ($this->status === TDBMObjectStateEnum::STATE_NEW || $this->status === TDBMObjectStateEnum::STATE_DETACHED) { |
|
532 | + throw new TDBMException("You cannot call discardChanges() on an object that has been created with the 'new' keyword and that has not yet been saved."); |
|
533 | + } |
|
534 | + |
|
535 | + if ($this->status === TDBMObjectStateEnum::STATE_DELETED) { |
|
536 | + throw new TDBMException('You cannot call discardChanges() on an object that has been deleted.'); |
|
537 | + } |
|
538 | + |
|
539 | + $this->_setStatus(TDBMObjectStateEnum::STATE_NOT_LOADED); |
|
540 | + } |
|
541 | + |
|
542 | + /** |
|
543 | + * Method used internally by TDBM. You should not use it directly. |
|
544 | + * This method returns the status of the TDBMObject. |
|
545 | + * This is one of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
546 | + * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
547 | + * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
548 | + * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
549 | + * |
|
550 | + * @return string |
|
551 | + */ |
|
552 | + public function _getStatus() |
|
553 | + { |
|
554 | + return $this->status; |
|
555 | + } |
|
556 | + |
|
557 | + /** |
|
558 | + * Override the native php clone function for TDBMObjects. |
|
559 | + */ |
|
560 | + public function __clone() |
|
561 | + { |
|
562 | + // Let's clone the many to many relationships |
|
563 | + if ($this->status === TDBMObjectStateEnum::STATE_DETACHED) { |
|
564 | + $pivotTableList = array_keys($this->relationships); |
|
565 | + } else { |
|
566 | + $pivotTableList = $this->tdbmService->_getPivotTablesLinkedToBean($this); |
|
567 | + } |
|
568 | + |
|
569 | + foreach ($pivotTableList as $pivotTable) { |
|
570 | + $storage = $this->retrieveRelationshipsStorage($pivotTable); |
|
571 | + |
|
572 | + // Let's duplicate the reverse side of the relationship // This is useless: already done by "retrieveRelationshipsStorage"!!! |
|
573 | + /*foreach ($storage as $remoteBean) { |
|
574 | 574 | $metadata = $storage[$remoteBean]; |
575 | 575 | |
576 | 576 | $remoteStorage = $remoteBean->getRelationshipStorage($pivotTable); |
577 | 577 | $remoteStorage->attach($this, ['status' => $metadata['status'], 'reverse' => !$metadata['reverse']]); |
578 | 578 | }*/ |
579 | - } |
|
580 | - |
|
581 | - // Let's clone each row |
|
582 | - foreach ($this->dbRows as $key => &$dbRow) { |
|
583 | - $dbRow = clone $dbRow; |
|
584 | - $dbRow->setTDBMObject($this); |
|
585 | - } |
|
586 | - |
|
587 | - $this->manyToOneRelationships = []; |
|
588 | - |
|
589 | - // Let's set the status to new (to enter the save function) |
|
590 | - $this->status = TDBMObjectStateEnum::STATE_DETACHED; |
|
591 | - } |
|
592 | - |
|
593 | - /** |
|
594 | - * Returns raw database rows. |
|
595 | - * |
|
596 | - * @return DbRow[] Key: table name, Value: DbRow object |
|
597 | - */ |
|
598 | - public function _getDbRows() |
|
599 | - { |
|
600 | - return $this->dbRows; |
|
601 | - } |
|
602 | - |
|
603 | - private function registerTable($tableName) |
|
604 | - { |
|
605 | - $dbRow = new DbRow($this, $tableName); |
|
606 | - |
|
607 | - if (in_array($this->status, [TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DIRTY])) { |
|
608 | - // Let's get the primary key for the new table |
|
609 | - $anotherDbRow = array_values($this->dbRows)[0]; |
|
610 | - /* @var $anotherDbRow DbRow */ |
|
611 | - $indexedPrimaryKeys = array_values($anotherDbRow->_getPrimaryKeys()); |
|
612 | - $primaryKeys = $this->tdbmService->_getPrimaryKeysFromIndexedPrimaryKeys($tableName, $indexedPrimaryKeys); |
|
613 | - $dbRow->_setPrimaryKeys($primaryKeys); |
|
614 | - } |
|
615 | - |
|
616 | - $dbRow->_setStatus($this->status); |
|
617 | - |
|
618 | - $this->dbRows[$tableName] = $dbRow; |
|
619 | - // TODO: look at status (if not new)=> get primary key from tdbmservice |
|
620 | - } |
|
621 | - |
|
622 | - /** |
|
623 | - * Internal function: return the list of relationships. |
|
624 | - * |
|
625 | - * @return \SplObjectStorage[] |
|
626 | - */ |
|
627 | - public function _getCachedRelationships() |
|
628 | - { |
|
629 | - return $this->relationships; |
|
630 | - } |
|
631 | - |
|
632 | - /** |
|
633 | - * Returns an array of used tables by this bean (from parent to child relationship). |
|
634 | - * |
|
635 | - * @return string[] |
|
636 | - */ |
|
637 | - abstract protected function getUsedTables() : array; |
|
638 | - |
|
639 | - /** |
|
640 | - * Method called when the bean is removed from database. |
|
641 | - */ |
|
642 | - protected function onDelete() : void |
|
643 | - { |
|
644 | - } |
|
579 | + } |
|
580 | + |
|
581 | + // Let's clone each row |
|
582 | + foreach ($this->dbRows as $key => &$dbRow) { |
|
583 | + $dbRow = clone $dbRow; |
|
584 | + $dbRow->setTDBMObject($this); |
|
585 | + } |
|
586 | + |
|
587 | + $this->manyToOneRelationships = []; |
|
588 | + |
|
589 | + // Let's set the status to new (to enter the save function) |
|
590 | + $this->status = TDBMObjectStateEnum::STATE_DETACHED; |
|
591 | + } |
|
592 | + |
|
593 | + /** |
|
594 | + * Returns raw database rows. |
|
595 | + * |
|
596 | + * @return DbRow[] Key: table name, Value: DbRow object |
|
597 | + */ |
|
598 | + public function _getDbRows() |
|
599 | + { |
|
600 | + return $this->dbRows; |
|
601 | + } |
|
602 | + |
|
603 | + private function registerTable($tableName) |
|
604 | + { |
|
605 | + $dbRow = new DbRow($this, $tableName); |
|
606 | + |
|
607 | + if (in_array($this->status, [TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DIRTY])) { |
|
608 | + // Let's get the primary key for the new table |
|
609 | + $anotherDbRow = array_values($this->dbRows)[0]; |
|
610 | + /* @var $anotherDbRow DbRow */ |
|
611 | + $indexedPrimaryKeys = array_values($anotherDbRow->_getPrimaryKeys()); |
|
612 | + $primaryKeys = $this->tdbmService->_getPrimaryKeysFromIndexedPrimaryKeys($tableName, $indexedPrimaryKeys); |
|
613 | + $dbRow->_setPrimaryKeys($primaryKeys); |
|
614 | + } |
|
615 | + |
|
616 | + $dbRow->_setStatus($this->status); |
|
617 | + |
|
618 | + $this->dbRows[$tableName] = $dbRow; |
|
619 | + // TODO: look at status (if not new)=> get primary key from tdbmservice |
|
620 | + } |
|
621 | + |
|
622 | + /** |
|
623 | + * Internal function: return the list of relationships. |
|
624 | + * |
|
625 | + * @return \SplObjectStorage[] |
|
626 | + */ |
|
627 | + public function _getCachedRelationships() |
|
628 | + { |
|
629 | + return $this->relationships; |
|
630 | + } |
|
631 | + |
|
632 | + /** |
|
633 | + * Returns an array of used tables by this bean (from parent to child relationship). |
|
634 | + * |
|
635 | + * @return string[] |
|
636 | + */ |
|
637 | + abstract protected function getUsedTables() : array; |
|
638 | + |
|
639 | + /** |
|
640 | + * Method called when the bean is removed from database. |
|
641 | + */ |
|
642 | + protected function onDelete() : void |
|
643 | + { |
|
644 | + } |
|
645 | 645 | } |