Completed
Branch 5.3 (f8a22c)
by Rémi
08:52
created
src/System/Aggregate.php 3 patches
Indentation   +1032 added lines, -1032 removed lines patch added patch discarded remove patch
@@ -15,1057 +15,1057 @@
 block discarded – undo
15 15
  */
16 16
 class Aggregate implements InternallyMappable
17 17
 {
18
-    /**
19
-     * The Root Entity
20
-     *
21
-     * @var \Analogue\ORM\System\Wrappers\Wrapper
22
-     */
23
-    protected $wrappedEntity;
24
-
25
-    /**
26
-     * Parent Root Aggregate
27
-     *
28
-     * @var \Analogue\ORM\System\Aggregate
29
-     */
30
-    protected $parent;
31
-
32
-    /**
33
-     * Parent's relationship method
34
-     *
35
-     * @var string
36
-     */
37
-    protected $parentRelationship;
38
-
39
-    /**
40
-     * Root Entity
41
-     *
42
-     * @var \Analogue\ORM\System\Aggregate
43
-     */
44
-    protected $root;
45
-
46
-    /**
47
-     * An associative array containing entity's
48
-     * relationships converted to Aggregates
49
-     *
50
-     * @var array
51
-     */
52
-    protected $relationships = [];
53
-
54
-    /**
55
-     * Relationship that need post-command synchronization
56
-     *
57
-     * @var array
58
-     */
59
-    protected $needSync = [];
60
-
61
-    /**
62
-     * Mapper
63
-     *
64
-     * @var \Analogue\ORM\System\Mapper;
65
-     */
66
-    protected $mapper;
67
-
68
-    /**
69
-     * Entity Map
70
-     *
71
-     * @var \Analogue\ORM\EntityMap;
72
-     */
73
-    protected $entityMap;
74
-
75
-    /**
76
-     * Create a new Aggregated Entity instance
77
-     *
78
-     * @param mixed          $entity
79
-     * @param Aggregate|null $parent
80
-     * @param string         $parentRelationship
81
-     * @param Aggregate|null $root
82
-     * @throws MappingException
83
-     */
84
-    public function __construct($entity, Aggregate $parent = null, $parentRelationship = null, Aggregate $root = null)
85
-    {
86
-        $factory = new Factory;
18
+	/**
19
+	 * The Root Entity
20
+	 *
21
+	 * @var \Analogue\ORM\System\Wrappers\Wrapper
22
+	 */
23
+	protected $wrappedEntity;
24
+
25
+	/**
26
+	 * Parent Root Aggregate
27
+	 *
28
+	 * @var \Analogue\ORM\System\Aggregate
29
+	 */
30
+	protected $parent;
31
+
32
+	/**
33
+	 * Parent's relationship method
34
+	 *
35
+	 * @var string
36
+	 */
37
+	protected $parentRelationship;
38
+
39
+	/**
40
+	 * Root Entity
41
+	 *
42
+	 * @var \Analogue\ORM\System\Aggregate
43
+	 */
44
+	protected $root;
45
+
46
+	/**
47
+	 * An associative array containing entity's
48
+	 * relationships converted to Aggregates
49
+	 *
50
+	 * @var array
51
+	 */
52
+	protected $relationships = [];
53
+
54
+	/**
55
+	 * Relationship that need post-command synchronization
56
+	 *
57
+	 * @var array
58
+	 */
59
+	protected $needSync = [];
60
+
61
+	/**
62
+	 * Mapper
63
+	 *
64
+	 * @var \Analogue\ORM\System\Mapper;
65
+	 */
66
+	protected $mapper;
67
+
68
+	/**
69
+	 * Entity Map
70
+	 *
71
+	 * @var \Analogue\ORM\EntityMap;
72
+	 */
73
+	protected $entityMap;
74
+
75
+	/**
76
+	 * Create a new Aggregated Entity instance
77
+	 *
78
+	 * @param mixed          $entity
79
+	 * @param Aggregate|null $parent
80
+	 * @param string         $parentRelationship
81
+	 * @param Aggregate|null $root
82
+	 * @throws MappingException
83
+	 */
84
+	public function __construct($entity, Aggregate $parent = null, $parentRelationship = null, Aggregate $root = null)
85
+	{
86
+		$factory = new Factory;
87 87
         
88
-        $this->wrappedEntity = $factory->make($entity);
88
+		$this->wrappedEntity = $factory->make($entity);
89 89
 
90
-        $this->parent = $parent;
90
+		$this->parent = $parent;
91 91
 
92
-        $this->parentRelationship = $parentRelationship;
92
+		$this->parentRelationship = $parentRelationship;
93 93
 
94
-        $this->root = $root;
94
+		$this->root = $root;
95 95
 
96
-        $this->mapper = Manager::getMapper($entity);
96
+		$this->mapper = Manager::getMapper($entity);
97 97
 
98
-        $this->entityMap = $this->mapper->getEntityMap();
98
+		$this->entityMap = $this->mapper->getEntityMap();
99 99
              
100
-        $this->parseRelationships();
101
-    }
102
-
103
-    /**
104
-     * Parse Every relationships defined on the entity
105
-     *
106
-     * @throws MappingException
107
-     * @return void
108
-     */
109
-    protected function parseRelationships()
110
-    {
111
-        foreach ($this->entityMap->getSingleRelationships() as $relation) {
112
-            $this->parseSingleRelationship($relation);
113
-        }
114
-
115
-        foreach ($this->entityMap->getManyRelationships() as $relation) {
116
-            $this->parseManyRelationship($relation);
117
-        }
118
-    }
119
-
120
-    /**
121
-     * Parse for values common to single & many relations
122
-     *
123
-     * @param  string $relation
124
-     * @throws MappingException
125
-     * @return mixed|boolean
126
-     */
127
-    protected function parseForCommonValues($relation)
128
-    {
129
-        if (!$this->hasAttribute($relation)) {
130
-            // If no attribute exists for this relationships
131
-            // we'll make it a simple empty array. This will
132
-            // save us from constantly checking for the attributes
133
-            // actual existence.
134
-            $this->relationships[$relation] = [];
135
-            return false;
136
-        }
137
-
138
-        $value = $this->getRelationshipValue($relation);
139
-
140
-        if (is_null($value)) {
141
-            $this->relationships[$relation] = [];
142
-
143
-            // If the relationship's content is the null value
144
-            // and the Entity's exist in DB, we'll interpret this
145
-            // as the need to detach all related Entities,
146
-            // therefore a sync operation is needed.
147
-            $this->needSync[] = $relation;
148
-            return false;
149
-        }
150
-
151
-        return $value;
152
-    }
153
-
154
-    /**
155
-     * Parse a 'single' relationship
156
-     *
157
-     * @param  string $relation
158
-     * @throws MappingException
159
-     * @return boolean
160
-     */
161
-    protected function parseSingleRelationship($relation)
162
-    {
163
-        if (!$value = $this->parseForCommonValues($relation)) {
164
-            return true;
165
-        }
100
+		$this->parseRelationships();
101
+	}
102
+
103
+	/**
104
+	 * Parse Every relationships defined on the entity
105
+	 *
106
+	 * @throws MappingException
107
+	 * @return void
108
+	 */
109
+	protected function parseRelationships()
110
+	{
111
+		foreach ($this->entityMap->getSingleRelationships() as $relation) {
112
+			$this->parseSingleRelationship($relation);
113
+		}
114
+
115
+		foreach ($this->entityMap->getManyRelationships() as $relation) {
116
+			$this->parseManyRelationship($relation);
117
+		}
118
+	}
119
+
120
+	/**
121
+	 * Parse for values common to single & many relations
122
+	 *
123
+	 * @param  string $relation
124
+	 * @throws MappingException
125
+	 * @return mixed|boolean
126
+	 */
127
+	protected function parseForCommonValues($relation)
128
+	{
129
+		if (!$this->hasAttribute($relation)) {
130
+			// If no attribute exists for this relationships
131
+			// we'll make it a simple empty array. This will
132
+			// save us from constantly checking for the attributes
133
+			// actual existence.
134
+			$this->relationships[$relation] = [];
135
+			return false;
136
+		}
137
+
138
+		$value = $this->getRelationshipValue($relation);
139
+
140
+		if (is_null($value)) {
141
+			$this->relationships[$relation] = [];
142
+
143
+			// If the relationship's content is the null value
144
+			// and the Entity's exist in DB, we'll interpret this
145
+			// as the need to detach all related Entities,
146
+			// therefore a sync operation is needed.
147
+			$this->needSync[] = $relation;
148
+			return false;
149
+		}
150
+
151
+		return $value;
152
+	}
153
+
154
+	/**
155
+	 * Parse a 'single' relationship
156
+	 *
157
+	 * @param  string $relation
158
+	 * @throws MappingException
159
+	 * @return boolean
160
+	 */
161
+	protected function parseSingleRelationship($relation)
162
+	{
163
+		if (!$value = $this->parseForCommonValues($relation)) {
164
+			return true;
165
+		}
166 166
         
167
-        if ($value instanceof Collection || is_array($value) || $value instanceof CollectionProxy) {
168
-            throw new MappingException("Entity's attribute $relation should not be array, or collection");
169
-        }
170
-
171
-        if ($value instanceof EntityProxy && !$value->isLoaded()) {
172
-            $this->relationships[$relation] = [];
173
-            return true;
174
-        }
175
-
176
-        // If the attribute is a loaded proxy, swap it for its
177
-        // loaded entity.
178
-        if ($value instanceof EntityProxy && $value->isLoaded()) {
179
-            $value = $value->getUnderlyingObject();
180
-        }
181
-
182
-        if ($this->isParentOrRoot($value)) {
183
-            $this->relationships[$relation] = [];
184
-            return true;
185
-        }
186
-
187
-        // At this point, we can assume the attribute is an Entity instance
188
-        // so we'll treat it as such.
189
-        $subAggregate = $this->createSubAggregate($value, $relation);
167
+		if ($value instanceof Collection || is_array($value) || $value instanceof CollectionProxy) {
168
+			throw new MappingException("Entity's attribute $relation should not be array, or collection");
169
+		}
170
+
171
+		if ($value instanceof EntityProxy && !$value->isLoaded()) {
172
+			$this->relationships[$relation] = [];
173
+			return true;
174
+		}
175
+
176
+		// If the attribute is a loaded proxy, swap it for its
177
+		// loaded entity.
178
+		if ($value instanceof EntityProxy && $value->isLoaded()) {
179
+			$value = $value->getUnderlyingObject();
180
+		}
181
+
182
+		if ($this->isParentOrRoot($value)) {
183
+			$this->relationships[$relation] = [];
184
+			return true;
185
+		}
186
+
187
+		// At this point, we can assume the attribute is an Entity instance
188
+		// so we'll treat it as such.
189
+		$subAggregate = $this->createSubAggregate($value, $relation);
190 190
          
191
-        // Even if it's a single entity, we'll store it as an array
192
-        // just for consistency with other relationships
193
-        $this->relationships[$relation] = [$subAggregate];
194
-
195
-        // We always need to check a loaded relation is in sync
196
-        // with its local key
197
-        $this->needSync[] = $relation;
198
-
199
-        return true;
200
-    }
201
-
202
-    /**
203
-     * Check if value isn't parent or root in the aggregate
204
-     *
205
-     * @param  mixed
206
-     * @return boolean|null
207
-     */
208
-    protected function isParentOrRoot($value)
209
-    {
210
-        if (!is_null($this->root)) {
211
-            $rootClass = get_class($this->root->getEntityObject());
212
-            if ($rootClass == get_class($value)) {
213
-                return true;
214
-            }
215
-        }
216
-
217
-        if (!is_null($this->parent)) {
218
-            $parentClass = get_class($this->parent->getEntityObject());
219
-            if ($parentClass == get_class($value)) {
220
-                return true;
221
-            }
222
-        }
223
-    }
224
-
225
-    /**
226
-     * Parse a 'many' relationship
227
-     *
228
-     * @param  string $relation
229
-     * @throws MappingException
230
-     * @return boolean
231
-     */
232
-    protected function parseManyRelationship($relation)
233
-    {
234
-        if (!$value = $this->parseForCommonValues($relation)) {
235
-            return true;
236
-        }
191
+		// Even if it's a single entity, we'll store it as an array
192
+		// just for consistency with other relationships
193
+		$this->relationships[$relation] = [$subAggregate];
194
+
195
+		// We always need to check a loaded relation is in sync
196
+		// with its local key
197
+		$this->needSync[] = $relation;
198
+
199
+		return true;
200
+	}
201
+
202
+	/**
203
+	 * Check if value isn't parent or root in the aggregate
204
+	 *
205
+	 * @param  mixed
206
+	 * @return boolean|null
207
+	 */
208
+	protected function isParentOrRoot($value)
209
+	{
210
+		if (!is_null($this->root)) {
211
+			$rootClass = get_class($this->root->getEntityObject());
212
+			if ($rootClass == get_class($value)) {
213
+				return true;
214
+			}
215
+		}
216
+
217
+		if (!is_null($this->parent)) {
218
+			$parentClass = get_class($this->parent->getEntityObject());
219
+			if ($parentClass == get_class($value)) {
220
+				return true;
221
+			}
222
+		}
223
+	}
224
+
225
+	/**
226
+	 * Parse a 'many' relationship
227
+	 *
228
+	 * @param  string $relation
229
+	 * @throws MappingException
230
+	 * @return boolean
231
+	 */
232
+	protected function parseManyRelationship($relation)
233
+	{
234
+		if (!$value = $this->parseForCommonValues($relation)) {
235
+			return true;
236
+		}
237 237
         
238
-        if (is_array($value) || $value instanceof Collection) {
239
-            $this->needSync[] = $relation;
240
-        }
241
-
242
-        // If the relation is a proxy, we test is the relation
243
-        // has been lazy loaded, otherwise we'll just treat
244
-        // the subset of newly added items.
245
-        if ($value instanceof CollectionProxy && $value->isLoaded()) {
246
-            $this->needSync[] = $relation;
247
-            $value = $value->getUnderlyingCollection();
248
-        }
249
-
250
-        if ($value instanceof CollectionProxy && !$value->isLoaded()) {
251
-            $value = $value->getAddedItems();
252
-        }
253
-
254
-        // At this point $value should be either an array or an instance
255
-        // of a collection class.
256
-        if (!is_array($value) && !$value instanceof Collection) {
257
-            throw new MappingException("'$relation' attribute should be array() or Collection");
258
-        }
259
-
260
-        $this->relationships[$relation] = $this->createSubAggregates($value, $relation);
238
+		if (is_array($value) || $value instanceof Collection) {
239
+			$this->needSync[] = $relation;
240
+		}
241
+
242
+		// If the relation is a proxy, we test is the relation
243
+		// has been lazy loaded, otherwise we'll just treat
244
+		// the subset of newly added items.
245
+		if ($value instanceof CollectionProxy && $value->isLoaded()) {
246
+			$this->needSync[] = $relation;
247
+			$value = $value->getUnderlyingCollection();
248
+		}
249
+
250
+		if ($value instanceof CollectionProxy && !$value->isLoaded()) {
251
+			$value = $value->getAddedItems();
252
+		}
253
+
254
+		// At this point $value should be either an array or an instance
255
+		// of a collection class.
256
+		if (!is_array($value) && !$value instanceof Collection) {
257
+			throw new MappingException("'$relation' attribute should be array() or Collection");
258
+		}
259
+
260
+		$this->relationships[$relation] = $this->createSubAggregates($value, $relation);
261 261
         
262
-        return true;
263
-    }
264
-
265
-    /**
266
-     * Return Entity's relationship attribute
267
-     *
268
-     * @param  string $relation
269
-     * @throws MappingException
270
-     * @return mixed
271
-     */
272
-    protected function getRelationshipValue($relation)
273
-    {
274
-        $value = $this->getEntityAttribute($relation);
275
-        //if($relation == "role") tdd($this->wrappedEntity->getEntityAttributes());
276
-        if (is_bool($value) || is_float($value) || is_int($value) || is_string($value)) {
277
-            throw new MappingException("Entity's attribute $relation should be array, object, collection or null");
278
-        }
279
-
280
-        return $value;
281
-    }
282
-
283
-    /**
284
-     * Create a child, aggregated entity
285
-     *
286
-     * @param  mixed $entities
287
-     * @param string $relation
288
-     * @return array
289
-     */
290
-    protected function createSubAggregates($entities, $relation)
291
-    {
292
-        $aggregates = [];
293
-
294
-        foreach ($entities as $entity) {
295
-            $aggregates[] = $this->createSubAggregate($entity, $relation);
296
-        }
297
-
298
-        return $aggregates;
299
-    }
300
-
301
-    /**
302
-     * Create a related subAggregate
303
-     *
304
-     * @param  mixed $entity
305
-     * @param  string $relation
306
-     * @throws MappingException
307
-     * @return self
308
-     */
309
-    protected function createSubAggregate($entity, $relation)
310
-    {
311
-        // If root isn't defined, then this is the Aggregate Root
312
-        if (is_null($this->root)) {
313
-            $root = $this;
314
-        } else {
315
-            $root = $this->root;
316
-        }
317
-
318
-        return new self($entity, $this, $relation, $root);
319
-    }
320
-
321
-    /**
322
-     * Get the Entity's primary key attribute
323
-     *
324
-     * @return string|integer
325
-     */
326
-    public function getEntityId()
327
-    {
328
-        return $this->wrappedEntity->getEntityAttribute($this->entityMap->getKeyName());
329
-    }
330
-
331
-    /**
332
-     * Get the name of the primary key
333
-     *
334
-     * @return string
335
-     */
336
-    public function getEntityKey()
337
-    {
338
-        return $this->entityMap->getKeyName();
339
-    }
340
-
341
-    /**
342
-     * Return the entity map for the current entity
343
-     *
344
-     * @return \Analogue\ORM\EntityMap
345
-     */
346
-    public function getEntityMap()
347
-    {
348
-        return $this->entityMap;
349
-    }
350
-
351
-    /**
352
-     * Return the Entity's hash $class.$id
353
-     *
354
-     * @return string
355
-     */
356
-    public function getEntityHash()
357
-    {
358
-        return $this->getEntityClass() . '.' . $this->getEntityId();
359
-    }
360
-
361
-    /**
362
-     * Get wrapped entity class
363
-     *
364
-     * @return string
365
-     */
366
-    public function getEntityClass()
367
-    {
368
-        return $this->entityMap->getClass();
369
-    }
370
-
371
-    /**
372
-     * Return the Mapper's entity cache
373
-     *
374
-     * @return \Analogue\ORM\System\EntityCache
375
-     */
376
-    protected function getEntityCache()
377
-    {
378
-        return $this->mapper->getEntityCache();
379
-    }
380
-
381
-    /**
382
-     * Get a relationship as an aggregated entities' array
383
-     *
384
-     * @param  string $name
385
-     * @return array
386
-     */
387
-    public function getRelationship($name)
388
-    {
389
-        if (array_key_exists($name, $this->relationships)) {
390
-            return $this->relationships[$name];
391
-        } else {
392
-            return [];
393
-        }
394
-    }
395
-
396
-    /**
397
-     * [TO IMPLEMENT]
398
-     *
399
-     * @return array
400
-     */
401
-    public function getPivotAttributes()
402
-    {
403
-        return [];
404
-    }
405
-
406
-    /**
407
-     * Get Non existing related entities from several relationships
408
-     *
409
-     * @param  array $relationships
410
-     * @return array
411
-     */
412
-    public function getNonExistingRelated(array $relationships)
413
-    {
414
-        $nonExisting = [];
415
-
416
-        foreach ($relationships as $relation) {
417
-
418
-            if ($this->hasAttribute($relation) && array_key_exists($relation, $this->relationships)) {
419
-                $nonExisting = array_merge($nonExisting, $this->getNonExistingFromRelation($relation));
420
-            }
421
-        }
422
-
423
-        return $nonExisting;
424
-    }
425
-
426
-    /**
427
-     * Get non-existing related entities from a single relation
428
-     *
429
-     * @param  string $relation
430
-     * @return array
431
-     */
432
-    protected function getNonExistingFromRelation($relation)
433
-    {
434
-        $nonExisting = [];
435
-
436
-        foreach ($this->relationships[$relation] as $aggregate) {
437
-            if (!$aggregate->exists()) {
438
-                $nonExisting[] = $aggregate;
439
-            }
440
-        }
441
-
442
-        return $nonExisting;
443
-    }
444
-
445
-    /**
446
-     * Synchronize relationships if needed
447
-     *
448
-     * @param array
449
-     * @return void
450
-     */
451
-    public function syncRelationships(array $relationships)
452
-    {
453
-        foreach ($relationships as $relation) {
454
-            if (in_array($relation, $this->needSync)) {
455
-                $this->synchronize($relation);
456
-            }
457
-        }
458
-    }
459
-
460
-    /**
461
-     * Synchronize a relationship attribute
462
-     *
463
-     * @param $relation
464
-     * @return void
465
-     */
466
-    protected function synchronize($relation)
467
-    {
468
-        $actualContent = $this->relationships[$relation];
469
-
470
-        $relationshipObject = $this->entityMap->$relation($this->getEntityObject());
471
-        $relationshipObject->setParent($this->wrappedEntity);
472
-        $relationshipObject->sync($actualContent);
473
-    }
474
-
475
-    /**
476
-     * Returns an array of Missing related Entities for the
477
-     * given $relation
478
-     *
479
-     * @param  string $relation
480
-     * @return array
481
-     */
482
-    public function getMissingEntities($relation)
483
-    {
484
-        $cachedRelations = $this->getCachedAttribute($relation);
485
-
486
-        if (!is_null($cachedRelations)) {
487
-            $missing = [];
488
-
489
-            foreach ($cachedRelations as $hash) {
490
-                if (!$this->getRelatedAggregateFromHash($hash, $relation)) {
491
-                    $missing[] = $hash;
492
-                }
493
-            }
494
-
495
-            return $missing;
496
-        } else {
497
-            return [];
498
-        }
499
-    }
262
+		return true;
263
+	}
264
+
265
+	/**
266
+	 * Return Entity's relationship attribute
267
+	 *
268
+	 * @param  string $relation
269
+	 * @throws MappingException
270
+	 * @return mixed
271
+	 */
272
+	protected function getRelationshipValue($relation)
273
+	{
274
+		$value = $this->getEntityAttribute($relation);
275
+		//if($relation == "role") tdd($this->wrappedEntity->getEntityAttributes());
276
+		if (is_bool($value) || is_float($value) || is_int($value) || is_string($value)) {
277
+			throw new MappingException("Entity's attribute $relation should be array, object, collection or null");
278
+		}
279
+
280
+		return $value;
281
+	}
282
+
283
+	/**
284
+	 * Create a child, aggregated entity
285
+	 *
286
+	 * @param  mixed $entities
287
+	 * @param string $relation
288
+	 * @return array
289
+	 */
290
+	protected function createSubAggregates($entities, $relation)
291
+	{
292
+		$aggregates = [];
293
+
294
+		foreach ($entities as $entity) {
295
+			$aggregates[] = $this->createSubAggregate($entity, $relation);
296
+		}
297
+
298
+		return $aggregates;
299
+	}
300
+
301
+	/**
302
+	 * Create a related subAggregate
303
+	 *
304
+	 * @param  mixed $entity
305
+	 * @param  string $relation
306
+	 * @throws MappingException
307
+	 * @return self
308
+	 */
309
+	protected function createSubAggregate($entity, $relation)
310
+	{
311
+		// If root isn't defined, then this is the Aggregate Root
312
+		if (is_null($this->root)) {
313
+			$root = $this;
314
+		} else {
315
+			$root = $this->root;
316
+		}
317
+
318
+		return new self($entity, $this, $relation, $root);
319
+	}
320
+
321
+	/**
322
+	 * Get the Entity's primary key attribute
323
+	 *
324
+	 * @return string|integer
325
+	 */
326
+	public function getEntityId()
327
+	{
328
+		return $this->wrappedEntity->getEntityAttribute($this->entityMap->getKeyName());
329
+	}
330
+
331
+	/**
332
+	 * Get the name of the primary key
333
+	 *
334
+	 * @return string
335
+	 */
336
+	public function getEntityKey()
337
+	{
338
+		return $this->entityMap->getKeyName();
339
+	}
340
+
341
+	/**
342
+	 * Return the entity map for the current entity
343
+	 *
344
+	 * @return \Analogue\ORM\EntityMap
345
+	 */
346
+	public function getEntityMap()
347
+	{
348
+		return $this->entityMap;
349
+	}
350
+
351
+	/**
352
+	 * Return the Entity's hash $class.$id
353
+	 *
354
+	 * @return string
355
+	 */
356
+	public function getEntityHash()
357
+	{
358
+		return $this->getEntityClass() . '.' . $this->getEntityId();
359
+	}
360
+
361
+	/**
362
+	 * Get wrapped entity class
363
+	 *
364
+	 * @return string
365
+	 */
366
+	public function getEntityClass()
367
+	{
368
+		return $this->entityMap->getClass();
369
+	}
370
+
371
+	/**
372
+	 * Return the Mapper's entity cache
373
+	 *
374
+	 * @return \Analogue\ORM\System\EntityCache
375
+	 */
376
+	protected function getEntityCache()
377
+	{
378
+		return $this->mapper->getEntityCache();
379
+	}
380
+
381
+	/**
382
+	 * Get a relationship as an aggregated entities' array
383
+	 *
384
+	 * @param  string $name
385
+	 * @return array
386
+	 */
387
+	public function getRelationship($name)
388
+	{
389
+		if (array_key_exists($name, $this->relationships)) {
390
+			return $this->relationships[$name];
391
+		} else {
392
+			return [];
393
+		}
394
+	}
395
+
396
+	/**
397
+	 * [TO IMPLEMENT]
398
+	 *
399
+	 * @return array
400
+	 */
401
+	public function getPivotAttributes()
402
+	{
403
+		return [];
404
+	}
405
+
406
+	/**
407
+	 * Get Non existing related entities from several relationships
408
+	 *
409
+	 * @param  array $relationships
410
+	 * @return array
411
+	 */
412
+	public function getNonExistingRelated(array $relationships)
413
+	{
414
+		$nonExisting = [];
415
+
416
+		foreach ($relationships as $relation) {
417
+
418
+			if ($this->hasAttribute($relation) && array_key_exists($relation, $this->relationships)) {
419
+				$nonExisting = array_merge($nonExisting, $this->getNonExistingFromRelation($relation));
420
+			}
421
+		}
422
+
423
+		return $nonExisting;
424
+	}
425
+
426
+	/**
427
+	 * Get non-existing related entities from a single relation
428
+	 *
429
+	 * @param  string $relation
430
+	 * @return array
431
+	 */
432
+	protected function getNonExistingFromRelation($relation)
433
+	{
434
+		$nonExisting = [];
435
+
436
+		foreach ($this->relationships[$relation] as $aggregate) {
437
+			if (!$aggregate->exists()) {
438
+				$nonExisting[] = $aggregate;
439
+			}
440
+		}
441
+
442
+		return $nonExisting;
443
+	}
444
+
445
+	/**
446
+	 * Synchronize relationships if needed
447
+	 *
448
+	 * @param array
449
+	 * @return void
450
+	 */
451
+	public function syncRelationships(array $relationships)
452
+	{
453
+		foreach ($relationships as $relation) {
454
+			if (in_array($relation, $this->needSync)) {
455
+				$this->synchronize($relation);
456
+			}
457
+		}
458
+	}
459
+
460
+	/**
461
+	 * Synchronize a relationship attribute
462
+	 *
463
+	 * @param $relation
464
+	 * @return void
465
+	 */
466
+	protected function synchronize($relation)
467
+	{
468
+		$actualContent = $this->relationships[$relation];
469
+
470
+		$relationshipObject = $this->entityMap->$relation($this->getEntityObject());
471
+		$relationshipObject->setParent($this->wrappedEntity);
472
+		$relationshipObject->sync($actualContent);
473
+	}
474
+
475
+	/**
476
+	 * Returns an array of Missing related Entities for the
477
+	 * given $relation
478
+	 *
479
+	 * @param  string $relation
480
+	 * @return array
481
+	 */
482
+	public function getMissingEntities($relation)
483
+	{
484
+		$cachedRelations = $this->getCachedAttribute($relation);
485
+
486
+		if (!is_null($cachedRelations)) {
487
+			$missing = [];
488
+
489
+			foreach ($cachedRelations as $hash) {
490
+				if (!$this->getRelatedAggregateFromHash($hash, $relation)) {
491
+					$missing[] = $hash;
492
+				}
493
+			}
494
+
495
+			return $missing;
496
+		} else {
497
+			return [];
498
+		}
499
+	}
500 500
        
501
-    /**
502
-     * Get Relationships who have dirty attributes / dirty relationships
503
-     *
504
-     * @return array
505
-     */
506
-    public function getDirtyRelationships()
507
-    {
508
-        $dirtyAggregates = [];
509
-
510
-        foreach ($this->relationships as $relation) {
511
-            foreach ($relation as $aggregate) {
512
-
513
-                if (!$aggregate->exists() || $aggregate->isDirty() || count($aggregate->getDirtyRelationships()) > 0) {
514
-                    $dirtyAggregates[] = $aggregate;
515
-                }
516
-            }
517
-        }
518
-
519
-        return $dirtyAggregates;
520
-    }
501
+	/**
502
+	 * Get Relationships who have dirty attributes / dirty relationships
503
+	 *
504
+	 * @return array
505
+	 */
506
+	public function getDirtyRelationships()
507
+	{
508
+		$dirtyAggregates = [];
509
+
510
+		foreach ($this->relationships as $relation) {
511
+			foreach ($relation as $aggregate) {
512
+
513
+				if (!$aggregate->exists() || $aggregate->isDirty() || count($aggregate->getDirtyRelationships()) > 0) {
514
+					$dirtyAggregates[] = $aggregate;
515
+				}
516
+			}
517
+		}
518
+
519
+		return $dirtyAggregates;
520
+	}
521 521
     
522
-    /**
523
-     * Compare the object's raw attributes with the record in cache
524
-     *
525
-     * @return boolean
526
-     */
527
-    public function isDirty()
528
-    {
529
-        if (count($this->getDirtyRawAttributes()) > 0) {
530
-            return true;
531
-        } else {
532
-            return false;
533
-        }
534
-    }
535
-
536
-    /**
537
-     * Get Raw Entity's attributes, as they are represented
538
-     * in the database, including value objects, foreign keys,
539
-     * and discriminator column
540
-     *
541
-     * @return array
542
-     */
543
-    public function getRawAttributes()
544
-    {
545
-        $attributes = $this->wrappedEntity->getEntityAttributes();
546
-
547
-        foreach ($this->entityMap->getRelationships() as $relation) {
548
-            unset($attributes[$relation]);
549
-        }
550
-
551
-        if($this->entityMap->getInheritanceType() == 'single_table') {
552
-            $attributes = $this->addDiscriminatorColumn($attributes);
553
-        }
554
-
555
-        $attributes = $this->flattenEmbeddables($attributes);
556
-
557
-        $foreignKeys = $this->getForeignKeyAttributes();
558
-
559
-        return $attributes + $foreignKeys;
560
-    }
561
-
562
-    /**
563
-     * Add Discriminator Column if it doesn't exist on the actual entity
564
-     * 
565
-     * @param array $attributes
566
-     * @return array
567
-     */
568
-    protected function addDiscriminatorColumn($attributes)
569
-    {
570
-        $discriminatorColumn = $this->entityMap->getDiscriminatorColumn();
571
-        $entityClass = $this->entityMap->getClass();
572
-
573
-        if(! array_key_exists($discriminatorColumn, $attributes)) {
522
+	/**
523
+	 * Compare the object's raw attributes with the record in cache
524
+	 *
525
+	 * @return boolean
526
+	 */
527
+	public function isDirty()
528
+	{
529
+		if (count($this->getDirtyRawAttributes()) > 0) {
530
+			return true;
531
+		} else {
532
+			return false;
533
+		}
534
+	}
535
+
536
+	/**
537
+	 * Get Raw Entity's attributes, as they are represented
538
+	 * in the database, including value objects, foreign keys,
539
+	 * and discriminator column
540
+	 *
541
+	 * @return array
542
+	 */
543
+	public function getRawAttributes()
544
+	{
545
+		$attributes = $this->wrappedEntity->getEntityAttributes();
546
+
547
+		foreach ($this->entityMap->getRelationships() as $relation) {
548
+			unset($attributes[$relation]);
549
+		}
550
+
551
+		if($this->entityMap->getInheritanceType() == 'single_table') {
552
+			$attributes = $this->addDiscriminatorColumn($attributes);
553
+		}
554
+
555
+		$attributes = $this->flattenEmbeddables($attributes);
556
+
557
+		$foreignKeys = $this->getForeignKeyAttributes();
558
+
559
+		return $attributes + $foreignKeys;
560
+	}
561
+
562
+	/**
563
+	 * Add Discriminator Column if it doesn't exist on the actual entity
564
+	 * 
565
+	 * @param array $attributes
566
+	 * @return array
567
+	 */
568
+	protected function addDiscriminatorColumn($attributes)
569
+	{
570
+		$discriminatorColumn = $this->entityMap->getDiscriminatorColumn();
571
+		$entityClass = $this->entityMap->getClass();
572
+
573
+		if(! array_key_exists($discriminatorColumn, $attributes)) {
574 574
             
575
-            // Use key if present in discriminatorMap
576
-            $map = $this->entityMap->getDiscriminatorColumnMap();
577
-
578
-            $type = array_search($entityClass, $map);
579
-
580
-            if($type === false) {
581
-                // Use entity FQDN if no corresponding key is set
582
-                $attributes[$discriminatorColumn] = $entityClass;
583
-            }
584
-            else {
585
-                $attributes[$discriminatorColumn] = $type;
586
-            }
587
-        }
588
-
589
-        return $attributes;
590
-    }
591
-
592
-    /**
593
-     * Convert Value Objects to raw db attributes
594
-     *
595
-     * @param  array $attributes
596
-     * @return array
597
-     */
598
-    protected function flattenEmbeddables($attributes)
599
-    {
600
-        $embeddables = $this->entityMap->getEmbeddables();
575
+			// Use key if present in discriminatorMap
576
+			$map = $this->entityMap->getDiscriminatorColumnMap();
577
+
578
+			$type = array_search($entityClass, $map);
579
+
580
+			if($type === false) {
581
+				// Use entity FQDN if no corresponding key is set
582
+				$attributes[$discriminatorColumn] = $entityClass;
583
+			}
584
+			else {
585
+				$attributes[$discriminatorColumn] = $type;
586
+			}
587
+		}
588
+
589
+		return $attributes;
590
+	}
591
+
592
+	/**
593
+	 * Convert Value Objects to raw db attributes
594
+	 *
595
+	 * @param  array $attributes
596
+	 * @return array
597
+	 */
598
+	protected function flattenEmbeddables($attributes)
599
+	{
600
+		$embeddables = $this->entityMap->getEmbeddables();
601 601
         
602
-        foreach ($embeddables as $localKey => $embed) {
603
-            // Retrieve the value object from the entity's attributes
604
-            $valueObject = $attributes[$localKey];
602
+		foreach ($embeddables as $localKey => $embed) {
603
+			// Retrieve the value object from the entity's attributes
604
+			$valueObject = $attributes[$localKey];
605 605
 
606
-            // Unset the corresponding key
607
-            unset($attributes[$localKey]);
606
+			// Unset the corresponding key
607
+			unset($attributes[$localKey]);
608 608
 
609
-            // TODO Make wrapper object compatible with value objects
610
-            $valueObjectAttributes = $valueObject->getEntityAttributes();
609
+			// TODO Make wrapper object compatible with value objects
610
+			$valueObjectAttributes = $valueObject->getEntityAttributes();
611 611
 
612
-            // Now (if setup in the entity map) we prefix the value object's
613
-            // attributes with the snake_case name of the embedded class.
614
-            $prefix = snake_case(class_basename($embed));
612
+			// Now (if setup in the entity map) we prefix the value object's
613
+			// attributes with the snake_case name of the embedded class.
614
+			$prefix = snake_case(class_basename($embed));
615 615
 
616
-            foreach ($valueObjectAttributes as $key=>$value) {
617
-                $valueObjectAttributes[$prefix . '_' . $key] = $value;
618
-                unset($valueObjectAttributes[$key]);
619
-            }
616
+			foreach ($valueObjectAttributes as $key=>$value) {
617
+				$valueObjectAttributes[$prefix . '_' . $key] = $value;
618
+				unset($valueObjectAttributes[$key]);
619
+			}
620 620
 
621
-            $attributes = array_merge($attributes, $valueObjectAttributes);
622
-        }
621
+			$attributes = array_merge($attributes, $valueObjectAttributes);
622
+		}
623 623
         
624
-        return $attributes;
625
-    }
626
-
627
-    /**
628
-     * Return's entity raw attributes in the state they were at last
629
-     * query.
630
-     *
631
-     * @param  array|null $columns
632
-     * @return array
633
-     */
634
-    protected function getCachedRawAttributes(array $columns = null)
635
-    {
636
-        $cachedAttributes = $this->getCache()->get($this->getEntityId());
637
-
638
-        if (is_null($columns)) {
639
-            return $cachedAttributes;
640
-        } else {
641
-            return array_only($cachedAttributes, $columns);
642
-        }
643
-    }
644
-
645
-    /**
646
-     * Return a single attribute from the cache
647
-     * @param  string $key
648
-     * @return mixed
649
-     */
650
-    protected function getCachedAttribute($key)
651
-    {
652
-        $cachedAttributes = $this->getCache()->get($this->getEntityId());
653
-
654
-        if (!array_key_exists($key, $cachedAttributes)) {
655
-            return null;
656
-        } else {
657
-            return $cachedAttributes[$key];
658
-        }
659
-    }
660
-
661
-    /**
662
-     * Convert related Entity's attributes to foreign keys
663
-     *
664
-     * @return array
665
-     */
666
-    protected function getForeignKeyAttributes()
667
-    {
668
-        $foreignKeys = [];
669
-
670
-        foreach ($this->entityMap->getLocalRelationships() as $relation) {
671
-            // check if relationship has been parsed, meaning it has an actual object
672
-            // in the entity's attributes
673
-            if ($this->isActualRelationships($relation)) {
674
-                $foreignKeys = $foreignKeys + $this->getForeignKeyAttributesFromRelation($relation);
675
-            }
676
-        }
677
-
678
-        if (!is_null($this->parent)) {
679
-            $foreignKeys = $foreignKeys + $this->getForeignKeyAttributesFromParent();
680
-        }
681
-
682
-        return $foreignKeys;
683
-    }
684
-
685
-    /**
686
-     * Return an associative array containing the key-value pair(s) from
687
-     * the related entity.
688
-     *
689
-     * @param  string $relation
690
-     * @return array
691
-     */
692
-    protected function getForeignKeyAttributesFromRelation($relation)
693
-    {
694
-        $localRelations = $this->entityMap->getLocalRelationships();
695
-
696
-        if (in_array($relation, $localRelations)) {
697
-            // Call Relationship's method
698
-            $relationship = $this->entityMap->$relation($this->getEntityObject());
699
-
700
-            $relatedAggregate = $this->relationships[$relation][0];
701
-
702
-            return $relationship->getForeignKeyValuePair($relatedAggregate->getEntityObject());
703
-        } else {
704
-            return [];
705
-        }
706
-    }
707
-
708
-    /**
709
-     * Get foreign key attribute(s) from a parent entity in this
710
-     * aggregate context
711
-     *
712
-     * @return array
713
-     */
714
-    protected function getForeignKeyAttributesFromParent()
715
-    {
716
-        $parentMap = $this->parent->getEntityMap();
717
-
718
-        $parentForeignRelations = $parentMap->getForeignRelationships();
719
-        $parentPivotRelations = $parentMap->getPivotRelationships();
720
-
721
-        $parentRelation = $this->parentRelationship;
722
-
723
-        if (in_array($parentRelation, $parentForeignRelations)
724
-            && !in_array($parentRelation, $parentPivotRelations)
725
-        ) {
726
-            $parentObject = $this->parent->getEntityObject();
727
-
728
-            // Call Relationship's method on parent map
729
-            $relationship = $parentMap->$parentRelation($parentObject);
730
-
731
-            return $relationship->getForeignKeyValuePair();
732
-        } else {
733
-            return [];
734
-        }
735
-    }
736
-
737
-    /**
738
-     * Update Pivot records on loaded relationships, by comparing the
739
-     * values from the Entity Cache to the actual relationship inside
740
-     * the aggregated entity.
741
-     *
742
-     * @return void
743
-     */
744
-    public function updatePivotRecords()
745
-    {
746
-        $pivots = $this->entityMap->getPivotRelationships();
747
-
748
-        foreach ($pivots as $pivot) {
749
-            if (array_key_exists($pivot, $this->relationships)) {
750
-                $this->updatePivotRelation($pivot);
751
-            }
752
-        }
753
-    }
754
-
755
-    /**
756
-     * Update Single pivot relationship
757
-     *
758
-     * @param  string $relation
759
-     * @return void
760
-     */
761
-    protected function updatePivotRelation($relation)
762
-    {
763
-        $hashes = $this->getEntityHashesFromRelation($relation);
764
-
765
-        $cachedAttributes = $this->getCachedRawAttributes();
766
-
767
-        if (array_key_exists($relation, $cachedAttributes)) {
768
-            // Compare the two array of hashes to find out existing
769
-            // pivot records, and the ones to be created.
770
-            $new = array_diff($hashes, array_keys($cachedAttributes[$relation]));
771
-            $existing = array_intersect($hashes, array_keys($cachedAttributes[$relation]));
772
-        } else {
773
-            $existing = [];
774
-            $new = $hashes;
775
-        }
776
-
777
-        if (count($new) > 0) {
778
-            $pivots = $this->getRelatedAggregatesFromHashes($new, $relation);
779
-
780
-            $this->entityMap->$relation($this->getEntityObject())->createPivots($pivots);
781
-        }
782
-
783
-        if (count($existing) > 0) {
784
-            foreach ($existing as $pivotHash) {
785
-                $this->updatePivotIfDirty($pivotHash, $relation);
786
-            }
787
-        }
788
-    }
789
-
790
-    /**
791
-     * Compare existing pivot record in cache and update it
792
-     * if the pivot attributes are dirty
793
-     *
794
-     * @param  string $pivotHash
795
-     * @param  string $relation
796
-     * @return void
797
-     */
798
-    protected function updatePivotIfDirty($pivotHash, $relation)
799
-    {
800
-        $aggregate = $this->getRelatedAggregateFromHash($pivotHash, $relation);
801
-
802
-        if ($aggregate->hasAttribute('pivot')) {
803
-            $pivot = $aggregate->getEntityAttribute('pivot')->getEntityAttributes();
804
-
805
-            $cachedPivotAttributes = $this->getPivotAttributesFromCache($pivotHash, $relation);
806
-
807
-            $actualPivotAttributes = array_only($pivot, array_keys($cachedPivotAttributes));
808
-
809
-            $dirty = $this->getDirtyAttributes($actualPivotAttributes, $cachedPivotAttributes);
810
-
811
-            if (count($dirty) > 0) {
812
-                $id = $aggregate->getEntityId();
813
-
814
-                $this->entityMap->$relation($this->getEntityObject())->updateExistingPivot($id, $dirty);
815
-            }
816
-        }
817
-    }
818
-
819
-    /**
820
-     * Compare two attributes array and return dirty attributes
821
-     *
822
-     * @param  array $actual
823
-     * @param  array $cached
824
-     * @return array
825
-     */
826
-    protected function getDirtyAttributes(array $actual, array $cached)
827
-    {
828
-        $dirty = [];
829
-
830
-        foreach ($actual as $key => $value) {
831
-            if (!$this->originalIsNumericallyEquivalent($value, $cached[$key])) {
832
-                $dirty[$key] = $actual[$key];
833
-            }
834
-        }
835
-
836
-        return $dirty;
837
-    }
838
-
839
-    /**
840
-     *
841
-     * @param  string $pivotHash
842
-     * @param  string $relation
843
-     * @return array
844
-     */
845
-    protected function getPivotAttributesFromCache($pivotHash, $relation)
846
-    {
847
-        $cachedAttributes = $this->getCachedRawAttributes();
848
-
849
-        $cachedRelations = $cachedAttributes[$relation];
850
-
851
-        foreach ($cachedRelations as $cachedRelation) {
852
-            if ($cachedRelation == $pivotHash) {
853
-                return $cachedRelation->getPivotAttributes();
854
-            }
855
-        }
856
-    }
857
-
858
-    /**
859
-     * Returns an array of related Aggregates from its entity hashes
860
-     *
861
-     * @param  array  $hashes
862
-     * @param  string $relation
863
-     * @return array
864
-     */
865
-    protected function getRelatedAggregatesFromHashes(array $hashes, $relation)
866
-    {
867
-        $related = [];
868
-
869
-        foreach ($hashes as $hash) {
870
-            $aggregate = $this->getRelatedAggregateFromHash($hash, $relation);
871
-
872
-            if (!is_null($aggregate)) {
873
-                $related[] = $aggregate;
874
-            }
875
-        }
876
-
877
-        return $related;
878
-    }
879
-
880
-    /**
881
-     * Get related aggregate from its hash
882
-     *
883
-     * @param  string $hash
884
-     * @param  string $relation
885
-     * @return \Analogue\ORM\System\Aggregate|null
886
-     */
887
-    protected function getRelatedAggregateFromHash($hash, $relation)
888
-    {
889
-        foreach ($this->relationships[$relation] as $aggregate) {
890
-            if ($aggregate->getEntityHash() == $hash) {
891
-                return $aggregate;
892
-            }
893
-        }
894
-        return null;
895
-    }
896
-
897
-    /**
898
-     * Return an array of Entity Hashes from a specific relation
899
-     *
900
-     * @param  string $relation
901
-     * @return array
902
-     */
903
-    protected function getEntityHashesFromRelation($relation)
904
-    {
905
-        return array_map(function ($aggregate) {
906
-            return $aggregate->getEntityHash();
907
-        }, $this->relationships[$relation]);
908
-    }
909
-
910
-    /**
911
-     * Check the existence of an actual relationship
912
-     *
913
-     * @param  string $relation
914
-     * @return boolean
915
-     */
916
-    protected function isActualRelationships($relation)
917
-    {
918
-        return array_key_exists($relation, $this->relationships)
919
-            && count($this->relationships[$relation]) > 0;
920
-    }
921
-
922
-    /**
923
-     * Return cache instance for the current entity type
924
-     *
925
-     * @return \Analogue\ORM\System\EntityCache
926
-     */
927
-    protected function getCache()
928
-    {
929
-        return $this->mapper->getEntityCache();
930
-    }
931
-
932
-    /**
933
-     * Get Only Raw Entiy's attributes which have been modified
934
-     * since last query
935
-     *
936
-     * @return array
937
-     */
938
-    public function getDirtyRawAttributes()
939
-    {
940
-        $attributes = $this->getRawAttributes();
941
-        $cachedAttributes = $this->getCachedRawAttributes(array_keys($attributes));
942
-
943
-        $dirty = [];
944
-
945
-        foreach ($attributes as $key => $value) {
946
-            if ($this->isRelation($key) || $key == 'pivot') {
947
-                continue;
948
-            }
949
-
950
-            if (!array_key_exists($key, $cachedAttributes) && !$value instanceof Pivot) {
951
-                $dirty[$key] = $value;
952
-            } elseif ($value !== $cachedAttributes[$key] &&
953
-                !$this->originalIsNumericallyEquivalent($value, $cachedAttributes[$key])) {
954
-                $dirty[$key] = $value;
955
-            }
956
-        }
957
-
958
-        return $dirty;
959
-    }
960
-
961
-    /**
962
-     * @param $key
963
-     * @return bool
964
-     */
965
-    protected function isRelation($key)
966
-    {
967
-        return in_array($key, $this->entityMap->getRelationships());
968
-    }
969
-
970
-    /**
971
-     * Determine if the new and old values for a given key are numerically equivalent.
972
-     *
973
-     * @param $current
974
-     * @param $original
975
-     * @return boolean
976
-     */
977
-    protected function originalIsNumericallyEquivalent($current, $original)
978
-    {
979
-        return is_numeric($current) && is_numeric($original) && strcmp((string) $current, (string) $original) === 0;
980
-    }
981
-
982
-    /**
983
-     * Get the underlying entity object
984
-     *
985
-     * @return mixed
986
-     */
987
-    public function getEntityObject()
988
-    {
989
-        return $this->wrappedEntity->getObject();
990
-    }
991
-
992
-    /**
993
-     * Return the Mapper instance for the current Entity Type
994
-     *
995
-     * @return \Analogue\ORM\System\Mapper
996
-     */
997
-    public function getMapper()
998
-    {
999
-        return $this->mapper;
1000
-    }
1001
-
1002
-    /**
1003
-     * Check that the entity already exists in the database, by checking
1004
-     * if it has an EntityCache record
1005
-     *
1006
-     * @return boolean
1007
-     */
1008
-    public function exists()
1009
-    {
1010
-        return $this->getCache()->has($this->getEntityId());
1011
-    }
1012
-
1013
-    /**
1014
-     * Set the object attribute raw values (hydration)
1015
-     *
1016
-     * @param array $attributes
1017
-     */
1018
-    public function setEntityAttributes(array $attributes)
1019
-    {
1020
-        $this->wrappedEntity->setEntityAttributes($attributes);
1021
-    }
1022
-
1023
-    /**
1024
-     * Get the raw object's values.
1025
-     *
1026
-     * @return array
1027
-     */
1028
-    public function getEntityAttributes()
1029
-    {
1030
-        return $this->wrappedEntity->getEntityAttributes();
1031
-    }
1032
-
1033
-    /**
1034
-     * Set the raw entity attributes
1035
-     * @param string $key
1036
-     * @param string $value
1037
-     */
1038
-    public function setEntityAttribute($key, $value)
1039
-    {
1040
-        $this->wrappedEntity->setEntityAttribute($key, $value);
1041
-    }
1042
-
1043
-    /**
1044
-     * Return the entity's attribute
1045
-     * @param  string $key
1046
-     * @return mixed
1047
-     */
1048
-    public function getEntityAttribute($key)
1049
-    {
1050
-        return $this->wrappedEntity->getEntityAttribute($key);
1051
-    }
1052
-
1053
-    /**
1054
-     * Does the attribute exists on the entity
1055
-     *
1056
-     * @param  string  $key
1057
-     * @return boolean
1058
-     */
1059
-    public function hasAttribute($key)
1060
-    {
1061
-        return $this->wrappedEntity->hasAttribute($key);
1062
-    }
1063
-
1064
-    /**
1065
-     * Set the lazyloading proxies on the wrapped entity
1066
-     */
1067
-    public function setProxies()
1068
-    {
1069
-        $this->wrappedEntity->setProxies();
1070
-    }
624
+		return $attributes;
625
+	}
626
+
627
+	/**
628
+	 * Return's entity raw attributes in the state they were at last
629
+	 * query.
630
+	 *
631
+	 * @param  array|null $columns
632
+	 * @return array
633
+	 */
634
+	protected function getCachedRawAttributes(array $columns = null)
635
+	{
636
+		$cachedAttributes = $this->getCache()->get($this->getEntityId());
637
+
638
+		if (is_null($columns)) {
639
+			return $cachedAttributes;
640
+		} else {
641
+			return array_only($cachedAttributes, $columns);
642
+		}
643
+	}
644
+
645
+	/**
646
+	 * Return a single attribute from the cache
647
+	 * @param  string $key
648
+	 * @return mixed
649
+	 */
650
+	protected function getCachedAttribute($key)
651
+	{
652
+		$cachedAttributes = $this->getCache()->get($this->getEntityId());
653
+
654
+		if (!array_key_exists($key, $cachedAttributes)) {
655
+			return null;
656
+		} else {
657
+			return $cachedAttributes[$key];
658
+		}
659
+	}
660
+
661
+	/**
662
+	 * Convert related Entity's attributes to foreign keys
663
+	 *
664
+	 * @return array
665
+	 */
666
+	protected function getForeignKeyAttributes()
667
+	{
668
+		$foreignKeys = [];
669
+
670
+		foreach ($this->entityMap->getLocalRelationships() as $relation) {
671
+			// check if relationship has been parsed, meaning it has an actual object
672
+			// in the entity's attributes
673
+			if ($this->isActualRelationships($relation)) {
674
+				$foreignKeys = $foreignKeys + $this->getForeignKeyAttributesFromRelation($relation);
675
+			}
676
+		}
677
+
678
+		if (!is_null($this->parent)) {
679
+			$foreignKeys = $foreignKeys + $this->getForeignKeyAttributesFromParent();
680
+		}
681
+
682
+		return $foreignKeys;
683
+	}
684
+
685
+	/**
686
+	 * Return an associative array containing the key-value pair(s) from
687
+	 * the related entity.
688
+	 *
689
+	 * @param  string $relation
690
+	 * @return array
691
+	 */
692
+	protected function getForeignKeyAttributesFromRelation($relation)
693
+	{
694
+		$localRelations = $this->entityMap->getLocalRelationships();
695
+
696
+		if (in_array($relation, $localRelations)) {
697
+			// Call Relationship's method
698
+			$relationship = $this->entityMap->$relation($this->getEntityObject());
699
+
700
+			$relatedAggregate = $this->relationships[$relation][0];
701
+
702
+			return $relationship->getForeignKeyValuePair($relatedAggregate->getEntityObject());
703
+		} else {
704
+			return [];
705
+		}
706
+	}
707
+
708
+	/**
709
+	 * Get foreign key attribute(s) from a parent entity in this
710
+	 * aggregate context
711
+	 *
712
+	 * @return array
713
+	 */
714
+	protected function getForeignKeyAttributesFromParent()
715
+	{
716
+		$parentMap = $this->parent->getEntityMap();
717
+
718
+		$parentForeignRelations = $parentMap->getForeignRelationships();
719
+		$parentPivotRelations = $parentMap->getPivotRelationships();
720
+
721
+		$parentRelation = $this->parentRelationship;
722
+
723
+		if (in_array($parentRelation, $parentForeignRelations)
724
+			&& !in_array($parentRelation, $parentPivotRelations)
725
+		) {
726
+			$parentObject = $this->parent->getEntityObject();
727
+
728
+			// Call Relationship's method on parent map
729
+			$relationship = $parentMap->$parentRelation($parentObject);
730
+
731
+			return $relationship->getForeignKeyValuePair();
732
+		} else {
733
+			return [];
734
+		}
735
+	}
736
+
737
+	/**
738
+	 * Update Pivot records on loaded relationships, by comparing the
739
+	 * values from the Entity Cache to the actual relationship inside
740
+	 * the aggregated entity.
741
+	 *
742
+	 * @return void
743
+	 */
744
+	public function updatePivotRecords()
745
+	{
746
+		$pivots = $this->entityMap->getPivotRelationships();
747
+
748
+		foreach ($pivots as $pivot) {
749
+			if (array_key_exists($pivot, $this->relationships)) {
750
+				$this->updatePivotRelation($pivot);
751
+			}
752
+		}
753
+	}
754
+
755
+	/**
756
+	 * Update Single pivot relationship
757
+	 *
758
+	 * @param  string $relation
759
+	 * @return void
760
+	 */
761
+	protected function updatePivotRelation($relation)
762
+	{
763
+		$hashes = $this->getEntityHashesFromRelation($relation);
764
+
765
+		$cachedAttributes = $this->getCachedRawAttributes();
766
+
767
+		if (array_key_exists($relation, $cachedAttributes)) {
768
+			// Compare the two array of hashes to find out existing
769
+			// pivot records, and the ones to be created.
770
+			$new = array_diff($hashes, array_keys($cachedAttributes[$relation]));
771
+			$existing = array_intersect($hashes, array_keys($cachedAttributes[$relation]));
772
+		} else {
773
+			$existing = [];
774
+			$new = $hashes;
775
+		}
776
+
777
+		if (count($new) > 0) {
778
+			$pivots = $this->getRelatedAggregatesFromHashes($new, $relation);
779
+
780
+			$this->entityMap->$relation($this->getEntityObject())->createPivots($pivots);
781
+		}
782
+
783
+		if (count($existing) > 0) {
784
+			foreach ($existing as $pivotHash) {
785
+				$this->updatePivotIfDirty($pivotHash, $relation);
786
+			}
787
+		}
788
+	}
789
+
790
+	/**
791
+	 * Compare existing pivot record in cache and update it
792
+	 * if the pivot attributes are dirty
793
+	 *
794
+	 * @param  string $pivotHash
795
+	 * @param  string $relation
796
+	 * @return void
797
+	 */
798
+	protected function updatePivotIfDirty($pivotHash, $relation)
799
+	{
800
+		$aggregate = $this->getRelatedAggregateFromHash($pivotHash, $relation);
801
+
802
+		if ($aggregate->hasAttribute('pivot')) {
803
+			$pivot = $aggregate->getEntityAttribute('pivot')->getEntityAttributes();
804
+
805
+			$cachedPivotAttributes = $this->getPivotAttributesFromCache($pivotHash, $relation);
806
+
807
+			$actualPivotAttributes = array_only($pivot, array_keys($cachedPivotAttributes));
808
+
809
+			$dirty = $this->getDirtyAttributes($actualPivotAttributes, $cachedPivotAttributes);
810
+
811
+			if (count($dirty) > 0) {
812
+				$id = $aggregate->getEntityId();
813
+
814
+				$this->entityMap->$relation($this->getEntityObject())->updateExistingPivot($id, $dirty);
815
+			}
816
+		}
817
+	}
818
+
819
+	/**
820
+	 * Compare two attributes array and return dirty attributes
821
+	 *
822
+	 * @param  array $actual
823
+	 * @param  array $cached
824
+	 * @return array
825
+	 */
826
+	protected function getDirtyAttributes(array $actual, array $cached)
827
+	{
828
+		$dirty = [];
829
+
830
+		foreach ($actual as $key => $value) {
831
+			if (!$this->originalIsNumericallyEquivalent($value, $cached[$key])) {
832
+				$dirty[$key] = $actual[$key];
833
+			}
834
+		}
835
+
836
+		return $dirty;
837
+	}
838
+
839
+	/**
840
+	 *
841
+	 * @param  string $pivotHash
842
+	 * @param  string $relation
843
+	 * @return array
844
+	 */
845
+	protected function getPivotAttributesFromCache($pivotHash, $relation)
846
+	{
847
+		$cachedAttributes = $this->getCachedRawAttributes();
848
+
849
+		$cachedRelations = $cachedAttributes[$relation];
850
+
851
+		foreach ($cachedRelations as $cachedRelation) {
852
+			if ($cachedRelation == $pivotHash) {
853
+				return $cachedRelation->getPivotAttributes();
854
+			}
855
+		}
856
+	}
857
+
858
+	/**
859
+	 * Returns an array of related Aggregates from its entity hashes
860
+	 *
861
+	 * @param  array  $hashes
862
+	 * @param  string $relation
863
+	 * @return array
864
+	 */
865
+	protected function getRelatedAggregatesFromHashes(array $hashes, $relation)
866
+	{
867
+		$related = [];
868
+
869
+		foreach ($hashes as $hash) {
870
+			$aggregate = $this->getRelatedAggregateFromHash($hash, $relation);
871
+
872
+			if (!is_null($aggregate)) {
873
+				$related[] = $aggregate;
874
+			}
875
+		}
876
+
877
+		return $related;
878
+	}
879
+
880
+	/**
881
+	 * Get related aggregate from its hash
882
+	 *
883
+	 * @param  string $hash
884
+	 * @param  string $relation
885
+	 * @return \Analogue\ORM\System\Aggregate|null
886
+	 */
887
+	protected function getRelatedAggregateFromHash($hash, $relation)
888
+	{
889
+		foreach ($this->relationships[$relation] as $aggregate) {
890
+			if ($aggregate->getEntityHash() == $hash) {
891
+				return $aggregate;
892
+			}
893
+		}
894
+		return null;
895
+	}
896
+
897
+	/**
898
+	 * Return an array of Entity Hashes from a specific relation
899
+	 *
900
+	 * @param  string $relation
901
+	 * @return array
902
+	 */
903
+	protected function getEntityHashesFromRelation($relation)
904
+	{
905
+		return array_map(function ($aggregate) {
906
+			return $aggregate->getEntityHash();
907
+		}, $this->relationships[$relation]);
908
+	}
909
+
910
+	/**
911
+	 * Check the existence of an actual relationship
912
+	 *
913
+	 * @param  string $relation
914
+	 * @return boolean
915
+	 */
916
+	protected function isActualRelationships($relation)
917
+	{
918
+		return array_key_exists($relation, $this->relationships)
919
+			&& count($this->relationships[$relation]) > 0;
920
+	}
921
+
922
+	/**
923
+	 * Return cache instance for the current entity type
924
+	 *
925
+	 * @return \Analogue\ORM\System\EntityCache
926
+	 */
927
+	protected function getCache()
928
+	{
929
+		return $this->mapper->getEntityCache();
930
+	}
931
+
932
+	/**
933
+	 * Get Only Raw Entiy's attributes which have been modified
934
+	 * since last query
935
+	 *
936
+	 * @return array
937
+	 */
938
+	public function getDirtyRawAttributes()
939
+	{
940
+		$attributes = $this->getRawAttributes();
941
+		$cachedAttributes = $this->getCachedRawAttributes(array_keys($attributes));
942
+
943
+		$dirty = [];
944
+
945
+		foreach ($attributes as $key => $value) {
946
+			if ($this->isRelation($key) || $key == 'pivot') {
947
+				continue;
948
+			}
949
+
950
+			if (!array_key_exists($key, $cachedAttributes) && !$value instanceof Pivot) {
951
+				$dirty[$key] = $value;
952
+			} elseif ($value !== $cachedAttributes[$key] &&
953
+				!$this->originalIsNumericallyEquivalent($value, $cachedAttributes[$key])) {
954
+				$dirty[$key] = $value;
955
+			}
956
+		}
957
+
958
+		return $dirty;
959
+	}
960
+
961
+	/**
962
+	 * @param $key
963
+	 * @return bool
964
+	 */
965
+	protected function isRelation($key)
966
+	{
967
+		return in_array($key, $this->entityMap->getRelationships());
968
+	}
969
+
970
+	/**
971
+	 * Determine if the new and old values for a given key are numerically equivalent.
972
+	 *
973
+	 * @param $current
974
+	 * @param $original
975
+	 * @return boolean
976
+	 */
977
+	protected function originalIsNumericallyEquivalent($current, $original)
978
+	{
979
+		return is_numeric($current) && is_numeric($original) && strcmp((string) $current, (string) $original) === 0;
980
+	}
981
+
982
+	/**
983
+	 * Get the underlying entity object
984
+	 *
985
+	 * @return mixed
986
+	 */
987
+	public function getEntityObject()
988
+	{
989
+		return $this->wrappedEntity->getObject();
990
+	}
991
+
992
+	/**
993
+	 * Return the Mapper instance for the current Entity Type
994
+	 *
995
+	 * @return \Analogue\ORM\System\Mapper
996
+	 */
997
+	public function getMapper()
998
+	{
999
+		return $this->mapper;
1000
+	}
1001
+
1002
+	/**
1003
+	 * Check that the entity already exists in the database, by checking
1004
+	 * if it has an EntityCache record
1005
+	 *
1006
+	 * @return boolean
1007
+	 */
1008
+	public function exists()
1009
+	{
1010
+		return $this->getCache()->has($this->getEntityId());
1011
+	}
1012
+
1013
+	/**
1014
+	 * Set the object attribute raw values (hydration)
1015
+	 *
1016
+	 * @param array $attributes
1017
+	 */
1018
+	public function setEntityAttributes(array $attributes)
1019
+	{
1020
+		$this->wrappedEntity->setEntityAttributes($attributes);
1021
+	}
1022
+
1023
+	/**
1024
+	 * Get the raw object's values.
1025
+	 *
1026
+	 * @return array
1027
+	 */
1028
+	public function getEntityAttributes()
1029
+	{
1030
+		return $this->wrappedEntity->getEntityAttributes();
1031
+	}
1032
+
1033
+	/**
1034
+	 * Set the raw entity attributes
1035
+	 * @param string $key
1036
+	 * @param string $value
1037
+	 */
1038
+	public function setEntityAttribute($key, $value)
1039
+	{
1040
+		$this->wrappedEntity->setEntityAttribute($key, $value);
1041
+	}
1042
+
1043
+	/**
1044
+	 * Return the entity's attribute
1045
+	 * @param  string $key
1046
+	 * @return mixed
1047
+	 */
1048
+	public function getEntityAttribute($key)
1049
+	{
1050
+		return $this->wrappedEntity->getEntityAttribute($key);
1051
+	}
1052
+
1053
+	/**
1054
+	 * Does the attribute exists on the entity
1055
+	 *
1056
+	 * @param  string  $key
1057
+	 * @return boolean
1058
+	 */
1059
+	public function hasAttribute($key)
1060
+	{
1061
+		return $this->wrappedEntity->hasAttribute($key);
1062
+	}
1063
+
1064
+	/**
1065
+	 * Set the lazyloading proxies on the wrapped entity
1066
+	 */
1067
+	public function setProxies()
1068
+	{
1069
+		$this->wrappedEntity->setProxies();
1070
+	}
1071 1071
 }
Please login to merge, or discard this patch.
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -355,7 +355,7 @@  discard block
 block discarded – undo
355 355
      */
356 356
     public function getEntityHash()
357 357
     {
358
-        return $this->getEntityClass() . '.' . $this->getEntityId();
358
+        return $this->getEntityClass().'.'.$this->getEntityId();
359 359
     }
360 360
 
361 361
     /**
@@ -548,7 +548,7 @@  discard block
 block discarded – undo
548 548
             unset($attributes[$relation]);
549 549
         }
550 550
 
551
-        if($this->entityMap->getInheritanceType() == 'single_table') {
551
+        if ($this->entityMap->getInheritanceType() == 'single_table') {
552 552
             $attributes = $this->addDiscriminatorColumn($attributes);
553 553
         }
554 554
 
@@ -570,14 +570,14 @@  discard block
 block discarded – undo
570 570
         $discriminatorColumn = $this->entityMap->getDiscriminatorColumn();
571 571
         $entityClass = $this->entityMap->getClass();
572 572
 
573
-        if(! array_key_exists($discriminatorColumn, $attributes)) {
573
+        if (!array_key_exists($discriminatorColumn, $attributes)) {
574 574
             
575 575
             // Use key if present in discriminatorMap
576 576
             $map = $this->entityMap->getDiscriminatorColumnMap();
577 577
 
578 578
             $type = array_search($entityClass, $map);
579 579
 
580
-            if($type === false) {
580
+            if ($type === false) {
581 581
                 // Use entity FQDN if no corresponding key is set
582 582
                 $attributes[$discriminatorColumn] = $entityClass;
583 583
             }
@@ -614,7 +614,7 @@  discard block
 block discarded – undo
614 614
             $prefix = snake_case(class_basename($embed));
615 615
 
616 616
             foreach ($valueObjectAttributes as $key=>$value) {
617
-                $valueObjectAttributes[$prefix . '_' . $key] = $value;
617
+                $valueObjectAttributes[$prefix.'_'.$key] = $value;
618 618
                 unset($valueObjectAttributes[$key]);
619 619
             }
620 620
 
@@ -902,7 +902,7 @@  discard block
 block discarded – undo
902 902
      */
903 903
     protected function getEntityHashesFromRelation($relation)
904 904
     {
905
-        return array_map(function ($aggregate) {
905
+        return array_map(function($aggregate) {
906 906
             return $aggregate->getEntityHash();
907 907
         }, $this->relationships[$relation]);
908 908
     }
Please login to merge, or discard this patch.
Braces   +1 added lines, -2 removed lines patch added patch discarded remove patch
@@ -129,8 +129,7 @@
 block discarded – undo
129 129
             if ($this->hasSetMutator($key)) {
130 130
                 $method = 'set' . $this->getMutatorMethod($key);
131 131
                 $this->attributes[$key] = $this->$method($attribute);
132
-            }
133
-            else {
132
+            } else {
134 133
                 $this->attributes[$key] = $attribute;
135 134
             }
136 135
         }
Please login to merge, or discard this patch.
src/Relationships/BelongsTo.php 2 patches
Indentation   +294 added lines, -294 removed lines patch added patch discarded remove patch
@@ -10,299 +10,299 @@
 block discarded – undo
10 10
 
11 11
 class BelongsTo extends Relationship
12 12
 {
13
-    /**
14
-     * The foreign key of the parent model.
15
-     *
16
-     * @var string
17
-     */
18
-    protected $foreignKey;
19
-
20
-    /**
21
-     * The associated key on the parent model.
22
-     *
23
-     * @var string
24
-     */
25
-    protected $otherKey;
26
-
27
-    /**
28
-     * The name of the relationship.
29
-     *
30
-     * @var string
31
-     */
32
-    protected $relation;
33
-
34
-    /**
35
-     * Indicate if the parent entity hold the key for the relation.
36
-     *
37
-     * @var boolean
38
-     */
39
-    protected static $ownForeignKey = true;
40
-
41
-    /**
42
-     * Create a new belongs to relationship instance.
43
-     *
44
-     * @param Mapper   $mapper
45
-     * @param Mappable $parent
46
-     * @param string   $foreignKey
47
-     * @param string   $otherKey
48
-     * @param string   $relation
49
-     */
50
-    public function __construct(Mapper $mapper, $parent, $foreignKey, $otherKey, $relation)
51
-    {
52
-        $this->otherKey = $otherKey;
53
-        $this->relation = $relation;
54
-        $this->foreignKey = $foreignKey;
55
-
56
-        parent::__construct($mapper, $parent);
57
-    }
58
-
59
-    /**
60
-     * Get the results of the relationship.
61
-     *
62
-     * @param  $relation
63
-     *
64
-     * @return \Analogue\ORM\Entity
65
-     */
66
-    public function getResults($relation)
67
-    {
68
-        $result = $this->query->first();
69
-
70
-        $this->cacheRelation($result, $relation);
71
-
72
-        return $result;
73
-    }
74
-
75
-    /**
76
-     * Set the base constraints on the relation query.
77
-     *
78
-     * @return void
79
-     */
80
-    public function addConstraints()
81
-    {
82
-        if (static::$constraints) {
83
-            // For belongs to relationships, which are essentially the inverse of has one
84
-            // or has many relationships, we need to actually query on the primary key
85
-            // of the related models matching on the foreign key that's on a parent.
86
-            $this->query->where($this->otherKey, '=', $this->parent->getEntityAttribute($this->foreignKey));
87
-        }
88
-    }
89
-
90
-    /**
91
-     * Add the constraints for a relationship count query.
92
-     *
93
-     * @param  Query $query
94
-     * @param  Query $parent
95
-     * @return Query
96
-     */
97
-    public function getRelationCountQuery(Query $query, Query $parent)
98
-    {
99
-        $query->select(new Expression('count(*)'));
100
-
101
-        $otherKey = $this->wrap($query->getTable() . '.' . $this->otherKey);
102
-
103
-        return $query->where($this->getQualifiedForeignKey(), '=', new Expression($otherKey));
104
-    }
105
-
106
-    /**
107
-     * Set the constraints for an eager load of the relation.
108
-     *
109
-     * @param  array $entities
110
-     * @return void
111
-     */
112
-    public function addEagerConstraints(array $entities)
113
-    {
114
-        // We'll grab the primary key name of the related models since it could be set to
115
-        // a non-standard name and not "id". We will then construct the constraint for
116
-        // our eagerly loading query so it returns the proper models from execution.
117
-        $key = $this->otherKey;
118
-
119
-        $this->query->whereIn($key, $this->getEagerModelKeys($entities));
120
-    }
121
-
122
-    /**
123
-     * Gather the keys from an array of related models.
124
-     *
125
-     * @param  array $entities
126
-     * @return array
127
-     */
128
-    protected function getEagerModelKeys(array $entities)
129
-    {
130
-        $keys = [];
131
-
132
-        // First we need to gather all of the keys from the parent models so we know what
133
-        // to query for via the eager loading query. We will add them to an array then
134
-        // execute a "where in" statement to gather up all of those related records.
135
-        foreach ($entities as $entity) {
136
-            $entity = $this->factory->make($entity);
137
-
138
-            if (!is_null($value = $entity->getEntityAttribute($this->foreignKey))) {
139
-                $keys[] = $value;
140
-            }
141
-        }
142
-
143
-        // If there are no keys that were not null we will just return an array with 0 in
144
-        // it so the query doesn't fail, but will not return any results, which should
145
-        // be what this developer is expecting in a case where this happens to them.
146
-        if (count($keys) == 0) {
147
-            return [0];
148
-        }
149
-
150
-        return array_values(array_unique($keys));
151
-    }
152
-
153
-    /**
154
-     * Initialize the relation on a set of models.
155
-     *
156
-     * @param  array  $entities
157
-     * @param  string $relation
158
-     * @return array
159
-     */
160
-    public function initRelation(array $entities, $relation)
161
-    {
162
-        foreach ($entities as $entity) {
163
-            $entity = $this->factory->make($entity);
164
-            $entity->setEntityAttribute($relation, null);
165
-        }
166
-
167
-        return $entities;
168
-    }
169
-
170
-    /**
171
-     * Match the eagerly loaded results to their parents.
172
-     *
173
-     * @param  array            $entities
174
-     * @param  EntityCollection $results
175
-     * @param  string           $relation
176
-     * @return array
177
-     */
178
-    public function match(array $entities, EntityCollection $results, $relation)
179
-    {
180
-        $foreign = $this->foreignKey;
181
-
182
-        $other = $this->otherKey;
183
-
184
-        // First we will get to build a dictionary of the child models by their primary
185
-        // key of the relationship, then we can easily match the children back onto
186
-        // the parents using that dictionary and the primary key of the children.
187
-        $dictionary = [];
188
-
189
-        foreach ($results as $result) {
190
-            $result = $this->factory->make($result);
191
-            $dictionary[$result->getEntityAttribute($other)] = $result->getObject();
192
-        }
193
-
194
-        // Once we have the dictionary constructed, we can loop through all the parents
195
-        // and match back onto their children using these keys of the dictionary and
196
-        // the primary key of the children to map them onto the correct instances.
197
-        foreach ($entities as $entity) {
198
-            $entity = $this->factory->make($entity);
199
-
200
-            if (isset($dictionary[$entity->getEntityAttribute($foreign)])) {
201
-                $entity->setEntityAttribute($relation, $dictionary[$entity->getEntityAttribute($foreign)]);
202
-            }
203
-        }
204
-
205
-        return $entities;
206
-    }
207
-
208
-    public function sync(array $entities)
209
-    {
210
-        if (count($entities) > 1) {
211
-            throw new MappingException("Single Relationship shouldn't be synced with more than one entity");
212
-        }
13
+	/**
14
+	 * The foreign key of the parent model.
15
+	 *
16
+	 * @var string
17
+	 */
18
+	protected $foreignKey;
19
+
20
+	/**
21
+	 * The associated key on the parent model.
22
+	 *
23
+	 * @var string
24
+	 */
25
+	protected $otherKey;
26
+
27
+	/**
28
+	 * The name of the relationship.
29
+	 *
30
+	 * @var string
31
+	 */
32
+	protected $relation;
33
+
34
+	/**
35
+	 * Indicate if the parent entity hold the key for the relation.
36
+	 *
37
+	 * @var boolean
38
+	 */
39
+	protected static $ownForeignKey = true;
40
+
41
+	/**
42
+	 * Create a new belongs to relationship instance.
43
+	 *
44
+	 * @param Mapper   $mapper
45
+	 * @param Mappable $parent
46
+	 * @param string   $foreignKey
47
+	 * @param string   $otherKey
48
+	 * @param string   $relation
49
+	 */
50
+	public function __construct(Mapper $mapper, $parent, $foreignKey, $otherKey, $relation)
51
+	{
52
+		$this->otherKey = $otherKey;
53
+		$this->relation = $relation;
54
+		$this->foreignKey = $foreignKey;
55
+
56
+		parent::__construct($mapper, $parent);
57
+	}
58
+
59
+	/**
60
+	 * Get the results of the relationship.
61
+	 *
62
+	 * @param  $relation
63
+	 *
64
+	 * @return \Analogue\ORM\Entity
65
+	 */
66
+	public function getResults($relation)
67
+	{
68
+		$result = $this->query->first();
69
+
70
+		$this->cacheRelation($result, $relation);
71
+
72
+		return $result;
73
+	}
74
+
75
+	/**
76
+	 * Set the base constraints on the relation query.
77
+	 *
78
+	 * @return void
79
+	 */
80
+	public function addConstraints()
81
+	{
82
+		if (static::$constraints) {
83
+			// For belongs to relationships, which are essentially the inverse of has one
84
+			// or has many relationships, we need to actually query on the primary key
85
+			// of the related models matching on the foreign key that's on a parent.
86
+			$this->query->where($this->otherKey, '=', $this->parent->getEntityAttribute($this->foreignKey));
87
+		}
88
+	}
89
+
90
+	/**
91
+	 * Add the constraints for a relationship count query.
92
+	 *
93
+	 * @param  Query $query
94
+	 * @param  Query $parent
95
+	 * @return Query
96
+	 */
97
+	public function getRelationCountQuery(Query $query, Query $parent)
98
+	{
99
+		$query->select(new Expression('count(*)'));
100
+
101
+		$otherKey = $this->wrap($query->getTable() . '.' . $this->otherKey);
102
+
103
+		return $query->where($this->getQualifiedForeignKey(), '=', new Expression($otherKey));
104
+	}
105
+
106
+	/**
107
+	 * Set the constraints for an eager load of the relation.
108
+	 *
109
+	 * @param  array $entities
110
+	 * @return void
111
+	 */
112
+	public function addEagerConstraints(array $entities)
113
+	{
114
+		// We'll grab the primary key name of the related models since it could be set to
115
+		// a non-standard name and not "id". We will then construct the constraint for
116
+		// our eagerly loading query so it returns the proper models from execution.
117
+		$key = $this->otherKey;
118
+
119
+		$this->query->whereIn($key, $this->getEagerModelKeys($entities));
120
+	}
121
+
122
+	/**
123
+	 * Gather the keys from an array of related models.
124
+	 *
125
+	 * @param  array $entities
126
+	 * @return array
127
+	 */
128
+	protected function getEagerModelKeys(array $entities)
129
+	{
130
+		$keys = [];
131
+
132
+		// First we need to gather all of the keys from the parent models so we know what
133
+		// to query for via the eager loading query. We will add them to an array then
134
+		// execute a "where in" statement to gather up all of those related records.
135
+		foreach ($entities as $entity) {
136
+			$entity = $this->factory->make($entity);
137
+
138
+			if (!is_null($value = $entity->getEntityAttribute($this->foreignKey))) {
139
+				$keys[] = $value;
140
+			}
141
+		}
142
+
143
+		// If there are no keys that were not null we will just return an array with 0 in
144
+		// it so the query doesn't fail, but will not return any results, which should
145
+		// be what this developer is expecting in a case where this happens to them.
146
+		if (count($keys) == 0) {
147
+			return [0];
148
+		}
149
+
150
+		return array_values(array_unique($keys));
151
+	}
152
+
153
+	/**
154
+	 * Initialize the relation on a set of models.
155
+	 *
156
+	 * @param  array  $entities
157
+	 * @param  string $relation
158
+	 * @return array
159
+	 */
160
+	public function initRelation(array $entities, $relation)
161
+	{
162
+		foreach ($entities as $entity) {
163
+			$entity = $this->factory->make($entity);
164
+			$entity->setEntityAttribute($relation, null);
165
+		}
166
+
167
+		return $entities;
168
+	}
169
+
170
+	/**
171
+	 * Match the eagerly loaded results to their parents.
172
+	 *
173
+	 * @param  array            $entities
174
+	 * @param  EntityCollection $results
175
+	 * @param  string           $relation
176
+	 * @return array
177
+	 */
178
+	public function match(array $entities, EntityCollection $results, $relation)
179
+	{
180
+		$foreign = $this->foreignKey;
181
+
182
+		$other = $this->otherKey;
183
+
184
+		// First we will get to build a dictionary of the child models by their primary
185
+		// key of the relationship, then we can easily match the children back onto
186
+		// the parents using that dictionary and the primary key of the children.
187
+		$dictionary = [];
188
+
189
+		foreach ($results as $result) {
190
+			$result = $this->factory->make($result);
191
+			$dictionary[$result->getEntityAttribute($other)] = $result->getObject();
192
+		}
193
+
194
+		// Once we have the dictionary constructed, we can loop through all the parents
195
+		// and match back onto their children using these keys of the dictionary and
196
+		// the primary key of the children to map them onto the correct instances.
197
+		foreach ($entities as $entity) {
198
+			$entity = $this->factory->make($entity);
199
+
200
+			if (isset($dictionary[$entity->getEntityAttribute($foreign)])) {
201
+				$entity->setEntityAttribute($relation, $dictionary[$entity->getEntityAttribute($foreign)]);
202
+			}
203
+		}
204
+
205
+		return $entities;
206
+	}
207
+
208
+	public function sync(array $entities)
209
+	{
210
+		if (count($entities) > 1) {
211
+			throw new MappingException("Single Relationship shouldn't be synced with more than one entity");
212
+		}
213 213
         
214
-        if (count($entities) == 1) {
215
-            return $this->associate($entities[0]);
216
-        }
217
-
218
-        return false;
219
-    }
220
-
221
-    /**
222
-     * Associate the model instance to the given parent.
223
-     *
224
-     * @param  mixed $entity
225
-     * @return void
226
-     */
227
-    public function associate($entity)
228
-    {
229
-        $this->parent->setEntityAttribute($this->foreignKey, $entity->getEntityAttribute($this->otherKey));
230
-    }
231
-
232
-    /**
233
-     * Dissociate previously associated model from the given parent.
234
-     *
235
-     * @return Mappable
236
-     */
237
-    public function dissociate()
238
-    {
239
-        // The Mapper will retrieve this association within the object model, we won't be using
240
-        // the foreign key attribute inside the parent Entity.
241
-        //
242
-        //$this->parent->setEntityAttribute($this->foreignKey, null);
243
-
244
-        $this->parent->setEntityAttribute($this->relation, null);
245
-    }
246
-
247
-    /**
248
-     * Get the foreign key of the relationship.
249
-     *
250
-     * @return string
251
-     */
252
-    public function getForeignKey()
253
-    {
254
-        return $this->foreignKey;
255
-    }
256
-
257
-    /**
258
-     * Get the foreign key value pair for a related object
259
-     *
260
-     * @param  mixed $related
261
-     *
262
-     * @return array
263
-     */
264
-    public function getForeignKeyValuePair($related)
265
-    {
266
-        $foreignKey = $this->getForeignKey();
267
-
268
-        if ($related) {
269
-            $wrapper = $this->factory->make($related);
270
-
271
-            $relatedKey = $this->relatedMap->getKeyName();
272
-
273
-            return [$foreignKey => $wrapper->getEntityAttribute($relatedKey)];
274
-        } else {
275
-            return [$foreignKey => null];
276
-        }
277
-    }
278
-
279
-    /**
280
-     * Get the fully qualified foreign key of the relationship.
281
-     *
282
-     * @return string
283
-     */
284
-    public function getQualifiedForeignKey()
285
-    {
286
-        return $this->parentMap->getTable() . '.' . $this->foreignKey;
287
-    }
288
-
289
-    /**
290
-     * Get the associated key of the relationship.
291
-     *
292
-     * @return string
293
-     */
294
-    public function getOtherKey()
295
-    {
296
-        return $this->otherKey;
297
-    }
298
-
299
-    /**
300
-     * Get the fully qualified associated key of the relationship.
301
-     *
302
-     * @return string
303
-     */
304
-    public function getQualifiedOtherKeyName()
305
-    {
306
-        return $this->relatedMap->getTable() . '.' . $this->otherKey;
307
-    }
214
+		if (count($entities) == 1) {
215
+			return $this->associate($entities[0]);
216
+		}
217
+
218
+		return false;
219
+	}
220
+
221
+	/**
222
+	 * Associate the model instance to the given parent.
223
+	 *
224
+	 * @param  mixed $entity
225
+	 * @return void
226
+	 */
227
+	public function associate($entity)
228
+	{
229
+		$this->parent->setEntityAttribute($this->foreignKey, $entity->getEntityAttribute($this->otherKey));
230
+	}
231
+
232
+	/**
233
+	 * Dissociate previously associated model from the given parent.
234
+	 *
235
+	 * @return Mappable
236
+	 */
237
+	public function dissociate()
238
+	{
239
+		// The Mapper will retrieve this association within the object model, we won't be using
240
+		// the foreign key attribute inside the parent Entity.
241
+		//
242
+		//$this->parent->setEntityAttribute($this->foreignKey, null);
243
+
244
+		$this->parent->setEntityAttribute($this->relation, null);
245
+	}
246
+
247
+	/**
248
+	 * Get the foreign key of the relationship.
249
+	 *
250
+	 * @return string
251
+	 */
252
+	public function getForeignKey()
253
+	{
254
+		return $this->foreignKey;
255
+	}
256
+
257
+	/**
258
+	 * Get the foreign key value pair for a related object
259
+	 *
260
+	 * @param  mixed $related
261
+	 *
262
+	 * @return array
263
+	 */
264
+	public function getForeignKeyValuePair($related)
265
+	{
266
+		$foreignKey = $this->getForeignKey();
267
+
268
+		if ($related) {
269
+			$wrapper = $this->factory->make($related);
270
+
271
+			$relatedKey = $this->relatedMap->getKeyName();
272
+
273
+			return [$foreignKey => $wrapper->getEntityAttribute($relatedKey)];
274
+		} else {
275
+			return [$foreignKey => null];
276
+		}
277
+	}
278
+
279
+	/**
280
+	 * Get the fully qualified foreign key of the relationship.
281
+	 *
282
+	 * @return string
283
+	 */
284
+	public function getQualifiedForeignKey()
285
+	{
286
+		return $this->parentMap->getTable() . '.' . $this->foreignKey;
287
+	}
288
+
289
+	/**
290
+	 * Get the associated key of the relationship.
291
+	 *
292
+	 * @return string
293
+	 */
294
+	public function getOtherKey()
295
+	{
296
+		return $this->otherKey;
297
+	}
298
+
299
+	/**
300
+	 * Get the fully qualified associated key of the relationship.
301
+	 *
302
+	 * @return string
303
+	 */
304
+	public function getQualifiedOtherKeyName()
305
+	{
306
+		return $this->relatedMap->getTable() . '.' . $this->otherKey;
307
+	}
308 308
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -98,7 +98,7 @@  discard block
 block discarded – undo
98 98
     {
99 99
         $query->select(new Expression('count(*)'));
100 100
 
101
-        $otherKey = $this->wrap($query->getTable() . '.' . $this->otherKey);
101
+        $otherKey = $this->wrap($query->getTable().'.'.$this->otherKey);
102 102
 
103 103
         return $query->where($this->getQualifiedForeignKey(), '=', new Expression($otherKey));
104 104
     }
@@ -283,7 +283,7 @@  discard block
 block discarded – undo
283 283
      */
284 284
     public function getQualifiedForeignKey()
285 285
     {
286
-        return $this->parentMap->getTable() . '.' . $this->foreignKey;
286
+        return $this->parentMap->getTable().'.'.$this->foreignKey;
287 287
     }
288 288
 
289 289
     /**
@@ -303,6 +303,6 @@  discard block
 block discarded – undo
303 303
      */
304 304
     public function getQualifiedOtherKeyName()
305 305
     {
306
-        return $this->relatedMap->getTable() . '.' . $this->otherKey;
306
+        return $this->relatedMap->getTable().'.'.$this->otherKey;
307 307
     }
308 308
 }
Please login to merge, or discard this patch.
src/System/SingleTableInheritanceScope.php 3 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -32,7 +32,7 @@
 block discarded – undo
32 32
     /**
33 33
      * Remove the scope from the given Analogue query builder.
34 34
      *
35
-     * @param  mixed $query
35
+     * @param  Query $query
36 36
      * @return void
37 37
      */
38 38
     public function remove(Query $query)
Please login to merge, or discard this patch.
Indentation   +41 added lines, -41 removed lines patch added patch discarded remove patch
@@ -69,47 +69,47 @@
 block discarded – undo
69 69
 		return $type[0];
70 70
 	}
71 71
 
72
-    /**
73
-     * Apply the scope to a given Analogue query builder.
74
-     *
75
-     * @param  \Analogue\ORM\System\Query $query
76
-     * @return void
77
-     */
78
-    public function apply(Query $query)
79
-    {
80
-        $query->whereIn($this->column, $this->types);
81
-    }
82
-
83
-    /**
84
-     * Remove the scope from the given Analogue query builder.
85
-     *
86
-     * @param  mixed $query
87
-     * @return void
88
-     */
89
-    public function remove(Query $query)
90
-    {
91
-        $query = $query->getQuery();
92
-
93
-        foreach ((array) $query->wheres as $key => $where) {
72
+	/**
73
+	 * Apply the scope to a given Analogue query builder.
74
+	 *
75
+	 * @param  \Analogue\ORM\System\Query $query
76
+	 * @return void
77
+	 */
78
+	public function apply(Query $query)
79
+	{
80
+		$query->whereIn($this->column, $this->types);
81
+	}
82
+
83
+	/**
84
+	 * Remove the scope from the given Analogue query builder.
85
+	 *
86
+	 * @param  mixed $query
87
+	 * @return void
88
+	 */
89
+	public function remove(Query $query)
90
+	{
91
+		$query = $query->getQuery();
92
+
93
+		foreach ((array) $query->wheres as $key => $where) {
94 94
             
95
-            if ($this->isSingleTableConstraint($where, $this->column)) {
96
-                unset($query->wheres[$key]);
97
-
98
-                $query->wheres = array_values($query->wheres);
99
-            }
100
-        }
101
-    }
102
-
103
-    /**
104
-     * Determine if the given where clause is a single table inheritance constraint.
105
-     *
106
-     * @param  array  $where
107
-     * @param  string $column
108
-     * @return bool
109
-     */
110
-    protected function isSingleTableConstraint(array $where, $column)
111
-    {
112
-        return $where['column'] == $column;
113
-    }
95
+			if ($this->isSingleTableConstraint($where, $this->column)) {
96
+				unset($query->wheres[$key]);
97
+
98
+				$query->wheres = array_values($query->wheres);
99
+			}
100
+		}
101
+	}
102
+
103
+	/**
104
+	 * Determine if the given where clause is a single table inheritance constraint.
105
+	 *
106
+	 * @param  array  $where
107
+	 * @param  string $column
108
+	 * @return bool
109
+	 */
110
+	protected function isSingleTableConstraint(array $where, $column)
111
+	{
112
+		return $where['column'] == $column;
113
+	}
114 114
 
115 115
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -37,9 +37,9 @@  discard block
 block discarded – undo
37 37
 		// child class.
38 38
 		$classes = Manager::getInstance()->getRegisteredEntities();
39 39
 
40
-		foreach($classes as $otherClass => $entityMap) {
40
+		foreach ($classes as $otherClass => $entityMap) {
41 41
 
42
-			if(is_subclass_of($otherClass, $class)) {
42
+			if (is_subclass_of($otherClass, $class)) {
43 43
 				$this->types[] = $this->getTypeStringForEntity($otherClass, $entityMap);
44 44
 			}
45 45
 		}
@@ -62,7 +62,7 @@  discard block
 block discarded – undo
62 62
 			$class
63 63
 		);
64 64
 
65
-		if(count($type) == 0) {
65
+		if (count($type) == 0) {
66 66
 			return $class;
67 67
 		}
68 68
 
Please login to merge, or discard this patch.
src/System/MapperFactory.php 2 patches
Indentation   +57 added lines, -57 removed lines patch added patch discarded remove patch
@@ -13,76 +13,76 @@
 block discarded – undo
13 13
  */
14 14
 class MapperFactory
15 15
 {
16
-    /**
17
-     * Manager instance
18
-     *
19
-     * @var \Analogue\ORM\System\Manager
20
-     */
21
-    protected $manager;
16
+	/**
17
+	 * Manager instance
18
+	 *
19
+	 * @var \Analogue\ORM\System\Manager
20
+	 */
21
+	protected $manager;
22 22
 
23
-    /**
24
-     * DriverManager instance
25
-     *
26
-     * @var \Analogue\ORM\Drivers\Manager
27
-     */
28
-    protected $drivers;
23
+	/**
24
+	 * DriverManager instance
25
+	 *
26
+	 * @var \Analogue\ORM\Drivers\Manager
27
+	 */
28
+	protected $drivers;
29 29
 
30
-    /**
31
-     * Event dispatcher instance
32
-     *
33
-     * @var \Illuminate\Contracts\Events\Dispatcher
34
-     */
35
-    protected $dispatcher;
30
+	/**
31
+	 * Event dispatcher instance
32
+	 *
33
+	 * @var \Illuminate\Contracts\Events\Dispatcher
34
+	 */
35
+	protected $dispatcher;
36 36
 
37
-    /**
38
-     * MapperFactory constructor.
39
-     * @param DriverManager $drivers
40
-     * @param Dispatcher    $dispatcher
41
-     * @param Manager       $manager
42
-     */
43
-    public function __construct(DriverManager $drivers, Dispatcher $dispatcher, Manager $manager)
44
-    {
45
-        $this->drivers = $drivers;
37
+	/**
38
+	 * MapperFactory constructor.
39
+	 * @param DriverManager $drivers
40
+	 * @param Dispatcher    $dispatcher
41
+	 * @param Manager       $manager
42
+	 */
43
+	public function __construct(DriverManager $drivers, Dispatcher $dispatcher, Manager $manager)
44
+	{
45
+		$this->drivers = $drivers;
46 46
 
47
-        $this->dispatcher = $dispatcher;
47
+		$this->dispatcher = $dispatcher;
48 48
 
49
-        $this->manager = $manager;
50
-    }
49
+		$this->manager = $manager;
50
+	}
51 51
 
52
-    /**
53
-     * Return a new Mapper instance
54
-     *
55
-     * @param  string    $entityClass
56
-     * @param  EntityMap $entityMap
57
-     * @return Mapper
58
-     */
59
-    public function make($entityClass, EntityMap $entityMap)
60
-    {
61
-        $driver = $entityMap->getDriver();
52
+	/**
53
+	 * Return a new Mapper instance
54
+	 *
55
+	 * @param  string    $entityClass
56
+	 * @param  EntityMap $entityMap
57
+	 * @return Mapper
58
+	 */
59
+	public function make($entityClass, EntityMap $entityMap)
60
+	{
61
+		$driver = $entityMap->getDriver();
62 62
         
63
-        $connection = $entityMap->getConnection();
63
+		$connection = $entityMap->getConnection();
64 64
 
65
-        $adapter = $this->drivers->getAdapter($driver, $connection);
65
+		$adapter = $this->drivers->getAdapter($driver, $connection);
66 66
         
67
-        $entityMap->setDateFormat($adapter->getDateFormat());
67
+		$entityMap->setDateFormat($adapter->getDateFormat());
68 68
 
69
-        $mapper = new Mapper($entityMap, $adapter, $this->dispatcher, $this->manager);
69
+		$mapper = new Mapper($entityMap, $adapter, $this->dispatcher, $this->manager);
70 70
 
71
-        // Fire Initializing Event
72
-        $mapper->fireEvent('initializing', $mapper);
71
+		// Fire Initializing Event
72
+		$mapper->fireEvent('initializing', $mapper);
73 73
         
74
-        // Proceed necessary parsing on the EntityMap object
75
-        $entityMap->initialize();
74
+		// Proceed necessary parsing on the EntityMap object
75
+		$entityMap->initialize();
76 76
 
77
-        // Apply Inheritance scope, if necessary
78
-        if($entityMap->getInheritanceType() == 'single_table') {
79
-            $scope = new SingleTableInheritanceScope($entityMap);
80
-            $mapper->addGlobalScope($scope);
81
-        }
77
+		// Apply Inheritance scope, if necessary
78
+		if($entityMap->getInheritanceType() == 'single_table') {
79
+			$scope = new SingleTableInheritanceScope($entityMap);
80
+			$mapper->addGlobalScope($scope);
81
+		}
82 82
 
83
-        // Fire Initialized Event
84
-        $mapper->fireEvent('initialized', $mapper);
83
+		// Fire Initialized Event
84
+		$mapper->fireEvent('initialized', $mapper);
85 85
 
86
-        return $mapper;
87
-    }
86
+		return $mapper;
87
+	}
88 88
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -75,7 +75,7 @@
 block discarded – undo
75 75
         $entityMap->initialize();
76 76
 
77 77
         // Apply Inheritance scope, if necessary
78
-        if($entityMap->getInheritanceType() == 'single_table') {
78
+        if ($entityMap->getInheritanceType() == 'single_table') {
79 79
             $scope = new SingleTableInheritanceScope($entityMap);
80 80
             $mapper->addGlobalScope($scope);
81 81
         }
Please login to merge, or discard this patch.
src/AnalogueServiceProvider.php 2 patches
Indentation   +42 added lines, -42 removed lines patch added patch discarded remove patch
@@ -13,59 +13,59 @@
 block discarded – undo
13 13
  */
14 14
 class AnalogueServiceProvider extends ServiceProvider
15 15
 {
16
-    /**
17
-     * Indicates if loading of the provider is deferred.
18
-     *
19
-     * @var bool
20
-     */
21
-    protected $defer = false;
16
+	/**
17
+	 * Indicates if loading of the provider is deferred.
18
+	 *
19
+	 * @var bool
20
+	 */
21
+	protected $defer = false;
22 22
 
23
-    public function boot()
24
-    {
25
-        //   
26
-    }
23
+	public function boot()
24
+	{
25
+		//   
26
+	}
27 27
 
28
-    /**
29
-     * Register the service provider.
30
-     *
31
-     * @return void
32
-     */
33
-    public function register()
34
-    {
35
-        $this->app->singleton('analogue', function ($app) {
28
+	/**
29
+	 * Register the service provider.
30
+	 *
31
+	 * @return void
32
+	 */
33
+	public function register()
34
+	{
35
+		$this->app->singleton('analogue', function ($app) {
36 36
 
37
-            $db = $app['db'];
37
+			$db = $app['db'];
38 38
 
39
-            $connectionProvider = new IlluminateConnectionProvider($db);
39
+			$connectionProvider = new IlluminateConnectionProvider($db);
40 40
 
41
-            $illuminate = new IlluminateDriver($connectionProvider);
41
+			$illuminate = new IlluminateDriver($connectionProvider);
42 42
 
43
-            $driverManager = new DriverManager;
43
+			$driverManager = new DriverManager;
44 44
 
45
-            $driverManager->addDriver($illuminate);
45
+			$driverManager->addDriver($illuminate);
46 46
 
47
-            $event = $app->make('events');
47
+			$event = $app->make('events');
48 48
 
49
-            $manager = new Manager($driverManager, $event);
49
+			$manager = new Manager($driverManager, $event);
50 50
 
51
-            $manager->registerPlugin(\Analogue\ORM\Plugins\Timestamps\TimestampsPlugin::class);
52
-            $manager->registerPlugin(\Analogue\ORM\Plugins\SoftDeletes\SoftDeletesPlugin::class);
51
+			$manager->registerPlugin(\Analogue\ORM\Plugins\Timestamps\TimestampsPlugin::class);
52
+			$manager->registerPlugin(\Analogue\ORM\Plugins\SoftDeletes\SoftDeletesPlugin::class);
53 53
 
54
-            return $manager;
55
-        });
54
+			return $manager;
55
+		});
56 56
 
57
-        $this->app->bind(Manager::class, function ($app) {
58
-            return $app->make('analogue');
59
-        }); 
60
-    }
57
+		$this->app->bind(Manager::class, function ($app) {
58
+			return $app->make('analogue');
59
+		}); 
60
+	}
61 61
     
62
-    /**
63
-     * Get the services provided by the provider.
64
-     *
65
-     * @return array
66
-     */
67
-    public function provides()
68
-    {
69
-        return ['analogue'];
70
-    }
62
+	/**
63
+	 * Get the services provided by the provider.
64
+	 *
65
+	 * @return array
66
+	 */
67
+	public function provides()
68
+	{
69
+		return ['analogue'];
70
+	}
71 71
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -32,7 +32,7 @@  discard block
 block discarded – undo
32 32
      */
33 33
     public function register()
34 34
     {
35
-        $this->app->singleton('analogue', function ($app) {
35
+        $this->app->singleton('analogue', function($app) {
36 36
 
37 37
             $db = $app['db'];
38 38
 
@@ -54,7 +54,7 @@  discard block
 block discarded – undo
54 54
             return $manager;
55 55
         });
56 56
 
57
-        $this->app->bind(Manager::class, function ($app) {
57
+        $this->app->bind(Manager::class, function($app) {
58 58
             return $app->make('analogue');
59 59
         }); 
60 60
     }
Please login to merge, or discard this patch.