| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Created by PhpStorm. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * User: VITALYIEGOROV | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Date: 09.12.15 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Time: 14:34 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | namespace samsoncms\api; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use samsonframework\orm\DatabaseInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * Entity classes generator. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @package samsoncms\api | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class Generator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** @var DatabaseInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     protected $database; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * Transliterate string to english. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @param string $string Source string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @return string Transliterated string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     protected function transliterated($string) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         return str_replace( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             ' ', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             ucwords(iconv("UTF-8", "UTF-8//IGNORE", strtr($string, array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                             "'" => "", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                             "`" => "", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                             "-" => " ", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |                             "_" => " ", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                             "а" => "a", "А" => "a", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                             "б" => "b", "Б" => "b", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                             "в" => "v", "В" => "v", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                             "г" => "g", "Г" => "g", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                             "д" => "d", "Д" => "d", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                             "е" => "e", "Е" => "e", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                             "ж" => "zh", "Ж" => "zh", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                             "з" => "z", "З" => "z", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                             "и" => "i", "И" => "i", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                             "й" => "y", "Й" => "y", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                             "к" => "k", "К" => "k", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                             "л" => "l", "Л" => "l", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                             "м" => "m", "М" => "m", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                             "н" => "n", "Н" => "n", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                             "о" => "o", "О" => "o", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                             "п" => "p", "П" => "p", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                             "р" => "r", "Р" => "r", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                             "с" => "s", "С" => "s", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                             "т" => "t", "Т" => "t", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                             "у" => "u", "У" => "u", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                             "ф" => "f", "Ф" => "f", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                             "х" => "h", "Х" => "h", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                             "ц" => "c", "Ц" => "c", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                             "ч" => "ch", "Ч" => "ch", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                             "ш" => "sh", "Ш" => "sh", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                             "щ" => "sch", "Щ" => "sch", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                             "ъ" => "", "Ъ" => "", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                             "ы" => "y", "Ы" => "y", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                             "ь" => "", "Ь" => "", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                             "э" => "e", "Э" => "e", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                             "ю" => "yu", "Ю" => "yu", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                             "я" => "ya", "Я" => "ya", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                             "і" => "i", "І" => "i", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                             "ї" => "yi", "Ї" => "yi", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                             "є" => "e", "Є" => "e" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                         ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * Get class constant name by its value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @param string $value Constant value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @param string $className Class name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @return string Full constant name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     protected function constantNameByValue($value, $className = Field::ENTITY) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         // Get array where class constants are values and their values are keys | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $nameByValue = array_flip((new \ReflectionClass($className))->getConstants()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         // Try to find constant by its value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         if (isset($nameByValue[$value])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             // Return constant name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             return $nameByValue[$value]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * Get correct entity name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @param string $navigationName Original navigation entity name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @return string Correct PHP-supported entity name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     protected function entityName($navigationName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         return ucfirst($this->transliterated($navigationName)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * Get correct full entity name with name space. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @param string $navigationName Original navigation entity name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @param string $namespace Namespace | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @return string Correct PHP-supported entity name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     protected function fullEntityName($navigationName, $namespace = __NAMESPACE__) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         return '\\'.$namespace.'\\'.$this->entityName($navigationName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * Get correct field name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * @param string $fieldName Original field name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * @return string Correct PHP-supported field name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     protected function fieldName($fieldName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         return $fieldName = lcfirst($this->transliterated($fieldName)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * Get additional field type in form of Field constant name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * by database additional field type identifier. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * @param integer $fieldType Additional field type identifier | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      * @return string Additional field type constant | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     protected function additionalFieldType($fieldType) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         return 'Field::'.$this->constantNameByValue($fieldType); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      * Generate Query::where() analog for specific field. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * @param string $fieldName Field name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * @param string $fieldId Field primary identifier | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * @param string $fieldType Field PHP type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * @return string Generated PHP method code | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     protected function generateFieldConditionMethod($fieldName, $fieldId, $fieldType) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         $code = "\n\t" . '/**'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         $code .= "\n\t" . ' * Add '.$fieldName.'(#' . $fieldId . ') field query condition.'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         $code .= "\n\t" . ' * @param '.Field::phpType($fieldType).' $value Field value'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         $code .= "\n\t" . ' * @return self Chaining'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         $code .= "\n\t" . ' * @see Generic::where()'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         $code .= "\n\t" . ' */'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         $code .= "\n\t" . 'public function ' . $fieldName . '($value)'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         $code .= "\n\t" . "{"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         $code .= "\n\t\t" . 'return $this->where("'.$fieldName.'", $value);'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         return $code . "\n\t" . "}"."\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |      * Create entity PHP class code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |      * @param string $navigationName Original entity name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |      * @param string $entityName PHP entity name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |      * @param array $navigationFields Collection of entity additional fields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |      * @return string Generated entity query PHP class code | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 174 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 175 |  |  |     protected function createEntityClass($navigationName, $entityName, $navigationFields) | 
            
                                                                        
                            
            
                                    
            
            
                | 176 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 177 |  |  |         $class = "\n\n" . '/** Class for getting "'.$navigationName.'" instances from database */'; | 
            
                                                                        
                            
            
                                    
            
            
                | 178 |  |  |         $class .= "\n" . 'class ' . $entityName . ' extends Entity'; | 
            
                                                                        
                            
            
                                    
            
            
                | 179 |  |  |         $class .= "\n" . '{'; | 
            
                                                                        
                            
            
                                    
            
            
                | 180 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 181 |  |  |         // Iterate additional fields | 
            
                                                                        
                            
            
                                    
            
            
                | 182 |  |  |         $constants = ''; | 
            
                                                                        
                            
            
                                    
            
            
                | 183 |  |  |         $variables = ''; | 
            
                                                                        
                            
            
                                    
            
            
                | 184 |  |  |         foreach ($navigationFields as $fieldID => $fieldRow) { | 
            
                                                                        
                            
            
                                    
            
            
                | 185 |  |  |             $fieldName = $this->fieldName($fieldRow['Name']); | 
            
                                                                        
                            
            
                                    
            
            
                | 186 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 187 |  |  |             $constants .= "\n\t" . '/** ' . Field::phpType($fieldRow['Type']) . ' Field #' . $fieldID . ' variable name */'; | 
            
                                                                        
                            
            
                                    
            
            
                | 188 |  |  |             $constants .= "\n\t" . 'const F_' . strtoupper($fieldName) . ' = "'.$fieldName.'";'; | 
            
                                                                        
                            
            
                                    
            
            
                | 189 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 190 |  |  |             $variables .= "\n\t" . '/** @var ' . Field::phpType($fieldRow['Type']) . ' Field #' . $fieldID . '*/'; | 
            
                                                                        
                            
            
                                    
            
            
                | 191 |  |  |             $variables .= "\n\t" . 'public $' . $fieldName . ';'; | 
            
                                                                        
                            
            
                                    
            
            
                | 192 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 193 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 194 |  |  |         $class .= $constants; | 
            
                                                                        
                            
            
                                    
            
            
                | 195 |  |  |         $class .= "\n\t"; | 
            
                                                                        
                            
            
                                    
            
            
                | 196 |  |  |         $class .= "\n\t" . '/** @var string Not transliterated entity name */'; | 
            
                                                                        
                            
            
                                    
            
            
                | 197 |  |  |         $class .= "\n\t" . 'protected static $viewName = "' . $navigationName . '";'; | 
            
                                                                        
                            
            
                                    
            
            
                | 198 |  |  |         $class .= "\n\t"; | 
            
                                                                        
                            
            
                                    
            
            
                | 199 |  |  |         $class .= $variables; | 
            
                                                                        
                            
            
                                    
            
            
                | 200 |  |  |         $class .= "\n" . '}'; | 
            
                                                                        
                            
            
                                    
            
            
                | 201 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 202 |  |  |         return $class; | 
            
                                                                        
                            
            
                                    
            
            
                | 203 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |      * Create entity query PHP class code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |      * @param integer $navigationID Entity navigation identifier | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |      * @param string $navigationName Original entity name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |      * @param string $entityName PHP entity name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |      * @param array $navigationFields Collection of entity additional fields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |      * @return string Generated entity query PHP class code | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |     protected function createQueryClass($navigationID, $navigationName, $entityName, $navigationFields) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |         $class = "\n\n" . '/** Class for getting "'.$navigationName.'" instances from database */'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |         $class .= "\n" . 'class ' . $entityName . ' extends Generic'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |         $class .= "\n" . '{'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |         // Iterate additional fields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |         $localizedFieldIDs = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |         $notLocalizedFieldIDs = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |         $allFieldIDs = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |         $allFieldNames = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |         $allFieldValueColumns = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |         foreach ($navigationFields as $fieldID => $fieldRow) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |             $fieldName = $this->fieldName($fieldRow['Name']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |             $class .= $this->generateFieldConditionMethod( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |                 $fieldName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |                 $fieldRow[Field::F_PRIMARY], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |                 $fieldRow[Field::F_TYPE] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |             // Store field metadata | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |             $allFieldIDs[] = '"' . $fieldID . '" => "' . $fieldName . '"'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |             $allFieldNames[] = '"' . $fieldName . '" => "' . $fieldID . '"'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |             $allFieldValueColumns[] = '"' . $fieldID . '" => "' . Field::valueColumn($fieldRow[Field::F_TYPE]) . '"'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |             if ($fieldRow[Field::F_LOCALIZED] == 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |                 $localizedFieldIDs[] = '"' . $fieldID . '" => "' . $fieldName . '"'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |                 $notLocalizedFieldIDs[] = '"' . $fieldID . '" => "' . $fieldName . '"'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |         $class .= "\n\t"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  |         $class .= "\n\t" . '/** @var string Not transliterated entity name */'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  |         $class .= "\n\t" . 'protected static $identifier = "'.$this->fullEntityName($navigationName).'";'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  |         $class .= "\n\t" . '/** @var array Collection of navigation identifiers */'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  |         $class .= "\n\t" . 'protected static $navigationIDs = array(' . $navigationID . ');'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |         $class .= "\n\t" . '/** @var array Collection of localized additional fields identifiers */'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  |         $class .= "\n\t" . 'protected static $localizedFieldIDs = array(' . "\n\t\t". implode(','."\n\t\t", $localizedFieldIDs) . "\n\t".');'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |         $class .= "\n\t" . '/** @var array Collection of NOT localized additional fields identifiers */'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  |         $class .= "\n\t" . 'protected static $notLocalizedFieldIDs = array(' . "\n\t\t". implode(','."\n\t\t", $notLocalizedFieldIDs) . "\n\t".');'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |         $class .= "\n\t" . '/** @var array Collection of all additional fields identifiers */'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  |         $class .= "\n\t" . 'protected static $fieldIDs = array(' . "\n\t\t". implode(','."\n\t\t", $allFieldIDs) . "\n\t".');'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  |         $class .= "\n\t" . '/** @var array Collection of additional fields value column names */'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |         $class .= "\n\t" . 'protected static $fieldValueColumns = array(' . "\n\t\t". implode(','."\n\t\t", $allFieldValueColumns) . "\n\t".');'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  |         $class .= "\n\t" . '/** @var array Collection of additional field names */'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |         $class .= "\n\t" . 'protected static $fieldNames = array(' . "\n\t\t". implode(','."\n\t\t", $allFieldNames) . "\n\t".');'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |         $class .= "\n" . '}'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |         // Replace tabs with spaces | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |         return $class; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  |     /** @return string Entity state hash */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  |     public function entityHash() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  |         // Получим информацию о всех таблицах из БД | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  |         return md5(serialize($this->database->fetch( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  |             'SELECT `TABLES`.`TABLE_NAME` as `TABLE_NAME` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  |               FROM `information_schema`.`TABLES` as `TABLES` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  |               WHERE `TABLES`.`TABLE_SCHEMA`="' . $this->database->database() . '";' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  |         ))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  |     /** @return array Get collection of navigation objects */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  |     protected function entityNavigations($type = 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  |         return $this->database->fetch(' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |         SELECT * FROM `structure` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  |         WHERE `Active` = "1" AND `Type` = "'.$type.'"' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  |     /** @return array Collection of navigation additional fields */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  |     protected function navigationFields($navigationID) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  |         $return = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  |         // TODO: Optimize queries make one single query with only needed data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  |         foreach ($this->database->fetch('SELECT * FROM `structurefield` WHERE `StructureID` = "' . $navigationID . '" AND `Active` = "1"') as $fieldStructureRow) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 |  |  |             foreach ($this->database->fetch('SELECT * FROM `field` WHERE `FieldID` = "' . $fieldStructureRow['FieldID'] . '"') as $fieldRow) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  |                 $return[$fieldRow['FieldID']] = $fieldRow; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  |         return $return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  |      * Generate entity classes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 |  |  |      * @param string $namespace Base namespace for generated classes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  |      * @return string Generated PHP code for entity classes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 |  |  |     public function createEntityClasses($namespace = __NAMESPACE__) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  |         $classes = "\n" . 'namespace ' . $namespace . ';'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 |  |  |         $classes .= "\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  |         $classes .= "\n" . 'use '.$namespace.'\Field;'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 |  |  |         $classes .= "\n" . 'use '.$namespace.'\query\Generic;'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 |  |  |         // Iterate all structures | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 |  |  |         foreach ($this-> entityNavigations() as $structureRow) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 316 |  |  |             $navigationFields = $this->navigationFields($structureRow['StructureID']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 |  |  |             $entityName = $this->entityName($structureRow['Name']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 318 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 319 |  |  |             $classes .= $this->createEntityClass( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 320 |  |  |                 $structureRow['Name'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 321 |  |  |                 $entityName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 322 |  |  |                 $navigationFields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 323 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 324 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 325 |  |  |             $classes .= $this->createQueryClass( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 326 |  |  |                 $structureRow['StructureID'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 327 |  |  |                 $structureRow['Name'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 328 |  |  |                 $entityName.'Query', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 329 |  |  |                 $navigationFields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 330 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 331 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 332 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 333 |  |  |         // Make correct code formatting | 
            
                                                                                                            
                            
            
                                    
            
            
                | 334 |  |  |         return str_replace("\t", '    ', $classes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 335 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 336 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 337 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 338 |  |  |      * Generator constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 339 |  |  |      * @param DatabaseInterface $database Database instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 340 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 341 |  |  |     public function __construct(DatabaseInterface $database) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 342 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 343 |  |  |         $this->database = $database; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 344 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 345 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 346 |  |  |  | 
            
                        
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.