Passed
Push — master ( 5a733a...de3595 )
by dima
06:55
created
src/AbstractDataMapper.php 1 patch
Spacing   +59 added lines, -59 removed lines patch added patch discarded remove patch
@@ -80,7 +80,7 @@  discard block
 block discarded – undo
80 80
 		
81 81
 		$this->setEntityTable($db_name);
82 82
 		
83
-		if($this->getEntityTable()=='' || $this->getPrimaryKey()==''){
83
+		if ($this->getEntityTable()=='' || $this->getPrimaryKey()=='') {
84 84
 			throw new InvalidEntityPropertyException('Свойства entityTable или key не заданы');
85 85
 		}		
86 86
 		
@@ -92,7 +92,7 @@  discard block
 block discarded – undo
92 92
         return $this->adapter;
93 93
     }
94 94
 
95
-	public function setAdapter(QueryBuilderInterface $adapter){
95
+	public function setAdapter(QueryBuilderInterface $adapter) {
96 96
 		 $this->adapter = $adapter;
97 97
 	}
98 98
 			
@@ -105,13 +105,13 @@  discard block
 block discarded – undo
105 105
 	 * Уставнока таблицы
106 106
 	 */
107 107
 	protected function setEntityTable($db_name) {
108
-		$this->entityTable = !empty($db_name)? "$db_name.".$this->table : $this->table;
108
+		$this->entityTable = !empty($db_name) ? "$db_name.".$this->table : $this->table;
109 109
 	}	
110 110
 
111 111
 
112 112
     public function findById($id)
113 113
     {
114
-		$Criteria = (new Specification())->setWhere($this->key , $id);
114
+		$Criteria = (new Specification())->setWhere($this->key, $id);
115 115
 		
116 116
         return $this->findBySpecification($Criteria);
117 117
     }	
@@ -126,7 +126,7 @@  discard block
 block discarded – undo
126 126
 		$data = $this->unbuildEntity($Entity);
127 127
 	
128 128
 		//protected function onAfterSave(\SimpleORM\EntityInterface $Entity, &$data)
129
-		if(method_exists($this, 'onAfterSave' )) $this->onAfterSave( $Entity, $data );
129
+		if (method_exists($this, 'onAfterSave')) $this->onAfterSave($Entity, $data);
130 130
 		
131 131
 		$id = $data[$this->getPrimaryKey()];
132 132
 		unset($data[$this->getPrimaryKey()]);		
@@ -136,7 +136,7 @@  discard block
 block discarded – undo
136 136
 			
137 137
 			unset($data[$this->setSoftDeleteKey()]);
138 138
 			
139
-			$this->getAdapter()->insert($this->getEntityTable(),$data);
139
+			$this->getAdapter()->insert($this->getEntityTable(), $data);
140 140
 			
141 141
 			if (!$id = $this->getAdapter()->insert_id()) {
142 142
 				return false;
@@ -147,13 +147,13 @@  discard block
 block discarded – undo
147 147
 		//update
148 148
 		else {
149 149
 			
150
-			if(!$this->getAdapter()->update($this->getEntityTable(), $data, "{$this->getPrimaryKey()} = '{$id}'")){
150
+			if (!$this->getAdapter()->update($this->getEntityTable(), $data, "{$this->getPrimaryKey()} = '{$id}'")) {
151 151
 				return false;
152 152
 			}
153 153
 
154 154
 		}		
155 155
 		
156
-		if(method_exists($this, 'onBeforeSave' )) $this->onBeforeSave( $Entity );
156
+		if (method_exists($this, 'onBeforeSave')) $this->onBeforeSave($Entity);
157 157
 
158 158
 		
159 159
 		return true;
@@ -163,13 +163,13 @@  discard block
 block discarded – undo
163 163
 	 * На успешное сохранение
164 164
 	 * @param \SimpleORM\EntityInterface $Entity
165 165
 	 */
166
-	protected function onBeforeSave(EntityInterface $Entity){
166
+	protected function onBeforeSave(EntityInterface $Entity) {
167 167
 		
168 168
 		foreach ($this->relations as $alias => $mapper) {
169 169
 		
170 170
 			$SaveEntity = $Entity->{'get'.$alias}();
171 171
 			
172
-			if(!$mapper->save($SaveEntity)){
172
+			if (!$mapper->save($SaveEntity)) {
173 173
 				throw new \Autoprice\Exceptions\EntityNotSaveException('Unable to save Entity!');
174 174
 			}
175 175
 			
@@ -187,9 +187,9 @@  discard block
 block discarded – undo
187 187
 		foreach ($this->relations as $alias => $cfg) {
188 188
 			$mapper = $cfg['mapper'];
189 189
 			//если связь один к одному то удаляем сущность
190
-			if($cgg['reltype'] == 'has_one'){
190
+			if ($cgg['reltype']=='has_one') {
191 191
 				$Entity = $Entity->{'get'.$alias}();
192
-				if(!$mapper->delete($Entity)){
192
+				if (!$mapper->delete($Entity)) {
193 193
 					throw new \Autoprice\Exceptions\EntityNotDeleteException('Unable to delete Entity!');
194 194
 				}
195 195
 			}
@@ -208,43 +208,43 @@  discard block
 block discarded – undo
208 208
 	 * @return \Core\Infrastructure\EntityInterface
209 209
 	 * @throws BadMethodCallException
210 210
 	 */
211
-	protected function buildEntity(EntityInterface $Entity, array $row){
211
+	protected function buildEntity(EntityInterface $Entity, array $row) {
212 212
 		
213
-        foreach ($this->mapping_fields as $alias => $cfg ) {
213
+        foreach ($this->mapping_fields as $alias => $cfg) {
214 214
 			
215 215
 			$value = false;
216 216
 			
217 217
 			$field = $cfg['field'];
218 218
 			
219
-			$method_set = 'set' . ucfirst($alias);
219
+			$method_set = 'set'.ucfirst($alias);
220 220
 			
221
-			if(!method_exists($Entity, $method_set )){
221
+			if (!method_exists($Entity, $method_set)) {
222 222
 				throw new BadMethodCallException("Метод {$method_set}  не определен");
223 223
 			}			
224 224
 			
225 225
 			//событие на формирование поля
226
-			if( isset($cfg['build']) && is_object($cfg['build']) ){
226
+			if (isset($cfg['build']) && is_object($cfg['build'])) {
227 227
 				$value = call_user_func($cfg['build'], $row);
228 228
 			}
229 229
 			//на связь
230
-			elseif(isset($cfg['relation'])){
230
+			elseif (isset($cfg['relation'])) {
231 231
 				
232 232
 				$mapper = $this->DI->get($cfg['relation']);
233 233
 				
234
-				if($this->use_joins===true || empty($row[$field])){
234
+				if ($this->use_joins===true || empty($row[$field])) {
235 235
 					$value = $mapper->createEntity($row);
236 236
 				}
237
-				else{
238
-					$fkey = isset($cfg['on']) ? $cfg['on'] :$mapper->key;
237
+				else {
238
+					$fkey = isset($cfg['on']) ? $cfg['on'] : $mapper->key;
239 239
 					$value = $mapper->findBySpecification((new Specification)->setWhere($fkey, $row[$field]));
240 240
 				}				
241 241
 				
242 242
 			}
243
-			elseif(is_string($field) && isset($row[strtolower($field)])){
243
+			elseif (is_string($field) && isset($row[strtolower($field)])) {
244 244
 				$value = $row[strtolower($field)];
245 245
 			}
246 246
 			
247
-			if($value!==false)
247
+			if ($value!==false)
248 248
 				$Entity->{$method_set}($value);
249 249
 			
250 250
         }
@@ -259,27 +259,27 @@  discard block
 block discarded – undo
259 259
 	 * @return \Core\Infrastructure\EntityInterface
260 260
 	 * @throws BadMethodCallException
261 261
 	 */
262
-	protected function unbuildEntity(EntityInterface $Entity){
262
+	protected function unbuildEntity(EntityInterface $Entity) {
263 263
 		
264 264
 		$row = [];
265 265
 
266
-        foreach ($this->mapping_fields as $alias => $cfg ) {
266
+        foreach ($this->mapping_fields as $alias => $cfg) {
267 267
 			
268 268
 			$field = $cfg['field'];
269 269
 			
270
-			$method_get = 'get' . ucfirst($alias);
270
+			$method_get = 'get'.ucfirst($alias);
271 271
 			
272
-			if(!method_exists($Entity, $method_get )){
272
+			if (!method_exists($Entity, $method_get)) {
273 273
 				throw new BadMethodCallException("Метод {$method_get}  не определен");
274 274
 			}		
275 275
 			
276 276
 			//--------------------------------------------------------------------
277
-			if( isset($cfg['unbuild']) && is_object($cfg['unbuild']) ){
278
-				$value = call_user_func($cfg['unbuild'], $Entity->{$method_get}() );
277
+			if (isset($cfg['unbuild']) && is_object($cfg['unbuild'])) {
278
+				$value = call_user_func($cfg['unbuild'], $Entity->{$method_get}());
279 279
 			}
280
-			elseif(isset($cfg['relation'])){
280
+			elseif (isset($cfg['relation'])) {
281 281
 				
282
-				if(isset($cfg['on']))
282
+				if (isset($cfg['on']))
283 283
 					$fkey = $this->DI->get($cfg['relation'])->getFieldAlias($cfg['on']);
284 284
 				else
285 285
 					$fkey = 'id';
@@ -287,7 +287,7 @@  discard block
 block discarded – undo
287 287
 				$value = $Entity->{$method_get}()->{'get'.$fkey}();
288 288
 				
289 289
 			}			
290
-			else{
290
+			else {
291 291
 				$value = $Entity->{$method_get}();
292 292
 			}			
293 293
 						
@@ -303,22 +303,22 @@  discard block
 block discarded – undo
303 303
 	/**
304 304
 	 * Установка поля для маппинга
305 305
 	 */
306
-	protected function addMappingField($alias,$field = null){
306
+	protected function addMappingField($alias, $field = null) {
307 307
 		
308
-		if(is_string($field)){
308
+		if (is_string($field)) {
309 309
 			$field = ['field'	=>	$field];
310 310
 		}
311
-		elseif( (is_array($field) && !isset($field['field'])) || empty($field)){
312
-			$field['field']	= $alias;
311
+		elseif ((is_array($field) && !isset($field['field'])) || empty($field)) {
312
+			$field['field'] = $alias;
313 313
 		}
314 314
 	
315 315
 		$this->mapping_fields[$alias] = $field;
316 316
 
317
-		if(isset($field['primary']) && $field['primary']===true){
317
+		if (isset($field['primary']) && $field['primary']===true) {
318 318
 			$this->key = $field['field'];
319 319
 		}
320 320
 		
321
-		if(isset($field['softdelete']) && $field['softdelete']===true){
321
+		if (isset($field['softdelete']) && $field['softdelete']===true) {
322 322
 			$this->soft_delete_key = $field['field'];
323 323
 		}
324 324
 		
@@ -345,7 +345,7 @@  discard block
 block discarded – undo
345 345
 
346 346
 
347 347
 	
348
-	public function getFieldAlias($field){
348
+	public function getFieldAlias($field) {
349 349
 		
350 350
 		return $this->mapping_fields_aliases[$field];
351 351
 		
@@ -357,7 +357,7 @@  discard block
 block discarded – undo
357 357
 	 * @param ISpecificationCriteria $specification
358 358
 	 * @return type
359 359
 	 */
360
-	public function findBySpecification(ISpecificationCriteria $specification){
360
+	public function findBySpecification(ISpecificationCriteria $specification) {
361 361
 
362 362
 		//псеводо удаление
363 363
 		$this->setSoftDelete($specification);
@@ -367,7 +367,7 @@  discard block
 block discarded – undo
367 367
 		$specification->setLimit(1);
368 368
 		
369 369
 		//получение записей
370
-		$res = $this->getAdapter()->getResultQuery($this->getEntityTable(),$specification);
370
+		$res = $this->getAdapter()->getResultQuery($this->getEntityTable(), $specification);
371 371
 
372 372
         if (!$row = $res->row_array()) {
373 373
             return null;
@@ -388,13 +388,13 @@  discard block
 block discarded – undo
388 388
 		
389 389
 		if (
390 390
 				$delete_key > '' && 
391
-				$Entity->getId() > 0){
392
-				$result = $this->getAdapter()->update($this->getEntityTable(), [ $delete_key => 1 ], "{$this->getPrimaryKey()} = '{$Entity->getId()}'");
391
+				$Entity->getId() > 0) {
392
+				$result = $this->getAdapter()->update($this->getEntityTable(), [$delete_key => 1], "{$this->getPrimaryKey()} = '{$Entity->getId()}'");
393 393
 		}
394
-		elseif($Entity->getId() > 0){
394
+		elseif ($Entity->getId() > 0) {
395 395
 			
396
-			if($result = $this->getAdapter()->delete($this->getEntityTable(), $this->getPrimaryKey()."  = ".$Entity->getId())){
397
-				if(method_exists($this, 'onBeforeDelete' )){ $result = $this->onBeforeDelete( $Entity );}
396
+			if ($result = $this->getAdapter()->delete($this->getEntityTable(), $this->getPrimaryKey()."  = ".$Entity->getId())) {
397
+				if (method_exists($this, 'onBeforeDelete')) { $result = $this->onBeforeDelete($Entity); }
398 398
 			}
399 399
 		}
400 400
 		
@@ -411,13 +411,13 @@  discard block
 block discarded – undo
411 411
 		
412 412
 		$this->setRelations($specification);
413 413
 		
414
-		$res = $this->getAdapter()->getResultQuery($this->getEntityTable(),$specification);
414
+		$res = $this->getAdapter()->getResultQuery($this->getEntityTable(), $specification);
415 415
 		
416 416
 		if (!$rows = $res->result_array()) {
417 417
             return null;
418 418
         }	
419 419
 		
420
-		foreach($rows as $k =>  $row){
420
+		foreach ($rows as $k =>  $row) {
421 421
 			$rows[$k] = $this->createEntity($row);
422 422
 		}
423 423
 		
@@ -433,13 +433,13 @@  discard block
 block discarded – undo
433 433
 	 * Выборка удаленных моделей
434 434
 	 * @param ISpecificationCriteria $specification
435 435
 	 */
436
-	private function setSoftDelete(ISpecificationCriteria $specification){
437
-		if(
438
-				$this->use_delete === false &&
439
-				$this->setSoftDeleteKey()>'' 
436
+	private function setSoftDelete(ISpecificationCriteria $specification) {
437
+		if (
438
+				$this->use_delete===false &&
439
+				$this->setSoftDeleteKey() > '' 
440 440
 				&& !isset($specification->getWhere()[$this->setSoftDeleteKey()])
441 441
 				)
442
-		$specification->setWhere($this->setSoftDeleteKey(),0);
442
+		$specification->setWhere($this->setSoftDeleteKey(), 0);
443 443
 	}
444 444
 	
445 445
 	/**
@@ -450,12 +450,12 @@  discard block
 block discarded – undo
450 450
 	 * belongs_to - многие к многим (пользователь имет множество оплат одного заказа)
451 451
 	 * has_one - один к одному
452 452
 	 */
453
-	protected function setRelations(ISpecificationCriteria $Specification){
453
+	protected function setRelations(ISpecificationCriteria $Specification) {
454 454
 
455 455
 		$joins = [];
456 456
 
457
-		foreach ($this->mapping_fields as $field => $cfg){
458
-			if(isset($cfg['relation'])){
457
+		foreach ($this->mapping_fields as $field => $cfg) {
458
+			if (isset($cfg['relation'])) {
459 459
 				
460 460
 				$this->relations[$field] = [
461 461
 					'mapper'	=>	$mapper = $this->DI->get($cfg['relation']),
@@ -464,7 +464,7 @@  discard block
 block discarded – undo
464 464
 
465 465
 				$table = $mapper->getEntityTable();
466 466
 
467
-				$relation_key = isset($cfg['on'])? $cfg['on'] : $mapper->key;
467
+				$relation_key = isset($cfg['on']) ? $cfg['on'] : $mapper->key;
468 468
 				
469 469
 				$joins[$table] = [
470 470
 						'alias'	=> $field,
@@ -475,7 +475,7 @@  discard block
 block discarded – undo
475 475
 			}
476 476
 		}	
477 477
 
478
-		if($this->use_joins===true){
478
+		if ($this->use_joins===true) {
479 479
 			$Specification->setJoins($joins);
480 480
 		}			
481 481
 	}
@@ -490,7 +490,7 @@  discard block
 block discarded – undo
490 490
 		return $o;
491 491
 	}
492 492
 	
493
-	public function withDelete(){
493
+	public function withDelete() {
494 494
 		$o = clone $this;
495 495
 		$o->use_delete = true;
496 496
 		return $o;
Please login to merge, or discard this patch.