1 | <?php |
||
2 | |||
3 | namespace ByJG\MicroOrm; |
||
4 | |||
5 | use ByJG\MicroOrm\Exception\OrmModelInvalidException; |
||
6 | |||
7 | class Mapper |
||
8 | { |
||
9 | |||
10 | const FIELDMAP_FIELD = 'fieldname'; |
||
11 | const FIELDMAP_UPDATEMASK = 'updatemask'; |
||
12 | const FIELDMAP_SELECTMASK = 'selectmask'; |
||
13 | |||
14 | private $entity; |
||
15 | private $table; |
||
16 | private $primaryKey; |
||
17 | private $keygenFunction = null; |
||
18 | private $fieldMap = []; |
||
19 | private $fieldAlias = []; |
||
20 | private $preserveCasename = false; |
||
21 | |||
22 | /** |
||
23 | * Mapper constructor. |
||
24 | * |
||
25 | * @param string $entity |
||
26 | * @param string $table |
||
27 | * @param string $primaryKey |
||
28 | * @param \Closure $keygenFunction |
||
29 | * @param bool $preserveCasename |
||
30 | * @throws \ByJG\MicroOrm\Exception\OrmModelInvalidException |
||
31 | */ |
||
32 | public function __construct( |
||
33 | $entity, |
||
34 | $table, |
||
35 | $primaryKey, |
||
36 | 21 | \Closure $keygenFunction = null, |
|
37 | $preserveCasename = false |
||
38 | ) { |
||
39 | if (!class_exists($entity)) { |
||
40 | throw new OrmModelInvalidException("Entity '$entity' does not exists"); |
||
41 | } |
||
42 | $this->entity = $entity; |
||
43 | 21 | $this->table = $table; |
|
44 | $this->preserveCasename = $preserveCasename; |
||
45 | $this->primaryKey = $this->prepareField($primaryKey); |
||
46 | 21 | $this->keygenFunction = $keygenFunction; |
|
47 | 21 | } |
|
48 | 21 | ||
49 | 21 | public function prepareField($field) |
|
50 | 21 | { |
|
51 | 21 | if (!$this->preserveCasename) { |
|
52 | if (!is_array($field)) { |
||
53 | 21 | return strtolower($field); |
|
54 | } |
||
55 | 21 | ||
56 | 21 | $result = []; |
|
57 | 21 | foreach ($field as $key => $value) { |
|
58 | $result[strtolower($key)] = $value; |
||
59 | } |
||
60 | 8 | return $result; |
|
61 | 8 | } |
|
62 | 8 | ||
63 | 8 | return $field; |
|
64 | 8 | } |
|
65 | |||
66 | /** |
||
67 | * @param string $property |
||
68 | * @param string $fieldName |
||
69 | * @param \Closure|null|bool $updateMask |
||
70 | * @param \Closure $selectMask |
||
71 | * @return $this |
||
72 | */ |
||
73 | public function addFieldMap($property, $fieldName, \Closure $updateMask = null, \Closure $selectMask = null) |
||
74 | { |
||
75 | if (empty($selectMask)) { |
||
76 | $selectMask = Mapper::defaultClosure(); |
||
77 | 21 | } |
|
78 | |||
79 | 21 | if (empty($updateMask)) { |
|
80 | 21 | $updateMask = Mapper::defaultClosure(); |
|
81 | 21 | } |
|
82 | |||
83 | 21 | if (!is_null($updateMask) && !($updateMask instanceof \Closure)) { |
|
84 | 21 | throw new InvalidArgumentException('UpdateMask must be a \Closure or NULL'); |
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
85 | 21 | } |
|
86 | |||
87 | 21 | $this->fieldMap[$this->prepareField($property)] = [ |
|
88 | 21 | self::FIELDMAP_FIELD => $this->prepareField($fieldName), |
|
89 | 21 | self::FIELDMAP_UPDATEMASK => $updateMask, |
|
90 | 21 | self::FIELDMAP_SELECTMASK => $selectMask |
|
91 | 21 | ]; |
|
92 | |||
93 | 21 | return $this; |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * @param $fieldName |
||
98 | * @param $alias |
||
99 | */ |
||
100 | 4 | public function addFieldAlias($fieldName, $alias) |
|
101 | { |
||
102 | 4 | $this->fieldAlias[$this->prepareField($fieldName)] = $this->prepareField($alias); |
|
103 | 4 | } |
|
104 | |||
105 | /** |
||
106 | * @return string |
||
107 | */ |
||
108 | 21 | public function getEntity() |
|
109 | { |
||
110 | 21 | $class = $this->entity; |
|
111 | 21 | return new $class(); |
|
112 | } |
||
113 | |||
114 | /** |
||
115 | * @return string |
||
116 | */ |
||
117 | 20 | public function getTable() |
|
118 | { |
||
119 | 20 | return $this->table; |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * @return string |
||
124 | */ |
||
125 | 14 | public function getPrimaryKey() |
|
126 | { |
||
127 | 14 | return $this->primaryKey; |
|
128 | } |
||
129 | |||
130 | /** |
||
131 | * @return bool |
||
132 | */ |
||
133 | public function isPreserveCasename() |
||
134 | { |
||
135 | return $this->preserveCasename; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @param string|null $property |
||
140 | * @param string|null $key |
||
141 | * @return array |
||
142 | */ |
||
143 | 21 | public function getFieldMap($property = null, $key = null) |
|
144 | { |
||
145 | 21 | if (empty($property)) { |
|
146 | 21 | return $this->fieldMap; |
|
147 | } |
||
148 | |||
149 | 1 | $property = $this->prepareField($property); |
|
150 | |||
151 | 1 | if (!isset($this->fieldMap[$property])) { |
|
152 | 1 | return null; |
|
153 | } |
||
154 | |||
155 | 1 | $fieldMap = $this->fieldMap[$property]; |
|
156 | |||
157 | 1 | if (empty($key)) { |
|
158 | return $fieldMap; |
||
159 | } |
||
160 | |||
161 | 1 | return $fieldMap[$key]; |
|
162 | } |
||
163 | |||
164 | 21 | public function getFieldAlias($fieldName = null) |
|
165 | { |
||
166 | 21 | if (empty($fieldName)) { |
|
167 | 21 | return $this->fieldAlias; |
|
168 | } |
||
169 | |||
170 | 1 | if (!isset($this->fieldAlias[$fieldName])) { |
|
171 | 1 | return null; |
|
172 | } |
||
173 | |||
174 | 1 | return $this->fieldAlias[$fieldName]; |
|
175 | } |
||
176 | |||
177 | /** |
||
178 | * @return mixed|null |
||
179 | */ |
||
180 | 5 | public function generateKey() |
|
181 | { |
||
182 | 5 | if (empty($this->keygenFunction)) { |
|
183 | 4 | return null; |
|
184 | } |
||
185 | |||
186 | 1 | $func = $this->keygenFunction; |
|
187 | |||
188 | 1 | return $func(); |
|
189 | } |
||
190 | |||
191 | 21 | public static function defaultClosure() |
|
192 | { |
||
193 | return function ($value) { |
||
194 | 9 | if (empty($value)) { |
|
195 | 21 | return null; |
|
196 | } |
||
197 | return $value; |
||
198 | }; |
||
199 | } |
||
200 | 2 | ||
201 | 2 | public static function doNotUpdateClosure() |
|
202 | 2 | { |
|
203 | return function () { |
||
204 | return false; |
||
205 | }; |
||
206 | } |
||
207 | } |
||
208 |