@@ -11,136 +11,136 @@ discard block |
||
| 11 | 11 | */ |
| 12 | 12 | class ScalarBeanPropertyDescriptor extends AbstractBeanPropertyDescriptor |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * @var Column |
|
| 16 | - */ |
|
| 17 | - private $column; |
|
| 18 | - /** |
|
| 19 | - * @var NamingStrategyInterface |
|
| 20 | - */ |
|
| 21 | - private $namingStrategy; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * ScalarBeanPropertyDescriptor constructor. |
|
| 25 | - * @param Table $table |
|
| 26 | - * @param Column $column |
|
| 27 | - * @param NamingStrategyInterface $namingStrategy |
|
| 28 | - */ |
|
| 29 | - public function __construct(Table $table, Column $column, NamingStrategyInterface $namingStrategy) |
|
| 30 | - { |
|
| 31 | - parent::__construct($table); |
|
| 32 | - $this->table = $table; |
|
| 33 | - $this->column = $column; |
|
| 34 | - $this->namingStrategy = $namingStrategy; |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Returns the foreign-key the column is part of, if any. null otherwise. |
|
| 39 | - * |
|
| 40 | - * @return ForeignKeyConstraint|null |
|
| 41 | - */ |
|
| 42 | - public function getForeignKey() |
|
| 43 | - { |
|
| 44 | - return false; |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Returns the param annotation for this property (useful for constructor). |
|
| 49 | - * |
|
| 50 | - * @return string |
|
| 51 | - */ |
|
| 52 | - public function getParamAnnotation() |
|
| 53 | - { |
|
| 54 | - $className = $this->getClassName(); |
|
| 55 | - $paramType = $className ?: TDBMDaoGenerator::dbalTypeToPhpType($this->column->getType()); |
|
| 56 | - |
|
| 57 | - $str = ' * @param %s %s'; |
|
| 58 | - |
|
| 59 | - return sprintf($str, $paramType, $this->getVariableName()); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - public function getUpperCamelCaseName() |
|
| 63 | - { |
|
| 64 | - return TDBMDaoGenerator::toCamelCase($this->column->getName()); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Returns the name of the class linked to this property or null if this is not a foreign key. |
|
| 69 | - * |
|
| 70 | - * @return null|string |
|
| 71 | - */ |
|
| 72 | - public function getClassName() |
|
| 73 | - { |
|
| 74 | - return; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Returns true if the property is compulsory (and therefore should be fetched in the constructor). |
|
| 79 | - * |
|
| 80 | - * @return bool |
|
| 81 | - */ |
|
| 82 | - public function isCompulsory() |
|
| 83 | - { |
|
| 84 | - return $this->column->getNotnull() && !$this->column->getAutoincrement() && $this->column->getDefault() === null; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Returns true if the property has a default value. |
|
| 89 | - * |
|
| 90 | - * @return bool |
|
| 91 | - */ |
|
| 92 | - public function hasDefault() |
|
| 93 | - { |
|
| 94 | - return $this->column->getDefault() !== null; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Returns the code that assigns a value to its default value. |
|
| 99 | - * |
|
| 100 | - * @return string |
|
| 101 | - */ |
|
| 102 | - public function assignToDefaultCode() |
|
| 103 | - { |
|
| 104 | - $str = ' $this->%s(%s);'; |
|
| 105 | - |
|
| 106 | - $default = $this->column->getDefault(); |
|
| 107 | - |
|
| 108 | - if (strtoupper($default) === 'CURRENT_TIMESTAMP') { |
|
| 109 | - $defaultCode = 'new \DateTimeImmutable()'; |
|
| 110 | - } else { |
|
| 111 | - $defaultCode = var_export($this->column->getDefault(), true); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - return sprintf($str, $this->getSetterName(), $defaultCode); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Returns true if the property is the primary key. |
|
| 119 | - * |
|
| 120 | - * @return bool |
|
| 121 | - */ |
|
| 122 | - public function isPrimaryKey() |
|
| 123 | - { |
|
| 124 | - return in_array($this->column->getName(), $this->table->getPrimaryKeyColumns()); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * Returns the PHP code for getters and setters. |
|
| 129 | - * |
|
| 130 | - * @return string |
|
| 131 | - */ |
|
| 132 | - public function getGetterSetterCode() |
|
| 133 | - { |
|
| 134 | - $type = $this->column->getType(); |
|
| 135 | - $normalizedType = TDBMDaoGenerator::dbalTypeToPhpType($type); |
|
| 136 | - |
|
| 137 | - $columnGetterName = $this->getGetterName(); |
|
| 138 | - $columnSetterName = $this->getSetterName(); |
|
| 139 | - |
|
| 140 | - // A column type can be forced if it is not nullable and not auto-incrementable (for auto-increment columns, we can get "null" as long as the bean is not saved). |
|
| 141 | - $isNullable = !$this->column->getNotnull() || $this->column->getAutoincrement(); |
|
| 142 | - |
|
| 143 | - $getterAndSetterCode = ' /** |
|
| 14 | + /** |
|
| 15 | + * @var Column |
|
| 16 | + */ |
|
| 17 | + private $column; |
|
| 18 | + /** |
|
| 19 | + * @var NamingStrategyInterface |
|
| 20 | + */ |
|
| 21 | + private $namingStrategy; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * ScalarBeanPropertyDescriptor constructor. |
|
| 25 | + * @param Table $table |
|
| 26 | + * @param Column $column |
|
| 27 | + * @param NamingStrategyInterface $namingStrategy |
|
| 28 | + */ |
|
| 29 | + public function __construct(Table $table, Column $column, NamingStrategyInterface $namingStrategy) |
|
| 30 | + { |
|
| 31 | + parent::__construct($table); |
|
| 32 | + $this->table = $table; |
|
| 33 | + $this->column = $column; |
|
| 34 | + $this->namingStrategy = $namingStrategy; |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Returns the foreign-key the column is part of, if any. null otherwise. |
|
| 39 | + * |
|
| 40 | + * @return ForeignKeyConstraint|null |
|
| 41 | + */ |
|
| 42 | + public function getForeignKey() |
|
| 43 | + { |
|
| 44 | + return false; |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Returns the param annotation for this property (useful for constructor). |
|
| 49 | + * |
|
| 50 | + * @return string |
|
| 51 | + */ |
|
| 52 | + public function getParamAnnotation() |
|
| 53 | + { |
|
| 54 | + $className = $this->getClassName(); |
|
| 55 | + $paramType = $className ?: TDBMDaoGenerator::dbalTypeToPhpType($this->column->getType()); |
|
| 56 | + |
|
| 57 | + $str = ' * @param %s %s'; |
|
| 58 | + |
|
| 59 | + return sprintf($str, $paramType, $this->getVariableName()); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + public function getUpperCamelCaseName() |
|
| 63 | + { |
|
| 64 | + return TDBMDaoGenerator::toCamelCase($this->column->getName()); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Returns the name of the class linked to this property or null if this is not a foreign key. |
|
| 69 | + * |
|
| 70 | + * @return null|string |
|
| 71 | + */ |
|
| 72 | + public function getClassName() |
|
| 73 | + { |
|
| 74 | + return; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Returns true if the property is compulsory (and therefore should be fetched in the constructor). |
|
| 79 | + * |
|
| 80 | + * @return bool |
|
| 81 | + */ |
|
| 82 | + public function isCompulsory() |
|
| 83 | + { |
|
| 84 | + return $this->column->getNotnull() && !$this->column->getAutoincrement() && $this->column->getDefault() === null; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Returns true if the property has a default value. |
|
| 89 | + * |
|
| 90 | + * @return bool |
|
| 91 | + */ |
|
| 92 | + public function hasDefault() |
|
| 93 | + { |
|
| 94 | + return $this->column->getDefault() !== null; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Returns the code that assigns a value to its default value. |
|
| 99 | + * |
|
| 100 | + * @return string |
|
| 101 | + */ |
|
| 102 | + public function assignToDefaultCode() |
|
| 103 | + { |
|
| 104 | + $str = ' $this->%s(%s);'; |
|
| 105 | + |
|
| 106 | + $default = $this->column->getDefault(); |
|
| 107 | + |
|
| 108 | + if (strtoupper($default) === 'CURRENT_TIMESTAMP') { |
|
| 109 | + $defaultCode = 'new \DateTimeImmutable()'; |
|
| 110 | + } else { |
|
| 111 | + $defaultCode = var_export($this->column->getDefault(), true); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + return sprintf($str, $this->getSetterName(), $defaultCode); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Returns true if the property is the primary key. |
|
| 119 | + * |
|
| 120 | + * @return bool |
|
| 121 | + */ |
|
| 122 | + public function isPrimaryKey() |
|
| 123 | + { |
|
| 124 | + return in_array($this->column->getName(), $this->table->getPrimaryKeyColumns()); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * Returns the PHP code for getters and setters. |
|
| 129 | + * |
|
| 130 | + * @return string |
|
| 131 | + */ |
|
| 132 | + public function getGetterSetterCode() |
|
| 133 | + { |
|
| 134 | + $type = $this->column->getType(); |
|
| 135 | + $normalizedType = TDBMDaoGenerator::dbalTypeToPhpType($type); |
|
| 136 | + |
|
| 137 | + $columnGetterName = $this->getGetterName(); |
|
| 138 | + $columnSetterName = $this->getSetterName(); |
|
| 139 | + |
|
| 140 | + // A column type can be forced if it is not nullable and not auto-incrementable (for auto-increment columns, we can get "null" as long as the bean is not saved). |
|
| 141 | + $isNullable = !$this->column->getNotnull() || $this->column->getAutoincrement(); |
|
| 142 | + |
|
| 143 | + $getterAndSetterCode = ' /** |
|
| 144 | 144 | * The getter for the "%s" column. |
| 145 | 145 | * |
| 146 | 146 | * @return %s |
@@ -162,54 +162,54 @@ discard block |
||
| 162 | 162 | |
| 163 | 163 | '; |
| 164 | 164 | |
| 165 | - return sprintf($getterAndSetterCode, |
|
| 166 | - // Getter |
|
| 167 | - $this->column->getName(), |
|
| 168 | - $normalizedType.($isNullable ? '|null' : ''), |
|
| 169 | - $columnGetterName, |
|
| 170 | - ($isNullable ? '?' : ''), |
|
| 171 | - $normalizedType, |
|
| 172 | - var_export($this->column->getName(), true), |
|
| 173 | - var_export($this->table->getName(), true), |
|
| 174 | - // Setter |
|
| 175 | - $this->column->getName(), |
|
| 176 | - $normalizedType, |
|
| 177 | - $this->column->getName(), |
|
| 178 | - $columnSetterName, |
|
| 179 | - $this->column->getNotnull() ? '' : '?', |
|
| 180 | - $normalizedType, |
|
| 181 | - //$castTo, |
|
| 182 | - $this->column->getName(), |
|
| 183 | - var_export($this->column->getName(), true), |
|
| 184 | - $this->column->getName(), |
|
| 185 | - var_export($this->table->getName(), true) |
|
| 186 | - ); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * Returns the part of code useful when doing json serialization. |
|
| 191 | - * |
|
| 192 | - * @return string |
|
| 193 | - */ |
|
| 194 | - public function getJsonSerializeCode() |
|
| 195 | - { |
|
| 196 | - $type = $this->column->getType(); |
|
| 197 | - $normalizedType = TDBMDaoGenerator::dbalTypeToPhpType($type); |
|
| 198 | - |
|
| 199 | - if ($normalizedType == '\\DateTimeInterface') { |
|
| 200 | - return ' $array['.var_export($this->getLowerCamelCaseName(), true).'] = ($this->'.$this->getGetterName().'() === null) ? null : $this->'.$this->getGetterName()."()->format('c');\n"; |
|
| 201 | - } else { |
|
| 202 | - return ' $array['.var_export($this->getLowerCamelCaseName(), true).'] = $this->'.$this->getGetterName()."();\n"; |
|
| 203 | - } |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * Returns the column name. |
|
| 208 | - * |
|
| 209 | - * @return string |
|
| 210 | - */ |
|
| 211 | - public function getColumnName() |
|
| 212 | - { |
|
| 213 | - return $this->column->getName(); |
|
| 214 | - } |
|
| 165 | + return sprintf($getterAndSetterCode, |
|
| 166 | + // Getter |
|
| 167 | + $this->column->getName(), |
|
| 168 | + $normalizedType.($isNullable ? '|null' : ''), |
|
| 169 | + $columnGetterName, |
|
| 170 | + ($isNullable ? '?' : ''), |
|
| 171 | + $normalizedType, |
|
| 172 | + var_export($this->column->getName(), true), |
|
| 173 | + var_export($this->table->getName(), true), |
|
| 174 | + // Setter |
|
| 175 | + $this->column->getName(), |
|
| 176 | + $normalizedType, |
|
| 177 | + $this->column->getName(), |
|
| 178 | + $columnSetterName, |
|
| 179 | + $this->column->getNotnull() ? '' : '?', |
|
| 180 | + $normalizedType, |
|
| 181 | + //$castTo, |
|
| 182 | + $this->column->getName(), |
|
| 183 | + var_export($this->column->getName(), true), |
|
| 184 | + $this->column->getName(), |
|
| 185 | + var_export($this->table->getName(), true) |
|
| 186 | + ); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * Returns the part of code useful when doing json serialization. |
|
| 191 | + * |
|
| 192 | + * @return string |
|
| 193 | + */ |
|
| 194 | + public function getJsonSerializeCode() |
|
| 195 | + { |
|
| 196 | + $type = $this->column->getType(); |
|
| 197 | + $normalizedType = TDBMDaoGenerator::dbalTypeToPhpType($type); |
|
| 198 | + |
|
| 199 | + if ($normalizedType == '\\DateTimeInterface') { |
|
| 200 | + return ' $array['.var_export($this->getLowerCamelCaseName(), true).'] = ($this->'.$this->getGetterName().'() === null) ? null : $this->'.$this->getGetterName()."()->format('c');\n"; |
|
| 201 | + } else { |
|
| 202 | + return ' $array['.var_export($this->getLowerCamelCaseName(), true).'] = $this->'.$this->getGetterName()."();\n"; |
|
| 203 | + } |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * Returns the column name. |
|
| 208 | + * |
|
| 209 | + * @return string |
|
| 210 | + */ |
|
| 211 | + public function getColumnName() |
|
| 212 | + { |
|
| 213 | + return $this->column->getName(); |
|
| 214 | + } |
|
| 215 | 215 | } |
@@ -7,105 +7,105 @@ discard block |
||
| 7 | 7 | |
| 8 | 8 | class PivotTableMethodsDescriptor implements MethodDescriptorInterface |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * @var Table |
|
| 12 | - */ |
|
| 13 | - private $pivotTable; |
|
| 14 | - |
|
| 15 | - private $useAlternateName = false; |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * @var ForeignKeyConstraint |
|
| 19 | - */ |
|
| 20 | - private $localFk; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * @var ForeignKeyConstraint |
|
| 24 | - */ |
|
| 25 | - private $remoteFk; |
|
| 26 | - /** |
|
| 27 | - * @var NamingStrategyInterface |
|
| 28 | - */ |
|
| 29 | - private $namingStrategy; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @param Table $pivotTable The pivot table |
|
| 33 | - * @param ForeignKeyConstraint $localFk |
|
| 34 | - * @param ForeignKeyConstraint $remoteFk |
|
| 35 | - * @param NamingStrategyInterface $namingStrategy |
|
| 36 | - */ |
|
| 37 | - public function __construct(Table $pivotTable, ForeignKeyConstraint $localFk, ForeignKeyConstraint $remoteFk, NamingStrategyInterface $namingStrategy) |
|
| 38 | - { |
|
| 39 | - $this->pivotTable = $pivotTable; |
|
| 40 | - $this->localFk = $localFk; |
|
| 41 | - $this->remoteFk = $remoteFk; |
|
| 42 | - $this->namingStrategy = $namingStrategy; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Requests the use of an alternative name for this method. |
|
| 47 | - */ |
|
| 48 | - public function useAlternativeName() |
|
| 49 | - { |
|
| 50 | - $this->useAlternateName = true; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Returns the name of the method to be generated. |
|
| 55 | - * |
|
| 56 | - * @return string |
|
| 57 | - */ |
|
| 58 | - public function getName() : string |
|
| 59 | - { |
|
| 60 | - if (!$this->useAlternateName) { |
|
| 61 | - return 'get'.TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()); |
|
| 62 | - } else { |
|
| 63 | - return 'get'.TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName()); |
|
| 64 | - } |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Returns the plural name. |
|
| 69 | - * |
|
| 70 | - * @return string |
|
| 71 | - */ |
|
| 72 | - private function getPluralName() : string |
|
| 73 | - { |
|
| 74 | - if (!$this->useAlternateName) { |
|
| 75 | - return TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()); |
|
| 76 | - } else { |
|
| 77 | - return TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName()); |
|
| 78 | - } |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Returns the singular name. |
|
| 83 | - * |
|
| 84 | - * @return string |
|
| 85 | - */ |
|
| 86 | - private function getSingularName() : string |
|
| 87 | - { |
|
| 88 | - if (!$this->useAlternateName) { |
|
| 89 | - return TDBMDaoGenerator::toCamelCase(TDBMDaoGenerator::toSingular($this->remoteFk->getForeignTableName())); |
|
| 90 | - } else { |
|
| 91 | - return TDBMDaoGenerator::toCamelCase(TDBMDaoGenerator::toSingular($this->remoteFk->getForeignTableName())).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName()); |
|
| 92 | - } |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Returns the code of the method. |
|
| 97 | - * |
|
| 98 | - * @return string |
|
| 99 | - */ |
|
| 100 | - public function getCode() : string |
|
| 101 | - { |
|
| 102 | - $singularName = $this->getSingularName(); |
|
| 103 | - $pluralName = $this->getPluralName(); |
|
| 104 | - $remoteBeanName = $this->namingStrategy->getBeanClassName($this->remoteFk->getForeignTableName()); |
|
| 105 | - $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName); |
|
| 106 | - $pluralVariableName = $variableName.'s'; |
|
| 107 | - |
|
| 108 | - $str = ' /** |
|
| 10 | + /** |
|
| 11 | + * @var Table |
|
| 12 | + */ |
|
| 13 | + private $pivotTable; |
|
| 14 | + |
|
| 15 | + private $useAlternateName = false; |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * @var ForeignKeyConstraint |
|
| 19 | + */ |
|
| 20 | + private $localFk; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * @var ForeignKeyConstraint |
|
| 24 | + */ |
|
| 25 | + private $remoteFk; |
|
| 26 | + /** |
|
| 27 | + * @var NamingStrategyInterface |
|
| 28 | + */ |
|
| 29 | + private $namingStrategy; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @param Table $pivotTable The pivot table |
|
| 33 | + * @param ForeignKeyConstraint $localFk |
|
| 34 | + * @param ForeignKeyConstraint $remoteFk |
|
| 35 | + * @param NamingStrategyInterface $namingStrategy |
|
| 36 | + */ |
|
| 37 | + public function __construct(Table $pivotTable, ForeignKeyConstraint $localFk, ForeignKeyConstraint $remoteFk, NamingStrategyInterface $namingStrategy) |
|
| 38 | + { |
|
| 39 | + $this->pivotTable = $pivotTable; |
|
| 40 | + $this->localFk = $localFk; |
|
| 41 | + $this->remoteFk = $remoteFk; |
|
| 42 | + $this->namingStrategy = $namingStrategy; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Requests the use of an alternative name for this method. |
|
| 47 | + */ |
|
| 48 | + public function useAlternativeName() |
|
| 49 | + { |
|
| 50 | + $this->useAlternateName = true; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Returns the name of the method to be generated. |
|
| 55 | + * |
|
| 56 | + * @return string |
|
| 57 | + */ |
|
| 58 | + public function getName() : string |
|
| 59 | + { |
|
| 60 | + if (!$this->useAlternateName) { |
|
| 61 | + return 'get'.TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()); |
|
| 62 | + } else { |
|
| 63 | + return 'get'.TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName()); |
|
| 64 | + } |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Returns the plural name. |
|
| 69 | + * |
|
| 70 | + * @return string |
|
| 71 | + */ |
|
| 72 | + private function getPluralName() : string |
|
| 73 | + { |
|
| 74 | + if (!$this->useAlternateName) { |
|
| 75 | + return TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()); |
|
| 76 | + } else { |
|
| 77 | + return TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName()); |
|
| 78 | + } |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Returns the singular name. |
|
| 83 | + * |
|
| 84 | + * @return string |
|
| 85 | + */ |
|
| 86 | + private function getSingularName() : string |
|
| 87 | + { |
|
| 88 | + if (!$this->useAlternateName) { |
|
| 89 | + return TDBMDaoGenerator::toCamelCase(TDBMDaoGenerator::toSingular($this->remoteFk->getForeignTableName())); |
|
| 90 | + } else { |
|
| 91 | + return TDBMDaoGenerator::toCamelCase(TDBMDaoGenerator::toSingular($this->remoteFk->getForeignTableName())).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName()); |
|
| 92 | + } |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Returns the code of the method. |
|
| 97 | + * |
|
| 98 | + * @return string |
|
| 99 | + */ |
|
| 100 | + public function getCode() : string |
|
| 101 | + { |
|
| 102 | + $singularName = $this->getSingularName(); |
|
| 103 | + $pluralName = $this->getPluralName(); |
|
| 104 | + $remoteBeanName = $this->namingStrategy->getBeanClassName($this->remoteFk->getForeignTableName()); |
|
| 105 | + $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName); |
|
| 106 | + $pluralVariableName = $variableName.'s'; |
|
| 107 | + |
|
| 108 | + $str = ' /** |
|
| 109 | 109 | * Returns the list of %s associated to this bean via the %s pivot table. |
| 110 | 110 | * |
| 111 | 111 | * @return %s[] |
@@ -116,9 +116,9 @@ discard block |
||
| 116 | 116 | } |
| 117 | 117 | '; |
| 118 | 118 | |
| 119 | - $getterCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $pluralName, var_export($this->remoteFk->getLocalTableName(), true)); |
|
| 119 | + $getterCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $pluralName, var_export($this->remoteFk->getLocalTableName(), true)); |
|
| 120 | 120 | |
| 121 | - $str = ' /** |
|
| 121 | + $str = ' /** |
|
| 122 | 122 | * Adds a relationship with %s associated to this bean via the %s pivot table. |
| 123 | 123 | * |
| 124 | 124 | * @param %s %s |
@@ -129,9 +129,9 @@ discard block |
||
| 129 | 129 | } |
| 130 | 130 | '; |
| 131 | 131 | |
| 132 | - $adderCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName); |
|
| 132 | + $adderCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName); |
|
| 133 | 133 | |
| 134 | - $str = ' /** |
|
| 134 | + $str = ' /** |
|
| 135 | 135 | * Deletes the relationship with %s associated to this bean via the %s pivot table. |
| 136 | 136 | * |
| 137 | 137 | * @param %s %s |
@@ -142,9 +142,9 @@ discard block |
||
| 142 | 142 | } |
| 143 | 143 | '; |
| 144 | 144 | |
| 145 | - $removerCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName); |
|
| 145 | + $removerCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName); |
|
| 146 | 146 | |
| 147 | - $str = ' /** |
|
| 147 | + $str = ' /** |
|
| 148 | 148 | * Returns whether this bean is associated with %s via the %s pivot table. |
| 149 | 149 | * |
| 150 | 150 | * @param %s %s |
@@ -156,9 +156,9 @@ discard block |
||
| 156 | 156 | } |
| 157 | 157 | '; |
| 158 | 158 | |
| 159 | - $hasCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName); |
|
| 159 | + $hasCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName); |
|
| 160 | 160 | |
| 161 | - $str = ' /** |
|
| 161 | + $str = ' /** |
|
| 162 | 162 | * Sets all relationships with %s associated to this bean via the %s pivot table. |
| 163 | 163 | * Exiting relationships will be removed and replaced by the provided relationships. |
| 164 | 164 | * |
@@ -170,38 +170,38 @@ discard block |
||
| 170 | 170 | } |
| 171 | 171 | '; |
| 172 | 172 | |
| 173 | - $setterCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $pluralVariableName, $pluralName, $pluralVariableName, var_export($this->remoteFk->getLocalTableName(), true), $pluralVariableName); |
|
| 174 | - |
|
| 175 | - $code = $getterCode.$adderCode.$removerCode.$hasCode.$setterCode; |
|
| 176 | - |
|
| 177 | - return $code; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Returns an array of classes that needs a "use" for this method. |
|
| 182 | - * |
|
| 183 | - * @return string[] |
|
| 184 | - */ |
|
| 185 | - public function getUsedClasses() : array |
|
| 186 | - { |
|
| 187 | - return [$this->namingStrategy->getBeanClassName($this->remoteFk->getForeignTableName())]; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * Returns the code to past in jsonSerialize. |
|
| 192 | - * |
|
| 193 | - * @return string |
|
| 194 | - */ |
|
| 195 | - public function getJsonSerializeCode() : string |
|
| 196 | - { |
|
| 197 | - $remoteBeanName = $this->namingStrategy->getBeanClassName($this->remoteFk->getForeignTableName()); |
|
| 198 | - $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName); |
|
| 199 | - |
|
| 200 | - return ' if (!$stopRecursion) { |
|
| 173 | + $setterCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $pluralVariableName, $pluralName, $pluralVariableName, var_export($this->remoteFk->getLocalTableName(), true), $pluralVariableName); |
|
| 174 | + |
|
| 175 | + $code = $getterCode.$adderCode.$removerCode.$hasCode.$setterCode; |
|
| 176 | + |
|
| 177 | + return $code; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Returns an array of classes that needs a "use" for this method. |
|
| 182 | + * |
|
| 183 | + * @return string[] |
|
| 184 | + */ |
|
| 185 | + public function getUsedClasses() : array |
|
| 186 | + { |
|
| 187 | + return [$this->namingStrategy->getBeanClassName($this->remoteFk->getForeignTableName())]; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * Returns the code to past in jsonSerialize. |
|
| 192 | + * |
|
| 193 | + * @return string |
|
| 194 | + */ |
|
| 195 | + public function getJsonSerializeCode() : string |
|
| 196 | + { |
|
| 197 | + $remoteBeanName = $this->namingStrategy->getBeanClassName($this->remoteFk->getForeignTableName()); |
|
| 198 | + $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName); |
|
| 199 | + |
|
| 200 | + return ' if (!$stopRecursion) { |
|
| 201 | 201 | $array[\''.lcfirst($this->getPluralName()).'\'] = array_map(function ('.$remoteBeanName.' '.$variableName.') { |
| 202 | 202 | return '.$variableName.'->jsonSerialize(true); |
| 203 | 203 | }, $this->'.$this->getName().'()); |
| 204 | 204 | } |
| 205 | 205 | '; |
| 206 | - } |
|
| 206 | + } |
|
| 207 | 207 | } |
@@ -7,28 +7,28 @@ |
||
| 7 | 7 | |
| 8 | 8 | class GeneratorEventDispatcher implements GeneratorListenerInterface |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * @var GeneratorListenerInterface[] |
|
| 12 | - */ |
|
| 13 | - private $listeners; |
|
| 10 | + /** |
|
| 11 | + * @var GeneratorListenerInterface[] |
|
| 12 | + */ |
|
| 13 | + private $listeners; |
|
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * GeneratorEventDispatcher constructor. |
|
| 17 | - * @param GeneratorListenerInterface[] $listeners |
|
| 18 | - */ |
|
| 19 | - public function __construct(array $listeners) |
|
| 20 | - { |
|
| 21 | - $this->listeners = $listeners; |
|
| 22 | - } |
|
| 15 | + /** |
|
| 16 | + * GeneratorEventDispatcher constructor. |
|
| 17 | + * @param GeneratorListenerInterface[] $listeners |
|
| 18 | + */ |
|
| 19 | + public function __construct(array $listeners) |
|
| 20 | + { |
|
| 21 | + $this->listeners = $listeners; |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * @param ConfigurationInterface $configuration |
|
| 26 | - * @param BeanDescriptorInterface[] $beanDescriptors |
|
| 27 | - */ |
|
| 28 | - public function onGenerate(ConfigurationInterface $configuration, array $beanDescriptors): void |
|
| 29 | - { |
|
| 30 | - foreach ($this->listeners as $listener) { |
|
| 31 | - $listener->onGenerate($configuration, $beanDescriptors); |
|
| 32 | - } |
|
| 33 | - } |
|
| 24 | + /** |
|
| 25 | + * @param ConfigurationInterface $configuration |
|
| 26 | + * @param BeanDescriptorInterface[] $beanDescriptors |
|
| 27 | + */ |
|
| 28 | + public function onGenerate(ConfigurationInterface $configuration, array $beanDescriptors): void |
|
| 29 | + { |
|
| 30 | + foreach ($this->listeners as $listener) { |
|
| 31 | + $listener->onGenerate($configuration, $beanDescriptors); |
|
| 32 | + } |
|
| 33 | + } |
|
| 34 | 34 | } |
@@ -12,71 +12,71 @@ discard block |
||
| 12 | 12 | */ |
| 13 | 13 | class DirectForeignKeyMethodDescriptor implements MethodDescriptorInterface |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * @var ForeignKeyConstraint |
|
| 17 | - */ |
|
| 18 | - private $fk; |
|
| 19 | - |
|
| 20 | - private $useAlternateName = false; |
|
| 21 | - /** |
|
| 22 | - * @var Table |
|
| 23 | - */ |
|
| 24 | - private $mainTable; |
|
| 25 | - /** |
|
| 26 | - * @var NamingStrategyInterface |
|
| 27 | - */ |
|
| 28 | - private $namingStrategy; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @param ForeignKeyConstraint $fk The foreign key pointing to our bean |
|
| 32 | - * @param Table $mainTable The main table that is pointed to |
|
| 33 | - * @param NamingStrategyInterface $namingStrategy |
|
| 34 | - */ |
|
| 35 | - public function __construct(ForeignKeyConstraint $fk, Table $mainTable, NamingStrategyInterface $namingStrategy) |
|
| 36 | - { |
|
| 37 | - $this->fk = $fk; |
|
| 38 | - $this->mainTable = $mainTable; |
|
| 39 | - $this->namingStrategy = $namingStrategy; |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Returns the name of the method to be generated. |
|
| 44 | - * |
|
| 45 | - * @return string |
|
| 46 | - */ |
|
| 47 | - public function getName() : string |
|
| 48 | - { |
|
| 49 | - if (!$this->useAlternateName) { |
|
| 50 | - return 'get'.TDBMDaoGenerator::toCamelCase($this->fk->getLocalTableName()); |
|
| 51 | - } else { |
|
| 52 | - $methodName = 'get'.TDBMDaoGenerator::toCamelCase($this->fk->getLocalTableName()).'By'; |
|
| 53 | - |
|
| 54 | - $camelizedColumns = array_map([TDBMDaoGenerator::class, 'toCamelCase'], $this->fk->getLocalColumns()); |
|
| 55 | - |
|
| 56 | - $methodName .= implode('And', $camelizedColumns); |
|
| 57 | - |
|
| 58 | - return $methodName; |
|
| 59 | - } |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Requests the use of an alternative name for this method. |
|
| 64 | - */ |
|
| 65 | - public function useAlternativeName() |
|
| 66 | - { |
|
| 67 | - $this->useAlternateName = true; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Returns the code of the method. |
|
| 72 | - * |
|
| 73 | - * @return string |
|
| 74 | - */ |
|
| 75 | - public function getCode() : string |
|
| 76 | - { |
|
| 77 | - $code = ''; |
|
| 78 | - |
|
| 79 | - $getterCode = ' /** |
|
| 15 | + /** |
|
| 16 | + * @var ForeignKeyConstraint |
|
| 17 | + */ |
|
| 18 | + private $fk; |
|
| 19 | + |
|
| 20 | + private $useAlternateName = false; |
|
| 21 | + /** |
|
| 22 | + * @var Table |
|
| 23 | + */ |
|
| 24 | + private $mainTable; |
|
| 25 | + /** |
|
| 26 | + * @var NamingStrategyInterface |
|
| 27 | + */ |
|
| 28 | + private $namingStrategy; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @param ForeignKeyConstraint $fk The foreign key pointing to our bean |
|
| 32 | + * @param Table $mainTable The main table that is pointed to |
|
| 33 | + * @param NamingStrategyInterface $namingStrategy |
|
| 34 | + */ |
|
| 35 | + public function __construct(ForeignKeyConstraint $fk, Table $mainTable, NamingStrategyInterface $namingStrategy) |
|
| 36 | + { |
|
| 37 | + $this->fk = $fk; |
|
| 38 | + $this->mainTable = $mainTable; |
|
| 39 | + $this->namingStrategy = $namingStrategy; |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Returns the name of the method to be generated. |
|
| 44 | + * |
|
| 45 | + * @return string |
|
| 46 | + */ |
|
| 47 | + public function getName() : string |
|
| 48 | + { |
|
| 49 | + if (!$this->useAlternateName) { |
|
| 50 | + return 'get'.TDBMDaoGenerator::toCamelCase($this->fk->getLocalTableName()); |
|
| 51 | + } else { |
|
| 52 | + $methodName = 'get'.TDBMDaoGenerator::toCamelCase($this->fk->getLocalTableName()).'By'; |
|
| 53 | + |
|
| 54 | + $camelizedColumns = array_map([TDBMDaoGenerator::class, 'toCamelCase'], $this->fk->getLocalColumns()); |
|
| 55 | + |
|
| 56 | + $methodName .= implode('And', $camelizedColumns); |
|
| 57 | + |
|
| 58 | + return $methodName; |
|
| 59 | + } |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Requests the use of an alternative name for this method. |
|
| 64 | + */ |
|
| 65 | + public function useAlternativeName() |
|
| 66 | + { |
|
| 67 | + $this->useAlternateName = true; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Returns the code of the method. |
|
| 72 | + * |
|
| 73 | + * @return string |
|
| 74 | + */ |
|
| 75 | + public function getCode() : string |
|
| 76 | + { |
|
| 77 | + $code = ''; |
|
| 78 | + |
|
| 79 | + $getterCode = ' /** |
|
| 80 | 80 | * Returns the list of %s pointing to this bean via the %s column. |
| 81 | 81 | * |
| 82 | 82 | * @return %s[]|AlterableResultIterator |
@@ -88,55 +88,55 @@ discard block |
||
| 88 | 88 | |
| 89 | 89 | '; |
| 90 | 90 | |
| 91 | - $beanClass = $this->namingStrategy->getBeanClassName($this->fk->getLocalTableName()); |
|
| 92 | - $code .= sprintf($getterCode, |
|
| 93 | - $beanClass, |
|
| 94 | - implode(', ', $this->fk->getColumns()), |
|
| 95 | - $beanClass, |
|
| 96 | - $this->getName(), |
|
| 97 | - var_export($this->fk->getLocalTableName(), true), |
|
| 98 | - var_export($this->fk->getName(), true), |
|
| 99 | - var_export($this->fk->getLocalTableName(), true), |
|
| 100 | - $this->getFilters($this->fk) |
|
| 101 | - ); |
|
| 102 | - |
|
| 103 | - return $code; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - private function getFilters(ForeignKeyConstraint $fk) : string |
|
| 107 | - { |
|
| 108 | - $counter = 0; |
|
| 109 | - $parameters = []; |
|
| 110 | - |
|
| 111 | - $pkColumns = $this->mainTable->getPrimaryKeyColumns(); |
|
| 112 | - |
|
| 113 | - foreach ($fk->getLocalColumns() as $columnName) { |
|
| 114 | - $pkColumn = $pkColumns[$counter]; |
|
| 115 | - $parameters[] = sprintf('%s => $this->get(%s, %s)', var_export($fk->getLocalTableName().'.'.$columnName, true), var_export($pkColumn, true), var_export($this->fk->getForeignTableName(), true)); |
|
| 116 | - ++$counter; |
|
| 117 | - } |
|
| 118 | - $parametersCode = '['.implode(', ', $parameters).']'; |
|
| 119 | - |
|
| 120 | - return $parametersCode; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Returns an array of classes that needs a "use" for this method. |
|
| 125 | - * |
|
| 126 | - * @return string[] |
|
| 127 | - */ |
|
| 128 | - public function getUsedClasses() : array |
|
| 129 | - { |
|
| 130 | - return [$this->namingStrategy->getBeanClassName($this->fk->getForeignTableName())]; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Returns the code to past in jsonSerialize. |
|
| 135 | - * |
|
| 136 | - * @return string |
|
| 137 | - */ |
|
| 138 | - public function getJsonSerializeCode() : string |
|
| 139 | - { |
|
| 140 | - return ''; |
|
| 141 | - } |
|
| 91 | + $beanClass = $this->namingStrategy->getBeanClassName($this->fk->getLocalTableName()); |
|
| 92 | + $code .= sprintf($getterCode, |
|
| 93 | + $beanClass, |
|
| 94 | + implode(', ', $this->fk->getColumns()), |
|
| 95 | + $beanClass, |
|
| 96 | + $this->getName(), |
|
| 97 | + var_export($this->fk->getLocalTableName(), true), |
|
| 98 | + var_export($this->fk->getName(), true), |
|
| 99 | + var_export($this->fk->getLocalTableName(), true), |
|
| 100 | + $this->getFilters($this->fk) |
|
| 101 | + ); |
|
| 102 | + |
|
| 103 | + return $code; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + private function getFilters(ForeignKeyConstraint $fk) : string |
|
| 107 | + { |
|
| 108 | + $counter = 0; |
|
| 109 | + $parameters = []; |
|
| 110 | + |
|
| 111 | + $pkColumns = $this->mainTable->getPrimaryKeyColumns(); |
|
| 112 | + |
|
| 113 | + foreach ($fk->getLocalColumns() as $columnName) { |
|
| 114 | + $pkColumn = $pkColumns[$counter]; |
|
| 115 | + $parameters[] = sprintf('%s => $this->get(%s, %s)', var_export($fk->getLocalTableName().'.'.$columnName, true), var_export($pkColumn, true), var_export($this->fk->getForeignTableName(), true)); |
|
| 116 | + ++$counter; |
|
| 117 | + } |
|
| 118 | + $parametersCode = '['.implode(', ', $parameters).']'; |
|
| 119 | + |
|
| 120 | + return $parametersCode; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Returns an array of classes that needs a "use" for this method. |
|
| 125 | + * |
|
| 126 | + * @return string[] |
|
| 127 | + */ |
|
| 128 | + public function getUsedClasses() : array |
|
| 129 | + { |
|
| 130 | + return [$this->namingStrategy->getBeanClassName($this->fk->getForeignTableName())]; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Returns the code to past in jsonSerialize. |
|
| 135 | + * |
|
| 136 | + * @return string |
|
| 137 | + */ |
|
| 138 | + public function getJsonSerializeCode() : string |
|
| 139 | + { |
|
| 140 | + return ''; |
|
| 141 | + } |
|
| 142 | 142 | } |
@@ -11,12 +11,12 @@ |
||
| 11 | 11 | class VoidListener implements GeneratorListenerInterface |
| 12 | 12 | { |
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * @param ConfigurationInterface $configuration |
|
| 16 | - * @param BeanDescriptorInterface[] $beanDescriptors |
|
| 17 | - */ |
|
| 18 | - public function onGenerate(ConfigurationInterface $configuration, array $beanDescriptors): void |
|
| 19 | - { |
|
| 20 | - // Let's do nothing. |
|
| 21 | - } |
|
| 14 | + /** |
|
| 15 | + * @param ConfigurationInterface $configuration |
|
| 16 | + * @param BeanDescriptorInterface[] $beanDescriptors |
|
| 17 | + */ |
|
| 18 | + public function onGenerate(ConfigurationInterface $configuration, array $beanDescriptors): void |
|
| 19 | + { |
|
| 20 | + // Let's do nothing. |
|
| 21 | + } |
|
| 22 | 22 | } |
@@ -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 | } |
@@ -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 | } |
@@ -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 | } |
@@ -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 | } |