These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 | private $preserveCasename = false; |
||
25 | |||
26 | /** |
||
27 | * Mapper constructor. |
||
28 | * |
||
29 | * @param string $entity |
||
30 | * @param string $table |
||
31 | * @param string $primaryKey |
||
32 | * @param \Closure $keygenFunction |
||
33 | * @param bool $preserveCasename |
||
34 | * @throws \Exception |
||
35 | */ |
||
36 | 21 | public function __construct($entity, $table, $primaryKey, \Closure $keygenFunction = null, $preserveCasename = false) |
|
37 | { |
||
38 | 21 | if (!class_exists($entity)) { |
|
39 | throw new \Exception("Entity '$entity' does not exists"); |
||
40 | } |
||
41 | 21 | $this->entity = $entity; |
|
42 | 21 | $this->table = $table; |
|
43 | 21 | $this->preserveCasename = $preserveCasename; |
|
44 | 21 | $this->primaryKey = $this->prepareField($primaryKey); |
|
45 | 21 | $this->keygenFunction = $keygenFunction; |
|
46 | 21 | } |
|
47 | |||
48 | 21 | public function prepareField($field) |
|
49 | { |
||
50 | 21 | if (!$this->preserveCasename) { |
|
51 | 21 | if (!is_array($field)) { |
|
52 | 21 | return strtolower($field); |
|
53 | } |
||
54 | |||
55 | 8 | $result = []; |
|
56 | 8 | foreach ($field as $key => $value) { |
|
57 | 8 | $result[strtolower($key)] = $value; |
|
58 | 8 | } |
|
59 | 8 | return $result; |
|
60 | } |
||
61 | |||
62 | return $field; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @param string $property |
||
67 | * @param string $fieldName |
||
68 | * @param \Closure|null|bool $updateMask |
||
69 | * @param \Closure $selectMask |
||
70 | * @return $this |
||
71 | */ |
||
72 | 21 | public function addFieldMap($property, $fieldName, $updateMask = false, \Closure $selectMask = null) |
|
73 | { |
||
74 | 21 | if (empty($selectMask)) { |
|
75 | 21 | $selectMask = Mapper::defaultClosure(); |
|
76 | 21 | } |
|
77 | |||
78 | 21 | if ($updateMask === false) { |
|
79 | 21 | $updateMask = Mapper::defaultClosure(); |
|
80 | 21 | } |
|
81 | |||
82 | 21 | if (!is_null($updateMask) && !($updateMask instanceof \Closure)) { |
|
83 | throw new \InvalidArgumentException('UpdateMask must be a \Closure or NULL'); |
||
84 | } |
||
85 | |||
86 | 21 | $this->fieldMap[$this->prepareField($property)] = [ |
|
87 | 21 | self::FIELDMAP_FIELD => $this->prepareField($fieldName), |
|
88 | 21 | self::FIELDMAP_UPDATEMASK => $updateMask, |
|
89 | 21 | self::FIELDMAP_SELECTMASK => $selectMask |
|
90 | 21 | ]; |
|
91 | |||
92 | 21 | return $this; |
|
93 | } |
||
94 | |||
95 | /** |
||
96 | * @param $fieldName |
||
97 | * @param $alias |
||
98 | */ |
||
99 | 4 | public function addFieldAlias($fieldName, $alias) |
|
100 | { |
||
101 | 4 | $this->fieldAlias[$this->prepareField($fieldName)] = $this->prepareField($alias); |
|
102 | 4 | } |
|
103 | |||
104 | /** |
||
105 | * @return string |
||
106 | */ |
||
107 | 21 | public function getEntity() |
|
108 | { |
||
109 | 21 | $class = $this->entity; |
|
110 | 21 | return new $class(); |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * @return string |
||
115 | */ |
||
116 | 20 | public function getTable() |
|
117 | { |
||
118 | 20 | return $this->table; |
|
119 | } |
||
120 | |||
121 | /** |
||
122 | * @return string |
||
123 | */ |
||
124 | 14 | public function getPrimaryKey() |
|
125 | { |
||
126 | 14 | return $this->primaryKey; |
|
127 | } |
||
128 | |||
129 | /** |
||
130 | * @return bool |
||
131 | */ |
||
132 | public function isPreserveCasename() |
||
133 | { |
||
134 | return $this->preserveCasename; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @param string|null $property |
||
139 | * @param string|null $key |
||
140 | * @return array |
||
141 | */ |
||
142 | 21 | public function getFieldMap($property = null, $key = null) |
|
143 | { |
||
144 | 21 | if (empty($property)) { |
|
145 | 21 | return $this->fieldMap; |
|
146 | } |
||
147 | |||
148 | 1 | $property = $this->prepareField($property); |
|
149 | |||
150 | 1 | if (!isset($this->fieldMap[$property])) { |
|
151 | 1 | return null; |
|
152 | } |
||
153 | |||
154 | 1 | $fieldMap = $this->fieldMap[$property]; |
|
155 | |||
156 | 1 | if (empty($key)) { |
|
157 | return $fieldMap; |
||
158 | } |
||
159 | |||
160 | 1 | return $fieldMap[$key]; |
|
161 | } |
||
162 | |||
163 | 21 | public function getFieldAlias($fieldName = null) |
|
164 | { |
||
165 | 21 | if (empty($fieldName)) { |
|
166 | 21 | return $this->fieldAlias; |
|
167 | } |
||
168 | |||
169 | 1 | if (!isset($this->fieldAlias[$fieldName])) { |
|
170 | 1 | return null; |
|
171 | } |
||
172 | |||
173 | 1 | return $this->fieldAlias[$fieldName]; |
|
174 | } |
||
175 | |||
176 | /** |
||
177 | * @return mixed|null |
||
178 | */ |
||
179 | 5 | public function generateKey() |
|
180 | { |
||
181 | 5 | if (empty($this->keygenFunction)) { |
|
182 | 4 | return null; |
|
183 | } |
||
184 | |||
185 | 1 | $func = $this->keygenFunction; |
|
186 | |||
187 | 1 | return $func(); |
|
188 | } |
||
189 | |||
190 | public static function defaultClosure() |
||
191 | { |
||
192 | 21 | return function ($value, $instance) { |
|
0 ignored issues
–
show
|
|||
193 | 9 | return $value; |
|
194 | 21 | }; |
|
195 | } |
||
196 | } |
||
197 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.