@@ -12,7 +12,7 @@ discard block |
||
12 | 12 | * |
13 | 13 | * @author d.lanec |
14 | 14 | */ |
15 | -abstract class AbstractOptDataMapper extends AbstractDataMapper{ |
|
15 | +abstract class AbstractOptDataMapper extends AbstractDataMapper { |
|
16 | 16 | |
17 | 17 | protected $soft_delete_key; |
18 | 18 | |
@@ -40,25 +40,25 @@ discard block |
||
40 | 40 | /** |
41 | 41 | * Установка поля для маппинга |
42 | 42 | */ |
43 | - protected function addMappingField($alias,$field = null){ |
|
43 | + protected function addMappingField($alias, $field = null) { |
|
44 | 44 | |
45 | - if(is_string($field)){ |
|
45 | + if (is_string($field)) { |
|
46 | 46 | $field = ['field' => $field]; |
47 | 47 | } |
48 | - elseif(empty($field)){ |
|
48 | + elseif (empty($field)) { |
|
49 | 49 | $field = ['field' => $alias]; |
50 | 50 | } |
51 | - elseif(is_array($field) && !isset($field['field'])){ |
|
52 | - $field['field'] = $alias; |
|
51 | + elseif (is_array($field) && !isset($field['field'])) { |
|
52 | + $field['field'] = $alias; |
|
53 | 53 | } |
54 | 54 | |
55 | 55 | $this->mapping_fields[$alias] = $field; |
56 | 56 | |
57 | - if(isset($field['primary']) && $field['primary']===true){ |
|
57 | + if (isset($field['primary']) && $field['primary']===true) { |
|
58 | 58 | $this->key = $field['field']; |
59 | 59 | } |
60 | 60 | |
61 | - if(isset($field['softdelete']) && $field['softdelete']===true){ |
|
61 | + if (isset($field['softdelete']) && $field['softdelete']===true) { |
|
62 | 62 | $this->soft_delete_key = $field['field']; |
63 | 63 | } |
64 | 64 | |
@@ -96,43 +96,43 @@ discard block |
||
96 | 96 | * @return \Core\Infrastructure\EntityInterface |
97 | 97 | * @throws BadMethodCallException |
98 | 98 | */ |
99 | - protected function buildEntity(EntityInterface $Entity, array $row){ |
|
99 | + protected function buildEntity(EntityInterface $Entity, array $row) { |
|
100 | 100 | |
101 | - foreach ($this->mapping_fields as $alias => $cfg ) { |
|
101 | + foreach ($this->mapping_fields as $alias => $cfg) { |
|
102 | 102 | |
103 | 103 | $value = false; |
104 | 104 | |
105 | 105 | $field = $cfg['field']; |
106 | 106 | |
107 | - $method_set = 'set' . ucfirst($alias); |
|
107 | + $method_set = 'set'.ucfirst($alias); |
|
108 | 108 | |
109 | - if(!method_exists($Entity, $method_set )){ |
|
109 | + if (!method_exists($Entity, $method_set)) { |
|
110 | 110 | throw new BadMethodCallException("Метод {$method_set} не определен"); |
111 | 111 | } |
112 | 112 | |
113 | 113 | //событие на формирование поля |
114 | - if( isset($cfg['build']) && is_object($cfg['build']) ){ |
|
114 | + if (isset($cfg['build']) && is_object($cfg['build'])) { |
|
115 | 115 | $value = call_user_func($cfg['build'], $row); |
116 | 116 | } |
117 | 117 | //на связь |
118 | - elseif(isset($cfg['relation'])){ |
|
118 | + elseif (isset($cfg['relation'])) { |
|
119 | 119 | |
120 | 120 | $mapper = $this->DI->get($cfg['relation']); |
121 | 121 | |
122 | - if($this->use_joins===true){ |
|
122 | + if ($this->use_joins===true) { |
|
123 | 123 | $value = $mapper->createEntity($row); |
124 | 124 | } |
125 | - else{ |
|
126 | - $fkey = isset($cfg['on']) ? $cfg['on'] :$mapper->key; |
|
125 | + else { |
|
126 | + $fkey = isset($cfg['on']) ? $cfg['on'] : $mapper->key; |
|
127 | 127 | $value = $mapper->findBySpecification((new Specification)->setWhere($fkey, $row[$field])); |
128 | 128 | } |
129 | 129 | |
130 | 130 | } |
131 | - elseif(is_string($field) && isset($row[strtolower($field)])){ |
|
131 | + elseif (is_string($field) && isset($row[strtolower($field)])) { |
|
132 | 132 | $value = $row[strtolower($field)]; |
133 | 133 | } |
134 | 134 | |
135 | - if($value!==false) |
|
135 | + if ($value!==false) |
|
136 | 136 | $Entity->{$method_set}($value); |
137 | 137 | |
138 | 138 | } |
@@ -147,27 +147,27 @@ discard block |
||
147 | 147 | * @return \Core\Infrastructure\EntityInterface |
148 | 148 | * @throws BadMethodCallException |
149 | 149 | */ |
150 | - protected function unbuildEntity(EntityInterface $Entity){ |
|
150 | + protected function unbuildEntity(EntityInterface $Entity) { |
|
151 | 151 | |
152 | 152 | $row = []; |
153 | 153 | |
154 | - foreach ($this->mapping_fields as $alias => $cfg ) { |
|
154 | + foreach ($this->mapping_fields as $alias => $cfg) { |
|
155 | 155 | |
156 | 156 | $field = $cfg['field']; |
157 | 157 | |
158 | - $method_get = 'get' . ucfirst($alias); |
|
158 | + $method_get = 'get'.ucfirst($alias); |
|
159 | 159 | |
160 | - if(!method_exists($Entity, $method_get )){ |
|
160 | + if (!method_exists($Entity, $method_get)) { |
|
161 | 161 | throw new BadMethodCallException("Метод {$method_get} не определен"); |
162 | 162 | } |
163 | 163 | |
164 | 164 | //-------------------------------------------------------------------- |
165 | - if( isset($cfg['unbuild']) && is_object($cfg['unbuild']) ){ |
|
166 | - $value = call_user_func($cfg['unbuild'], $Entity->{$method_get}() ); |
|
165 | + if (isset($cfg['unbuild']) && is_object($cfg['unbuild'])) { |
|
166 | + $value = call_user_func($cfg['unbuild'], $Entity->{$method_get}()); |
|
167 | 167 | } |
168 | - elseif(isset($cfg['relation'])){ |
|
168 | + elseif (isset($cfg['relation'])) { |
|
169 | 169 | |
170 | - if(isset($cfg['on'])) |
|
170 | + if (isset($cfg['on'])) |
|
171 | 171 | $fkey = $this->DI->get($cfg['relation'])->getFieldAlias($cfg['on']); |
172 | 172 | else |
173 | 173 | $fkey = 'id'; |
@@ -175,7 +175,7 @@ discard block |
||
175 | 175 | $value = $Entity->{$method_get}()->{'get'.$fkey}(); |
176 | 176 | |
177 | 177 | } |
178 | - else{ |
|
178 | + else { |
|
179 | 179 | $value = $Entity->{$method_get}(); |
180 | 180 | } |
181 | 181 | |
@@ -187,7 +187,7 @@ discard block |
||
187 | 187 | } |
188 | 188 | |
189 | 189 | |
190 | - public function getFieldAlias($field){ |
|
190 | + public function getFieldAlias($field) { |
|
191 | 191 | |
192 | 192 | return $this->mapping_fields_aliases[$field]; |
193 | 193 | |
@@ -196,18 +196,18 @@ discard block |
||
196 | 196 | /** |
197 | 197 | * Построение join-ов |
198 | 198 | */ |
199 | - protected function setRelations(ISpecificationCriteria $Specification){ |
|
199 | + protected function setRelations(ISpecificationCriteria $Specification) { |
|
200 | 200 | |
201 | 201 | $joins = []; |
202 | 202 | |
203 | - foreach ($this->mapping_fields as $field => $cfg){ |
|
204 | - if(isset($cfg['relation'])){ |
|
203 | + foreach ($this->mapping_fields as $field => $cfg) { |
|
204 | + if (isset($cfg['relation'])) { |
|
205 | 205 | |
206 | 206 | $this->relations[$field] = $mapper = $this->DI->get($cfg['relation']); |
207 | 207 | |
208 | 208 | $table = $mapper->getEntityTable(); |
209 | 209 | |
210 | - $relation_key = isset($cfg['on'])? $cfg['on'] : $mapper->key; |
|
210 | + $relation_key = isset($cfg['on']) ? $cfg['on'] : $mapper->key; |
|
211 | 211 | |
212 | 212 | $joins[$table] = [ |
213 | 213 | 'alias' => $field, |
@@ -218,7 +218,7 @@ discard block |
||
218 | 218 | } |
219 | 219 | } |
220 | 220 | |
221 | - if($this->use_joins===true){ |
|
221 | + if ($this->use_joins===true) { |
|
222 | 222 | $Specification->setJoins($joins); |
223 | 223 | } |
224 | 224 | } |
@@ -227,12 +227,12 @@ discard block |
||
227 | 227 | * На успешное сохранение |
228 | 228 | * @param \SimpleORM\EntityInterface $Entity |
229 | 229 | */ |
230 | - protected function onSaveSuccess(EntityInterface $Entity){ |
|
230 | + protected function onSaveSuccess(EntityInterface $Entity) { |
|
231 | 231 | |
232 | 232 | |
233 | 233 | foreach ($this->relations as $alias => $mapper) { |
234 | 234 | $Entity = $Entity->{'get'.$alias}(); |
235 | - if(!$mapper->save($Entity)){ |
|
235 | + if (!$mapper->save($Entity)) { |
|
236 | 236 | throw new \Autoprice\Exceptions\EntityNotSaveException('Unable to save Entity!'); |
237 | 237 | } |
238 | 238 | } |
@@ -247,7 +247,7 @@ discard block |
||
247 | 247 | protected function onDeleteSuccess(EntityInterface $Entity) { |
248 | 248 | foreach ($this->relations as $alias => $mapper) { |
249 | 249 | $Entity = $Entity->{'get'.$alias}(); |
250 | - if(!$mapper->delete($Entity)){ |
|
250 | + if (!$mapper->delete($Entity)) { |
|
251 | 251 | throw new \Autoprice\Exceptions\EntityNotDeleteException('Unable to delete Entity!'); |
252 | 252 | } |
253 | 253 | } |
@@ -44,11 +44,9 @@ discard block |
||
44 | 44 | |
45 | 45 | if(is_string($field)){ |
46 | 46 | $field = ['field' => $field]; |
47 | - } |
|
48 | - elseif(empty($field)){ |
|
47 | + } elseif(empty($field)){ |
|
49 | 48 | $field = ['field' => $alias]; |
50 | - } |
|
51 | - elseif(is_array($field) && !isset($field['field'])){ |
|
49 | + } elseif(is_array($field) && !isset($field['field'])){ |
|
52 | 50 | $field['field'] = $alias; |
53 | 51 | } |
54 | 52 | |
@@ -121,19 +119,18 @@ discard block |
||
121 | 119 | |
122 | 120 | if($this->use_joins===true){ |
123 | 121 | $value = $mapper->createEntity($row); |
124 | - } |
|
125 | - else{ |
|
122 | + } else{ |
|
126 | 123 | $fkey = isset($cfg['on']) ? $cfg['on'] :$mapper->key; |
127 | 124 | $value = $mapper->findBySpecification((new Specification)->setWhere($fkey, $row[$field])); |
128 | 125 | } |
129 | 126 | |
130 | - } |
|
131 | - elseif(is_string($field) && isset($row[strtolower($field)])){ |
|
127 | + } elseif(is_string($field) && isset($row[strtolower($field)])){ |
|
132 | 128 | $value = $row[strtolower($field)]; |
133 | 129 | } |
134 | 130 | |
135 | - if($value!==false) |
|
136 | - $Entity->{$method_set}($value); |
|
131 | + if($value!==false) { |
|
132 | + $Entity->{$method_set}($value); |
|
133 | + } |
|
137 | 134 | |
138 | 135 | } |
139 | 136 | |
@@ -164,18 +161,17 @@ discard block |
||
164 | 161 | //-------------------------------------------------------------------- |
165 | 162 | if( isset($cfg['unbuild']) && is_object($cfg['unbuild']) ){ |
166 | 163 | $value = call_user_func($cfg['unbuild'], $Entity->{$method_get}() ); |
167 | - } |
|
168 | - elseif(isset($cfg['relation'])){ |
|
164 | + } elseif(isset($cfg['relation'])){ |
|
169 | 165 | |
170 | - if(isset($cfg['on'])) |
|
171 | - $fkey = $this->DI->get($cfg['relation'])->getFieldAlias($cfg['on']); |
|
172 | - else |
|
173 | - $fkey = 'id'; |
|
166 | + if(isset($cfg['on'])) { |
|
167 | + $fkey = $this->DI->get($cfg['relation'])->getFieldAlias($cfg['on']); |
|
168 | + } else { |
|
169 | + $fkey = 'id'; |
|
170 | + } |
|
174 | 171 | |
175 | 172 | $value = $Entity->{$method_get}()->{'get'.$fkey}(); |
176 | 173 | |
177 | - } |
|
178 | - else{ |
|
174 | + } else{ |
|
179 | 175 | $value = $Entity->{$method_get}(); |
180 | 176 | } |
181 | 177 |