| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace HnrAzevedo\Datamanager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | abstract class Datamanager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     use DataTrait, SynchronizeTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     protected ?string $table = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     protected ?string $primary = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     protected array $data = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     protected array $where = [''=> ["1",'=',"1"] ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     public function __set(string $prop,$value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         if(is_array($value)){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |             $attr = array_keys($value)[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |             $this->data[$prop][$attr] = $value[$attr]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |             return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $this->isSettable($prop); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $this->data[$prop]['changed'] = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $this->data[$prop]['value'] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     public function getData(): ?array | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         return $this->data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     public function __get(string $field) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->isSettable($field); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         return $this->data[$field]['value']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     public function getCount(): int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         return $this->count; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     public function except($deniable) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $deniable = (is_array($deniable)) ? $deniable : [$deniable]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         foreach ($deniable as $field) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             if(!array_key_exists($field,$this->data)){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 throw new Exception("{$field} field does not exist in the table {$this->table}."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             $this->excepts[$field] = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     public function deny() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         foreach ($this->excepts as $field => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             unset($this->select[$field]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     public function orderBy(string $field, string $ord = 'ASC') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $this->isSettable( str_replace(['asc','ASC','desc','DESC',' '],'',$field) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $ord = (strpos(strtolower($field),'asc') || strpos(strtolower($field),'desc')) ? '' : $ord; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $this->order = " ORDER BY {$field} {$ord} "; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     public function only($params) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $params = (is_array($params)) ? $params : [$params]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $this->select = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         foreach ($params as $field) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             $this->isSettable($field); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             $this->select[$field] = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $this->select[$this->primary] = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     public function where(array $where) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $this->where['AND'] = (array_key_exists('AND',$this->where)) ?? ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $w = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         foreach ($where as $condition => $values) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             if(!is_array($values)){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                 $w['AND'][] = $values; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             $this->check_where_array($values); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             $w[(is_int($condition) ? 'AND' : $condition)][] = $values; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                         | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $this->where = array_merge($this->where,$w); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     public function limit(string $limit) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $this->limit = $limit; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     public function offset(int $offset) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $this->checkLimit(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         $this->offset = $offset; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     public function result(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         return $this->result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     public function first() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         return  (count($this->result) > 0) ? $this->setByDatabase($this->result[0]) : $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     public function setByDatabase(array $arrayValues) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         $clone = clone $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         $clone->result = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             0 => $this->result[0] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         $clone->count = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         foreach ($arrayValues as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |             $this->isSettable($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |             $clone->data[$key]['value'] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         return $clone; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     public function toJson(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         $string = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         foreach ($this->data as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |             if(gettype($value)==='object'){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |                 $value = $value->getData()[$this->primary]['value']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |             $string .= '"'.$key.'"'.':"'.$value.'",'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         return str_replace(',}', '}', '{'.$string.'}'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |     public function remove(?bool $exec = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         if(!$exec){ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |             $this->clause = 'remove';     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |             return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |         $this->clause = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |         if(count($this->where) == 1){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |             $this->removeById(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |             return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |         $this->delete($this->mountRemove()['where'], substr( $this->mountRemove()['data'] ,0,-1) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |         $this->check_fail(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |              | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |     private function removeById(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |         $delete = $this->delete("{$this->primary}=:{$this->primary}","{$this->primary}={$this->getData()[$this->primary]['value']}"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |         $this->check_fail(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |         return $delete; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |     public function persist() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |         $columns = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |         $values = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |         $data = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |         foreach ($this->data as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |             if(strstr($this->data[$key]['extra'],'auto_increment')){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |             $this->checkMaxlength($key, $value['value'], $value['maxlength']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |             $columns .= $key.','; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |             $values .= ':'.$key.','; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |             $data[$key] = $value['value']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |         $this->transaction('begin'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |         try{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |             | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |             $id = $this->insert($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |             $this->check_fail(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |             $this->getData()[$this->primary]['value'] = $id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |              | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |             $this->transaction('commit'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |         }catch(Exception $er){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |             $this->transaction('rollback'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |             throw $er; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  |     public function toEntity() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  |         if($this->getCount() === 0){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |         $entity = $this->setByDatabase($this->result[0]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |         if(count($this->result) > 1){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  |             $entity = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |             foreach ($this->result as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  |                 $entity[] = $this->setByDatabase($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |         return $entity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |     public function findById($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  |         return $this->where([$this->primary,'=',$id]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  |     public function execute() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  |         if(!is_null($this->clause) && $this->clause == 'remove'){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  |             return $this->remove(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  |         $this->deny(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  |         $this->mountSelect(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  |         $where = substr($this->mountWhereExec()['where'],0,-1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  |         $this->query .= " WHERE {$where} "; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  |         $this->query .= $this->order; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |         | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  |         $this->mountLimit(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  |         $this->mountOffset(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  |         $this->result = $this->select($this->query, $this->mountWhereExec()['data']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  |         $this->check_fail(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  |         $this->count = count($this->result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  |         $this->query = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  |     public function find(?int $key = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  |         $this->query = " SELECT * FROM {$this->table} "; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  |         return (is_int($key)) ? $this->findById($key) : $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  |     public function save() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 |  |  |         $this->transaction('begin'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  |         try{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 |  |  |             $this->update( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  |                 $this->mountSave()['data'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  |                 "{$this->primary}=:{$this->primary}",  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 |  |  |                 $this->primary.'='.$this->getData()[$this->primary]['value'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 |  |  |             $this->check_fail(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 |  |  |             $this->transaction('commit'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 316 |  |  |         }catch(Exception $er){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 |  |  |             $this->transaction('rollback'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 318 |  |  |             throw $er; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 319 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 320 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 321 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 322 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 323 |  |  |      | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 324 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 325 |  |  |  | 
            
                        
If an expression can have both
false, andnullas possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.