Completed
Branch master (a17b64)
by Rémi
15:50
created
src/Exceptions/EntityMapNotFoundException.php 1 patch
Indentation   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -6,5 +6,5 @@
 block discarded – undo
6 6
 
7 7
 class EntityMapNotFoundException extends RuntimeException
8 8
 {
9
-    //
9
+	//
10 10
 }
Please login to merge, or discard this patch.
src/helpers.php 2 patches
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -2,7 +2,7 @@  discard block
 block discarded – undo
2 2
 
3 3
 use Analogue\ORM\System\Manager;
4 4
 
5
-if (! function_exists('analogue')) {
5
+if (!function_exists('analogue')) {
6 6
 
7 7
     /**
8 8
      * Return analogue's manager instance
@@ -17,7 +17,7 @@  discard block
 block discarded – undo
17 17
 }
18 18
 
19 19
 
20
-if (! function_exists('mapper')) {
20
+if (!function_exists('mapper')) {
21 21
 
22 22
     /**
23 23
      * Create a mapper for a given entity (static alias)
Please login to merge, or discard this patch.
Indentation   +20 added lines, -20 removed lines patch added patch discarded remove patch
@@ -4,29 +4,29 @@
 block discarded – undo
4 4
 
5 5
 if (! function_exists('analogue')) {
6 6
 
7
-    /**
8
-     * Return analogue's manager instance
9
-     * 
10
-     * @return \Analogue\ORM\System\Manager
11
-     */
12
-    function analogue()
13
-    {
14
-        return Manager::getInstance();
15
-    }
7
+	/**
8
+	 * Return analogue's manager instance
9
+	 * 
10
+	 * @return \Analogue\ORM\System\Manager
11
+	 */
12
+	function analogue()
13
+	{
14
+		return Manager::getInstance();
15
+	}
16 16
 }
17 17
 
18 18
 
19 19
 if (! function_exists('mapper')) {
20 20
 
21
-    /**
22
-     * Create a mapper for a given entity (static alias)
23
-     * 
24
-     * @param \Analogue\ORM\Mappable|string $entity
25
-     * @param mixed $entityMap 
26
-     * @return Mapper
27
-     */
28
-    function mapper($entity, $entityMap = null)
29
-    {
30
-        return Manager::getMapper($entity, $entityMap);
31
-    }
21
+	/**
22
+	 * Create a mapper for a given entity (static alias)
23
+	 * 
24
+	 * @param \Analogue\ORM\Mappable|string $entity
25
+	 * @param mixed $entityMap 
26
+	 * @return Mapper
27
+	 */
28
+	function mapper($entity, $entityMap = null)
29
+	{
30
+		return Manager::getMapper($entity, $entityMap);
31
+	}
32 32
 }
Please login to merge, or discard this patch.
src/Drivers/IlluminateDBAdapter.php 2 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -27,7 +27,7 @@
 block discarded – undo
27 27
     /**
28 28
      * Return a new Query instance for this driver
29 29
      *
30
-     * @return QueryAdapter
30
+     * @return IlluminateQueryBuilder
31 31
      */
32 32
     public function getQuery()
33 33
     {
Please login to merge, or discard this patch.
Indentation   +56 added lines, -56 removed lines patch added patch discarded remove patch
@@ -10,68 +10,68 @@
 block discarded – undo
10 10
  */
11 11
 class IlluminateDBAdapter implements DBAdapter
12 12
 {
13
-    /**
14
-     * @var Connection
15
-     */
16
-    protected $connection;
13
+	/**
14
+	 * @var Connection
15
+	 */
16
+	protected $connection;
17 17
 
18
-    /**
19
-     * IlluminateDBAdapter constructor.
20
-     * @param Connection $connection
21
-     */
22
-    public function __construct(Connection $connection)
23
-    {
24
-        $this->connection = $connection;
25
-    }
18
+	/**
19
+	 * IlluminateDBAdapter constructor.
20
+	 * @param Connection $connection
21
+	 */
22
+	public function __construct(Connection $connection)
23
+	{
24
+		$this->connection = $connection;
25
+	}
26 26
 
27
-    /**
28
-     * Return a new Query instance for this driver
29
-     *
30
-     * @return QueryAdapter
31
-     */
32
-    public function getQuery()
33
-    {
34
-        $connection = $this->connection;
27
+	/**
28
+	 * Return a new Query instance for this driver
29
+	 *
30
+	 * @return QueryAdapter
31
+	 */
32
+	public function getQuery()
33
+	{
34
+		$connection = $this->connection;
35 35
 
36
-        $grammar = $connection->getQueryGrammar();
36
+		$grammar = $connection->getQueryGrammar();
37 37
 
38
-        return new IlluminateQueryBuilder($connection, $grammar, $connection->getPostProcessor());
39
-    }
38
+		return new IlluminateQueryBuilder($connection, $grammar, $connection->getPostProcessor());
39
+	}
40 40
 
41
-    /**
42
-     * Get the date format supported by the current connection
43
-     *
44
-     * @return string
45
-     */
46
-    public function getDateFormat()
47
-    {
48
-        return $this->connection->getQueryGrammar()->getDateFormat();
49
-    }
41
+	/**
42
+	 * Get the date format supported by the current connection
43
+	 *
44
+	 * @return string
45
+	 */
46
+	public function getDateFormat()
47
+	{
48
+		return $this->connection->getQueryGrammar()->getDateFormat();
49
+	}
50 50
 
51
-    /**
52
-     * Start a DB transaction on driver that supports it.
53
-     * @return void
54
-     */
55
-    public function beginTransaction()
56
-    {
57
-        $this->connection->beginTransaction();
58
-    }
51
+	/**
52
+	 * Start a DB transaction on driver that supports it.
53
+	 * @return void
54
+	 */
55
+	public function beginTransaction()
56
+	{
57
+		$this->connection->beginTransaction();
58
+	}
59 59
 
60
-    /**
61
-     * Commit a DB transaction on driver that supports it.
62
-     * @return void
63
-     */
64
-    public function commit()
65
-    {
66
-        $this->connection->commit();
67
-    }
60
+	/**
61
+	 * Commit a DB transaction on driver that supports it.
62
+	 * @return void
63
+	 */
64
+	public function commit()
65
+	{
66
+		$this->connection->commit();
67
+	}
68 68
 
69
-    /**
70
-     * Rollback a DB transaction
71
-     * @return void
72
-     */
73
-    public function rollback()
74
-    {
75
-        $this->connection->rollBack();
76
-    }
69
+	/**
70
+	 * Rollback a DB transaction
71
+	 * @return void
72
+	 */
73
+	public function rollback()
74
+	{
75
+		$this->connection->rollBack();
76
+	}
77 77
 }
Please login to merge, or discard this patch.
src/System/Mapper.php 3 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -177,7 +177,7 @@
 block discarded – undo
177 177
     /**
178 178
      * Get a the Underlying QueryAdapter.
179 179
      *
180
-     * @return \Analogue\ORM\Drivers\QueryAdapter
180
+     * @return \Illuminate\Database\Query\Builder
181 181
      */
182 182
     public function newQueryBuilder()
183 183
     {
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -277,7 +277,7 @@  discard block
 block discarded – undo
277 277
             throw new InvalidArgumentException('Fired Event with invalid Entity Object');
278 278
         }
279 279
 
280
-        $event = "analogue.{$event}." . $this->entityMap->getClass();
280
+        $event = "analogue.{$event}.".$this->entityMap->getClass();
281 281
 
282 282
         $method = $halt ? 'until' : 'fire';
283 283
 
@@ -328,7 +328,7 @@  discard block
 block discarded – undo
328 328
      */
329 329
     public function getGlobalScope($scope)
330 330
     {
331
-        return array_first($this->globalScopes, function ($key, $value) use ($scope) {
331
+        return array_first($this->globalScopes, function($key, $value) use ($scope) {
332 332
             return $scope instanceof $value;
333 333
         });
334 334
     }
@@ -404,7 +404,7 @@  discard block
 block discarded – undo
404 404
     {
405 405
         $class = $this->entityMap->getClass();
406 406
 
407
-        if($this->entityMap->useDependencyInjection()) {
407
+        if ($this->entityMap->useDependencyInjection()) {
408 408
             return $this->newInstanceUsingDependencyInjection($class);
409 409
         } 
410 410
         
@@ -419,7 +419,7 @@  discard block
 block discarded – undo
419 419
      */
420 420
     protected function newInstanceUsingDependencyInjection($class)
421 421
     {
422
-        if(! class_exists(Container::class)) {
422
+        if (!class_exists(Container::class)) {
423 423
             throw new ErrorException("Illuminate\Container\Container is required to use Dependency Injection");
424 424
         }
425 425
 
Please login to merge, or discard this patch.
Indentation   +573 added lines, -573 removed lines patch added patch discarded remove patch
@@ -25,577 +25,577 @@
 block discarded – undo
25 25
  */
26 26
 class Mapper
27 27
 {
28
-    /**
29
-     * The Manager instance
30
-     *
31
-     * @var \Analogue\ORM\System\Manager
32
-     */
33
-    protected $manager;
34
-
35
-    /**
36
-     * Instance of EntityMapper Object
37
-     *
38
-     * @var \Analogue\ORM\EntityMap
39
-     */
40
-    protected $entityMap;
41
-
42
-    /**
43
-     * The instance of db adapter
44
-     *
45
-     * @var \Analogue\ORM\Drivers\DBAdapter
46
-     */
47
-    protected $adapter;
48
-
49
-
50
-    /**
51
-     * Event dispatcher instance
52
-     *
53
-     * @var \Illuminate\Contracts\Events\Dispatcher
54
-     */
55
-    protected $dispatcher;
56
-
57
-    /**
58
-     * Entity Cache
59
-     *
60
-     * @var  \Analogue\ORM\System\EntityCache
61
-     */
62
-    protected $cache;
63
-
64
-    /**
65
-     * Global scopes
66
-     *
67
-     * @var array
68
-     */
69
-    protected $globalScopes = [];
70
-
71
-    /**
72
-     * Custom Commands
73
-     *
74
-     * @var array
75
-     */
76
-    protected $customCommands = [];
77
-
78
-    /**
79
-     * @param EntityMap  $entityMap
80
-     * @param DBAdapter  $adapter
81
-     * @param Dispatcher $dispatcher
82
-     * @param Manager    $manager
83
-     */
84
-    public function __construct(EntityMap $entityMap, DBAdapter $adapter, Dispatcher $dispatcher, Manager $manager)
85
-    {
86
-        $this->entityMap = $entityMap;
87
-
88
-        $this->adapter = $adapter;
89
-
90
-        $this->dispatcher = $dispatcher;
91
-
92
-        $this->manager = $manager;
93
-
94
-        $this->cache = new EntityCache($entityMap);
95
-    }
96
-
97
-    /**
98
-     * Map results to a Collection
99
-     *
100
-     * @param  Collection $results 
101
-     * @return Collection          
102
-     */
103
-    public function map($results)
104
-    {
105
-        // To implement, will allow decoupling result
106
-        // instantiation from Query class
107
-    }
108
-
109
-    /**
110
-     * Return all records for a mapped object
111
-     *
112
-     * @return EntityCollection
113
-     */
114
-    public function all()
115
-    {
116
-        return $this->query()->get();
117
-    }
118
-
119
-    /**
120
-     * Persist an entity or an entity collection into the database
121
-     *
122
-     * @param  Mappable|\Traversable|array $entity
123
-     * @throws \InvalidArgumentException
124
-     * @throws MappingException
125
-     * @return Mappable|\Traversable|array
126
-     */
127
-    public function store($entity)
128
-    {
129
-        if ($this->manager->isTraversable($entity)) {
130
-            return $this->storeCollection($entity);
131
-        } else {
132
-            return $this->storeEntity($entity);
133
-        }
134
-    }
135
-
136
-    /**
137
-     * Store an entity collection inside a single DB Transaction
138
-     *
139
-     * @param  \Traversable|array $entities
140
-     * @throws \InvalidArgumentException
141
-     * @throws MappingException
142
-     * @return \Traversable|array
143
-     */
144
-    protected function storeCollection($entities)
145
-    {
146
-        $this->adapter->beginTransaction();
147
-
148
-        foreach ($entities as $entity) {
149
-            $this->storeEntity($entity);
150
-        }
151
-
152
-        $this->adapter->commit();
153
-
154
-        return $entities;
155
-    }
156
-
157
-    /**
158
-     * Store a single entity into the database
159
-     *
160
-     * @param  Mappable $entity
161
-     * @throws \InvalidArgumentException
162
-     * @throws MappingException
163
-     * @return \Analogue\ORM\Entity
164
-     */
165
-    protected function storeEntity($entity)
166
-    {
167
-        $this->checkEntityType($entity);
168
-
169
-        $store = new Store($this->aggregate($entity), $this->newQueryBuilder());
170
-
171
-        return $store->execute();
172
-    }
173
-
174
-    /**
175
-     * Check that the entity correspond to the current mapper.
176
-     *
177
-     * @param  mixed $entity
178
-     * @throws InvalidArgumentException
179
-     * @return void
180
-     */
181
-    protected function checkEntityType($entity)
182
-    {
183
-        if (get_class($entity) != $this->entityMap->getClass() && !is_subclass_of($entity, $this->entityMap->getClass())) {
184
-            $expected = $this->entityMap->getClass();
185
-            $actual = get_class($entity);
186
-            throw new InvalidArgumentException("Expected : $expected, got $actual.");
187
-        }
188
-    }
189
-
190
-    /**
191
-     * Convert an entity into an aggregate root
192
-     *
193
-     * @param  mixed $entity
194
-     * @throws MappingException
195
-     * @return \Analogue\ORM\System\Aggregate
196
-     */
197
-    protected function aggregate($entity)
198
-    {
199
-        return new Aggregate($entity);
200
-    }
201
-
202
-    /**
203
-     * Get a the Underlying QueryAdapter.
204
-     *
205
-     * @return \Analogue\ORM\Drivers\QueryAdapter
206
-     */
207
-    public function newQueryBuilder()
208
-    {
209
-        return $this->adapter->getQuery();
210
-    }
211
-
212
-    /**
213
-     * Delete an entity or an entity collection from the database
214
-     *
215
-     * @param  Mappable|\Traversable|array
216
-     * @throws MappingException
217
-     * @throws \InvalidArgumentException
218
-     * @return \Traversable|array
219
-     */
220
-    public function delete($entity)
221
-    {
222
-        if ($this->manager->isTraversable($entity)) {
223
-            return $this->deleteCollection($entity);
224
-        } else {
225
-            $this->deleteEntity($entity);
226
-        }
227
-    }
228
-
229
-    /**
230
-     * Delete an Entity Collection inside a single db transaction
231
-     *
232
-     * @param  \Traversable|array $entities
233
-     * @throws \InvalidArgumentException
234
-     * @throws MappingException
235
-     * @return \Traversable|array
236
-     */
237
-    protected function deleteCollection($entities)
238
-    {
239
-        $this->adapter->beginTransaction();
240
-
241
-        foreach ($entities as $entity) {
242
-            $this->deleteEntity($entity);
243
-        }
244
-
245
-        $this->adapter->commit();
246
-
247
-        return $entities;
248
-    }
249
-
250
-    /**
251
-     * Delete a single entity from the database.
252
-     *
253
-     * @param  Mappable $entity
254
-     * @throws \InvalidArgumentException
255
-     * @throws MappingException
256
-     * @return void
257
-     */
258
-    protected function deleteEntity($entity)
259
-    {
260
-        $this->checkEntityType($entity);
261
-
262
-        $delete = new Delete($this->aggregate($entity), $this->newQueryBuilder());
263
-
264
-        $delete->execute();
265
-    }
266
-
267
-    /**
268
-     * Return the entity map for this mapper
269
-     *
270
-     * @return EntityMap
271
-     */
272
-    public function getEntityMap()
273
-    {
274
-        return $this->entityMap;
275
-    }
276
-
277
-    /**
278
-     * Get the entity cache for the current mapper
279
-     *
280
-     * @return EntityCache  $entityCache
281
-     */
282
-    public function getEntityCache()
283
-    {
284
-        return $this->cache;
285
-    }
286
-
287
-    /**
288
-     * Fire the given event for the entity
289
-     *
290
-     * @param  string               $event
291
-     * @param  \Analogue\ORM\Entity $entity
292
-     * @param  bool                 $halt
293
-     * @throws InvalidArgumentException
294
-     * @return mixed
295
-     */
296
-    public function fireEvent($event, $entity, $halt = true)
297
-    {
298
-        if ($entity instanceof Wrapper) {
299
-            throw new InvalidArgumentException('Fired Event with invalid Entity Object');
300
-        }
301
-
302
-        $event = "analogue.{$event}." . $this->entityMap->getClass();
303
-
304
-        $method = $halt ? 'until' : 'fire';
305
-
306
-        return $this->dispatcher->$method($event, $entity);
307
-    }
308
-
309
-    /**
310
-     * Register an entity event with the dispatcher.
311
-     *
312
-     * @param  string   $event
313
-     * @param  \Closure $callback
314
-     * @return void
315
-     */
316
-    public function registerEvent($event, $callback)
317
-    {
318
-        $name = $this->entityMap->getClass();
319
-
320
-        $this->dispatcher->listen("analogue.{$event}.{$name}", $callback);
321
-    }
322
-
323
-    /**
324
-     * Add a global scope to this mapper query builder
325
-     *
326
-     * @param  ScopeInterface $scope
327
-     * @return void
328
-     */
329
-    public function addGlobalScope(ScopeInterface $scope)
330
-    {
331
-        $this->globalScopes[get_class($scope)] = $scope;
332
-    }
333
-
334
-    /**
335
-     * Determine if the mapper has a global scope.
336
-     *
337
-     * @param  \Analogue\ORM\System\ScopeInterface $scope
338
-     * @return bool
339
-     */
340
-    public function hasGlobalScope($scope)
341
-    {
342
-        return !is_null($this->getGlobalScope($scope));
343
-    }
344
-
345
-    /**
346
-     * Get a global scope registered with the modal.
347
-     *
348
-     * @param  \Analogue\ORM\System\ScopeInterface $scope
349
-     * @return \Analogue\ORM\System\ScopeInterface|null
350
-     */
351
-    public function getGlobalScope($scope)
352
-    {
353
-        return array_first($this->globalScopes, function ($key, $value) use ($scope) {
354
-            return $scope instanceof $value;
355
-        });
356
-    }
357
-
358
-    /**
359
-     * Get a new query instance without a given scope.
360
-     *
361
-     * @param  \Analogue\ORM\System\ScopeInterface $scope
362
-     * @return \Analogue\ORM\System\Query
363
-     */
364
-    public function newQueryWithoutScope($scope)
365
-    {
366
-        $this->getGlobalScope($scope)->remove($query = $this->getQuery(), $this);
367
-
368
-        return $query;
369
-    }
370
-
371
-    /**
372
-     * Get the Analogue Query Builder for this instance
373
-     *
374
-     * @return \Analogue\ORM\System\Query
375
-     */
376
-    public function getQuery()
377
-    {
378
-        $query = new Query($this, $this->adapter);
379
-
380
-        return $this->applyGlobalScopes($query);
381
-    }
382
-
383
-    /**
384
-     * Apply all of the global scopes to an Analogue Query builder.
385
-     *
386
-     * @param Query $query
387
-     * @return \Analogue\ORM\System\Query
388
-     */
389
-    public function applyGlobalScopes($query)
390
-    {
391
-        foreach ($this->getGlobalScopes() as $scope) {
392
-            $scope->apply($query, $this);
393
-        }
394
-
395
-        return $query;
396
-    }
397
-
398
-    /**
399
-     * Get the global scopes for this class instance.
400
-     *
401
-     * @return \Analogue\ORM\System\ScopeInterface
402
-     */
403
-    public function getGlobalScopes()
404
-    {
405
-        return $this->globalScopes;
406
-    }
407
-
408
-    /**
409
-     * Add a dynamic method that extends the mapper/repository
410
-     *
411
-     * @param string $command
412
-     */
413
-    public function addCustomCommand($command)
414
-    {
415
-        $name = lcfirst(class_basename($command));
416
-
417
-        $this->customCommands[$name] = $command;
418
-    }
419
-
420
-    /**
421
-     * Create a new instance of the mapped entity class
422
-     *
423
-     * @return mixed
424
-     */
425
-    public function newInstance()
426
-    {
427
-        $class = $this->entityMap->getClass();
428
-
429
-        if($this->entityMap->useDependencyInjection()) {
430
-            return $this->newInstanceUsingDependencyInjection($class);
431
-        }
432
-
433
-        return $this->newInstanceUsingInstantiator($class);
434
-    }
435
-
436
-    /**
437
-     * Return a new object instance using dependency injection
438
-     *
439
-     * @param  string  $class
440
-     * @return mixed
441
-     */
442
-    protected function newInstanceUsingDependencyInjection($class)
443
-    {
444
-        if(! class_exists(Container::class)) {
445
-            throw new ErrorException("Illuminate\Container\Container is required to use Dependency Injection");
446
-        }
447
-
448
-        return Container::getInstance()->make($class);
449
-    }
450
-
451
-    /**
452
-     * Return a new object instance using doctrine's instantiator
453
-     *
454
-     * @param  string  $class
455
-     * @return mixed
456
-     */
457
-    protected function newInstanceUsingInstantiator($class)
458
-    {
459
-        $instantiator = new \Doctrine\Instantiator\Instantiator();
460
-
461
-        return $instantiator->instantiate($class);
462
-    }
463
-
464
-    /**
465
-     * Get an unscoped Analogue Query Builder for this instance
466
-     *
467
-     * @return \Analogue\ORM\System\Query
468
-     */
469
-    public function globalQuery()
470
-    {
471
-        return $this->newQueryWithoutScopes();
472
-    }
473
-
474
-    /**
475
-     * Get a new query builder that doesn't have any global scopes.
476
-     *
477
-     * @return Query
478
-     */
479
-    public function newQueryWithoutScopes()
480
-    {
481
-        return $this->removeGlobalScopes($this->getQuery());
482
-    }
483
-
484
-    /**
485
-     * Remove all of the global scopes from an Analogue Query builder.
486
-     *
487
-     * @param Query $query
488
-     * @return \Analogue\ORM\System\Query
489
-     */
490
-    public function removeGlobalScopes($query)
491
-    {
492
-        foreach ($this->getGlobalScopes() as $scope) {
493
-            $scope->remove($query, $this);
494
-        }
495
-
496
-        return $query;
497
-    }
498
-
499
-    /**
500
-     * Return the manager instance
501
-     *
502
-     * @return \Analogue\ORM\System\Manager
503
-     */
504
-    public function getManager()
505
-    {
506
-        return $this->manager;
507
-    }
508
-
509
-    /**
510
-     * Dynamically handle calls to custom commands, or Redirects to query()
511
-     *
512
-     * @param  string $method
513
-     * @param  array  $parameters
514
-     * @throws \Exception
515
-     * @return mixed
516
-     */
517
-    public function __call($method, $parameters)
518
-    {
519
-        // Check if method is a custom command on the mapper
520
-        if ($this->hasCustomCommand($method)) {
521
-            if (count($parameters) == 0) {
522
-                throw new \Exception("$method must at least have 1 argument");
523
-            }
524
-
525
-            return $this->executeCustomCommand($method, $parameters[0]);
526
-        }
527
-
528
-        // Redirect call on a new query instance
529
-        return call_user_func_array([$this->query(), $method], $parameters);
530
-    }
531
-
532
-    /**
533
-     * Check if this mapper supports this command
534
-     * @param  string $command
535
-     * @return boolean
536
-     */
537
-    public function hasCustomCommand($command)
538
-    {
539
-        return in_array($command, $this->getCustomCommands());
540
-    }
541
-
542
-    /**
543
-     * Get all the custom commands registered on this mapper
544
-     *
545
-     * @return array
546
-     */
547
-    public function getCustomCommands()
548
-    {
549
-        return array_keys($this->customCommands);
550
-    }
551
-
552
-    /**
553
-     * Execute a custom command on an Entity
554
-     *
555
-     * @param  string                 $command
556
-     * @param  mixed|Collection|array $entity
557
-     * @throws \InvalidArgumentException
558
-     * @throws MappingException
559
-     * @return mixed
560
-     */
561
-    public function executeCustomCommand($command, $entity)
562
-    {
563
-        $commandClass = $this->customCommands[$command];
564
-
565
-        if ($this->manager->isTraversable($entity)) {
566
-            foreach ($entity as $instance) {
567
-                $this->executeSingleCustomCommand($commandClass, $instance);
568
-            }
569
-        } else {
570
-            return $this->executeSingleCustomCommand($commandClass, $entity);
571
-        }
572
-    }
573
-
574
-    /**
575
-     * Execute a single command instance
576
-     *
577
-     * @param  string $commandClass
578
-     * @param  mixed  $entity
579
-     * @throws \InvalidArgumentException
580
-     * @throws MappingException
581
-     * @return mixed
582
-     */
583
-    protected function executeSingleCustomCommand($commandClass, $entity)
584
-    {
585
-        $this->checkEntityType($entity);
586
-
587
-        $instance = new $commandClass($this->aggregate($entity), $this->newQueryBuilder());
588
-
589
-        return $instance->execute();
590
-    }
591
-
592
-    /**
593
-     * Get the Analogue Query Builder for this instance
594
-     *
595
-     * @return \Analogue\ORM\System\Query
596
-     */
597
-    public function query()
598
-    {
599
-        return $this->getQuery();
600
-    }
28
+	/**
29
+	 * The Manager instance
30
+	 *
31
+	 * @var \Analogue\ORM\System\Manager
32
+	 */
33
+	protected $manager;
34
+
35
+	/**
36
+	 * Instance of EntityMapper Object
37
+	 *
38
+	 * @var \Analogue\ORM\EntityMap
39
+	 */
40
+	protected $entityMap;
41
+
42
+	/**
43
+	 * The instance of db adapter
44
+	 *
45
+	 * @var \Analogue\ORM\Drivers\DBAdapter
46
+	 */
47
+	protected $adapter;
48
+
49
+
50
+	/**
51
+	 * Event dispatcher instance
52
+	 *
53
+	 * @var \Illuminate\Contracts\Events\Dispatcher
54
+	 */
55
+	protected $dispatcher;
56
+
57
+	/**
58
+	 * Entity Cache
59
+	 *
60
+	 * @var  \Analogue\ORM\System\EntityCache
61
+	 */
62
+	protected $cache;
63
+
64
+	/**
65
+	 * Global scopes
66
+	 *
67
+	 * @var array
68
+	 */
69
+	protected $globalScopes = [];
70
+
71
+	/**
72
+	 * Custom Commands
73
+	 *
74
+	 * @var array
75
+	 */
76
+	protected $customCommands = [];
77
+
78
+	/**
79
+	 * @param EntityMap  $entityMap
80
+	 * @param DBAdapter  $adapter
81
+	 * @param Dispatcher $dispatcher
82
+	 * @param Manager    $manager
83
+	 */
84
+	public function __construct(EntityMap $entityMap, DBAdapter $adapter, Dispatcher $dispatcher, Manager $manager)
85
+	{
86
+		$this->entityMap = $entityMap;
87
+
88
+		$this->adapter = $adapter;
89
+
90
+		$this->dispatcher = $dispatcher;
91
+
92
+		$this->manager = $manager;
93
+
94
+		$this->cache = new EntityCache($entityMap);
95
+	}
96
+
97
+	/**
98
+	 * Map results to a Collection
99
+	 *
100
+	 * @param  Collection $results 
101
+	 * @return Collection          
102
+	 */
103
+	public function map($results)
104
+	{
105
+		// To implement, will allow decoupling result
106
+		// instantiation from Query class
107
+	}
108
+
109
+	/**
110
+	 * Return all records for a mapped object
111
+	 *
112
+	 * @return EntityCollection
113
+	 */
114
+	public function all()
115
+	{
116
+		return $this->query()->get();
117
+	}
118
+
119
+	/**
120
+	 * Persist an entity or an entity collection into the database
121
+	 *
122
+	 * @param  Mappable|\Traversable|array $entity
123
+	 * @throws \InvalidArgumentException
124
+	 * @throws MappingException
125
+	 * @return Mappable|\Traversable|array
126
+	 */
127
+	public function store($entity)
128
+	{
129
+		if ($this->manager->isTraversable($entity)) {
130
+			return $this->storeCollection($entity);
131
+		} else {
132
+			return $this->storeEntity($entity);
133
+		}
134
+	}
135
+
136
+	/**
137
+	 * Store an entity collection inside a single DB Transaction
138
+	 *
139
+	 * @param  \Traversable|array $entities
140
+	 * @throws \InvalidArgumentException
141
+	 * @throws MappingException
142
+	 * @return \Traversable|array
143
+	 */
144
+	protected function storeCollection($entities)
145
+	{
146
+		$this->adapter->beginTransaction();
147
+
148
+		foreach ($entities as $entity) {
149
+			$this->storeEntity($entity);
150
+		}
151
+
152
+		$this->adapter->commit();
153
+
154
+		return $entities;
155
+	}
156
+
157
+	/**
158
+	 * Store a single entity into the database
159
+	 *
160
+	 * @param  Mappable $entity
161
+	 * @throws \InvalidArgumentException
162
+	 * @throws MappingException
163
+	 * @return \Analogue\ORM\Entity
164
+	 */
165
+	protected function storeEntity($entity)
166
+	{
167
+		$this->checkEntityType($entity);
168
+
169
+		$store = new Store($this->aggregate($entity), $this->newQueryBuilder());
170
+
171
+		return $store->execute();
172
+	}
173
+
174
+	/**
175
+	 * Check that the entity correspond to the current mapper.
176
+	 *
177
+	 * @param  mixed $entity
178
+	 * @throws InvalidArgumentException
179
+	 * @return void
180
+	 */
181
+	protected function checkEntityType($entity)
182
+	{
183
+		if (get_class($entity) != $this->entityMap->getClass() && !is_subclass_of($entity, $this->entityMap->getClass())) {
184
+			$expected = $this->entityMap->getClass();
185
+			$actual = get_class($entity);
186
+			throw new InvalidArgumentException("Expected : $expected, got $actual.");
187
+		}
188
+	}
189
+
190
+	/**
191
+	 * Convert an entity into an aggregate root
192
+	 *
193
+	 * @param  mixed $entity
194
+	 * @throws MappingException
195
+	 * @return \Analogue\ORM\System\Aggregate
196
+	 */
197
+	protected function aggregate($entity)
198
+	{
199
+		return new Aggregate($entity);
200
+	}
201
+
202
+	/**
203
+	 * Get a the Underlying QueryAdapter.
204
+	 *
205
+	 * @return \Analogue\ORM\Drivers\QueryAdapter
206
+	 */
207
+	public function newQueryBuilder()
208
+	{
209
+		return $this->adapter->getQuery();
210
+	}
211
+
212
+	/**
213
+	 * Delete an entity or an entity collection from the database
214
+	 *
215
+	 * @param  Mappable|\Traversable|array
216
+	 * @throws MappingException
217
+	 * @throws \InvalidArgumentException
218
+	 * @return \Traversable|array
219
+	 */
220
+	public function delete($entity)
221
+	{
222
+		if ($this->manager->isTraversable($entity)) {
223
+			return $this->deleteCollection($entity);
224
+		} else {
225
+			$this->deleteEntity($entity);
226
+		}
227
+	}
228
+
229
+	/**
230
+	 * Delete an Entity Collection inside a single db transaction
231
+	 *
232
+	 * @param  \Traversable|array $entities
233
+	 * @throws \InvalidArgumentException
234
+	 * @throws MappingException
235
+	 * @return \Traversable|array
236
+	 */
237
+	protected function deleteCollection($entities)
238
+	{
239
+		$this->adapter->beginTransaction();
240
+
241
+		foreach ($entities as $entity) {
242
+			$this->deleteEntity($entity);
243
+		}
244
+
245
+		$this->adapter->commit();
246
+
247
+		return $entities;
248
+	}
249
+
250
+	/**
251
+	 * Delete a single entity from the database.
252
+	 *
253
+	 * @param  Mappable $entity
254
+	 * @throws \InvalidArgumentException
255
+	 * @throws MappingException
256
+	 * @return void
257
+	 */
258
+	protected function deleteEntity($entity)
259
+	{
260
+		$this->checkEntityType($entity);
261
+
262
+		$delete = new Delete($this->aggregate($entity), $this->newQueryBuilder());
263
+
264
+		$delete->execute();
265
+	}
266
+
267
+	/**
268
+	 * Return the entity map for this mapper
269
+	 *
270
+	 * @return EntityMap
271
+	 */
272
+	public function getEntityMap()
273
+	{
274
+		return $this->entityMap;
275
+	}
276
+
277
+	/**
278
+	 * Get the entity cache for the current mapper
279
+	 *
280
+	 * @return EntityCache  $entityCache
281
+	 */
282
+	public function getEntityCache()
283
+	{
284
+		return $this->cache;
285
+	}
286
+
287
+	/**
288
+	 * Fire the given event for the entity
289
+	 *
290
+	 * @param  string               $event
291
+	 * @param  \Analogue\ORM\Entity $entity
292
+	 * @param  bool                 $halt
293
+	 * @throws InvalidArgumentException
294
+	 * @return mixed
295
+	 */
296
+	public function fireEvent($event, $entity, $halt = true)
297
+	{
298
+		if ($entity instanceof Wrapper) {
299
+			throw new InvalidArgumentException('Fired Event with invalid Entity Object');
300
+		}
301
+
302
+		$event = "analogue.{$event}." . $this->entityMap->getClass();
303
+
304
+		$method = $halt ? 'until' : 'fire';
305
+
306
+		return $this->dispatcher->$method($event, $entity);
307
+	}
308
+
309
+	/**
310
+	 * Register an entity event with the dispatcher.
311
+	 *
312
+	 * @param  string   $event
313
+	 * @param  \Closure $callback
314
+	 * @return void
315
+	 */
316
+	public function registerEvent($event, $callback)
317
+	{
318
+		$name = $this->entityMap->getClass();
319
+
320
+		$this->dispatcher->listen("analogue.{$event}.{$name}", $callback);
321
+	}
322
+
323
+	/**
324
+	 * Add a global scope to this mapper query builder
325
+	 *
326
+	 * @param  ScopeInterface $scope
327
+	 * @return void
328
+	 */
329
+	public function addGlobalScope(ScopeInterface $scope)
330
+	{
331
+		$this->globalScopes[get_class($scope)] = $scope;
332
+	}
333
+
334
+	/**
335
+	 * Determine if the mapper has a global scope.
336
+	 *
337
+	 * @param  \Analogue\ORM\System\ScopeInterface $scope
338
+	 * @return bool
339
+	 */
340
+	public function hasGlobalScope($scope)
341
+	{
342
+		return !is_null($this->getGlobalScope($scope));
343
+	}
344
+
345
+	/**
346
+	 * Get a global scope registered with the modal.
347
+	 *
348
+	 * @param  \Analogue\ORM\System\ScopeInterface $scope
349
+	 * @return \Analogue\ORM\System\ScopeInterface|null
350
+	 */
351
+	public function getGlobalScope($scope)
352
+	{
353
+		return array_first($this->globalScopes, function ($key, $value) use ($scope) {
354
+			return $scope instanceof $value;
355
+		});
356
+	}
357
+
358
+	/**
359
+	 * Get a new query instance without a given scope.
360
+	 *
361
+	 * @param  \Analogue\ORM\System\ScopeInterface $scope
362
+	 * @return \Analogue\ORM\System\Query
363
+	 */
364
+	public function newQueryWithoutScope($scope)
365
+	{
366
+		$this->getGlobalScope($scope)->remove($query = $this->getQuery(), $this);
367
+
368
+		return $query;
369
+	}
370
+
371
+	/**
372
+	 * Get the Analogue Query Builder for this instance
373
+	 *
374
+	 * @return \Analogue\ORM\System\Query
375
+	 */
376
+	public function getQuery()
377
+	{
378
+		$query = new Query($this, $this->adapter);
379
+
380
+		return $this->applyGlobalScopes($query);
381
+	}
382
+
383
+	/**
384
+	 * Apply all of the global scopes to an Analogue Query builder.
385
+	 *
386
+	 * @param Query $query
387
+	 * @return \Analogue\ORM\System\Query
388
+	 */
389
+	public function applyGlobalScopes($query)
390
+	{
391
+		foreach ($this->getGlobalScopes() as $scope) {
392
+			$scope->apply($query, $this);
393
+		}
394
+
395
+		return $query;
396
+	}
397
+
398
+	/**
399
+	 * Get the global scopes for this class instance.
400
+	 *
401
+	 * @return \Analogue\ORM\System\ScopeInterface
402
+	 */
403
+	public function getGlobalScopes()
404
+	{
405
+		return $this->globalScopes;
406
+	}
407
+
408
+	/**
409
+	 * Add a dynamic method that extends the mapper/repository
410
+	 *
411
+	 * @param string $command
412
+	 */
413
+	public function addCustomCommand($command)
414
+	{
415
+		$name = lcfirst(class_basename($command));
416
+
417
+		$this->customCommands[$name] = $command;
418
+	}
419
+
420
+	/**
421
+	 * Create a new instance of the mapped entity class
422
+	 *
423
+	 * @return mixed
424
+	 */
425
+	public function newInstance()
426
+	{
427
+		$class = $this->entityMap->getClass();
428
+
429
+		if($this->entityMap->useDependencyInjection()) {
430
+			return $this->newInstanceUsingDependencyInjection($class);
431
+		}
432
+
433
+		return $this->newInstanceUsingInstantiator($class);
434
+	}
435
+
436
+	/**
437
+	 * Return a new object instance using dependency injection
438
+	 *
439
+	 * @param  string  $class
440
+	 * @return mixed
441
+	 */
442
+	protected function newInstanceUsingDependencyInjection($class)
443
+	{
444
+		if(! class_exists(Container::class)) {
445
+			throw new ErrorException("Illuminate\Container\Container is required to use Dependency Injection");
446
+		}
447
+
448
+		return Container::getInstance()->make($class);
449
+	}
450
+
451
+	/**
452
+	 * Return a new object instance using doctrine's instantiator
453
+	 *
454
+	 * @param  string  $class
455
+	 * @return mixed
456
+	 */
457
+	protected function newInstanceUsingInstantiator($class)
458
+	{
459
+		$instantiator = new \Doctrine\Instantiator\Instantiator();
460
+
461
+		return $instantiator->instantiate($class);
462
+	}
463
+
464
+	/**
465
+	 * Get an unscoped Analogue Query Builder for this instance
466
+	 *
467
+	 * @return \Analogue\ORM\System\Query
468
+	 */
469
+	public function globalQuery()
470
+	{
471
+		return $this->newQueryWithoutScopes();
472
+	}
473
+
474
+	/**
475
+	 * Get a new query builder that doesn't have any global scopes.
476
+	 *
477
+	 * @return Query
478
+	 */
479
+	public function newQueryWithoutScopes()
480
+	{
481
+		return $this->removeGlobalScopes($this->getQuery());
482
+	}
483
+
484
+	/**
485
+	 * Remove all of the global scopes from an Analogue Query builder.
486
+	 *
487
+	 * @param Query $query
488
+	 * @return \Analogue\ORM\System\Query
489
+	 */
490
+	public function removeGlobalScopes($query)
491
+	{
492
+		foreach ($this->getGlobalScopes() as $scope) {
493
+			$scope->remove($query, $this);
494
+		}
495
+
496
+		return $query;
497
+	}
498
+
499
+	/**
500
+	 * Return the manager instance
501
+	 *
502
+	 * @return \Analogue\ORM\System\Manager
503
+	 */
504
+	public function getManager()
505
+	{
506
+		return $this->manager;
507
+	}
508
+
509
+	/**
510
+	 * Dynamically handle calls to custom commands, or Redirects to query()
511
+	 *
512
+	 * @param  string $method
513
+	 * @param  array  $parameters
514
+	 * @throws \Exception
515
+	 * @return mixed
516
+	 */
517
+	public function __call($method, $parameters)
518
+	{
519
+		// Check if method is a custom command on the mapper
520
+		if ($this->hasCustomCommand($method)) {
521
+			if (count($parameters) == 0) {
522
+				throw new \Exception("$method must at least have 1 argument");
523
+			}
524
+
525
+			return $this->executeCustomCommand($method, $parameters[0]);
526
+		}
527
+
528
+		// Redirect call on a new query instance
529
+		return call_user_func_array([$this->query(), $method], $parameters);
530
+	}
531
+
532
+	/**
533
+	 * Check if this mapper supports this command
534
+	 * @param  string $command
535
+	 * @return boolean
536
+	 */
537
+	public function hasCustomCommand($command)
538
+	{
539
+		return in_array($command, $this->getCustomCommands());
540
+	}
541
+
542
+	/**
543
+	 * Get all the custom commands registered on this mapper
544
+	 *
545
+	 * @return array
546
+	 */
547
+	public function getCustomCommands()
548
+	{
549
+		return array_keys($this->customCommands);
550
+	}
551
+
552
+	/**
553
+	 * Execute a custom command on an Entity
554
+	 *
555
+	 * @param  string                 $command
556
+	 * @param  mixed|Collection|array $entity
557
+	 * @throws \InvalidArgumentException
558
+	 * @throws MappingException
559
+	 * @return mixed
560
+	 */
561
+	public function executeCustomCommand($command, $entity)
562
+	{
563
+		$commandClass = $this->customCommands[$command];
564
+
565
+		if ($this->manager->isTraversable($entity)) {
566
+			foreach ($entity as $instance) {
567
+				$this->executeSingleCustomCommand($commandClass, $instance);
568
+			}
569
+		} else {
570
+			return $this->executeSingleCustomCommand($commandClass, $entity);
571
+		}
572
+	}
573
+
574
+	/**
575
+	 * Execute a single command instance
576
+	 *
577
+	 * @param  string $commandClass
578
+	 * @param  mixed  $entity
579
+	 * @throws \InvalidArgumentException
580
+	 * @throws MappingException
581
+	 * @return mixed
582
+	 */
583
+	protected function executeSingleCustomCommand($commandClass, $entity)
584
+	{
585
+		$this->checkEntityType($entity);
586
+
587
+		$instance = new $commandClass($this->aggregate($entity), $this->newQueryBuilder());
588
+
589
+		return $instance->execute();
590
+	}
591
+
592
+	/**
593
+	 * Get the Analogue Query Builder for this instance
594
+	 *
595
+	 * @return \Analogue\ORM\System\Query
596
+	 */
597
+	public function query()
598
+	{
599
+		return $this->getQuery();
600
+	}
601 601
 }
Please login to merge, or discard this patch.
src/Commands/Command.php 1 patch
Indentation   +23 added lines, -23 removed lines patch added patch discarded remove patch
@@ -7,31 +7,31 @@
 block discarded – undo
7 7
 
8 8
 abstract class Command
9 9
 {
10
-    /**
11
-     * The aggregated entity on which the command is executed
12
-     *
13
-     * @var \Analogue\ORM\System\Aggregate
14
-     */
15
-    protected $aggregate;
10
+	/**
11
+	 * The aggregated entity on which the command is executed
12
+	 *
13
+	 * @var \Analogue\ORM\System\Aggregate
14
+	 */
15
+	protected $aggregate;
16 16
 
17
-    /**
18
-     * Query Builder instance
19
-     *
20
-     * @var \Illuminate\Database\Query\Builder
21
-     */
22
-    protected $query;
17
+	/**
18
+	 * Query Builder instance
19
+	 *
20
+	 * @var \Illuminate\Database\Query\Builder
21
+	 */
22
+	protected $query;
23 23
 
24
-    /**
25
-     * Command constructor.
26
-     * @param Aggregate $aggregate
27
-     * @param QueryAdapter|\Analogue\ORM\Drivers\IlluminateQueryAdapter $query
28
-     */
29
-    public function __construct(Aggregate $aggregate, Builder $query)
30
-    {
31
-        $this->aggregate = $aggregate;
24
+	/**
25
+	 * Command constructor.
26
+	 * @param Aggregate $aggregate
27
+	 * @param QueryAdapter|\Analogue\ORM\Drivers\IlluminateQueryAdapter $query
28
+	 */
29
+	public function __construct(Aggregate $aggregate, Builder $query)
30
+	{
31
+		$this->aggregate = $aggregate;
32 32
 
33
-        $this->query = $query->from($aggregate->getEntityMap()->getTable());
34
-    }
33
+		$this->query = $query->from($aggregate->getEntityMap()->getTable());
34
+	}
35 35
 
36
-    abstract public function execute();
36
+	abstract public function execute();
37 37
 }
Please login to merge, or discard this patch.
src/Commands/Store.php 2 patches
Braces   +1 added lines, -2 removed lines patch added patch discarded remove patch
@@ -44,8 +44,7 @@
 block discarded – undo
44 44
             $this->insert();
45 45
 
46 46
             $mapper->fireEvent('created', $entity, false);
47
-        }
48
-        else if ($this->aggregate->isDirty()) {
47
+        } else if ($this->aggregate->isDirty()) {
49 48
             if ($mapper->fireEvent('updating', $entity) === false) {
50 49
                 return false;
51 50
             }
Please login to merge, or discard this patch.
Indentation   +221 added lines, -221 removed lines patch added patch discarded remove patch
@@ -14,238 +14,238 @@
 block discarded – undo
14 14
  */
15 15
 class Store extends Command
16 16
 {
17
-    /**
18
-     * Persist the entity in the database
19
-     *
20
-     * @throws \InvalidArgumentException
21
-     * @return false|mixed
22
-     */
23
-    public function execute()
24
-    {
25
-        $entity = $this->aggregate->getEntityObject();
26
-
27
-        $mapper = $this->aggregate->getMapper();
28
-
29
-        if ($mapper->fireEvent('storing', $entity) === false) {
30
-            return false;
31
-        }
32
-
33
-        $this->preStoreProcess();
34
-
35
-        /**
36
-         * We will test the entity for existence
37
-         * and run a creation if it doesn't exists
38
-         */
39
-        if (!$this->aggregate->exists()) {
40
-            if ($mapper->fireEvent('creating', $entity) === false) {
41
-                return false;
42
-            }
43
-
44
-            $this->insert();
45
-
46
-            $mapper->fireEvent('created', $entity, false);
47
-        }
48
-        else if ($this->aggregate->isDirty()) {
49
-            if ($mapper->fireEvent('updating', $entity) === false) {
50
-                return false;
51
-            }
52
-            $this->update();
53
-
54
-            $mapper->fireEvent('updated', $entity, false);
55
-        }
56
-
57
-        $this->postStoreProcess();
58
-
59
-        $mapper->fireEvent('stored', $entity, false);
60
-
61
-        return $entity;
62
-    }
63
-
64
-    /**
65
-     * Run all operations that have to occur before actually
66
-     * storing the entity
67
-     *
68
-     * @throws \InvalidArgumentException
69
-     * @return void
70
-     */
71
-    protected function preStoreProcess()
72
-    {
73
-        // Create any related object that doesn't exist in the database.
74
-        $localRelationships = $this->aggregate->getEntityMap()->getLocalRelationships();
75
-
76
-        $this->createRelatedEntities($localRelationships);
77
-
78
-        // Now we can sync the related collections
79
-        $this->aggregate->syncRelationships($localRelationships);
80
-    }
81
-
82
-    /**
83
-     * Check for existence and create non-existing related entities
84
-     *
85
-     * @param  array
86
-     * @throws \InvalidArgumentException
87
-     * @return void
88
-     */
89
-    protected function createRelatedEntities($relations)
90
-    {
91
-        $entitiesToCreate = $this->aggregate->getNonExistingRelated($relations);
92
-
93
-        foreach ($entitiesToCreate as $aggregate) {
94
-            $this->createStoreCommand($aggregate)->execute();
95
-        }
17
+	/**
18
+	 * Persist the entity in the database
19
+	 *
20
+	 * @throws \InvalidArgumentException
21
+	 * @return false|mixed
22
+	 */
23
+	public function execute()
24
+	{
25
+		$entity = $this->aggregate->getEntityObject();
26
+
27
+		$mapper = $this->aggregate->getMapper();
28
+
29
+		if ($mapper->fireEvent('storing', $entity) === false) {
30
+			return false;
31
+		}
32
+
33
+		$this->preStoreProcess();
34
+
35
+		/**
36
+		 * We will test the entity for existence
37
+		 * and run a creation if it doesn't exists
38
+		 */
39
+		if (!$this->aggregate->exists()) {
40
+			if ($mapper->fireEvent('creating', $entity) === false) {
41
+				return false;
42
+			}
43
+
44
+			$this->insert();
45
+
46
+			$mapper->fireEvent('created', $entity, false);
47
+		}
48
+		else if ($this->aggregate->isDirty()) {
49
+			if ($mapper->fireEvent('updating', $entity) === false) {
50
+				return false;
51
+			}
52
+			$this->update();
53
+
54
+			$mapper->fireEvent('updated', $entity, false);
55
+		}
56
+
57
+		$this->postStoreProcess();
58
+
59
+		$mapper->fireEvent('stored', $entity, false);
60
+
61
+		return $entity;
62
+	}
63
+
64
+	/**
65
+	 * Run all operations that have to occur before actually
66
+	 * storing the entity
67
+	 *
68
+	 * @throws \InvalidArgumentException
69
+	 * @return void
70
+	 */
71
+	protected function preStoreProcess()
72
+	{
73
+		// Create any related object that doesn't exist in the database.
74
+		$localRelationships = $this->aggregate->getEntityMap()->getLocalRelationships();
75
+
76
+		$this->createRelatedEntities($localRelationships);
77
+
78
+		// Now we can sync the related collections
79
+		$this->aggregate->syncRelationships($localRelationships);
80
+	}
81
+
82
+	/**
83
+	 * Check for existence and create non-existing related entities
84
+	 *
85
+	 * @param  array
86
+	 * @throws \InvalidArgumentException
87
+	 * @return void
88
+	 */
89
+	protected function createRelatedEntities($relations)
90
+	{
91
+		$entitiesToCreate = $this->aggregate->getNonExistingRelated($relations);
92
+
93
+		foreach ($entitiesToCreate as $aggregate) {
94
+			$this->createStoreCommand($aggregate)->execute();
95
+		}
96 96
         
97
-    }
98
-
99
-    /**
100
-     * Create a new store command
101
-     *
102
-     * @param  Aggregate $aggregate
103
-     * @return Store
104
-     */
105
-    protected function createStoreCommand(Aggregate $aggregate)
106
-    {
107
-        // We gotta retrieve the corresponding query adapter to use.
108
-        $mapper = $aggregate->getMapper();
109
-
110
-        return new Store($aggregate, $mapper->newQueryBuilder());
111
-    }
112
-
113
-    /**
114
-     * Run all operations that have to occur after the entity
115
-     * is stored.
116
-     *
117
-     * @throws \InvalidArgumentException
118
-     * @return void
119
-     */
120
-    protected function postStoreProcess()
121
-    {
122
-        $aggregate = $this->aggregate;
123
-
124
-        // Create any related object that doesn't exist in the database.
125
-        $foreignRelationships = $aggregate->getEntityMap()->getForeignRelationships();
126
-
127
-        $this->createRelatedEntities($foreignRelationships);
128
-
129
-        // Update any pivot tables that has been modified.
130
-        $aggregate->updatePivotRecords();
131
-
132
-        // Update any dirty relationship. This include relationships that already exists, have
133
-        // dirty attributes / newly created related entities / dirty related entities.
134
-        $dirtyRelatedAggregates = $aggregate->getDirtyRelationships();
97
+	}
98
+
99
+	/**
100
+	 * Create a new store command
101
+	 *
102
+	 * @param  Aggregate $aggregate
103
+	 * @return Store
104
+	 */
105
+	protected function createStoreCommand(Aggregate $aggregate)
106
+	{
107
+		// We gotta retrieve the corresponding query adapter to use.
108
+		$mapper = $aggregate->getMapper();
109
+
110
+		return new Store($aggregate, $mapper->newQueryBuilder());
111
+	}
112
+
113
+	/**
114
+	 * Run all operations that have to occur after the entity
115
+	 * is stored.
116
+	 *
117
+	 * @throws \InvalidArgumentException
118
+	 * @return void
119
+	 */
120
+	protected function postStoreProcess()
121
+	{
122
+		$aggregate = $this->aggregate;
123
+
124
+		// Create any related object that doesn't exist in the database.
125
+		$foreignRelationships = $aggregate->getEntityMap()->getForeignRelationships();
126
+
127
+		$this->createRelatedEntities($foreignRelationships);
128
+
129
+		// Update any pivot tables that has been modified.
130
+		$aggregate->updatePivotRecords();
131
+
132
+		// Update any dirty relationship. This include relationships that already exists, have
133
+		// dirty attributes / newly created related entities / dirty related entities.
134
+		$dirtyRelatedAggregates = $aggregate->getDirtyRelationships();
135 135
         
136
-        foreach ($dirtyRelatedAggregates as $related) {
137
-            $this->createStoreCommand($related)->execute();
138
-        }
139
-
140
-        // Now we can sync the related collections
141
-        if ($this->aggregate->exists()) {
142
-            $this->aggregate->syncRelationships($foreignRelationships);
143
-        }
136
+		foreach ($dirtyRelatedAggregates as $related) {
137
+			$this->createStoreCommand($related)->execute();
138
+		}
139
+
140
+		// Now we can sync the related collections
141
+		if ($this->aggregate->exists()) {
142
+			$this->aggregate->syncRelationships($foreignRelationships);
143
+		}
144 144
         
145
-        // TODO be move it to the wrapper class
146
-        // so it's the same code for the entity builder
147
-        $aggregate->setProxies();
148
-
149
-        // Update Entity Cache
150
-        $aggregate->getMapper()->getEntityCache()->refresh($aggregate);
151
-    }
152
-
153
-    /**
154
-     * Update Related Entities which attributes have
155
-     * been modified.
156
-     *
157
-     * @return void
158
-     */
159
-    protected function updateDirtyRelated()
160
-    {
161
-        $relations = $this->entityMap->getRelationships();
162
-        $attributes = $this->getAttributes();
163
-
164
-        foreach ($relations as $relation) {
165
-            if (!array_key_exists($relation, $attributes)) {
166
-                continue;
167
-            }
168
-
169
-            $value = $attributes[$relation];
170
-
171
-            if ($value == null) {
172
-                continue;
173
-            }
174
-
175
-            if ($value instanceof EntityProxy) {
176
-                continue;
177
-            }
178
-
179
-            if ($value instanceof CollectionProxy && $value->isLoaded()) {
180
-                $value = $value->getUnderlyingCollection();
181
-            }
182
-            if ($value instanceof CollectionProxy && !$value->isLoaded()) {
183
-                foreach ($value->getAddedItems() as $entity) {
184
-                    $this->updateEntityIfDirty($entity);
185
-                }
186
-                continue;
187
-            }
188
-
189
-            if ($value instanceof EntityCollection) {
190
-                foreach ($value as $entity) {
191
-                    if (!$this->createEntityIfNotExists($entity)) {
192
-                        $this->updateEntityIfDirty($entity);
193
-                    }
194
-                }
195
-                continue;
196
-            }
197
-            if ($value instanceof Mappable) {
198
-                $this->updateEntityIfDirty($value);
199
-                continue;
200
-            }
201
-        }
202
-    }
203
-
204
-    /**
205
-     * Execute an insert statement on the database
206
-     *
207
-     * @return void
208
-     */
209
-    protected function insert()
210
-    {
211
-        $aggregate = $this->aggregate;
212
-
213
-        $attributes = $aggregate->getRawAttributes();
145
+		// TODO be move it to the wrapper class
146
+		// so it's the same code for the entity builder
147
+		$aggregate->setProxies();
148
+
149
+		// Update Entity Cache
150
+		$aggregate->getMapper()->getEntityCache()->refresh($aggregate);
151
+	}
152
+
153
+	/**
154
+	 * Update Related Entities which attributes have
155
+	 * been modified.
156
+	 *
157
+	 * @return void
158
+	 */
159
+	protected function updateDirtyRelated()
160
+	{
161
+		$relations = $this->entityMap->getRelationships();
162
+		$attributes = $this->getAttributes();
163
+
164
+		foreach ($relations as $relation) {
165
+			if (!array_key_exists($relation, $attributes)) {
166
+				continue;
167
+			}
168
+
169
+			$value = $attributes[$relation];
170
+
171
+			if ($value == null) {
172
+				continue;
173
+			}
174
+
175
+			if ($value instanceof EntityProxy) {
176
+				continue;
177
+			}
178
+
179
+			if ($value instanceof CollectionProxy && $value->isLoaded()) {
180
+				$value = $value->getUnderlyingCollection();
181
+			}
182
+			if ($value instanceof CollectionProxy && !$value->isLoaded()) {
183
+				foreach ($value->getAddedItems() as $entity) {
184
+					$this->updateEntityIfDirty($entity);
185
+				}
186
+				continue;
187
+			}
188
+
189
+			if ($value instanceof EntityCollection) {
190
+				foreach ($value as $entity) {
191
+					if (!$this->createEntityIfNotExists($entity)) {
192
+						$this->updateEntityIfDirty($entity);
193
+					}
194
+				}
195
+				continue;
196
+			}
197
+			if ($value instanceof Mappable) {
198
+				$this->updateEntityIfDirty($value);
199
+				continue;
200
+			}
201
+		}
202
+	}
203
+
204
+	/**
205
+	 * Execute an insert statement on the database
206
+	 *
207
+	 * @return void
208
+	 */
209
+	protected function insert()
210
+	{
211
+		$aggregate = $this->aggregate;
212
+
213
+		$attributes = $aggregate->getRawAttributes();
214 214
         
215
-        $keyName = $aggregate->getEntityMap()->getKeyName();
215
+		$keyName = $aggregate->getEntityMap()->getKeyName();
216 216
 
217
-        // Check if the primary key is defined in the attributes
218
-        if (array_key_exists($keyName, $attributes) && $attributes[$keyName] != null) {
219
-            $this->query->insert($attributes);
220
-        } else {
221
-            $sequence = $aggregate->getEntityMap()->getSequence();
217
+		// Check if the primary key is defined in the attributes
218
+		if (array_key_exists($keyName, $attributes) && $attributes[$keyName] != null) {
219
+			$this->query->insert($attributes);
220
+		} else {
221
+			$sequence = $aggregate->getEntityMap()->getSequence();
222 222
 
223
-            $id = $this->query->insertGetId($attributes, $sequence);
223
+			$id = $this->query->insertGetId($attributes, $sequence);
224 224
 
225
-            $aggregate->setEntityAttribute($keyName, $id);
226
-        }
225
+			$aggregate->setEntityAttribute($keyName, $id);
226
+		}
227 227
 
228
-    }
228
+	}
229 229
 
230
-    /**
231
-     * Run an update statement on the entity
232
-     *
233
-     * @throws \InvalidArgumentException
234
-     *
235
-     * @return void
236
-     */
237
-    protected function update()
238
-    {
239
-        $query = $this->query;
230
+	/**
231
+	 * Run an update statement on the entity
232
+	 *
233
+	 * @throws \InvalidArgumentException
234
+	 *
235
+	 * @return void
236
+	 */
237
+	protected function update()
238
+	{
239
+		$query = $this->query;
240 240
 
241
-        $keyName = $this->aggregate->getEntityKey();
241
+		$keyName = $this->aggregate->getEntityKey();
242 242
 
243
-        $query = $query->where($keyName, '=', $this->aggregate->getEntityId());
243
+		$query = $query->where($keyName, '=', $this->aggregate->getEntityId());
244 244
 
245
-        $dirtyAttributes = $this->aggregate->getDirtyRawAttributes();
245
+		$dirtyAttributes = $this->aggregate->getDirtyRawAttributes();
246 246
 
247
-        if (count($dirtyAttributes) > 0) {
248
-            $query->update($dirtyAttributes);
249
-        }
250
-    }
247
+		if (count($dirtyAttributes) > 0) {
248
+			$query->update($dirtyAttributes);
249
+		}
250
+	}
251 251
 }
Please login to merge, or discard this patch.
src/Drivers/IlluminateQueryBuilder.php 1 patch
Indentation   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -6,5 +6,5 @@
 block discarded – undo
6 6
 
7 7
 class EntityMapNotFoundException extends RuntimeException
8 8
 {
9
-    //
9
+	//
10 10
 }
Please login to merge, or discard this patch.
src/Entity.php 3 patches
Indentation   +116 added lines, -116 removed lines patch added patch discarded remove patch
@@ -6,133 +6,133 @@
 block discarded – undo
6 6
 
7 7
 class Entity extends ValueObject
8 8
 {
9
-    /**
10
-     * Entities Hidden Attributes, that will be discarded when converting
11
-     * the entity to Array/Json
12
-     * (can include any embedded object's attribute)
13
-     *
14
-     * @var array
15
-     */
16
-    protected $hidden = [];
9
+	/**
10
+	 * Entities Hidden Attributes, that will be discarded when converting
11
+	 * the entity to Array/Json
12
+	 * (can include any embedded object's attribute)
13
+	 *
14
+	 * @var array
15
+	 */
16
+	protected $hidden = [];
17 17
 
18
-    /**
19
-     * Return the entity's attribute
20
-     * @param  string $key
21
-     * @return mixed
22
-     */
23
-    public function __get($key)
24
-    {
25
-        if ($this->hasGetMutator($key)) {
26
-            $method = 'get' . $this->getMutatorMethod($key);
18
+	/**
19
+	 * Return the entity's attribute
20
+	 * @param  string $key
21
+	 * @return mixed
22
+	 */
23
+	public function __get($key)
24
+	{
25
+		if ($this->hasGetMutator($key)) {
26
+			$method = 'get' . $this->getMutatorMethod($key);
27 27
 
28
-            $attribute = null;
28
+			$attribute = null;
29 29
 
30
-            if (isset($this->attributes[$key])) {
31
-                $attribute = $this->attributes[$key];
32
-            }
30
+			if (isset($this->attributes[$key])) {
31
+				$attribute = $this->attributes[$key];
32
+			}
33 33
 
34
-            return $this->$method($attribute);
35
-        }
36
-        if (!array_key_exists($key, $this->attributes)) {
37
-            return null;
38
-        }
39
-        if ($this->attributes[$key] instanceof EntityProxy) {
40
-            $this->attributes[$key] = $this->attributes[$key]->load();
41
-        }
42
-        return $this->attributes[$key];
43
-    }
34
+			return $this->$method($attribute);
35
+		}
36
+		if (!array_key_exists($key, $this->attributes)) {
37
+			return null;
38
+		}
39
+		if ($this->attributes[$key] instanceof EntityProxy) {
40
+			$this->attributes[$key] = $this->attributes[$key]->load();
41
+		}
42
+		return $this->attributes[$key];
43
+	}
44 44
 
45
-    /**
46
-     * Dynamically set attributes on the entity.
47
-     *
48
-     * @param  string $key
49
-     * @param  mixed  $value
50
-     * @return void
51
-     */
52
-    public function __set($key, $value)
53
-    {
54
-        if ($this->hasSetMutator($key)) {
55
-            $method = 'set' . $this->getMutatorMethod($key);
45
+	/**
46
+	 * Dynamically set attributes on the entity.
47
+	 *
48
+	 * @param  string $key
49
+	 * @param  mixed  $value
50
+	 * @return void
51
+	 */
52
+	public function __set($key, $value)
53
+	{
54
+		if ($this->hasSetMutator($key)) {
55
+			$method = 'set' . $this->getMutatorMethod($key);
56 56
 
57
-            $this->$method($value);
58
-        } else {
59
-            $this->attributes[$key] = $value;
60
-        }
61
-    }
57
+			$this->$method($value);
58
+		} else {
59
+			$this->attributes[$key] = $value;
60
+		}
61
+	}
62 62
 
63
-    /**
64
-     * Is a getter method defined ?
65
-     *
66
-     * @param  string $key
67
-     * @return boolean
68
-     */
69
-    protected function hasGetMutator($key)
70
-    {
71
-        return method_exists($this, 'get' . $this->getMutatorMethod($key)) ? true : false;
72
-    }
63
+	/**
64
+	 * Is a getter method defined ?
65
+	 *
66
+	 * @param  string $key
67
+	 * @return boolean
68
+	 */
69
+	protected function hasGetMutator($key)
70
+	{
71
+		return method_exists($this, 'get' . $this->getMutatorMethod($key)) ? true : false;
72
+	}
73 73
 
74
-    /**
75
-     * Is a setter method defined ?
76
-     *
77
-     * @param  string $key
78
-     * @return boolean
79
-     */
80
-    protected function hasSetMutator($key)
81
-    {
82
-        return method_exists($this, 'set' . $this->getMutatorMethod($key)) ? true : false;
83
-    }
74
+	/**
75
+	 * Is a setter method defined ?
76
+	 *
77
+	 * @param  string $key
78
+	 * @return boolean
79
+	 */
80
+	protected function hasSetMutator($key)
81
+	{
82
+		return method_exists($this, 'set' . $this->getMutatorMethod($key)) ? true : false;
83
+	}
84 84
 
85
-    /**
86
-     * @param $key
87
-     * @return string
88
-     */
89
-    protected function getMutatorMethod($key)
90
-    {
91
-        $key = ucwords(str_replace(['-', '_'], ' ', $key));
92
-        return str_replace(' ', '', $key) . "Attribute";
93
-    }
85
+	/**
86
+	 * @param $key
87
+	 * @return string
88
+	 */
89
+	protected function getMutatorMethod($key)
90
+	{
91
+		$key = ucwords(str_replace(['-', '_'], ' ', $key));
92
+		return str_replace(' ', '', $key) . "Attribute";
93
+	}
94 94
 
95
-    /**
96
-     * Convert every attributes to value / arrays
97
-     *
98
-     * @return array
99
-     */
100
-    public function toArray()
101
-    {
102
-        // First, call the trait method before filtering
103
-        // with Entity specific methods
104
-        $attributes = $this->attributesToArray($this->attributes);
95
+	/**
96
+	 * Convert every attributes to value / arrays
97
+	 *
98
+	 * @return array
99
+	 */
100
+	public function toArray()
101
+	{
102
+		// First, call the trait method before filtering
103
+		// with Entity specific methods
104
+		$attributes = $this->attributesToArray($this->attributes);
105 105
 
106
-        foreach ($this->attributes as $key => $attribute) {
107
-            if (in_array($key, $this->hidden)) {
108
-                unset($attributes[$key]);
109
-                continue;
110
-            }
111
-            if ($this->hasGetMutator($key)) {
112
-                $method = 'get' . $this->getMutatorMethod($key);
113
-                $attributes[$key] = $this->$method($attribute);
114
-            }
115
-        }
116
-        return $attributes;
117
-    }
106
+		foreach ($this->attributes as $key => $attribute) {
107
+			if (in_array($key, $this->hidden)) {
108
+				unset($attributes[$key]);
109
+				continue;
110
+			}
111
+			if ($this->hasGetMutator($key)) {
112
+				$method = 'get' . $this->getMutatorMethod($key);
113
+				$attributes[$key] = $this->$method($attribute);
114
+			}
115
+		}
116
+		return $attributes;
117
+	}
118 118
 
119
-    /**
120
-     * Fill an entity with key-value pairs
121
-     * 
122
-     * @param  array  $attributes 
123
-     * @return void
124
-     */
125
-    public function fill(array $attributes)
126
-    {
127
-        foreach ($attributes as $key => $attribute) {
119
+	/**
120
+	 * Fill an entity with key-value pairs
121
+	 * 
122
+	 * @param  array  $attributes 
123
+	 * @return void
124
+	 */
125
+	public function fill(array $attributes)
126
+	{
127
+		foreach ($attributes as $key => $attribute) {
128 128
             
129
-            if ($this->hasSetMutator($key)) {
130
-                $method = 'set' . $this->getMutatorMethod($key);
131
-                $this->attributes[$key] = $this->$method($attribute);
132
-            }
133
-            else {
134
-                $this->attributes[$key] = $attribute;
135
-            }
136
-        }
137
-    }
129
+			if ($this->hasSetMutator($key)) {
130
+				$method = 'set' . $this->getMutatorMethod($key);
131
+				$this->attributes[$key] = $this->$method($attribute);
132
+			}
133
+			else {
134
+				$this->attributes[$key] = $attribute;
135
+			}
136
+		}
137
+	}
138 138
 }
Please login to merge, or discard this patch.
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -23,7 +23,7 @@  discard block
 block discarded – undo
23 23
     public function __get($key)
24 24
     {
25 25
         if ($this->hasGetMutator($key)) {
26
-            $method = 'get' . $this->getMutatorMethod($key);
26
+            $method = 'get'.$this->getMutatorMethod($key);
27 27
 
28 28
             $attribute = null;
29 29
 
@@ -52,7 +52,7 @@  discard block
 block discarded – undo
52 52
     public function __set($key, $value)
53 53
     {
54 54
         if ($this->hasSetMutator($key)) {
55
-            $method = 'set' . $this->getMutatorMethod($key);
55
+            $method = 'set'.$this->getMutatorMethod($key);
56 56
 
57 57
             $this->$method($value);
58 58
         } else {
@@ -68,7 +68,7 @@  discard block
 block discarded – undo
68 68
      */
69 69
     protected function hasGetMutator($key)
70 70
     {
71
-        return method_exists($this, 'get' . $this->getMutatorMethod($key)) ? true : false;
71
+        return method_exists($this, 'get'.$this->getMutatorMethod($key)) ? true : false;
72 72
     }
73 73
 
74 74
     /**
@@ -79,7 +79,7 @@  discard block
 block discarded – undo
79 79
      */
80 80
     protected function hasSetMutator($key)
81 81
     {
82
-        return method_exists($this, 'set' . $this->getMutatorMethod($key)) ? true : false;
82
+        return method_exists($this, 'set'.$this->getMutatorMethod($key)) ? true : false;
83 83
     }
84 84
 
85 85
     /**
@@ -89,7 +89,7 @@  discard block
 block discarded – undo
89 89
     protected function getMutatorMethod($key)
90 90
     {
91 91
         $key = ucwords(str_replace(['-', '_'], ' ', $key));
92
-        return str_replace(' ', '', $key) . "Attribute";
92
+        return str_replace(' ', '', $key)."Attribute";
93 93
     }
94 94
 
95 95
     /**
@@ -109,7 +109,7 @@  discard block
 block discarded – undo
109 109
                 continue;
110 110
             }
111 111
             if ($this->hasGetMutator($key)) {
112
-                $method = 'get' . $this->getMutatorMethod($key);
112
+                $method = 'get'.$this->getMutatorMethod($key);
113 113
                 $attributes[$key] = $this->$method($attribute);
114 114
             }
115 115
         }
@@ -127,7 +127,7 @@  discard block
 block discarded – undo
127 127
         foreach ($attributes as $key => $attribute) {
128 128
             
129 129
             if ($this->hasSetMutator($key)) {
130
-                $method = 'set' . $this->getMutatorMethod($key);
130
+                $method = 'set'.$this->getMutatorMethod($key);
131 131
                 $this->attributes[$key] = $this->$method($attribute);
132 132
             }
133 133
             else {
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/System/Manager.php 5 patches
Doc Comments   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -141,7 +141,7 @@  discard block
 block discarded – undo
141 141
      * Create a mapper for a given entity
142 142
      *
143 143
      * @param  \Analogue\ORM\Mappable|string|array|\Traversable $entity
144
-     * @param  mixed                                            $entityMap
144
+     * @param  null|EntityMap                                            $entityMap
145 145
      * @throws MappingException
146 146
      * @throws \InvalidArgumentException
147 147
      * @return Mapper
@@ -243,7 +243,7 @@  discard block
 block discarded – undo
243 243
     /**
244 244
      * Check if the entity is already registered
245 245
      *
246
-     * @param  string|Entity $entity
246
+     * @param  string $entity
247 247
      * @return boolean
248 248
      */
249 249
     public function isRegisteredEntity($entity)
@@ -312,7 +312,7 @@  discard block
 block discarded – undo
312 312
     /**
313 313
      * Register an entity
314 314
      *
315
-     * @param  string|\Analogue\ORM\Mappable $entity    entity's class name
315
+     * @param  string $entity    entity's class name
316 316
      * @param  string|EntityMap              $entityMap map's class name
317 317
      * @throws MappingException
318 318
      * @return void
Please login to merge, or discard this patch.
Unused Use Statements   -1 removed lines patch added patch discarded remove patch
@@ -5,7 +5,6 @@
 block discarded – undo
5 5
 use Analogue\ORM\Entity;
6 6
 use Analogue\ORM\Plugins\AnaloguePluginInterface;
7 7
 use Analogue\ORM\ValueMap;
8
-use Exception;
9 8
 use Analogue\ORM\EntityMap;
10 9
 use Analogue\ORM\Repository;
11 10
 use Illuminate\Support\Collection;
Please login to merge, or discard this patch.
Spacing   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -177,7 +177,7 @@  discard block
 block discarded – undo
177 177
         // We first check if the entity is traversable and we'll resolve
178 178
         // the entity based on the first item of the object.   
179 179
         if ($this->isTraversable($entity)) {
180
-            if (! count($entity)) {
180
+            if (!count($entity)) {
181 181
                 throw new \InvalidArgumentException('Length of Entity collection must be greater than 0');
182 182
             }
183 183
 
@@ -341,7 +341,7 @@  discard block
 block discarded – undo
341 341
         }
342 342
 
343 343
         if (!$entityMap instanceof EntityMap) {
344
-            throw new MappingException(get_class($entityMap) . ' must be an instance of EntityMap.');
344
+            throw new MappingException(get_class($entityMap).' must be an instance of EntityMap.');
345 345
         }
346 346
 
347 347
         $entityMap->setClass($entity);
@@ -359,8 +359,8 @@  discard block
 block discarded – undo
359 359
      */
360 360
     protected function getEntityMapInstanceFor($entity)
361 361
     {
362
-        if (class_exists($entity . 'Map')) {
363
-            $map = $entity . 'Map';
362
+        if (class_exists($entity.'Map')) {
363
+            $map = $entity.'Map';
364 364
             $map = new $map;
365 365
             return $map;
366 366
         }
@@ -388,7 +388,7 @@  discard block
 block discarded – undo
388 388
     protected function getMapFromNamespaces($class)
389 389
     {
390 390
         foreach ($this->customMapNamespaces as $namespace) {
391
-            if($map = $this->findMapInNamespace($class, $namespace)) {
391
+            if ($map = $this->findMapInNamespace($class, $namespace)) {
392 392
                 return $map;
393 393
             }
394 394
         }
@@ -530,15 +530,15 @@  discard block
 block discarded – undo
530 530
 
531 531
             // First, we'll look into registered namespaces for Entity Maps,
532 532
             // if any. Then we'll fallback to the same namespace of the object
533
-            if(! $valueMap = $this->getMapFromNamespaces($valueObject)) {
534
-                $valueMap = $valueObject . 'Map';    
533
+            if (!$valueMap = $this->getMapFromNamespaces($valueObject)) {
534
+                $valueMap = $valueObject.'Map';    
535 535
             }
536 536
             else {
537 537
                 $valueMap = get_class($valueMap);
538 538
             }
539 539
         }
540 540
 
541
-        if (! class_exists($valueMap)) {
541
+        if (!class_exists($valueMap)) {
542 542
             throw new MappingException("$valueMap doesn't exists");
543 543
         }
544 544
 
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.
Indentation   +646 added lines, -646 removed lines patch added patch discarded remove patch
@@ -21,656 +21,656 @@
 block discarded – undo
21 21
  */
22 22
 class Manager
23 23
 {
24
-    /**
25
-     * Manager instance
26
-     *
27
-     * @var Manager
28
-     */
29
-    protected static $instance;
30
-
31
-    /**
32
-     * Driver Manager
33
-     *
34
-     * @var \Analogue\ORM\Drivers\Manager
35
-     */
36
-    protected $drivers;
37
-
38
-    /**
39
-     * Registered entity classes and corresponding map objects.
40
-     *
41
-     * @var array
42
-     */
43
-    protected $entityClasses = [];
44
-
45
-    /**
46
-     * Key value store of ValueObject Classes and corresponding map classes
47
-     *
48
-     * @var array|ValueMap[]
49
-     */
50
-    protected $valueClasses = [];
51
-
52
-    /**
53
-     * Morph map
54
-     */
55
-    protected $morphMap = [];
56
-
57
-    /**
58
-     * Loaded Mappers
59
-     *
60
-     * @var array
61
-     */
62
-    protected $mappers = [];
63
-
64
-    /**
65
-     * Loaded Repositories
66
-     *
67
-     * @var array
68
-     */
69
-    protected $repositories = [];
70
-
71
-    /**
72
-     * Event dispatcher instance
73
-     *
74
-     * @var \Illuminate\Contracts\Events\Dispatcher
75
-     */
76
-    protected $eventDispatcher;
77
-
78
-    /**
79
-     * Available Analogue Events
80
-     *
81
-     * @var array
82
-     */
83
-    protected $events = [
84
-        'initializing',
85
-        'initialized',
86
-        'store',
87
-        'stored',
88
-        'creating',
89
-        'created',
90
-        'updating',
91
-        'updated',
92
-        'deleting',
93
-        'deleted',
94
-    ];
95
-
96
-    /**
97
-     * If strictMode is set to true, Manager will throw
98
-     * an exception if no entityMap class are registered
99
-     * for a given entity class.
100
-     * 
101
-     * @var boolean
102
-     */
103
-    protected $strictMode = true;
104
-
105
-    /**
106
-     * We can add namespaces in this array where the manager
107
-     * will look for when auto registering entityMaps.
108
-     * 
109
-     * @var array
110
-     */
111
-    protected $customMapNamespaces = [];
112
-
113
-    /**
114
-     * @param \Analogue\ORM\Drivers\Manager $driverManager
115
-     * @param Dispatcher                    $event
116
-     */
117
-    public function __construct(DriverManager $driverManager, Dispatcher $event)
118
-    {
119
-        $this->drivers = $driverManager;
120
-
121
-        $this->eventDispatcher = $event;
122
-
123
-        static::$instance = $this;
124
-    }
125
-
126
-    /**
127
-     * Create a mapper for a given entity (static alias)
128
-     *
129
-     * @param  \Analogue\ORM\Mappable|string $entity
130
-     * @param  null|EntityMap                $entityMap
131
-     * @throws MappingException
132
-     * @return Mapper
133
-     * @throws \InvalidArgumentException
134
-     */
135
-    public static function getMapper($entity, $entityMap = null)
136
-    {
137
-        return static::$instance->mapper($entity, $entityMap);
138
-    }
139
-
140
-    /**
141
-     * Create a mapper for a given entity
142
-     *
143
-     * @param  \Analogue\ORM\Mappable|string|array|\Traversable $entity
144
-     * @param  mixed                                            $entityMap
145
-     * @throws MappingException
146
-     * @throws \InvalidArgumentException
147
-     * @return Mapper
148
-     */
149
-    public function mapper($entity, $entityMap = null)
150
-    {
151
-        if ($entity instanceof Wrapper) {
152
-            throw new MappingException('Tried to instantiate mapper on wrapped Entity');
153
-        }
154
-
155
-        $entity = $this->resolveEntityClass($entity);
156
-
157
-        $entity = $this->getInverseMorphMap($entity);
158
-
159
-        // Return existing mapper instance if exists.
160
-        if (array_key_exists($entity, $this->mappers)) {
161
-            return $this->mappers[$entity];
162
-        } else {
163
-            return $this->buildMapper($entity, $entityMap);
164
-        }
165
-    }
166
-
167
-    /**
168
-     * This method resolve entity class from mappable instances or iterators
169
-     *
170
-     * @param \Analogue\ORM\Mappable|string|array|\Traversable $entity
171
-     * @return string
172
-     *
173
-     * @throws \InvalidArgumentException
174
-     */
175
-    protected function resolveEntityClass($entity)
176
-    {
177
-        // We first check if the entity is traversable and we'll resolve
178
-        // the entity based on the first item of the object.   
179
-        if ($this->isTraversable($entity)) {
180
-            if (! count($entity)) {
181
-                throw new \InvalidArgumentException('Length of Entity collection must be greater than 0');
182
-            }
183
-
184
-            $firstEntityItem = ($entity instanceof \Iterator)
185
-                ? $entity->current()
186
-                : current($entity);
187
-
188
-            return $this->resolveEntityClass($firstEntityItem);
189
-        }
24
+	/**
25
+	 * Manager instance
26
+	 *
27
+	 * @var Manager
28
+	 */
29
+	protected static $instance;
30
+
31
+	/**
32
+	 * Driver Manager
33
+	 *
34
+	 * @var \Analogue\ORM\Drivers\Manager
35
+	 */
36
+	protected $drivers;
37
+
38
+	/**
39
+	 * Registered entity classes and corresponding map objects.
40
+	 *
41
+	 * @var array
42
+	 */
43
+	protected $entityClasses = [];
44
+
45
+	/**
46
+	 * Key value store of ValueObject Classes and corresponding map classes
47
+	 *
48
+	 * @var array|ValueMap[]
49
+	 */
50
+	protected $valueClasses = [];
51
+
52
+	/**
53
+	 * Morph map
54
+	 */
55
+	protected $morphMap = [];
56
+
57
+	/**
58
+	 * Loaded Mappers
59
+	 *
60
+	 * @var array
61
+	 */
62
+	protected $mappers = [];
63
+
64
+	/**
65
+	 * Loaded Repositories
66
+	 *
67
+	 * @var array
68
+	 */
69
+	protected $repositories = [];
70
+
71
+	/**
72
+	 * Event dispatcher instance
73
+	 *
74
+	 * @var \Illuminate\Contracts\Events\Dispatcher
75
+	 */
76
+	protected $eventDispatcher;
77
+
78
+	/**
79
+	 * Available Analogue Events
80
+	 *
81
+	 * @var array
82
+	 */
83
+	protected $events = [
84
+		'initializing',
85
+		'initialized',
86
+		'store',
87
+		'stored',
88
+		'creating',
89
+		'created',
90
+		'updating',
91
+		'updated',
92
+		'deleting',
93
+		'deleted',
94
+	];
95
+
96
+	/**
97
+	 * If strictMode is set to true, Manager will throw
98
+	 * an exception if no entityMap class are registered
99
+	 * for a given entity class.
100
+	 * 
101
+	 * @var boolean
102
+	 */
103
+	protected $strictMode = true;
104
+
105
+	/**
106
+	 * We can add namespaces in this array where the manager
107
+	 * will look for when auto registering entityMaps.
108
+	 * 
109
+	 * @var array
110
+	 */
111
+	protected $customMapNamespaces = [];
112
+
113
+	/**
114
+	 * @param \Analogue\ORM\Drivers\Manager $driverManager
115
+	 * @param Dispatcher                    $event
116
+	 */
117
+	public function __construct(DriverManager $driverManager, Dispatcher $event)
118
+	{
119
+		$this->drivers = $driverManager;
120
+
121
+		$this->eventDispatcher = $event;
122
+
123
+		static::$instance = $this;
124
+	}
125
+
126
+	/**
127
+	 * Create a mapper for a given entity (static alias)
128
+	 *
129
+	 * @param  \Analogue\ORM\Mappable|string $entity
130
+	 * @param  null|EntityMap                $entityMap
131
+	 * @throws MappingException
132
+	 * @return Mapper
133
+	 * @throws \InvalidArgumentException
134
+	 */
135
+	public static function getMapper($entity, $entityMap = null)
136
+	{
137
+		return static::$instance->mapper($entity, $entityMap);
138
+	}
139
+
140
+	/**
141
+	 * Create a mapper for a given entity
142
+	 *
143
+	 * @param  \Analogue\ORM\Mappable|string|array|\Traversable $entity
144
+	 * @param  mixed                                            $entityMap
145
+	 * @throws MappingException
146
+	 * @throws \InvalidArgumentException
147
+	 * @return Mapper
148
+	 */
149
+	public function mapper($entity, $entityMap = null)
150
+	{
151
+		if ($entity instanceof Wrapper) {
152
+			throw new MappingException('Tried to instantiate mapper on wrapped Entity');
153
+		}
154
+
155
+		$entity = $this->resolveEntityClass($entity);
156
+
157
+		$entity = $this->getInverseMorphMap($entity);
158
+
159
+		// Return existing mapper instance if exists.
160
+		if (array_key_exists($entity, $this->mappers)) {
161
+			return $this->mappers[$entity];
162
+		} else {
163
+			return $this->buildMapper($entity, $entityMap);
164
+		}
165
+	}
166
+
167
+	/**
168
+	 * This method resolve entity class from mappable instances or iterators
169
+	 *
170
+	 * @param \Analogue\ORM\Mappable|string|array|\Traversable $entity
171
+	 * @return string
172
+	 *
173
+	 * @throws \InvalidArgumentException
174
+	 */
175
+	protected function resolveEntityClass($entity)
176
+	{
177
+		// We first check if the entity is traversable and we'll resolve
178
+		// the entity based on the first item of the object.   
179
+		if ($this->isTraversable($entity)) {
180
+			if (! count($entity)) {
181
+				throw new \InvalidArgumentException('Length of Entity collection must be greater than 0');
182
+			}
183
+
184
+			$firstEntityItem = ($entity instanceof \Iterator)
185
+				? $entity->current()
186
+				: current($entity);
187
+
188
+			return $this->resolveEntityClass($firstEntityItem);
189
+		}
190 190
         
191
-        if (is_object($entity)) {
192
-            return get_class($entity);
193
-        }
191
+		if (is_object($entity)) {
192
+			return get_class($entity);
193
+		}
194 194
         
195
-        if (is_string($entity)) {
196
-            return $entity;
197
-        }
195
+		if (is_string($entity)) {
196
+			return $entity;
197
+		}
198 198
      
199
-        throw new \InvalidArgumentException('Invalid entity type');
200
-    }
201
-
202
-    /**
203
-     * @param string $key
204
-     * @return string
205
-     */
206
-    public function getInverseMorphMap($key)
207
-    {
208
-        return array_key_exists($key, $this->morphMap) ? $this->morphMap[$key] : $key;
209
-    }
210
-
211
-    /**
212
-     * Build a new Mapper instance for a given Entity
213
-     *
214
-     * @param  string $entity
215
-     * @param         $entityMap
216
-     * @throws MappingException
217
-     * @return Mapper
218
-     */
219
-    protected function buildMapper($entity, $entityMap)
220
-    {
221
-        // If an EntityMap hasn't been manually registered by the user
222
-        // register it at runtime.
223
-        if (!$this->isRegisteredEntity($entity)) {
224
-            $this->register($entity, $entityMap);
225
-        }
226
-
227
-        $entityMap = $this->entityClasses[$entity];
228
-
229
-        $factory = new MapperFactory($this->drivers, $this->eventDispatcher, $this);
230
-
231
-        $mapper = $factory->make($entity, $entityMap);
232
-
233
-        $this->mappers[$entity] = $mapper;
234
-
235
-        // At this point we can safely call the boot() method on the entityMap as
236
-        // the mapper is now instantiated & registered within the manager.
237
-
238
-        $mapper->getEntityMap()->boot();
239
-
240
-        return $mapper;
241
-    }
242
-
243
-    /**
244
-     * Check if the entity is already registered
245
-     *
246
-     * @param  string|Entity $entity
247
-     * @return boolean
248
-     */
249
-    public function isRegisteredEntity($entity)
250
-    {
251
-        if (!is_string($entity)) {
252
-            $entity = get_class($entity);
253
-        }
254
-
255
-        return array_key_exists($entity, $this->entityClasses);
256
-    }
257
-
258
-    /**
259
-     * Return an array containing registered entities & entityMap instances
260
-     * 
261
-     * @return  array
262
-     */
263
-    public function getRegisteredEntities()
264
-    {
265
-        return $this->entityClasses;
266
-    }
267
-
268
-    /**
269
-     * Check if a value class is already registered
270
-     *
271
-     * @param string|sdtClass $object
272
-     * @return boolean
273
-     * 
274
-     */
275
-    public function isRegisteredValueObject($object)
276
-    {
277
-        if (!is_string($object)) {
278
-            $object = get_class($object);
279
-        }
280
-
281
-        return array_key_exists($object, $this->valueClasses);
282
-    }
283
-
284
-    /**
285
-     * Return true if an object is an array or iterator
286
-     *
287
-     * @param  mixed $argument
288
-     * @return boolean
289
-     */
290
-    public function isTraversable($argument)
291
-    {
292
-        return $argument instanceof \Traversable || is_array($argument);
293
-    }
294
-
295
-    /**
296
-     * Set strict mode for entityMap instantiation
297
-     * 
298
-     * @param boolean $mode
299
-     */
300
-    public function setStrictMode($mode)
301
-    {
302
-        $this->strictMode = $mode;
303
-    }
304
-
305
-    /**
306
-     * Register a namespace in where Analogue
307
-     * will scan for EntityMaps & ValueMaps
308
-     *
309
-     * @param string $namespace
310
-     * @return void
311
-     */
312
-    public function registerMapNamespace($namespace)
313
-    {
314
-        // Add a trailing antislash to namespace if not present
315
-        if (substr("testers", -1) != "\\") {
316
-            $namespace = $namespace."\\";
317
-        }
318
-
319
-        $this->customMapNamespaces[] = $namespace;
320
-    }
321
-
322
-    /**
323
-     * Register an entity
324
-     *
325
-     * @param  string|\Analogue\ORM\Mappable $entity    entity's class name
326
-     * @param  string|EntityMap              $entityMap map's class name
327
-     * @throws MappingException
328
-     * @return void
329
-     */
330
-    public function register($entity, $entityMap = null)
331
-    {
332
-        // If an object is provider, get the class name from it
333
-        if (!is_string($entity)) {
334
-            $entity = get_class($entity);
335
-        }
336
-
337
-        if ($this->isRegisteredEntity($entity)) {
338
-            throw new MappingException("Entity $entity is already registered.");
339
-        }
340
-
341
-        if (!class_exists($entity)) {
342
-            throw new MappingException("Class $entity does not exists");
343
-        }
344
-
345
-        if ($entityMap === null) {
346
-            $entityMap = $this->getEntityMapInstanceFor($entity);
347
-        }
348
-
349
-        if (is_string($entityMap)) {
350
-            $entityMap = new $entityMap;
351
-        }
352
-
353
-        if (!$entityMap instanceof EntityMap) {
354
-            throw new MappingException(get_class($entityMap) . ' must be an instance of EntityMap.');
355
-        }
356
-
357
-        $entityMap->setClass($entity);
358
-
359
-        $this->entityClasses[$entity] = $entityMap;
360
-    }
361
-
362
-    /**
363
-     * Get the entity map instance for a custom entity
364
-     *
365
-     * @param  string $entity
366
-     * @return \Analogue\ORM\EntityMap
367
-     */
368
-    protected function getEntityMapInstanceFor($entity)
369
-    {
370
-        if (class_exists($entity . 'Map')) {
371
-            $map = $entity . 'Map';
372
-            $map = new $map;
373
-            return $map;
374
-        }
375
-
376
-        if ($map = $this->getMapFromNamespaces($entity)) {
377
-            return $map;
378
-        }
199
+		throw new \InvalidArgumentException('Invalid entity type');
200
+	}
201
+
202
+	/**
203
+	 * @param string $key
204
+	 * @return string
205
+	 */
206
+	public function getInverseMorphMap($key)
207
+	{
208
+		return array_key_exists($key, $this->morphMap) ? $this->morphMap[$key] : $key;
209
+	}
210
+
211
+	/**
212
+	 * Build a new Mapper instance for a given Entity
213
+	 *
214
+	 * @param  string $entity
215
+	 * @param         $entityMap
216
+	 * @throws MappingException
217
+	 * @return Mapper
218
+	 */
219
+	protected function buildMapper($entity, $entityMap)
220
+	{
221
+		// If an EntityMap hasn't been manually registered by the user
222
+		// register it at runtime.
223
+		if (!$this->isRegisteredEntity($entity)) {
224
+			$this->register($entity, $entityMap);
225
+		}
226
+
227
+		$entityMap = $this->entityClasses[$entity];
228
+
229
+		$factory = new MapperFactory($this->drivers, $this->eventDispatcher, $this);
230
+
231
+		$mapper = $factory->make($entity, $entityMap);
232
+
233
+		$this->mappers[$entity] = $mapper;
234
+
235
+		// At this point we can safely call the boot() method on the entityMap as
236
+		// the mapper is now instantiated & registered within the manager.
237
+
238
+		$mapper->getEntityMap()->boot();
239
+
240
+		return $mapper;
241
+	}
242
+
243
+	/**
244
+	 * Check if the entity is already registered
245
+	 *
246
+	 * @param  string|Entity $entity
247
+	 * @return boolean
248
+	 */
249
+	public function isRegisteredEntity($entity)
250
+	{
251
+		if (!is_string($entity)) {
252
+			$entity = get_class($entity);
253
+		}
254
+
255
+		return array_key_exists($entity, $this->entityClasses);
256
+	}
257
+
258
+	/**
259
+	 * Return an array containing registered entities & entityMap instances
260
+	 * 
261
+	 * @return  array
262
+	 */
263
+	public function getRegisteredEntities()
264
+	{
265
+		return $this->entityClasses;
266
+	}
267
+
268
+	/**
269
+	 * Check if a value class is already registered
270
+	 *
271
+	 * @param string|sdtClass $object
272
+	 * @return boolean
273
+	 * 
274
+	 */
275
+	public function isRegisteredValueObject($object)
276
+	{
277
+		if (!is_string($object)) {
278
+			$object = get_class($object);
279
+		}
280
+
281
+		return array_key_exists($object, $this->valueClasses);
282
+	}
283
+
284
+	/**
285
+	 * Return true if an object is an array or iterator
286
+	 *
287
+	 * @param  mixed $argument
288
+	 * @return boolean
289
+	 */
290
+	public function isTraversable($argument)
291
+	{
292
+		return $argument instanceof \Traversable || is_array($argument);
293
+	}
294
+
295
+	/**
296
+	 * Set strict mode for entityMap instantiation
297
+	 * 
298
+	 * @param boolean $mode
299
+	 */
300
+	public function setStrictMode($mode)
301
+	{
302
+		$this->strictMode = $mode;
303
+	}
304
+
305
+	/**
306
+	 * Register a namespace in where Analogue
307
+	 * will scan for EntityMaps & ValueMaps
308
+	 *
309
+	 * @param string $namespace
310
+	 * @return void
311
+	 */
312
+	public function registerMapNamespace($namespace)
313
+	{
314
+		// Add a trailing antislash to namespace if not present
315
+		if (substr("testers", -1) != "\\") {
316
+			$namespace = $namespace."\\";
317
+		}
318
+
319
+		$this->customMapNamespaces[] = $namespace;
320
+	}
321
+
322
+	/**
323
+	 * Register an entity
324
+	 *
325
+	 * @param  string|\Analogue\ORM\Mappable $entity    entity's class name
326
+	 * @param  string|EntityMap              $entityMap map's class name
327
+	 * @throws MappingException
328
+	 * @return void
329
+	 */
330
+	public function register($entity, $entityMap = null)
331
+	{
332
+		// If an object is provider, get the class name from it
333
+		if (!is_string($entity)) {
334
+			$entity = get_class($entity);
335
+		}
336
+
337
+		if ($this->isRegisteredEntity($entity)) {
338
+			throw new MappingException("Entity $entity is already registered.");
339
+		}
340
+
341
+		if (!class_exists($entity)) {
342
+			throw new MappingException("Class $entity does not exists");
343
+		}
344
+
345
+		if ($entityMap === null) {
346
+			$entityMap = $this->getEntityMapInstanceFor($entity);
347
+		}
348
+
349
+		if (is_string($entityMap)) {
350
+			$entityMap = new $entityMap;
351
+		}
352
+
353
+		if (!$entityMap instanceof EntityMap) {
354
+			throw new MappingException(get_class($entityMap) . ' must be an instance of EntityMap.');
355
+		}
356
+
357
+		$entityMap->setClass($entity);
358
+
359
+		$this->entityClasses[$entity] = $entityMap;
360
+	}
361
+
362
+	/**
363
+	 * Get the entity map instance for a custom entity
364
+	 *
365
+	 * @param  string $entity
366
+	 * @return \Analogue\ORM\EntityMap
367
+	 */
368
+	protected function getEntityMapInstanceFor($entity)
369
+	{
370
+		if (class_exists($entity . 'Map')) {
371
+			$map = $entity . 'Map';
372
+			$map = new $map;
373
+			return $map;
374
+		}
375
+
376
+		if ($map = $this->getMapFromNamespaces($entity)) {
377
+			return $map;
378
+		}
379 379
                 
380
-        if ($this->strictMode) {
381
-            throw new EntityMapNotFoundException("No Map registered for $entity");
382
-        }
380
+		if ($this->strictMode) {
381
+			throw new EntityMapNotFoundException("No Map registered for $entity");
382
+		}
383 383
         
384
-        $map = $this->getNewEntityMap();
385
-
386
-        return $map;
387
-    }
388
-
389
-    /**
390
-     * Scan through registered custom namespace
391
-     * for an Entity/ValueMap
392
-     * 
393
-     * @param  string $class
394
-     * @return ValueMap|EntityMap|boolean
395
-     */
396
-    protected function getMapFromNamespaces($class)
397
-    {
398
-        foreach ($this->customMapNamespaces as $namespace) {
399
-            if($map = $this->findMapInNamespace($class, $namespace)) {
400
-                return $map;
401
-            }
402
-        }
403
-
404
-        return false;
405
-    }
406
-
407
-    /**
408
-     * Look in a custom namespace for an Entity/ValueMap
409
-     * 
410
-     * @param  string $class
411
-     * @param  string $namespace
412
-     * @return ValueMap|EntityMap|boolean
413
-     */
414
-    protected function findMapInNamespace($class, $namespace)
415
-    {
416
-        $parts = explode("\\", $class);
384
+		$map = $this->getNewEntityMap();
385
+
386
+		return $map;
387
+	}
388
+
389
+	/**
390
+	 * Scan through registered custom namespace
391
+	 * for an Entity/ValueMap
392
+	 * 
393
+	 * @param  string $class
394
+	 * @return ValueMap|EntityMap|boolean
395
+	 */
396
+	protected function getMapFromNamespaces($class)
397
+	{
398
+		foreach ($this->customMapNamespaces as $namespace) {
399
+			if($map = $this->findMapInNamespace($class, $namespace)) {
400
+				return $map;
401
+			}
402
+		}
403
+
404
+		return false;
405
+	}
406
+
407
+	/**
408
+	 * Look in a custom namespace for an Entity/ValueMap
409
+	 * 
410
+	 * @param  string $class
411
+	 * @param  string $namespace
412
+	 * @return ValueMap|EntityMap|boolean
413
+	 */
414
+	protected function findMapInNamespace($class, $namespace)
415
+	{
416
+		$parts = explode("\\", $class);
417 417
         
418
-        $baseClass = $parts[count($parts) - 1];
419
-
420
-        $expectedClass = $namespace.$baseClass.'Map';
421
-
422
-        if (class_exists($expectedClass)) {
423
-            return new $expectedClass;
424
-        }
425
-
426
-        return false;
427
-    }
428
-
429
-    /**
430
-     * Dynamically create an entity map for a custom entity class
431
-     *
432
-     * @return EntityMap
433
-     */
434
-    protected function getNewEntityMap()
435
-    {
436
-        return new EntityMap;
437
-    }
438
-
439
-    /**
440
-     * Return the Singleton instance of the manager
441
-     *
442
-     * @return Manager
443
-     */
444
-    public static function getInstance()
445
-    {
446
-        return static::$instance;
447
-    }
448
-
449
-    /**
450
-     * Return the Driver Manager's instance
451
-     *
452
-     * @return \Analogue\ORM\Drivers\Manager
453
-     */
454
-    public function getDriverManager()
455
-    {
456
-        return $this->drivers;
457
-    }
458
-
459
-    /**
460
-     * Get the Repository instance for the given Entity
461
-     *
462
-     * @param  \Analogue\ORM\Mappable|string $entity
463
-     * @throws \InvalidArgumentException
464
-     * @throws MappingException
465
-     * @return \Analogue\ORM\Repository
466
-     */
467
-    public function repository($entity)
468
-    {
469
-        if (!is_string($entity)) {
470
-            $entity = get_class($entity);
471
-        }
472
-
473
-        // First we check if the repository is not already created.
474
-        if (array_key_exists($entity, $this->repositories)) {
475
-            return $this->repositories[$entity];
476
-        }
477
-
478
-        $this->repositories[$entity] = new Repository($this->mapper($entity));
479
-
480
-        return $this->repositories[$entity];
481
-    }
482
-
483
-    /**
484
-     * Return true is the object is registered as value object
485
-     *
486
-     * @param  mixed $object
487
-     * @return boolean
488
-     */
489
-    public function isValueObject($object)
490
-    {
491
-        if (!is_string($object)) {
492
-            $object = get_class($object);
493
-        }
494
-
495
-        return array_key_exists($object, $this->valueClasses);
496
-    }
497
-
498
-    /**
499
-     * Get the Value Map for a given Value Object Class
500
-     *
501
-     * @param  string $valueObject
502
-     * @throws MappingException
503
-     * @return \Analogue\ORM\ValueMap
504
-     */
505
-    public function getValueMap($valueObject)
506
-    {
507
-        if (!is_string($valueObject)) {
508
-            $valueObject = get_class($valueObject);
509
-        }
510
-
511
-        if (!array_key_exists($valueObject, $this->valueClasses)) {
512
-            $this->registerValueObject($valueObject);
513
-        }
514
-
515
-        /** @var ValueMap $valueMap */
516
-        $valueMap = new $this->valueClasses[$valueObject];
517
-
518
-        $valueMap->setClass($valueObject);
519
-
520
-        return $valueMap;
521
-    }
522
-
523
-    /**
524
-     * Register a Value Object
525
-     *
526
-     * @param  string $valueObject
527
-     * @param  string $valueMap
528
-     * @throws MappingException
529
-     * @return void
530
-     */
531
-    public function registerValueObject($valueObject, $valueMap = null)
532
-    {
533
-        if (!is_string($valueObject)) {
534
-            $valueObject = get_class($valueObject);
535
-        }
536
-
537
-        if ($valueMap === null) {
538
-
539
-            // First, we'll look into registered namespaces for Entity Maps,
540
-            // if any. Then we'll fallback to the same namespace of the object
541
-            if(! $valueMap = $this->getMapFromNamespaces($valueObject)) {
542
-                $valueMap = $valueObject . 'Map';    
543
-            }
544
-            else {
545
-                $valueMap = get_class($valueMap);
546
-            }
547
-        }
548
-
549
-        if (! class_exists($valueMap)) {
550
-            throw new MappingException("$valueMap doesn't exists");
551
-        }
552
-
553
-        $this->valueClasses[$valueObject] = $valueMap;
554
-    }
555
-
556
-    /**
557
-     * Instantiate a new Value Object instance
558
-     *
559
-     * @param  string $valueObject
560
-     * @return \Analogue\ORM\ValueObject
561
-     */
562
-    public function getValueObjectInstance($valueObject)
563
-    {
564
-        $prototype = unserialize(sprintf('O:%d:"%s":0:{}', strlen($valueObject), $valueObject));
565
-
566
-        return $prototype;
567
-    }
568
-
569
-    /**
570
-     * Register Analogue Plugin
571
-     *
572
-     * @param  string $plugin class
573
-     * @return void
574
-     */
575
-    public function registerPlugin($plugin)
576
-    {
577
-        /** @var AnaloguePluginInterface $plugin */
578
-        $plugin = new $plugin($this);
579
-
580
-        $this->events = array_merge($this->events, $plugin->getCustomEvents());
581
-
582
-        $plugin->register();
583
-    }
584
-
585
-    /**
586
-     * Register event listeners that will be fired regardless the type
587
-     * of the entity.
588
-     *
589
-     * @param  string   $event
590
-     * @param  \Closure $callback
591
-     * @throws \LogicException
592
-     * @return void
593
-     */
594
-    public function registerGlobalEvent($event, $callback)
595
-    {
596
-        if (!in_array($event, $this->events, false)) {
597
-            throw new \LogicException("Analogue : Event $event doesn't exist");
598
-        }
599
-
600
-        $this->eventDispatcher->listen("analogue.{$event}.*", $callback);
601
-    }
602
-
603
-    /**
604
-     * Shortcut to Mapper store
605
-     *
606
-     * @param  mixed $entity
607
-     * @throws MappingException
608
-     * @return mixed
609
-     * @throws \InvalidArgumentException
610
-     */
611
-    public function store($entity)
612
-    {
613
-        return $this->mapper($entity)->store($entity);
614
-    }
615
-
616
-    /**
617
-     * Shortcut to Mapper delete
618
-     *
619
-     * @param  mixed $entity
620
-     * @throws MappingException
621
-     * @return \Illuminate\Support\Collection|null
622
-     * @throws \InvalidArgumentException
623
-     */
624
-    public function delete($entity)
625
-    {
626
-        return $this->mapper($entity)->delete($entity);
627
-    }
628
-
629
-    /**
630
-     * Shortcut to Mapper query
631
-     *
632
-     * @param  mixed $entity
633
-     * @throws MappingException
634
-     * @return Query
635
-     * @throws \InvalidArgumentException
636
-     */
637
-    public function query($entity)
638
-    {
639
-        return $this->mapper($entity)->query();
640
-    }
641
-
642
-    /**
643
-     * Shortcut to Mapper Global Query
644
-     *
645
-     * @param  mixed $entity
646
-     * @throws MappingException
647
-     * @return Query
648
-     * @throws \InvalidArgumentException
649
-     */
650
-    public function globalQuery($entity)
651
-    {
652
-        return $this->mapper($entity)->globalQuery();
653
-    }
654
-
655
-    /**
656
-     * @param array $morphMap
657
-     * @return $this
658
-     */
659
-    public function morphMap(array $morphMap)
660
-    {
661
-        $this->morphMap = $morphMap;
662
-
663
-        return $this;
664
-    }
665
-
666
-    /**
667
-     * @param string $class
668
-     * @return mixed
669
-     */
670
-    public function getMorphMap($class)
671
-    {
672
-        $key = array_search($class, $this->morphMap, false);
673
-
674
-        return $key !== false ? $key : $class;
675
-    }
418
+		$baseClass = $parts[count($parts) - 1];
419
+
420
+		$expectedClass = $namespace.$baseClass.'Map';
421
+
422
+		if (class_exists($expectedClass)) {
423
+			return new $expectedClass;
424
+		}
425
+
426
+		return false;
427
+	}
428
+
429
+	/**
430
+	 * Dynamically create an entity map for a custom entity class
431
+	 *
432
+	 * @return EntityMap
433
+	 */
434
+	protected function getNewEntityMap()
435
+	{
436
+		return new EntityMap;
437
+	}
438
+
439
+	/**
440
+	 * Return the Singleton instance of the manager
441
+	 *
442
+	 * @return Manager
443
+	 */
444
+	public static function getInstance()
445
+	{
446
+		return static::$instance;
447
+	}
448
+
449
+	/**
450
+	 * Return the Driver Manager's instance
451
+	 *
452
+	 * @return \Analogue\ORM\Drivers\Manager
453
+	 */
454
+	public function getDriverManager()
455
+	{
456
+		return $this->drivers;
457
+	}
458
+
459
+	/**
460
+	 * Get the Repository instance for the given Entity
461
+	 *
462
+	 * @param  \Analogue\ORM\Mappable|string $entity
463
+	 * @throws \InvalidArgumentException
464
+	 * @throws MappingException
465
+	 * @return \Analogue\ORM\Repository
466
+	 */
467
+	public function repository($entity)
468
+	{
469
+		if (!is_string($entity)) {
470
+			$entity = get_class($entity);
471
+		}
472
+
473
+		// First we check if the repository is not already created.
474
+		if (array_key_exists($entity, $this->repositories)) {
475
+			return $this->repositories[$entity];
476
+		}
477
+
478
+		$this->repositories[$entity] = new Repository($this->mapper($entity));
479
+
480
+		return $this->repositories[$entity];
481
+	}
482
+
483
+	/**
484
+	 * Return true is the object is registered as value object
485
+	 *
486
+	 * @param  mixed $object
487
+	 * @return boolean
488
+	 */
489
+	public function isValueObject($object)
490
+	{
491
+		if (!is_string($object)) {
492
+			$object = get_class($object);
493
+		}
494
+
495
+		return array_key_exists($object, $this->valueClasses);
496
+	}
497
+
498
+	/**
499
+	 * Get the Value Map for a given Value Object Class
500
+	 *
501
+	 * @param  string $valueObject
502
+	 * @throws MappingException
503
+	 * @return \Analogue\ORM\ValueMap
504
+	 */
505
+	public function getValueMap($valueObject)
506
+	{
507
+		if (!is_string($valueObject)) {
508
+			$valueObject = get_class($valueObject);
509
+		}
510
+
511
+		if (!array_key_exists($valueObject, $this->valueClasses)) {
512
+			$this->registerValueObject($valueObject);
513
+		}
514
+
515
+		/** @var ValueMap $valueMap */
516
+		$valueMap = new $this->valueClasses[$valueObject];
517
+
518
+		$valueMap->setClass($valueObject);
519
+
520
+		return $valueMap;
521
+	}
522
+
523
+	/**
524
+	 * Register a Value Object
525
+	 *
526
+	 * @param  string $valueObject
527
+	 * @param  string $valueMap
528
+	 * @throws MappingException
529
+	 * @return void
530
+	 */
531
+	public function registerValueObject($valueObject, $valueMap = null)
532
+	{
533
+		if (!is_string($valueObject)) {
534
+			$valueObject = get_class($valueObject);
535
+		}
536
+
537
+		if ($valueMap === null) {
538
+
539
+			// First, we'll look into registered namespaces for Entity Maps,
540
+			// if any. Then we'll fallback to the same namespace of the object
541
+			if(! $valueMap = $this->getMapFromNamespaces($valueObject)) {
542
+				$valueMap = $valueObject . 'Map';    
543
+			}
544
+			else {
545
+				$valueMap = get_class($valueMap);
546
+			}
547
+		}
548
+
549
+		if (! class_exists($valueMap)) {
550
+			throw new MappingException("$valueMap doesn't exists");
551
+		}
552
+
553
+		$this->valueClasses[$valueObject] = $valueMap;
554
+	}
555
+
556
+	/**
557
+	 * Instantiate a new Value Object instance
558
+	 *
559
+	 * @param  string $valueObject
560
+	 * @return \Analogue\ORM\ValueObject
561
+	 */
562
+	public function getValueObjectInstance($valueObject)
563
+	{
564
+		$prototype = unserialize(sprintf('O:%d:"%s":0:{}', strlen($valueObject), $valueObject));
565
+
566
+		return $prototype;
567
+	}
568
+
569
+	/**
570
+	 * Register Analogue Plugin
571
+	 *
572
+	 * @param  string $plugin class
573
+	 * @return void
574
+	 */
575
+	public function registerPlugin($plugin)
576
+	{
577
+		/** @var AnaloguePluginInterface $plugin */
578
+		$plugin = new $plugin($this);
579
+
580
+		$this->events = array_merge($this->events, $plugin->getCustomEvents());
581
+
582
+		$plugin->register();
583
+	}
584
+
585
+	/**
586
+	 * Register event listeners that will be fired regardless the type
587
+	 * of the entity.
588
+	 *
589
+	 * @param  string   $event
590
+	 * @param  \Closure $callback
591
+	 * @throws \LogicException
592
+	 * @return void
593
+	 */
594
+	public function registerGlobalEvent($event, $callback)
595
+	{
596
+		if (!in_array($event, $this->events, false)) {
597
+			throw new \LogicException("Analogue : Event $event doesn't exist");
598
+		}
599
+
600
+		$this->eventDispatcher->listen("analogue.{$event}.*", $callback);
601
+	}
602
+
603
+	/**
604
+	 * Shortcut to Mapper store
605
+	 *
606
+	 * @param  mixed $entity
607
+	 * @throws MappingException
608
+	 * @return mixed
609
+	 * @throws \InvalidArgumentException
610
+	 */
611
+	public function store($entity)
612
+	{
613
+		return $this->mapper($entity)->store($entity);
614
+	}
615
+
616
+	/**
617
+	 * Shortcut to Mapper delete
618
+	 *
619
+	 * @param  mixed $entity
620
+	 * @throws MappingException
621
+	 * @return \Illuminate\Support\Collection|null
622
+	 * @throws \InvalidArgumentException
623
+	 */
624
+	public function delete($entity)
625
+	{
626
+		return $this->mapper($entity)->delete($entity);
627
+	}
628
+
629
+	/**
630
+	 * Shortcut to Mapper query
631
+	 *
632
+	 * @param  mixed $entity
633
+	 * @throws MappingException
634
+	 * @return Query
635
+	 * @throws \InvalidArgumentException
636
+	 */
637
+	public function query($entity)
638
+	{
639
+		return $this->mapper($entity)->query();
640
+	}
641
+
642
+	/**
643
+	 * Shortcut to Mapper Global Query
644
+	 *
645
+	 * @param  mixed $entity
646
+	 * @throws MappingException
647
+	 * @return Query
648
+	 * @throws \InvalidArgumentException
649
+	 */
650
+	public function globalQuery($entity)
651
+	{
652
+		return $this->mapper($entity)->globalQuery();
653
+	}
654
+
655
+	/**
656
+	 * @param array $morphMap
657
+	 * @return $this
658
+	 */
659
+	public function morphMap(array $morphMap)
660
+	{
661
+		$this->morphMap = $morphMap;
662
+
663
+		return $this;
664
+	}
665
+
666
+	/**
667
+	 * @param string $class
668
+	 * @return mixed
669
+	 */
670
+	public function getMorphMap($class)
671
+	{
672
+		$key = array_search($class, $this->morphMap, false);
673
+
674
+		return $key !== false ? $key : $class;
675
+	}
676 676
 }
Please login to merge, or discard this patch.