| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace TheCodingMachine\TDBM\Utils; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Doctrine\DBAL\Platforms\MySQL57Platform; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Doctrine\DBAL\Schema\Column; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Doctrine\DBAL\Schema\Table; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Doctrine\DBAL\Types\Type; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use TheCodingMachine\TDBM\TDBMException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use TheCodingMachine\TDBM\Utils\Annotation\AnnotationParser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use TheCodingMachine\TDBM\Utils\Annotation\Annotations; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use \TheCodingMachine\TDBM\Utils\Annotation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Zend\Code\Generator\AbstractMemberGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Zend\Code\Generator\DocBlock\Tag\ParamTag; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Zend\Code\Generator\DocBlock\Tag\ReturnTag; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Zend\Code\Generator\MethodGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Zend\Code\Generator\ParameterGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * This class represent a property in a bean (a property has a getter, a setter, etc...). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | class ScalarBeanPropertyDescriptor extends AbstractBeanPropertyDescriptor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @var Column | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     private $column; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @var Annotations | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     private $annotations; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @var AnnotationParser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     private $annotationParser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * ScalarBeanPropertyDescriptor constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @param Table $table | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @param Column $column | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param NamingStrategyInterface $namingStrategy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     public function __construct(Table $table, Column $column, NamingStrategyInterface $namingStrategy, AnnotationParser $annotationParser) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         parent::__construct($table, $namingStrategy); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $this->table = $table; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $this->column = $column; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $this->annotationParser = $annotationParser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * Returns the name of the class linked to this property or null if this is not a foreign key. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @return null|string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     public function getClassName(): ?string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * Returns the PHP type for the property (it can be a scalar like int, bool, or class names, like \DateTimeInterface, App\Bean\User....) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     public function getPhpType(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $type = $this->column->getType(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         return TDBMDaoGenerator::dbalTypeToPhpType($type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * Returns true if the property is compulsory (and therefore should be fetched in the constructor). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     public function isCompulsory(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         return $this->column->getNotnull() && !$this->isAutoincrement() && $this->column->getDefault() === null && !$this->hasUuidAnnotation(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     private function isAutoincrement() : bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         return $this->column->getAutoincrement() || $this->getAutoincrementAnnotation() !== null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     private function hasUuidAnnotation(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         return $this->getUuidAnnotation() !== null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     private function getUuidAnnotation(): ?Annotation\UUID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         /** @var Annotation\UUID $annotation */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $annotation = $this->getAnnotations()->findAnnotation(Annotation\UUID::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         return $annotation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     private function getAutoincrementAnnotation(): ?Annotation\Autoincrement | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         /** @var Annotation\Autoincrement $annotation */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $annotation = $this->getAnnotations()->findAnnotation(Annotation\Autoincrement::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         return $annotation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     private function getAnnotations(): Annotations | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         if ($this->annotations === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             $this->annotations = $this->annotationParser->getColumnAnnotations($this->column, $this->table); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         return $this->annotations; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * Returns true if the property has a default value (or if the @UUID annotation is set for the column) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * @return bool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 121 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |     public function hasDefault(): bool | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |         // MariaDB 10.3 issue: it returns "NULL" (the string) instead of *null* | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |         return ($this->column->getDefault() !== null && $this->column->getDefault() !== 'NULL') || $this->hasUuidAnnotation(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * Returns the code that assigns a value to its default value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     public function assignToDefaultCode(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         $str = '$this->%s(%s);'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $uuidAnnotation = $this->getUuidAnnotation(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         if ($uuidAnnotation !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             $defaultCode = $this->getUuidCode($uuidAnnotation); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             $default = $this->column->getDefault(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |             $type = $this->column->getType(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             if (in_array($type->getName(), [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |                 'datetime', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |                 'datetime_immutable', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |                 'datetimetz', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |                 'datetimetz_immutable', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |                 'date', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |                 'date_immutable', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |                 'time', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |                 'time_immutable', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |             ], true)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |                 if ($default !== null && in_array(strtoupper($default), ['CURRENT_TIMESTAMP' /* MySQL */, 'NOW()' /* PostgreSQL */, 'SYSDATE' /* Oracle */ , 'CURRENT_TIMESTAMP()' /* MariaDB 10.3 */], true)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |                     $defaultCode = 'new \DateTimeImmutable()'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |                     throw new TDBMException('Unable to set default value for date in "'.$this->table->getName().'.'.$this->column->getName().'". Database passed this default value: "'.$default.'"'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |                 $defaultValue = $type->convertToPHPValue($this->column->getDefault(), new MySQL57Platform()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |                 $defaultCode = var_export($defaultValue, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         return sprintf($str, $this->getSetterName(), $defaultCode); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     private function getUuidCode(Annotation\UUID $uuidAnnotation): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         $comment = $uuidAnnotation->value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         switch ($comment) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |             case '': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |             case 'v1': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |                 return 'Uuid::uuid1()->toString()'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |             case 'v4': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |                 return 'Uuid::uuid4()->toString()'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |                 throw new TDBMException('@UUID annotation accepts either "v1" or "v4" parameter. Unexpected parameter: ' . $comment); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |      * Returns true if the property is the primary key. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |     public function isPrimaryKey(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |         $primaryKey = $this->table->getPrimaryKey(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |         if ($primaryKey === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |         return in_array($this->column->getName(), $primaryKey->getUnquotedColumns()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |      * Returns the PHP code for getters and setters. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |      * @return MethodGenerator[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |     public function getGetterSetterCode(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |         $normalizedType = $this->getPhpType(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |         $columnGetterName = $this->getGetterName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |         $columnSetterName = $this->getSetterName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |         // A column type can be forced if it is not nullable and not auto-incrementable (for auto-increment columns, we can get "null" as long as the bean is not saved). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |         $isNullable = !$this->column->getNotnull() || $this->isAutoincrement(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |         $resourceTypeCheck = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |         if ($normalizedType === 'resource') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |             $resourceTypeCheck .= <<<EOF | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  | if (!\is_resource($%s)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |     throw \TheCodingMachine\TDBM\TDBMInvalidArgumentException::badType('resource', $%s, __METHOD__); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  | EOF; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |             $resourceTypeCheck = sprintf($resourceTypeCheck, $this->column->getName(), $this->column->getName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |         $paramType = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |         if ($this->isTypeHintable()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |             $paramType = ($isNullable?'?':'').$normalizedType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |         $getter = new MethodGenerator($columnGetterName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |         $getter->setDocBlock(sprintf('The getter for the "%s" column.', $this->column->getName())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |         $types = [ $normalizedType ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |         if ($isNullable) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |             $types[] = 'null'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |         $getter->getDocBlock()->setTag(new ReturnTag($types)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |         $getter->setReturnType($paramType); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |         $getter->setBody(sprintf('return $this->get(%s, %s);', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |             var_export($this->column->getName(), true), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |             var_export($this->table->getName(), true))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |         if ($this->isGetterProtected()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |             $getter->setVisibility(AbstractMemberGenerator::VISIBILITY_PROTECTED); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  |         $setter = new MethodGenerator($columnSetterName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  |         $setter->setDocBlock(sprintf('The setter for the "%s" column.', $this->column->getName())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  |         $types = [ $normalizedType ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |         if ($isNullable) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  |             $types[] = 'null'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  |         $setter->getDocBlock()->setTag(new ParamTag($this->column->getName(), $types)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  |         $parameter = new ParameterGenerator($this->column->getName(), $paramType); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  |         $setter->setParameter($parameter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |         $setter->setReturnType('void'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |         $setter->setBody(sprintf('%s | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  | $this->set(%s, $%s, %s);', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  |             $resourceTypeCheck, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |             var_export($this->column->getName(), true), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |             $this->column->getName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  |             var_export($this->table->getName(), true))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  |         if ($this->isSetterProtected()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  |             $setter->setVisibility(AbstractMemberGenerator::VISIBILITY_PROTECTED); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  |         return [$getter, $setter]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  |      * Returns the part of code useful when doing json serialization. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  |     public function getJsonSerializeCode(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  |         if ($this->findAnnotation(Annotation\JsonIgnore::class)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |             return ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  |         if (!$this->canBeSerialized()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  |             return ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |         // Do not export the property is the getter is protected. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  |         if ($this->isGetterProtected()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  |             return ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  |         /** @var Annotation\JsonKey|null $jsonKey */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  |         $jsonKey = $this->findAnnotation(Annotation\JsonKey::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  |         $index = $jsonKey ? $jsonKey->key : $this->namingStrategy->getJsonProperty($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 |  |  |         $getter = $this->getGetterName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  |         switch ($this->getPhpType()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  |             case '\\DateTimeImmutable': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 |  |  |                 /** @var Annotation\JsonFormat|null $jsonFormat */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 |  |  |                 $jsonFormat = $this->findAnnotation(Annotation\JsonFormat::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  |                 $format = $jsonFormat ? $jsonFormat->datetime : 'c'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  |                 if ($this->column->getNotnull()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 |  |  |                     return "\$array['$index'] = \$this->$getter()->format('$format');"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  |                     return "\$array['$index'] = (\$date = \$this->$getter()) ? \$date->format('$format') : null;"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  |             case 'int': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  |             case 'float': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 |  |  |                 /** @var Annotation\JsonFormat|null $jsonFormat */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  |                 $jsonFormat = $this->findAnnotation(Annotation\JsonFormat::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 |  |  |                 if ($jsonFormat) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 |  |  |                     $args = [$jsonFormat->decimals, $jsonFormat->point, $jsonFormat->separator]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 |  |  |                     for ($i = 2; $i >= 0; --$i) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 |  |  |                         if ($args[$i] === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 316 |  |  |                             unset($args[$i]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 |  |  |                         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 318 |  |  |                             break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 319 |  |  |                         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 320 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 321 |  |  |                     $args = array_map(function ($v) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 322 |  |  |                         return var_export($v, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 323 |  |  |                     }, $args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 324 |  |  |                     $args = empty($args) ? '' : ', ' . implode(', ', $args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 325 |  |  |                     $unit = $jsonFormat->unit ? ' . ' . var_export($jsonFormat->unit, true) : ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 326 |  |  |                     if ($this->column->getNotnull()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 327 |  |  |                         return "\$array['$index'] = number_format(\$this->$getter()$args)$unit;"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 328 |  |  |                     } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 329 |  |  |                         return "\$array['$index'] = \$this->$getter() !== null ? number_format(\$this->$getter()$args)$unit : null;"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 330 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 331 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 332 |  |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 333 |  |  |                 return "\$array['$index'] = \$this->$getter();"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 334 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 335 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 336 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 337 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 338 |  |  |      * Returns the column name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 339 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 340 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 341 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 342 |  |  |     public function getColumnName(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 343 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 344 |  |  |         return $this->column->getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 345 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 346 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 347 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 348 |  |  |      * The code to past in the __clone method. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 349 |  |  |      * @return null|string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 350 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 351 |  |  |     public function getCloneRule(): ?string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 352 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 353 |  |  |         $uuidAnnotation = $this->getUuidAnnotation(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 354 |  |  |         if ($uuidAnnotation !== null && $this->isPrimaryKey()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 355 |  |  |             return sprintf("\$this->%s(%s);\n", $this->getSetterName(), $this->getUuidCode($uuidAnnotation)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 356 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 357 |  |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 358 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 359 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 360 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 361 |  |  |      * tells is this type is suitable for Json Serialization | 
            
                                                                                                            
                            
            
                                    
            
            
                | 362 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 363 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 364 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 365 |  |  |     public function canBeSerialized() : bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 366 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 367 |  |  |         $type = $this->column->getType(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 368 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 369 |  |  |         $unserialisableTypes = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 370 |  |  |             Type::BLOB, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 371 |  |  |             Type::BINARY | 
            
                                                                                                            
                            
            
                                    
            
            
                | 372 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 373 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 374 |  |  |         return \in_array($type->getName(), $unserialisableTypes, true) === false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 375 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 376 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 377 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 378 |  |  |      * Tells if this property is a type-hintable in PHP (resource isn't for example) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 379 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 380 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 381 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 382 |  |  |     public function isTypeHintable() : bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 383 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 384 |  |  |         $type = $this->getPhpType(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 385 |  |  |         $invalidScalarTypes = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 386 |  |  |             'resource' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 387 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 388 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 389 |  |  |         return \in_array($type, $invalidScalarTypes, true) === false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 390 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 391 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 392 |  |  |     private function isGetterProtected(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 393 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 394 |  |  |         return $this->findAnnotation(Annotation\ProtectedGetter::class) !== null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 395 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 396 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 397 |  |  |     private function isSetterProtected(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 398 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 399 |  |  |         return $this->findAnnotation(Annotation\ProtectedSetter::class) !== null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 400 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 401 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 402 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 403 |  |  |      * @param string $type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 404 |  |  |      * @return null|object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 405 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 406 |  |  |     private function findAnnotation(string $type) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 407 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 408 |  |  |         return $this->getAnnotations()->findAnnotation($type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 409 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 410 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 411 |  |  |  |