@@ -59,7 +59,7 @@ discard block |
||
| 59 | 59 | * |
| 60 | 60 | * @throws TDBMException |
| 61 | 61 | * |
| 62 | - * @return \string[] the list of tables |
|
| 62 | + * @return string[] the list of tables |
|
| 63 | 63 | */ |
| 64 | 64 | public function generateAllDaosAndBeans($daoFactoryClassName, $daonamespace, $beannamespace, $support, $storeInUtc, $castDatesToDateTime) |
| 65 | 65 | { |
@@ -87,7 +87,12 @@ discard block |
||
| 87 | 87 | /** |
| 88 | 88 | * Generates in one method call the daos and the beans for one table. |
| 89 | 89 | * |
| 90 | - * @param $tableName |
|
| 90 | + * @param string $tableName |
|
| 91 | + * @param string $daonamespace |
|
| 92 | + * @param string $beannamespace |
|
| 93 | + * @param boolean $support |
|
| 94 | + * @param boolean $storeInUtc |
|
| 95 | + * @param boolean $castDatesToDateTime |
|
| 91 | 96 | */ |
| 92 | 97 | public function generateDaoAndBean($tableName, $daonamespace, $beannamespace, ClassNameMapper $classNameMapper, $support, $storeInUtc, $castDatesToDateTime) |
| 93 | 98 | { |
@@ -415,9 +420,10 @@ discard block |
||
| 415 | 420 | /** |
| 416 | 421 | * Writes the PHP bean DAO with simple functions to create/get/save objects. |
| 417 | 422 | * |
| 418 | - * @param string $fileName The file that will be written (without the directory) |
|
| 419 | 423 | * @param string $className The name of the class |
| 420 | 424 | * @param string $tableName The name of the table |
| 425 | + * @param string $baseClassName |
|
| 426 | + * @param string $beanClassName |
|
| 421 | 427 | */ |
| 422 | 428 | public function generateDao($className, $baseClassName, $beanClassName, $tableName, ClassNameMapper $classNameMapper, $support) |
| 423 | 429 | { |
@@ -706,7 +712,9 @@ discard block |
||
| 706 | 712 | /** |
| 707 | 713 | * Generates the factory bean. |
| 708 | 714 | * |
| 709 | - * @param $tableList |
|
| 715 | + * @param string[] $tableList |
|
| 716 | + * @param string $daoFactoryClassName |
|
| 717 | + * @param string $daoNamespace |
|
| 710 | 718 | */ |
| 711 | 719 | private function generateFactory($tableList, $daoFactoryClassName, $daoNamespace, ClassNameMapper $classNameMapper) |
| 712 | 720 | { |
@@ -778,7 +786,6 @@ discard block |
||
| 778 | 786 | * Transforms the property name in a setter name. |
| 779 | 787 | * For instance, phone => getPhone or name => getName. |
| 780 | 788 | * |
| 781 | - * @param string $methodName |
|
| 782 | 789 | * |
| 783 | 790 | * @return string |
| 784 | 791 | */ |
@@ -874,7 +881,7 @@ discard block |
||
| 874 | 881 | * Tries to put string to the singular form (if it is plural). |
| 875 | 882 | * Obviously, this can't be perfect, be we do the best we can. |
| 876 | 883 | * |
| 877 | - * @param $str string |
|
| 884 | + * @param string $str string |
|
| 878 | 885 | * |
| 879 | 886 | * @return string |
| 880 | 887 | */ |
@@ -898,7 +905,7 @@ discard block |
||
| 898 | 905 | * Put the first letter of the string in lower case. |
| 899 | 906 | * Very useful to transform a class name into a variable name. |
| 900 | 907 | * |
| 901 | - * @param $str string |
|
| 908 | + * @param string $str string |
|
| 902 | 909 | * |
| 903 | 910 | * @return string |
| 904 | 911 | */ |
@@ -12,153 +12,153 @@ discard block |
||
| 12 | 12 | */ |
| 13 | 13 | class TDBMDaoGenerator |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * @var ConnectionInterface |
|
| 17 | - */ |
|
| 18 | - private $dbConnection; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * The namespace for the DAOs, without trailing \. |
|
| 22 | - * |
|
| 23 | - * @var string |
|
| 24 | - */ |
|
| 25 | - private $daoNamespace; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * The Namespace for the beans, without trailing \. |
|
| 29 | - * |
|
| 30 | - * @var string |
|
| 31 | - */ |
|
| 32 | - private $beanNamespace; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @var string |
|
| 36 | - */ |
|
| 37 | - private $rootPath; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Constructor. |
|
| 41 | - * |
|
| 42 | - * @param ConnectionInterface $dbConnection The connection to the database. |
|
| 43 | - */ |
|
| 44 | - public function __construct(ConnectionInterface $dbConnection) |
|
| 45 | - { |
|
| 46 | - $this->dbConnection = $dbConnection; |
|
| 47 | - $this->rootPath = __DIR__.'/../../../../../../../../'; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Generates all the daos and beans. |
|
| 52 | - * |
|
| 53 | - * @param string $daoFactoryClassName The classe name of the DAO factory |
|
| 54 | - * @param string $daonamespace The namespace for the DAOs, without trailing \ |
|
| 55 | - * @param string $beannamespace The Namespace for the beans, without trailing \ |
|
| 56 | - * @param bool $support If the generated daos should keep support for old functions (eg : getUserList and getList) |
|
| 57 | - * @param bool $storeInUtc If the generated daos should store the date in UTC timezone instead of user's timezone. |
|
| 58 | - * @param bool $castDatesToDateTime Whether dates are converted to DateTimeImmutable or to timestamp |
|
| 59 | - * |
|
| 60 | - * @throws TDBMException |
|
| 61 | - * |
|
| 62 | - * @return \string[] the list of tables |
|
| 63 | - */ |
|
| 64 | - public function generateAllDaosAndBeans($daoFactoryClassName, $daonamespace, $beannamespace, $support, $storeInUtc, $castDatesToDateTime) |
|
| 65 | - { |
|
| 66 | - // TODO: migrate $this->daoNamespace to $daonamespace that is passed in parameter! |
|
| 67 | - $classNameMapper = ClassNameMapper::createFromComposerFile($this->rootPath.'composer.json'); |
|
| 68 | - |
|
| 69 | - $this->daoNamespace = $daonamespace; |
|
| 70 | - $this->beanNamespace = $beannamespace; |
|
| 71 | - |
|
| 72 | - // TODO: check that no class name ends with "Base". Otherwise, there will be name clash. |
|
| 73 | - |
|
| 74 | - $tableList = $this->dbConnection->getListOfTables(); |
|
| 75 | - foreach ($tableList as $table) { |
|
| 76 | - $this->generateDaoAndBean($table, $daonamespace, $beannamespace, $classNameMapper, $support, $storeInUtc, $castDatesToDateTime); |
|
| 77 | - } |
|
| 15 | + /** |
|
| 16 | + * @var ConnectionInterface |
|
| 17 | + */ |
|
| 18 | + private $dbConnection; |
|
| 78 | 19 | |
| 79 | - $this->generateFactory($tableList, $daoFactoryClassName, $daonamespace, $classNameMapper); |
|
| 20 | + /** |
|
| 21 | + * The namespace for the DAOs, without trailing \. |
|
| 22 | + * |
|
| 23 | + * @var string |
|
| 24 | + */ |
|
| 25 | + private $daoNamespace; |
|
| 80 | 26 | |
| 81 | - // Ok, let's return the list of all tables. |
|
| 82 | - // These will be used by the calling script to create Mouf instances. |
|
| 27 | + /** |
|
| 28 | + * The Namespace for the beans, without trailing \. |
|
| 29 | + * |
|
| 30 | + * @var string |
|
| 31 | + */ |
|
| 32 | + private $beanNamespace; |
|
| 83 | 33 | |
| 84 | - return $tableList; |
|
| 85 | - } |
|
| 34 | + /** |
|
| 35 | + * @var string |
|
| 36 | + */ |
|
| 37 | + private $rootPath; |
|
| 86 | 38 | |
| 87 | - /** |
|
| 88 | - * Generates in one method call the daos and the beans for one table. |
|
| 89 | - * |
|
| 90 | - * @param $tableName |
|
| 91 | - */ |
|
| 92 | - public function generateDaoAndBean($tableName, $daonamespace, $beannamespace, ClassNameMapper $classNameMapper, $support, $storeInUtc, $castDatesToDateTime) |
|
| 93 | - { |
|
| 94 | - $daoName = $this->getDaoNameFromTableName($tableName); |
|
| 95 | - $beanName = $this->getBeanNameFromTableName($tableName); |
|
| 96 | - $baseBeanName = $this->getBaseBeanNameFromTableName($tableName); |
|
| 39 | + /** |
|
| 40 | + * Constructor. |
|
| 41 | + * |
|
| 42 | + * @param ConnectionInterface $dbConnection The connection to the database. |
|
| 43 | + */ |
|
| 44 | + public function __construct(ConnectionInterface $dbConnection) |
|
| 45 | + { |
|
| 46 | + $this->dbConnection = $dbConnection; |
|
| 47 | + $this->rootPath = __DIR__.'/../../../../../../../../'; |
|
| 48 | + } |
|
| 97 | 49 | |
| 98 | - $connection = $this->dbConnection; |
|
| 99 | - if ($connection instanceof CachedConnection) { |
|
| 100 | - $connection->cacheService->purgeAll(); |
|
| 101 | - } |
|
| 50 | + /** |
|
| 51 | + * Generates all the daos and beans. |
|
| 52 | + * |
|
| 53 | + * @param string $daoFactoryClassName The classe name of the DAO factory |
|
| 54 | + * @param string $daonamespace The namespace for the DAOs, without trailing \ |
|
| 55 | + * @param string $beannamespace The Namespace for the beans, without trailing \ |
|
| 56 | + * @param bool $support If the generated daos should keep support for old functions (eg : getUserList and getList) |
|
| 57 | + * @param bool $storeInUtc If the generated daos should store the date in UTC timezone instead of user's timezone. |
|
| 58 | + * @param bool $castDatesToDateTime Whether dates are converted to DateTimeImmutable or to timestamp |
|
| 59 | + * |
|
| 60 | + * @throws TDBMException |
|
| 61 | + * |
|
| 62 | + * @return \string[] the list of tables |
|
| 63 | + */ |
|
| 64 | + public function generateAllDaosAndBeans($daoFactoryClassName, $daonamespace, $beannamespace, $support, $storeInUtc, $castDatesToDateTime) |
|
| 65 | + { |
|
| 66 | + // TODO: migrate $this->daoNamespace to $daonamespace that is passed in parameter! |
|
| 67 | + $classNameMapper = ClassNameMapper::createFromComposerFile($this->rootPath.'composer.json'); |
|
| 68 | + |
|
| 69 | + $this->daoNamespace = $daonamespace; |
|
| 70 | + $this->beanNamespace = $beannamespace; |
|
| 71 | + |
|
| 72 | + // TODO: check that no class name ends with "Base". Otherwise, there will be name clash. |
|
| 73 | + |
|
| 74 | + $tableList = $this->dbConnection->getListOfTables(); |
|
| 75 | + foreach ($tableList as $table) { |
|
| 76 | + $this->generateDaoAndBean($table, $daonamespace, $beannamespace, $classNameMapper, $support, $storeInUtc, $castDatesToDateTime); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + $this->generateFactory($tableList, $daoFactoryClassName, $daonamespace, $classNameMapper); |
|
| 80 | + |
|
| 81 | + // Ok, let's return the list of all tables. |
|
| 82 | + // These will be used by the calling script to create Mouf instances. |
|
| 83 | + |
|
| 84 | + return $tableList; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Generates in one method call the daos and the beans for one table. |
|
| 89 | + * |
|
| 90 | + * @param $tableName |
|
| 91 | + */ |
|
| 92 | + public function generateDaoAndBean($tableName, $daonamespace, $beannamespace, ClassNameMapper $classNameMapper, $support, $storeInUtc, $castDatesToDateTime) |
|
| 93 | + { |
|
| 94 | + $daoName = $this->getDaoNameFromTableName($tableName); |
|
| 95 | + $beanName = $this->getBeanNameFromTableName($tableName); |
|
| 96 | + $baseBeanName = $this->getBaseBeanNameFromTableName($tableName); |
|
| 97 | + |
|
| 98 | + $connection = $this->dbConnection; |
|
| 99 | + if ($connection instanceof CachedConnection) { |
|
| 100 | + $connection->cacheService->purgeAll(); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + $this->generateBean($beanName, $baseBeanName, $tableName, $beannamespace, $classNameMapper, $storeInUtc, $castDatesToDateTime); |
|
| 104 | + $this->generateDao($daoName, $daoName.'Base', $beanName, $tableName, $classNameMapper, $support); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Returns the name of the bean class from the table name. |
|
| 109 | + * |
|
| 110 | + * @param $tableName |
|
| 111 | + * |
|
| 112 | + * @return string |
|
| 113 | + */ |
|
| 114 | + public static function getBeanNameFromTableName($tableName) |
|
| 115 | + { |
|
| 116 | + return self::toSingular(self::toCamelCase($tableName)).'Bean'; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Returns the name of the base bean class from the table name. |
|
| 121 | + * |
|
| 122 | + * @param $tableName |
|
| 123 | + * |
|
| 124 | + * @return string |
|
| 125 | + */ |
|
| 126 | + public static function getDaoNameFromTableName($tableName) |
|
| 127 | + { |
|
| 128 | + return self::toSingular(self::toCamelCase($tableName)).'Dao'; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Returns the name of the DAO class from the table name. |
|
| 133 | + * |
|
| 134 | + * @param $tableName |
|
| 135 | + * |
|
| 136 | + * @return string |
|
| 137 | + */ |
|
| 138 | + public static function getBaseBeanNameFromTableName($tableName) |
|
| 139 | + { |
|
| 140 | + return self::toSingular(self::toCamelCase($tableName)).'BaseBean'; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * Writes the PHP bean file with all getters and setters from the table passed in parameter. |
|
| 145 | + * |
|
| 146 | + * @param string $className The name of the class |
|
| 147 | + * @param string $baseClassName The name of the base class which will be extended (name only, no directory) |
|
| 148 | + * @param string $tableName The name of the table |
|
| 149 | + * @param string $beannamespace The namespace of the bean |
|
| 150 | + * @param ClassNameMapper $classNameMapper |
|
| 151 | + * |
|
| 152 | + * @throws TDBMException |
|
| 153 | + */ |
|
| 154 | + public function generateBean($className, $baseClassName, $tableName, $beannamespace, ClassNameMapper $classNameMapper, $storeInUtc, $castDatesToDateTime) |
|
| 155 | + { |
|
| 156 | + $table = $this->dbConnection->getTableFromDbModel($tableName); |
|
| 157 | + |
|
| 158 | + // List of methods already written. |
|
| 159 | + $methodsList = []; |
|
| 102 | 160 | |
| 103 | - $this->generateBean($beanName, $baseBeanName, $tableName, $beannamespace, $classNameMapper, $storeInUtc, $castDatesToDateTime); |
|
| 104 | - $this->generateDao($daoName, $daoName.'Base', $beanName, $tableName, $classNameMapper, $support); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Returns the name of the bean class from the table name. |
|
| 109 | - * |
|
| 110 | - * @param $tableName |
|
| 111 | - * |
|
| 112 | - * @return string |
|
| 113 | - */ |
|
| 114 | - public static function getBeanNameFromTableName($tableName) |
|
| 115 | - { |
|
| 116 | - return self::toSingular(self::toCamelCase($tableName)).'Bean'; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Returns the name of the base bean class from the table name. |
|
| 121 | - * |
|
| 122 | - * @param $tableName |
|
| 123 | - * |
|
| 124 | - * @return string |
|
| 125 | - */ |
|
| 126 | - public static function getDaoNameFromTableName($tableName) |
|
| 127 | - { |
|
| 128 | - return self::toSingular(self::toCamelCase($tableName)).'Dao'; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Returns the name of the DAO class from the table name. |
|
| 133 | - * |
|
| 134 | - * @param $tableName |
|
| 135 | - * |
|
| 136 | - * @return string |
|
| 137 | - */ |
|
| 138 | - public static function getBaseBeanNameFromTableName($tableName) |
|
| 139 | - { |
|
| 140 | - return self::toSingular(self::toCamelCase($tableName)).'BaseBean'; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * Writes the PHP bean file with all getters and setters from the table passed in parameter. |
|
| 145 | - * |
|
| 146 | - * @param string $className The name of the class |
|
| 147 | - * @param string $baseClassName The name of the base class which will be extended (name only, no directory) |
|
| 148 | - * @param string $tableName The name of the table |
|
| 149 | - * @param string $beannamespace The namespace of the bean |
|
| 150 | - * @param ClassNameMapper $classNameMapper |
|
| 151 | - * |
|
| 152 | - * @throws TDBMException |
|
| 153 | - */ |
|
| 154 | - public function generateBean($className, $baseClassName, $tableName, $beannamespace, ClassNameMapper $classNameMapper, $storeInUtc, $castDatesToDateTime) |
|
| 155 | - { |
|
| 156 | - $table = $this->dbConnection->getTableFromDbModel($tableName); |
|
| 157 | - |
|
| 158 | - // List of methods already written. |
|
| 159 | - $methodsList = []; |
|
| 160 | - |
|
| 161 | - $str = "<?php |
|
| 161 | + $str = "<?php |
|
| 162 | 162 | namespace {$beannamespace}; |
| 163 | 163 | |
| 164 | 164 | use Mouf\\Database\\TDBM\\TDBMObject; |
@@ -177,18 +177,18 @@ discard block |
||
| 177 | 177 | { |
| 178 | 178 | "; |
| 179 | 179 | |
| 180 | - foreach ($table->columns as $column) { |
|
| 181 | - $type = $column->type; |
|
| 182 | - $normalizedType = $this->dbConnection->getUnderlyingType($type); |
|
| 180 | + foreach ($table->columns as $column) { |
|
| 181 | + $type = $column->type; |
|
| 182 | + $normalizedType = $this->dbConnection->getUnderlyingType($type); |
|
| 183 | 183 | |
| 184 | - $columnGetterName = self::getGetterNameForPropertyName($column->name); |
|
| 185 | - $columnSetterName = self::getSetterNameForPropertyName($column->name); |
|
| 184 | + $columnGetterName = self::getGetterNameForPropertyName($column->name); |
|
| 185 | + $columnSetterName = self::getSetterNameForPropertyName($column->name); |
|
| 186 | 186 | |
| 187 | - $methodsList[$columnGetterName] = $columnGetterName; |
|
| 188 | - $methodsList[$columnSetterName] = $columnSetterName; |
|
| 187 | + $methodsList[$columnGetterName] = $columnGetterName; |
|
| 188 | + $methodsList[$columnSetterName] = $columnSetterName; |
|
| 189 | 189 | |
| 190 | - if ($normalizedType == 'timestamp' || $normalizedType == 'datetime' || $normalizedType == 'date') { |
|
| 191 | - $str .= ' /** |
|
| 190 | + if ($normalizedType == 'timestamp' || $normalizedType == 'datetime' || $normalizedType == 'date') { |
|
| 191 | + $str .= ' /** |
|
| 192 | 192 | * The getter for the "'.$column->name.'" column. |
| 193 | 193 | * It is returned as a PHP timestamp. |
| 194 | 194 | * |
@@ -216,35 +216,35 @@ discard block |
||
| 216 | 216 | if($'.$column->name.' === null) { |
| 217 | 217 | $this->__set(\''.$column->name.'\', null); |
| 218 | 218 | } else {'; |
| 219 | - if ($castDatesToDateTime) { |
|
| 220 | - if ($storeInUtc) { |
|
| 221 | - $str .= ' |
|
| 219 | + if ($castDatesToDateTime) { |
|
| 220 | + if ($storeInUtc) { |
|
| 221 | + $str .= ' |
|
| 222 | 222 | $this->__set(\''.$column->name.'\', $'.$column->name.'->setTimeZone(\\DateTimeZone::UTC)->format("Y-m-d H:i:s")); |
| 223 | 223 | '; |
| 224 | - } else { |
|
| 225 | - $str .= ' |
|
| 224 | + } else { |
|
| 225 | + $str .= ' |
|
| 226 | 226 | $this->__set(\''.$column->name.'\', $'.$column->name.'->format("Y-m-d H:i:s")); |
| 227 | 227 | '; |
| 228 | - } |
|
| 229 | - } else { |
|
| 230 | - if ($storeInUtc) { |
|
| 231 | - $str .= ' |
|
| 228 | + } |
|
| 229 | + } else { |
|
| 230 | + if ($storeInUtc) { |
|
| 231 | + $str .= ' |
|
| 232 | 232 | $date = new \DateTimeImmutable(\'@\'.$'.$column->name.'); |
| 233 | 233 | $this->__set(\''.$column->name.'\', $date->format("Y-m-d H:i:s")); |
| 234 | 234 | '; |
| 235 | - } else { |
|
| 236 | - $str .= ' |
|
| 235 | + } else { |
|
| 236 | + $str .= ' |
|
| 237 | 237 | $this->__set(\''.$column->name.'\', date("Y-m-d H:i:s", $'.$column->name.')); |
| 238 | 238 | '; |
| 239 | - } |
|
| 240 | - } |
|
| 241 | - $str .= ' |
|
| 239 | + } |
|
| 240 | + } |
|
| 241 | + $str .= ' |
|
| 242 | 242 | } |
| 243 | 243 | } |
| 244 | 244 | |
| 245 | 245 | '; |
| 246 | - } else { |
|
| 247 | - $str .= ' /** |
|
| 246 | + } else { |
|
| 247 | + $str .= ' /** |
|
| 248 | 248 | * The getter for the "'.$column->name.'" column. |
| 249 | 249 | * |
| 250 | 250 | * @dbType '.$normalizedType.' |
@@ -266,34 +266,34 @@ discard block |
||
| 266 | 266 | } |
| 267 | 267 | |
| 268 | 268 | '; |
| 269 | - } |
|
| 270 | - } |
|
| 269 | + } |
|
| 270 | + } |
|
| 271 | 271 | |
| 272 | - $referencedTablesList = []; |
|
| 273 | - // Now, let's get the constraints from this table on another table. |
|
| 274 | - // We will generate getters and setters for those. |
|
| 275 | - //$constraints = $this->dbConnection->getConstraintsFromTable($tableName); |
|
| 276 | - $constraints = $this->dbConnection->getConstraintsOnTable($tableName); |
|
| 277 | - |
|
| 278 | - foreach ($constraints as $array) { |
|
| 279 | - if (!isset($referencedTablesList[$array['table2']])) { |
|
| 280 | - $referencedTablesList[$array['table2']] = 1; |
|
| 281 | - } else { |
|
| 282 | - $referencedTablesList[$array['table2']] += 1; |
|
| 283 | - } |
|
| 284 | - $getterName = self::getGetterNameForConstrainedObject($array['table2'], $array['col1']); |
|
| 285 | - $setterName = self::getSetterNameForConstrainedObject($array['table2'], $array['col1']); |
|
| 286 | - |
|
| 287 | - // If the method has already been defined, lets not write it. |
|
| 288 | - if (isset($methodsList[$getterName]) || isset($methodsList[$setterName])) { |
|
| 289 | - continue; |
|
| 290 | - } |
|
| 291 | - $methodsList[$getterName] = $getterName; |
|
| 292 | - $methodsList[$setterName] = $setterName; |
|
| 293 | - |
|
| 294 | - $referencedBeanName = $this->getBeanNameFromTableName($array['table2']); |
|
| 295 | - |
|
| 296 | - $str .= ' /** |
|
| 272 | + $referencedTablesList = []; |
|
| 273 | + // Now, let's get the constraints from this table on another table. |
|
| 274 | + // We will generate getters and setters for those. |
|
| 275 | + //$constraints = $this->dbConnection->getConstraintsFromTable($tableName); |
|
| 276 | + $constraints = $this->dbConnection->getConstraintsOnTable($tableName); |
|
| 277 | + |
|
| 278 | + foreach ($constraints as $array) { |
|
| 279 | + if (!isset($referencedTablesList[$array['table2']])) { |
|
| 280 | + $referencedTablesList[$array['table2']] = 1; |
|
| 281 | + } else { |
|
| 282 | + $referencedTablesList[$array['table2']] += 1; |
|
| 283 | + } |
|
| 284 | + $getterName = self::getGetterNameForConstrainedObject($array['table2'], $array['col1']); |
|
| 285 | + $setterName = self::getSetterNameForConstrainedObject($array['table2'], $array['col1']); |
|
| 286 | + |
|
| 287 | + // If the method has already been defined, lets not write it. |
|
| 288 | + if (isset($methodsList[$getterName]) || isset($methodsList[$setterName])) { |
|
| 289 | + continue; |
|
| 290 | + } |
|
| 291 | + $methodsList[$getterName] = $getterName; |
|
| 292 | + $methodsList[$setterName] = $setterName; |
|
| 293 | + |
|
| 294 | + $referencedBeanName = $this->getBeanNameFromTableName($array['table2']); |
|
| 295 | + |
|
| 296 | + $str .= ' /** |
|
| 297 | 297 | * Returns the '.$referencedBeanName.' object bound to this object via the '.$array['col1'].' column. |
| 298 | 298 | * |
| 299 | 299 | * @return '.$referencedBeanName.' |
@@ -315,34 +315,34 @@ discard block |
||
| 315 | 315 | } |
| 316 | 316 | |
| 317 | 317 | '; |
| 318 | - } |
|
| 318 | + } |
|
| 319 | 319 | |
| 320 | - // Now, let's implement the shortcuts to the getter of objects. |
|
| 321 | - // Shortcuts are used to save typing. They are available only if a referenced table is referenced only once by our tables. |
|
| 322 | - foreach ($referencedTablesList as $referrencedTable=>$number) { |
|
| 323 | - if ($number == 1) { |
|
| 324 | - foreach ($constraints as $array) { |
|
| 325 | - if ($array['table2'] == $referrencedTable) { |
|
| 326 | - $columnName = $array['col1']; |
|
| 327 | - $targetColumnName = $array['col2']; |
|
| 328 | - break; |
|
| 329 | - } |
|
| 330 | - } |
|
| 331 | - $fullGetterName = self::getGetterNameForConstrainedObject($referrencedTable, $columnName); |
|
| 332 | - $shortGetterName = self::getGetterNameForConstrainedObject($referrencedTable); |
|
| 333 | - $fullSetterName = self::getSetterNameForConstrainedObject($referrencedTable, $columnName); |
|
| 334 | - $shortSetterName = self::getSetterNameForConstrainedObject($referrencedTable); |
|
| 335 | - |
|
| 336 | - // If the method has already been defined, lets not write it. |
|
| 337 | - if (isset($methodsList[$shortGetterName]) || isset($methodsList[$shortSetterName])) { |
|
| 338 | - continue; |
|
| 339 | - } |
|
| 340 | - $methodsList[$shortGetterName] = $shortGetterName; |
|
| 341 | - $methodsList[$shortSetterName] = $shortSetterName; |
|
| 342 | - |
|
| 343 | - $referencedBeanName = $this->getBeanNameFromTableName($array['table2']); |
|
| 344 | - |
|
| 345 | - $str .= ' /** |
|
| 320 | + // Now, let's implement the shortcuts to the getter of objects. |
|
| 321 | + // Shortcuts are used to save typing. They are available only if a referenced table is referenced only once by our tables. |
|
| 322 | + foreach ($referencedTablesList as $referrencedTable=>$number) { |
|
| 323 | + if ($number == 1) { |
|
| 324 | + foreach ($constraints as $array) { |
|
| 325 | + if ($array['table2'] == $referrencedTable) { |
|
| 326 | + $columnName = $array['col1']; |
|
| 327 | + $targetColumnName = $array['col2']; |
|
| 328 | + break; |
|
| 329 | + } |
|
| 330 | + } |
|
| 331 | + $fullGetterName = self::getGetterNameForConstrainedObject($referrencedTable, $columnName); |
|
| 332 | + $shortGetterName = self::getGetterNameForConstrainedObject($referrencedTable); |
|
| 333 | + $fullSetterName = self::getSetterNameForConstrainedObject($referrencedTable, $columnName); |
|
| 334 | + $shortSetterName = self::getSetterNameForConstrainedObject($referrencedTable); |
|
| 335 | + |
|
| 336 | + // If the method has already been defined, lets not write it. |
|
| 337 | + if (isset($methodsList[$shortGetterName]) || isset($methodsList[$shortSetterName])) { |
|
| 338 | + continue; |
|
| 339 | + } |
|
| 340 | + $methodsList[$shortGetterName] = $shortGetterName; |
|
| 341 | + $methodsList[$shortSetterName] = $shortSetterName; |
|
| 342 | + |
|
| 343 | + $referencedBeanName = $this->getBeanNameFromTableName($array['table2']); |
|
| 344 | + |
|
| 345 | + $str .= ' /** |
|
| 346 | 346 | * Returns the '.$referencedBeanName.' object bound to this object via the '.$array['col1'].' column. |
| 347 | 347 | * This is an alias for the '.$fullGetterName.' method. |
| 348 | 348 | * |
@@ -366,30 +366,30 @@ discard block |
||
| 366 | 366 | } |
| 367 | 367 | |
| 368 | 368 | '; |
| 369 | - } |
|
| 370 | - } |
|
| 369 | + } |
|
| 370 | + } |
|
| 371 | 371 | |
| 372 | - $str .= '} |
|
| 372 | + $str .= '} |
|
| 373 | 373 | ?>'; |
| 374 | 374 | |
| 375 | - $possibleBaseFileNames = $classNameMapper->getPossibleFileNames($beannamespace.'\\'.$baseClassName); |
|
| 376 | - if (!$possibleBaseFileNames) { |
|
| 377 | - throw new TDBMException('Sorry, autoload namespace issue. The class "'.$beannamespace.'\\'.$baseClassName.'" is not autoloadable.'); |
|
| 378 | - } |
|
| 379 | - $possibleBaseFileName = $this->rootPath.$possibleBaseFileNames[0]; |
|
| 375 | + $possibleBaseFileNames = $classNameMapper->getPossibleFileNames($beannamespace.'\\'.$baseClassName); |
|
| 376 | + if (!$possibleBaseFileNames) { |
|
| 377 | + throw new TDBMException('Sorry, autoload namespace issue. The class "'.$beannamespace.'\\'.$baseClassName.'" is not autoloadable.'); |
|
| 378 | + } |
|
| 379 | + $possibleBaseFileName = $this->rootPath.$possibleBaseFileNames[0]; |
|
| 380 | 380 | |
| 381 | - $this->ensureDirectoryExist($possibleBaseFileName); |
|
| 382 | - file_put_contents($possibleBaseFileName, $str); |
|
| 383 | - @chmod($possibleBaseFileName, 0664); |
|
| 381 | + $this->ensureDirectoryExist($possibleBaseFileName); |
|
| 382 | + file_put_contents($possibleBaseFileName, $str); |
|
| 383 | + @chmod($possibleBaseFileName, 0664); |
|
| 384 | 384 | |
| 385 | - $possibleFileNames = $classNameMapper->getPossibleFileNames($beannamespace.'\\'.$className); |
|
| 386 | - if (!$possibleFileNames) { |
|
| 387 | - throw new TDBMException('Sorry, autoload namespace issue. The class "'.$beannamespace.'\\'.$className.'" is not autoloadable.'); |
|
| 388 | - } |
|
| 389 | - $possibleFileName = $this->rootPath.$possibleFileNames[0]; |
|
| 385 | + $possibleFileNames = $classNameMapper->getPossibleFileNames($beannamespace.'\\'.$className); |
|
| 386 | + if (!$possibleFileNames) { |
|
| 387 | + throw new TDBMException('Sorry, autoload namespace issue. The class "'.$beannamespace.'\\'.$className.'" is not autoloadable.'); |
|
| 388 | + } |
|
| 389 | + $possibleFileName = $this->rootPath.$possibleFileNames[0]; |
|
| 390 | 390 | |
| 391 | - if (!file_exists($possibleFileName)) { |
|
| 392 | - $str = "<?php |
|
| 391 | + if (!file_exists($possibleFileName)) { |
|
| 392 | + $str = "<?php |
|
| 393 | 393 | /* |
| 394 | 394 | * This file has been automatically generated by TDBM. |
| 395 | 395 | * You can edit this file as it will not be overwritten. |
@@ -406,42 +406,42 @@ discard block |
||
| 406 | 406 | { |
| 407 | 407 | |
| 408 | 408 | }"; |
| 409 | - $this->ensureDirectoryExist($possibleFileName); |
|
| 410 | - file_put_contents($possibleFileName, $str); |
|
| 411 | - @chmod($possibleFileName, 0664); |
|
| 412 | - } |
|
| 413 | - } |
|
| 414 | - |
|
| 415 | - /** |
|
| 416 | - * Writes the PHP bean DAO with simple functions to create/get/save objects. |
|
| 417 | - * |
|
| 418 | - * @param string $fileName The file that will be written (without the directory) |
|
| 419 | - * @param string $className The name of the class |
|
| 420 | - * @param string $tableName The name of the table |
|
| 421 | - */ |
|
| 422 | - public function generateDao($className, $baseClassName, $beanClassName, $tableName, ClassNameMapper $classNameMapper, $support) |
|
| 423 | - { |
|
| 424 | - $info = $this->dbConnection->getTableInfo($tableName); |
|
| 425 | - $defaultSort = null; |
|
| 426 | - foreach ($info as $index => $data) { |
|
| 427 | - $comments = $data['column_comment']; |
|
| 428 | - $matches = []; |
|
| 429 | - if (preg_match('/@defaultSort(\((desc|asc)\))*/', $comments, $matches) != 0) { |
|
| 430 | - $defaultSort = $data['column_name']; |
|
| 431 | - if (count($matches == 3)) { |
|
| 432 | - $defaultSortDirection = $matches[2]; |
|
| 433 | - } else { |
|
| 434 | - $defaultSortDirection = 'ASC'; |
|
| 435 | - } |
|
| 436 | - } |
|
| 437 | - } |
|
| 409 | + $this->ensureDirectoryExist($possibleFileName); |
|
| 410 | + file_put_contents($possibleFileName, $str); |
|
| 411 | + @chmod($possibleFileName, 0664); |
|
| 412 | + } |
|
| 413 | + } |
|
| 438 | 414 | |
| 439 | - $tableCamel = self::toSingular(self::toCamelCase($tableName)); |
|
| 415 | + /** |
|
| 416 | + * Writes the PHP bean DAO with simple functions to create/get/save objects. |
|
| 417 | + * |
|
| 418 | + * @param string $fileName The file that will be written (without the directory) |
|
| 419 | + * @param string $className The name of the class |
|
| 420 | + * @param string $tableName The name of the table |
|
| 421 | + */ |
|
| 422 | + public function generateDao($className, $baseClassName, $beanClassName, $tableName, ClassNameMapper $classNameMapper, $support) |
|
| 423 | + { |
|
| 424 | + $info = $this->dbConnection->getTableInfo($tableName); |
|
| 425 | + $defaultSort = null; |
|
| 426 | + foreach ($info as $index => $data) { |
|
| 427 | + $comments = $data['column_comment']; |
|
| 428 | + $matches = []; |
|
| 429 | + if (preg_match('/@defaultSort(\((desc|asc)\))*/', $comments, $matches) != 0) { |
|
| 430 | + $defaultSort = $data['column_name']; |
|
| 431 | + if (count($matches == 3)) { |
|
| 432 | + $defaultSortDirection = $matches[2]; |
|
| 433 | + } else { |
|
| 434 | + $defaultSortDirection = 'ASC'; |
|
| 435 | + } |
|
| 436 | + } |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + $tableCamel = self::toSingular(self::toCamelCase($tableName)); |
|
| 440 | 440 | |
| 441 | - $beanClassWithoutNameSpace = $beanClassName; |
|
| 442 | - $beanClassName = $this->beanNamespace.'\\'.$beanClassName; |
|
| 441 | + $beanClassWithoutNameSpace = $beanClassName; |
|
| 442 | + $beanClassName = $this->beanNamespace.'\\'.$beanClassName; |
|
| 443 | 443 | |
| 444 | - $str = "<?php |
|
| 444 | + $str = "<?php |
|
| 445 | 445 | /* |
| 446 | 446 | * This file has been automatically generated by TDBM. |
| 447 | 447 | * DO NOT edit this file, as it might be overwritten. |
@@ -582,9 +582,9 @@ discard block |
||
| 582 | 582 | \$this->defaultSort = \$defaultSort; |
| 583 | 583 | } |
| 584 | 584 | "; |
| 585 | - // If we want compatibility with TDBM < 2.3 |
|
| 586 | - if ($support) { |
|
| 587 | - $str .= " |
|
| 585 | + // If we want compatibility with TDBM < 2.3 |
|
| 586 | + if ($support) { |
|
| 587 | + $str .= " |
|
| 588 | 588 | |
| 589 | 589 | /** |
| 590 | 590 | * Return a new instance of $beanClassWithoutNameSpace object, that will be persisted in database. |
@@ -658,30 +658,30 @@ discard block |
||
| 658 | 658 | return $this->getByFilter($filterBag); |
| 659 | 659 | } |
| 660 | 660 | '; |
| 661 | - } |
|
| 662 | - $str .= ' |
|
| 661 | + } |
|
| 662 | + $str .= ' |
|
| 663 | 663 | } |
| 664 | 664 | ?>'; |
| 665 | 665 | |
| 666 | - $possibleBaseFileNames = $classNameMapper->getPossibleFileNames($this->daoNamespace.'\\'.$baseClassName); |
|
| 667 | - if (!$possibleBaseFileNames) { |
|
| 668 | - throw new TDBMException('Sorry, autoload namespace issue. The class "'.$baseClassName.'" is not autoloadable.'); |
|
| 669 | - } |
|
| 670 | - $possibleBaseFileName = $this->rootPath.$possibleBaseFileNames[0]; |
|
| 666 | + $possibleBaseFileNames = $classNameMapper->getPossibleFileNames($this->daoNamespace.'\\'.$baseClassName); |
|
| 667 | + if (!$possibleBaseFileNames) { |
|
| 668 | + throw new TDBMException('Sorry, autoload namespace issue. The class "'.$baseClassName.'" is not autoloadable.'); |
|
| 669 | + } |
|
| 670 | + $possibleBaseFileName = $this->rootPath.$possibleBaseFileNames[0]; |
|
| 671 | 671 | |
| 672 | - $this->ensureDirectoryExist($possibleBaseFileName); |
|
| 673 | - file_put_contents($possibleBaseFileName, $str); |
|
| 674 | - @chmod($possibleBaseFileName, 0664); |
|
| 672 | + $this->ensureDirectoryExist($possibleBaseFileName); |
|
| 673 | + file_put_contents($possibleBaseFileName, $str); |
|
| 674 | + @chmod($possibleBaseFileName, 0664); |
|
| 675 | 675 | |
| 676 | - $possibleFileNames = $classNameMapper->getPossibleFileNames($this->daoNamespace.'\\'.$className); |
|
| 677 | - if (!$possibleFileNames) { |
|
| 678 | - throw new TDBMException('Sorry, autoload namespace issue. The class "'.$className.'" is not autoloadable.'); |
|
| 679 | - } |
|
| 680 | - $possibleFileName = $this->rootPath.$possibleFileNames[0]; |
|
| 676 | + $possibleFileNames = $classNameMapper->getPossibleFileNames($this->daoNamespace.'\\'.$className); |
|
| 677 | + if (!$possibleFileNames) { |
|
| 678 | + throw new TDBMException('Sorry, autoload namespace issue. The class "'.$className.'" is not autoloadable.'); |
|
| 679 | + } |
|
| 680 | + $possibleFileName = $this->rootPath.$possibleFileNames[0]; |
|
| 681 | 681 | |
| 682 | - // Now, let's generate the "editable" class |
|
| 683 | - if (!file_exists($possibleFileName)) { |
|
| 684 | - $str = "<?php |
|
| 682 | + // Now, let's generate the "editable" class |
|
| 683 | + if (!file_exists($possibleFileName)) { |
|
| 684 | + $str = "<?php |
|
| 685 | 685 | /* |
| 686 | 686 | * This file has been automatically generated by TDBM. |
| 687 | 687 | * You can edit this file as it will not be overwritten. |
@@ -697,22 +697,22 @@ discard block |
||
| 697 | 697 | { |
| 698 | 698 | |
| 699 | 699 | }"; |
| 700 | - $this->ensureDirectoryExist($possibleFileName); |
|
| 701 | - file_put_contents($possibleFileName, $str); |
|
| 702 | - @chmod($possibleFileName, 0664); |
|
| 703 | - } |
|
| 704 | - } |
|
| 705 | - |
|
| 706 | - /** |
|
| 707 | - * Generates the factory bean. |
|
| 708 | - * |
|
| 709 | - * @param $tableList |
|
| 710 | - */ |
|
| 711 | - private function generateFactory($tableList, $daoFactoryClassName, $daoNamespace, ClassNameMapper $classNameMapper) |
|
| 712 | - { |
|
| 713 | - // For each table, let's write a property. |
|
| 714 | - |
|
| 715 | - $str = "<?php |
|
| 700 | + $this->ensureDirectoryExist($possibleFileName); |
|
| 701 | + file_put_contents($possibleFileName, $str); |
|
| 702 | + @chmod($possibleFileName, 0664); |
|
| 703 | + } |
|
| 704 | + } |
|
| 705 | + |
|
| 706 | + /** |
|
| 707 | + * Generates the factory bean. |
|
| 708 | + * |
|
| 709 | + * @param $tableList |
|
| 710 | + */ |
|
| 711 | + private function generateFactory($tableList, $daoFactoryClassName, $daoNamespace, ClassNameMapper $classNameMapper) |
|
| 712 | + { |
|
| 713 | + // For each table, let's write a property. |
|
| 714 | + |
|
| 715 | + $str = "<?php |
|
| 716 | 716 | /* |
| 717 | 717 | * This file has been automatically generated by TDBM. |
| 718 | 718 | * DO NOT edit this file, as it might be overwritten. |
@@ -728,11 +728,11 @@ discard block |
||
| 728 | 728 | { |
| 729 | 729 | "; |
| 730 | 730 | |
| 731 | - foreach ($tableList as $table) { |
|
| 732 | - $daoClassName = $this->getDaoNameFromTableName($table); |
|
| 733 | - $daoInstanceName = self::toVariableName($daoClassName); |
|
| 731 | + foreach ($tableList as $table) { |
|
| 732 | + $daoClassName = $this->getDaoNameFromTableName($table); |
|
| 733 | + $daoInstanceName = self::toVariableName($daoClassName); |
|
| 734 | 734 | |
| 735 | - $str .= ' /** |
|
| 735 | + $str .= ' /** |
|
| 736 | 736 | * @var '.$daoClassName.' |
| 737 | 737 | */ |
| 738 | 738 | private $'.$daoInstanceName.'; |
@@ -758,179 +758,179 @@ discard block |
||
| 758 | 758 | } |
| 759 | 759 | |
| 760 | 760 | '; |
| 761 | - } |
|
| 761 | + } |
|
| 762 | 762 | |
| 763 | - $str .= ' |
|
| 763 | + $str .= ' |
|
| 764 | 764 | } |
| 765 | 765 | ?>'; |
| 766 | 766 | |
| 767 | - $possibleFileNames = $classNameMapper->getPossibleFileNames($daoNamespace.'\\'.$daoFactoryClassName); |
|
| 768 | - if (!$possibleFileNames) { |
|
| 769 | - throw new TDBMException('Sorry, autoload namespace issue. The class "'.$daoNamespace.'\\'.$daoFactoryClassName.'" is not autoloadable.'); |
|
| 770 | - } |
|
| 771 | - $possibleFileName = $this->rootPath.$possibleFileNames[0]; |
|
| 772 | - |
|
| 773 | - $this->ensureDirectoryExist($possibleFileName); |
|
| 774 | - file_put_contents($possibleFileName, $str); |
|
| 775 | - } |
|
| 776 | - |
|
| 777 | - /** |
|
| 778 | - * Transforms the property name in a setter name. |
|
| 779 | - * For instance, phone => getPhone or name => getName. |
|
| 780 | - * |
|
| 781 | - * @param string $methodName |
|
| 782 | - * |
|
| 783 | - * @return string |
|
| 784 | - */ |
|
| 785 | - public static function getSetterNameForPropertyName($propertyName) |
|
| 786 | - { |
|
| 787 | - $propName2 = self::toCamelCase($propertyName); |
|
| 788 | - |
|
| 789 | - return 'set'.$propName2; |
|
| 790 | - } |
|
| 791 | - |
|
| 792 | - /** |
|
| 793 | - * Transforms the property name in a getter name. |
|
| 794 | - * For instance, phone => getPhone or name => getName. |
|
| 795 | - * |
|
| 796 | - * @param string $propertyName |
|
| 797 | - * |
|
| 798 | - * @return string |
|
| 799 | - */ |
|
| 800 | - public static function getGetterNameForPropertyName($propertyName) |
|
| 801 | - { |
|
| 802 | - $propName2 = self::toCamelCase($propertyName); |
|
| 803 | - |
|
| 804 | - return 'get'.$propName2; |
|
| 805 | - } |
|
| 806 | - |
|
| 807 | - /** |
|
| 808 | - * Transforms the table name constrained by this object into a setter name. |
|
| 809 | - * For instance, users => setUserByUserId or role => setRoleByRoleId. |
|
| 810 | - * |
|
| 811 | - * @param $tableName The table that is constrained |
|
| 812 | - * @param $columnName The column used to constrain the table (optional). If omitted, the "By[columnname]" part of the name will be omitted. |
|
| 813 | - * |
|
| 814 | - * @return string |
|
| 815 | - */ |
|
| 816 | - public static function getSetterNameForConstrainedObject($tableName, $columnName = null) |
|
| 817 | - { |
|
| 818 | - $getter = self::toSingular(self::toCamelCase($tableName)); |
|
| 819 | - if ($columnName) { |
|
| 820 | - $getter .= 'By'.self::toCamelCase($columnName); |
|
| 821 | - } |
|
| 767 | + $possibleFileNames = $classNameMapper->getPossibleFileNames($daoNamespace.'\\'.$daoFactoryClassName); |
|
| 768 | + if (!$possibleFileNames) { |
|
| 769 | + throw new TDBMException('Sorry, autoload namespace issue. The class "'.$daoNamespace.'\\'.$daoFactoryClassName.'" is not autoloadable.'); |
|
| 770 | + } |
|
| 771 | + $possibleFileName = $this->rootPath.$possibleFileNames[0]; |
|
| 822 | 772 | |
| 823 | - return 'set'.$getter; |
|
| 824 | - } |
|
| 825 | - |
|
| 826 | - /** |
|
| 827 | - * Transforms the table name constrained by this object into a getter name. |
|
| 828 | - * For instance, users => getUserByUserId or role => getRoleByRoleId. |
|
| 829 | - * |
|
| 830 | - * @param $tableName The table that is constrained |
|
| 831 | - * @param $columnName The column used to constrain the table (optional). If omitted, the "By[columnname]" part of the name will be omitted. |
|
| 832 | - * |
|
| 833 | - * @return string |
|
| 834 | - */ |
|
| 835 | - public static function getGetterNameForConstrainedObject($tableName, $columnName = null) |
|
| 836 | - { |
|
| 837 | - $getter = self::toSingular(self::toCamelCase($tableName)); |
|
| 838 | - if ($columnName) { |
|
| 839 | - $getter .= 'By'.self::toCamelCase($columnName); |
|
| 840 | - } |
|
| 773 | + $this->ensureDirectoryExist($possibleFileName); |
|
| 774 | + file_put_contents($possibleFileName, $str); |
|
| 775 | + } |
|
| 841 | 776 | |
| 842 | - return 'get'.$getter; |
|
| 843 | - } |
|
| 844 | - |
|
| 845 | - /** |
|
| 846 | - * Transforms a string to camelCase (except the first letter will be uppercase too). |
|
| 847 | - * Underscores and spaces are removed and the first letter after the underscore is uppercased. |
|
| 848 | - * |
|
| 849 | - * @param $str string |
|
| 850 | - * |
|
| 851 | - * @return string |
|
| 852 | - */ |
|
| 853 | - public static function toCamelCase($str) |
|
| 854 | - { |
|
| 855 | - $str = strtoupper(substr($str, 0, 1)).substr($str, 1); |
|
| 856 | - while (true) { |
|
| 857 | - if (strpos($str, '_') === false && strpos($str, ' ') === false) { |
|
| 858 | - break; |
|
| 859 | - } |
|
| 860 | - |
|
| 861 | - $pos = strpos($str, '_'); |
|
| 862 | - if ($pos === false) { |
|
| 863 | - $pos = strpos($str, ' '); |
|
| 864 | - } |
|
| 865 | - $before = substr($str, 0, $pos); |
|
| 866 | - $after = substr($str, $pos + 1); |
|
| 867 | - $str = $before.strtoupper(substr($after, 0, 1)).substr($after, 1); |
|
| 868 | - } |
|
| 777 | + /** |
|
| 778 | + * Transforms the property name in a setter name. |
|
| 779 | + * For instance, phone => getPhone or name => getName. |
|
| 780 | + * |
|
| 781 | + * @param string $methodName |
|
| 782 | + * |
|
| 783 | + * @return string |
|
| 784 | + */ |
|
| 785 | + public static function getSetterNameForPropertyName($propertyName) |
|
| 786 | + { |
|
| 787 | + $propName2 = self::toCamelCase($propertyName); |
|
| 869 | 788 | |
| 870 | - return $str; |
|
| 871 | - } |
|
| 872 | - |
|
| 873 | - /** |
|
| 874 | - * Tries to put string to the singular form (if it is plural). |
|
| 875 | - * Obviously, this can't be perfect, be we do the best we can. |
|
| 876 | - * |
|
| 877 | - * @param $str string |
|
| 878 | - * |
|
| 879 | - * @return string |
|
| 880 | - */ |
|
| 881 | - public static function toSingular($str) |
|
| 882 | - { |
|
| 883 | - // First, ignore "ss" words (like access). |
|
| 884 | - if (strpos($str, 'ss', strlen($str) - 2) !== false) { |
|
| 885 | - return $str; |
|
| 886 | - } |
|
| 789 | + return 'set'.$propName2; |
|
| 790 | + } |
|
| 887 | 791 | |
| 888 | - // Now, let's see if the string ends with s: |
|
| 889 | - if (strpos($str, 's', strlen($str) - 1) !== false) { |
|
| 890 | - // Yes? Let's remove the s. |
|
| 891 | - return substr($str, 0, strlen($str) - 1); |
|
| 892 | - } |
|
| 792 | + /** |
|
| 793 | + * Transforms the property name in a getter name. |
|
| 794 | + * For instance, phone => getPhone or name => getName. |
|
| 795 | + * |
|
| 796 | + * @param string $propertyName |
|
| 797 | + * |
|
| 798 | + * @return string |
|
| 799 | + */ |
|
| 800 | + public static function getGetterNameForPropertyName($propertyName) |
|
| 801 | + { |
|
| 802 | + $propName2 = self::toCamelCase($propertyName); |
|
| 893 | 803 | |
| 894 | - return $str; |
|
| 895 | - } |
|
| 896 | - |
|
| 897 | - /** |
|
| 898 | - * Put the first letter of the string in lower case. |
|
| 899 | - * Very useful to transform a class name into a variable name. |
|
| 900 | - * |
|
| 901 | - * @param $str string |
|
| 902 | - * |
|
| 903 | - * @return string |
|
| 904 | - */ |
|
| 905 | - public static function toVariableName($str) |
|
| 906 | - { |
|
| 907 | - return strtolower(substr($str, 0, 1)).substr($str, 1); |
|
| 908 | - } |
|
| 909 | - |
|
| 910 | - /** |
|
| 911 | - * Ensures the file passed in parameter can be written in its directory. |
|
| 912 | - * |
|
| 913 | - * @param string $fileName |
|
| 914 | - */ |
|
| 915 | - private function ensureDirectoryExist($fileName) |
|
| 916 | - { |
|
| 917 | - $dirName = dirname($fileName); |
|
| 918 | - if (!file_exists($dirName)) { |
|
| 919 | - $old = umask(0); |
|
| 920 | - $result = mkdir($dirName, 0775, true); |
|
| 921 | - umask($old); |
|
| 922 | - if ($result == false) { |
|
| 923 | - echo 'Unable to create directory: '.$dirName.'.'; |
|
| 924 | - exit; |
|
| 925 | - } |
|
| 926 | - } |
|
| 927 | - } |
|
| 928 | - |
|
| 929 | - /** |
|
| 930 | - * @param string $rootPath |
|
| 931 | - */ |
|
| 932 | - public function setRootPath($rootPath) |
|
| 933 | - { |
|
| 934 | - $this->rootPath = $rootPath; |
|
| 935 | - } |
|
| 804 | + return 'get'.$propName2; |
|
| 805 | + } |
|
| 806 | + |
|
| 807 | + /** |
|
| 808 | + * Transforms the table name constrained by this object into a setter name. |
|
| 809 | + * For instance, users => setUserByUserId or role => setRoleByRoleId. |
|
| 810 | + * |
|
| 811 | + * @param $tableName The table that is constrained |
|
| 812 | + * @param $columnName The column used to constrain the table (optional). If omitted, the "By[columnname]" part of the name will be omitted. |
|
| 813 | + * |
|
| 814 | + * @return string |
|
| 815 | + */ |
|
| 816 | + public static function getSetterNameForConstrainedObject($tableName, $columnName = null) |
|
| 817 | + { |
|
| 818 | + $getter = self::toSingular(self::toCamelCase($tableName)); |
|
| 819 | + if ($columnName) { |
|
| 820 | + $getter .= 'By'.self::toCamelCase($columnName); |
|
| 821 | + } |
|
| 822 | + |
|
| 823 | + return 'set'.$getter; |
|
| 824 | + } |
|
| 825 | + |
|
| 826 | + /** |
|
| 827 | + * Transforms the table name constrained by this object into a getter name. |
|
| 828 | + * For instance, users => getUserByUserId or role => getRoleByRoleId. |
|
| 829 | + * |
|
| 830 | + * @param $tableName The table that is constrained |
|
| 831 | + * @param $columnName The column used to constrain the table (optional). If omitted, the "By[columnname]" part of the name will be omitted. |
|
| 832 | + * |
|
| 833 | + * @return string |
|
| 834 | + */ |
|
| 835 | + public static function getGetterNameForConstrainedObject($tableName, $columnName = null) |
|
| 836 | + { |
|
| 837 | + $getter = self::toSingular(self::toCamelCase($tableName)); |
|
| 838 | + if ($columnName) { |
|
| 839 | + $getter .= 'By'.self::toCamelCase($columnName); |
|
| 840 | + } |
|
| 841 | + |
|
| 842 | + return 'get'.$getter; |
|
| 843 | + } |
|
| 844 | + |
|
| 845 | + /** |
|
| 846 | + * Transforms a string to camelCase (except the first letter will be uppercase too). |
|
| 847 | + * Underscores and spaces are removed and the first letter after the underscore is uppercased. |
|
| 848 | + * |
|
| 849 | + * @param $str string |
|
| 850 | + * |
|
| 851 | + * @return string |
|
| 852 | + */ |
|
| 853 | + public static function toCamelCase($str) |
|
| 854 | + { |
|
| 855 | + $str = strtoupper(substr($str, 0, 1)).substr($str, 1); |
|
| 856 | + while (true) { |
|
| 857 | + if (strpos($str, '_') === false && strpos($str, ' ') === false) { |
|
| 858 | + break; |
|
| 859 | + } |
|
| 860 | + |
|
| 861 | + $pos = strpos($str, '_'); |
|
| 862 | + if ($pos === false) { |
|
| 863 | + $pos = strpos($str, ' '); |
|
| 864 | + } |
|
| 865 | + $before = substr($str, 0, $pos); |
|
| 866 | + $after = substr($str, $pos + 1); |
|
| 867 | + $str = $before.strtoupper(substr($after, 0, 1)).substr($after, 1); |
|
| 868 | + } |
|
| 869 | + |
|
| 870 | + return $str; |
|
| 871 | + } |
|
| 872 | + |
|
| 873 | + /** |
|
| 874 | + * Tries to put string to the singular form (if it is plural). |
|
| 875 | + * Obviously, this can't be perfect, be we do the best we can. |
|
| 876 | + * |
|
| 877 | + * @param $str string |
|
| 878 | + * |
|
| 879 | + * @return string |
|
| 880 | + */ |
|
| 881 | + public static function toSingular($str) |
|
| 882 | + { |
|
| 883 | + // First, ignore "ss" words (like access). |
|
| 884 | + if (strpos($str, 'ss', strlen($str) - 2) !== false) { |
|
| 885 | + return $str; |
|
| 886 | + } |
|
| 887 | + |
|
| 888 | + // Now, let's see if the string ends with s: |
|
| 889 | + if (strpos($str, 's', strlen($str) - 1) !== false) { |
|
| 890 | + // Yes? Let's remove the s. |
|
| 891 | + return substr($str, 0, strlen($str) - 1); |
|
| 892 | + } |
|
| 893 | + |
|
| 894 | + return $str; |
|
| 895 | + } |
|
| 896 | + |
|
| 897 | + /** |
|
| 898 | + * Put the first letter of the string in lower case. |
|
| 899 | + * Very useful to transform a class name into a variable name. |
|
| 900 | + * |
|
| 901 | + * @param $str string |
|
| 902 | + * |
|
| 903 | + * @return string |
|
| 904 | + */ |
|
| 905 | + public static function toVariableName($str) |
|
| 906 | + { |
|
| 907 | + return strtolower(substr($str, 0, 1)).substr($str, 1); |
|
| 908 | + } |
|
| 909 | + |
|
| 910 | + /** |
|
| 911 | + * Ensures the file passed in parameter can be written in its directory. |
|
| 912 | + * |
|
| 913 | + * @param string $fileName |
|
| 914 | + */ |
|
| 915 | + private function ensureDirectoryExist($fileName) |
|
| 916 | + { |
|
| 917 | + $dirName = dirname($fileName); |
|
| 918 | + if (!file_exists($dirName)) { |
|
| 919 | + $old = umask(0); |
|
| 920 | + $result = mkdir($dirName, 0775, true); |
|
| 921 | + umask($old); |
|
| 922 | + if ($result == false) { |
|
| 923 | + echo 'Unable to create directory: '.$dirName.'.'; |
|
| 924 | + exit; |
|
| 925 | + } |
|
| 926 | + } |
|
| 927 | + } |
|
| 928 | + |
|
| 929 | + /** |
|
| 930 | + * @param string $rootPath |
|
| 931 | + */ |
|
| 932 | + public function setRootPath($rootPath) |
|
| 933 | + { |
|
| 934 | + $this->rootPath = $rootPath; |
|
| 935 | + } |
|
| 936 | 936 | } |
@@ -863,7 +863,7 @@ discard block |
||
| 863 | 863 | $pos = strpos($str, ' '); |
| 864 | 864 | } |
| 865 | 865 | $before = substr($str, 0, $pos); |
| 866 | - $after = substr($str, $pos + 1); |
|
| 866 | + $after = substr($str, $pos+1); |
|
| 867 | 867 | $str = $before.strtoupper(substr($after, 0, 1)).substr($after, 1); |
| 868 | 868 | } |
| 869 | 869 | |
@@ -881,14 +881,14 @@ discard block |
||
| 881 | 881 | public static function toSingular($str) |
| 882 | 882 | { |
| 883 | 883 | // First, ignore "ss" words (like access). |
| 884 | - if (strpos($str, 'ss', strlen($str) - 2) !== false) { |
|
| 884 | + if (strpos($str, 'ss', strlen($str)-2) !== false) { |
|
| 885 | 885 | return $str; |
| 886 | 886 | } |
| 887 | 887 | |
| 888 | 888 | // Now, let's see if the string ends with s: |
| 889 | - if (strpos($str, 's', strlen($str) - 1) !== false) { |
|
| 889 | + if (strpos($str, 's', strlen($str)-1) !== false) { |
|
| 890 | 890 | // Yes? Let's remove the s. |
| 891 | - return substr($str, 0, strlen($str) - 1); |
|
| 891 | + return substr($str, 0, strlen($str)-1); |
|
| 892 | 892 | } |
| 893 | 893 | |
| 894 | 894 | return $str; |