| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Nip\Records\Traits\Unique; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Nip\Records\AbstractModels\Record; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Class RecordsTrait | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @package Nip\Records\Traits\Unique | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | trait RecordsTrait | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     protected $uniqueFields = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @param Record $item | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @return bool|false|Record | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 | 1 |  |     public function exists(Record $item) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 1 |  |         $params = $this->generateExistsParams($item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 1 |  |         if (!$params) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 1 |  |         $where = $params['where']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 1 |  |         $uniqueWhere = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 1 |  |         foreach ($where as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 1 |  |             if (strpos($key, 'UNQ') !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 1 |  |                 $uniqueWhere[] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 1 |  |                 unset($params['where'][$key]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 1 |  |         $params['where'][] = implode(' OR ', $uniqueWhere); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 1 |  |         return $this->findOneByParams($params); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @param Record $item | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @return array|bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 2 |  |     public function generateExistsParams(Record $item) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 2 |  |         $conditions = $this->generateUniqueConditions($item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 2 |  |         if (count($conditions) < 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 2 |  |         $params = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 2 |  |         $params['where'] = $conditions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 2 |  |         $pk = $this->getPrimaryKey(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 2 |  |         if ($item->getPrimaryKey()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             $params['where'][] = ["$pk != ?", $item->getPrimaryKey()]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 2 |  |         return $params; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @param Record $item | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @return array|bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 2 |  |     public function generateUniqueConditions(Record $item) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 2 |  |         $uniqueFields = $this->getUniqueFields(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 2 |  |         $conditions = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 2 |  |         foreach ($uniqueFields as $uniqueName => $fields) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 2 |  |             $conditions[$uniqueName . '-UNQ'] = $this->generateUniqueCondition($item, $fields); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 2 |  |         return $conditions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @param Record $item | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @param $fields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 81 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 82 | 2 |  |     protected function generateUniqueCondition(Record $item, $fields) | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 84 | 2 |  |         $conditions = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 85 | 2 |  |         foreach ($fields as $field) { | 
            
                                                                        
                            
            
                                    
            
            
                | 86 | 2 |  |             $conditions[] = "`$field` = '{$item->{$field}}'"; | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 88 | 2 |  |         return implode(' AND ', $conditions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @return null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 3 |  |     public function getUniqueFields() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 3 |  |         if ($this->uniqueFields === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 3 |  |             $this->initUniqueFields(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 3 |  |         return $this->uniqueFields; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @return array|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 3 |  |     public function initUniqueFields() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 3 |  |         $this->uniqueFields = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 3 |  |         $structure = $this->getTableStructure(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 3 |  |         foreach ($structure['indexes'] as $name => $index) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 3 |  |             if ($index['unique'] && $name != 'PRIMARY') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 3 |  |                 $this->uniqueFields[$name] = $index['fields']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 3 |  |         return $this->uniqueFields; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 118 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 119 |  |  |  | 
            
                        
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.