| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Created by PhpStorm. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * User: jg | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Date: 21/06/16 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Time: 13:55 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace ByJG\MicroOrm; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class Mapper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     const FIELDMAP_FIELD = 'fieldname'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     const FIELDMAP_UPDATEMASK = 'updatemask'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     const FIELDMAP_SELECTMASK = 'selectmask'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     private $entity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private $table; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     private $primaryKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     private $keygenFunction = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $fieldMap = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     private $fieldAlias = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * Mapper constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @param string $entity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @param string $table | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param string $primaryKey | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param \Closure $keygenFunction | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 21 |  |     public function __construct($entity, $table, $primaryKey, \Closure $keygenFunction = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 21 |  |         if (!class_exists($entity)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             throw new \Exception("Entity '$entity' does not exists"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 21 |  |         $this->entity = $entity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 21 |  |         $this->table = $table; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 21 |  |         $this->primaryKey = $primaryKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 21 |  |         $this->keygenFunction = $keygenFunction; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 21 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param string $property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param string $fieldName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @param \Closure|null|bool $updateMask | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @param \Closure $selectMask | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 21 |  |     public function addFieldMap($property, $fieldName, $updateMask = false, \Closure $selectMask = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 21 |  |         if (empty($selectMask)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 21 |  |             $selectMask = Mapper::defaultClosure(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 21 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 21 |  |         if ($updateMask === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 21 |  |             $updateMask = Mapper::defaultClosure(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 21 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 21 |  |         if (!is_null($updateMask) && !($updateMask instanceof \Closure)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             throw new \InvalidArgumentException('UpdateMask must be a \Closure or NULL'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 21 |  |         $this->fieldMap[$property] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 21 |  |             self::FIELDMAP_FIELD => $fieldName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 21 |  |             self::FIELDMAP_UPDATEMASK => $updateMask, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 21 |  |             self::FIELDMAP_SELECTMASK => $selectMask | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 21 |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 21 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @param $fieldName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @param $alias | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 4 |  |     public function addFieldAlias($fieldName, $alias) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 4 |  |         $this->fieldAlias[$fieldName] = $alias; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 21 |  |     public function getEntity() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 21 |  |         $class = $this->entity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 21 |  |         return new $class(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 20 |  |     public function getTable() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 20 |  |         return $this->table; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 14 |  |     public function getPrimaryKey() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 14 |  |         return $this->primaryKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @param string|null $property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @param string|null $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 21 |  |     public function getFieldMap($property = null, $key = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 21 |  |         if (empty($property)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 21 |  |             return $this->fieldMap; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 1 |  |         if (!isset($this->fieldMap[$property])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 1 |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 1 |  |         $fieldMap = $this->fieldMap[$property]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 1 |  |         if (empty($key)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             return $fieldMap; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 1 |  |         return $fieldMap[$key]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 21 |  |     public function getFieldAlias($fieldName = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 21 |  |         if (empty($fieldName)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 21 |  |             return $this->fieldAlias; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 1 |  |         if (!isset($this->fieldAlias[$fieldName])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 1 |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 1 |  |         return $this->fieldAlias[$fieldName]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * @return mixed|null | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 148 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 149 | 5 |  |     public function generateKey() | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 151 | 5 |  |         if (empty($this->keygenFunction)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 152 | 4 |  |             return null; | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 155 | 1 |  |         $func = $this->keygenFunction; | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 157 | 1 |  |         return $func(); | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     public static function defaultClosure() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 | 21 |  |         return function ($value, $instance) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 | 9 |  |             return $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 | 21 |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 166 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 167 |  |  |  | 
            
                        
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.