Completed
Pull Request — 5.1 (#84)
by Maxim
08:05
created
src/Relationships/Relationship.php 2 patches
Indentation   +429 added lines, -429 removed lines patch added patch discarded remove patch
@@ -19,433 +19,433 @@
 block discarded – undo
19 19
  */
20 20
 abstract class Relationship
21 21
 {
22
-    /**
23
-     * The mapper instance for the related entity
24
-     *
25
-     * @var Mapper
26
-     */
27
-    protected $relatedMapper;
28
-
29
-    /**
30
-     * The Analogue Query Builder instance.
31
-     *
32
-     * @var Query
33
-     */
34
-    protected $query;
35
-
36
-    /**
37
-     * The parent entity proxy instance.
38
-     *
39
-     * @var InternallyMappable
40
-     */
41
-    protected $parent;
42
-
43
-    /**
44
-     * The parent entity map
45
-     * @var \Analogue\ORM\EntityMap
46
-     */
47
-    protected $parentMap;
48
-
49
-    /**
50
-     * The Parent Mapper instance
51
-     *
52
-     * @var Mapper
53
-     */
54
-    protected $parentMapper;
55
-
56
-    /**
57
-     * The related entity instance.
58
-     *
59
-     * @var object
60
-     */
61
-    protected $related;
62
-
63
-    /**
64
-     * The related entity Map
65
-     * @var \Analogue\ORM\EntityMap
66
-     */
67
-    protected $relatedMap;
68
-
69
-    /**
70
-     * Indicate if the parent entity hold the key for the relation.
71
-     *
72
-     * @var boolean
73
-     */
74
-    protected static $ownForeignKey = false;
75
-
76
-    /**
77
-     * Indicate if the relationships use a pivot table.*
78
-     *
79
-     * @var boolean
80
-     */
81
-    protected static $hasPivot = false;
82
-
83
-    /**
84
-     * Indicates if the relation is adding constraints.
85
-     *
86
-     * @var bool
87
-     */
88
-    protected static $constraints = true;
89
-
90
-    /**
91
-     * Wrapper factory
92
-     *
93
-     * @var \Analogue\ORM\System\Wrappers\Factory
94
-     */
95
-    protected $factory;
96
-
97
-    /**
98
-     * Create a new relation instance.
99
-     *
100
-     * @param  Mapper   $mapper
101
-     * @param  Mappable $parent
102
-     * @throws \Analogue\ORM\Exceptions\MappingException
103
-     */
104
-    public function __construct(Mapper $mapper, $parent)
105
-    {
106
-        $this->relatedMapper = $mapper;
107
-
108
-        $this->query = $mapper->getQuery();
109
-
110
-        $this->factory = new Factory;
111
-
112
-        $this->parent = $this->factory->make($parent);
113
-
114
-        $this->parentMapper = $mapper->getManager()->getMapper($parent);
115
-
116
-        $this->parentMap = $this->parentMapper->getEntityMap();
117
-
118
-        $this->related = $this->query->getEntityInstance();
119
-
120
-        $this->relatedMap = $mapper->getEntityMap();
121
-
122
-        $this->addConstraints();
123
-    }
124
-
125
-    /**
126
-     * @param $related
127
-     * @return mixed
128
-     */
129
-    abstract public function attachTo($related);
130
-
131
-    /**
132
-     * @param $related
133
-     * @return mixed
134
-     */
135
-    abstract public function detachFrom($related);
136
-
137
-    /**
138
-     * Indicate if the parent entity hold the foreign key for relation.
139
-     *
140
-     * @return boolean
141
-     */
142
-    public function ownForeignKey()
143
-    {
144
-        return static::$ownForeignKey;
145
-    }
146
-
147
-    /**
148
-     * Indicate if the relationship uses a pivot table
149
-     *
150
-     * @return boolean
151
-     */
152
-    public function hasPivot()
153
-    {
154
-        return static::$hasPivot;
155
-    }
156
-
157
-    /**
158
-     * Set the base constraints on the relation query.
159
-     *
160
-     * @return void
161
-     */
162
-    abstract public function addConstraints();
163
-
164
-    /**
165
-     * Set the constraints for an eager load of the relation.
166
-     *
167
-     * @param  array $models
168
-     * @return void
169
-     */
170
-    abstract public function addEagerConstraints(array $models);
171
-
172
-    /**
173
-     * Initialize the relation on a set of models.
174
-     *
175
-     * @param  array  $models
176
-     * @param  string $relation
177
-     * @return array
178
-     */
179
-    abstract public function initRelation(array $models, $relation);
180
-
181
-    /**
182
-     * Match the eagerly loaded results to their parents.
183
-     *
184
-     * @param  array            $entities
185
-     * @param  EntityCollection $results
186
-     * @param  string           $relation
187
-     * @return array
188
-     */
189
-    abstract public function match(array $entities, EntityCollection $results, $relation);
190
-
191
-    /**
192
-     * Get the results of the relationship.
193
-     *
194
-     * @param string $relation relation name in parent's entity map
195
-     * @return mixed
196
-     */
197
-    abstract public function getResults($relation);
198
-
199
-    /**
200
-     * Get the relationship for eager loading.
201
-     *
202
-     * @return EntityCollection
203
-     */
204
-    public function getEager()
205
-    {
206
-        return $this->get();
207
-    }
208
-
209
-    /**
210
-     * Add the constraints for a relationship count query.
211
-     *
212
-     * @param  Query $query
213
-     * @param  Query $parent
214
-     * @return Query
215
-     */
216
-    public function getRelationCountQuery(Query $query, Query $parent)
217
-    {
218
-        $query->select(new Expression('count(*)'));
219
-
220
-        $key = $this->wrap($this->getQualifiedParentKeyName());
221
-
222
-        return $query->where($this->getHasCompareKey(), '=', new Expression($key));
223
-    }
224
-
225
-    /**
226
-     * Run a callback with constraints disabled on the relation.
227
-     *
228
-     * @param  Closure $callback
229
-     * @return mixed
230
-     */
231
-    public static function noConstraints(Closure $callback)
232
-    {
233
-        static::$constraints = false;
234
-
235
-        // When resetting the relation where clause, we want to shift the first element
236
-        // off of the bindings, leaving only the constraints that the developers put
237
-        // as "extra" on the relationships, and not original relation constraints.
238
-        $results = call_user_func($callback);
239
-
240
-        static::$constraints = true;
241
-
242
-        return $results;
243
-    }
244
-
245
-    /**
246
-     * Get all of the primary keys for an array of entities.
247
-     *
248
-     * @param  array  $entities
249
-     * @param  string $key
250
-     * @return array
251
-     */
252
-    protected function getKeys(array $entities, $key = null)
253
-    {
254
-        if (is_null($key)) {
255
-            $key = $this->relatedMap->getKeyName();
256
-        }
257
-
258
-        $host = $this;
259
-
260
-        return array_unique(array_values(array_map(function ($value) use ($key, $host) {
261
-            if (!$value instanceof InternallyMappable) {
262
-                $value = $host->factory->make($value);
263
-            }
264
-
265
-            return $value->getEntityAttribute($key);
266
-
267
-        }, $entities)));
268
-    }
269
-
270
-    /**
271
-     * Get the underlying query for the relation.
272
-     *
273
-     * @return Query
274
-     */
275
-    public function getQuery()
276
-    {
277
-        return $this->query;
278
-    }
279
-
280
-    /**
281
-     * Get the base query builder
282
-     *
283
-     * @return \Analogue\ORM\Drivers\QueryAdapter
284
-     */
285
-    public function getBaseQuery()
286
-    {
287
-        return $this->query->getQuery();
288
-    }
289
-
290
-    /**
291
-     * Get the parent model of the relation.
292
-     *
293
-     * @return InternallyMappable
294
-     */
295
-    public function getParent()
296
-    {
297
-        return $this->parent;
298
-    }
299
-
300
-    /**
301
-     * Get the fully qualified parent key name.
302
-     *
303
-     * @return string
304
-     */
305
-    protected function getQualifiedParentKeyName()
306
-    {
307
-        return $this->parent->getQualifiedKeyName();
308
-    }
309
-
310
-    /**
311
-     * Get the related entity of the relation.
312
-     *
313
-     * @return \Analogue\ORM\Entity
314
-     */
315
-    public function getRelated()
316
-    {
317
-        return $this->related;
318
-    }
319
-
320
-    /**
321
-     * Get the related mapper for the relation
322
-     *
323
-     * @return Mapper
324
-     */
325
-    public function getRelatedMapper()
326
-    {
327
-        return $this->relatedMapper;
328
-    }
329
-
330
-
331
-    /**
332
-     * Get the name of the "created at" column.
333
-     *
334
-     * @return string
335
-     */
336
-    public function createdAt()
337
-    {
338
-        return $this->parentMap->getCreatedAtColumn();
339
-    }
340
-
341
-    /**
342
-     * Get the name of the "updated at" column.
343
-     *
344
-     * @return string
345
-     */
346
-    public function updatedAt()
347
-    {
348
-        return $this->parentMap->getUpdatedAtColumn();
349
-    }
350
-
351
-    /**
352
-     * Get the name of the related model's "updated at" column.
353
-     *
354
-     * @return string
355
-     */
356
-    public function relatedUpdatedAt()
357
-    {
358
-        return $this->related->getUpdatedAtColumn();
359
-    }
360
-
361
-    /**
362
-     * Wrap the given value with the parent query's grammar.
363
-     *
364
-     * @param  string $value
365
-     * @return string
366
-     */
367
-    public function wrap($value)
368
-    {
369
-        return $this->parentMapper->getQuery()->getQuery()->getGrammar()->wrap($value);
370
-    }
371
-
372
-    /**
373
-     * Get a fresh timestamp
374
-     *
375
-     * @return Carbon
376
-     */
377
-    protected function freshTimestamp()
378
-    {
379
-        return new Carbon;
380
-    }
381
-
382
-    /**
383
-     * Cache the link between parent and related
384
-     * into the mapper's Entity Cache.
385
-     *
386
-     * @param  EntityCollection|Mappable $results  result of the relation query
387
-     * @param  string                    $relation name of the relation method on the parent entity
388
-     * @return void
389
-     */
390
-    protected function cacheRelation($results, $relation)
391
-    {
392
-        $cache = $this->parentMapper->getEntityCache();
393
-
394
-        $cache->cacheLoadedRelationResult($this->parent, $relation, $results, $this);
395
-    }
396
-
397
-    /**
398
-     * Return Pivot attributes when available on a relationship
399
-     *
400
-     * @return array
401
-     */
402
-    public function getPivotAttributes()
403
-    {
404
-        return [];
405
-    }
406
-
407
-    /**
408
-     * Get a combo type.primaryKey
409
-     *
410
-     * @param  Mappable $entity
411
-     * @return string
412
-     */
413
-    protected function getEntityHash(Mappable $entity)
414
-    {
415
-        $class = get_class($entity);
416
-
417
-        $keyName = Mapper::getMapper($class)->getEntityMap()->getKeyName();
418
-
419
-        return $class . '.' . $entity->getEntityAttribute($keyName);
420
-    }
421
-
422
-    /**
423
-     * Run synchronization content if needed by the
424
-     * relation type.
425
-     *
426
-     * @param  array $actualContent
427
-     * @return void
428
-     */
429
-    public function sync(array $actualContent)
430
-    {
431
-        //
432
-    }
433
-
434
-    /**
435
-     * Handle dynamic method calls to the relationship.
436
-     *
437
-     * @param  string $method
438
-     * @param  array  $parameters
439
-     * @return mixed
440
-     */
441
-    public function __call($method, $parameters)
442
-    {
443
-        $result = call_user_func_array([$this->query, $method], $parameters);
444
-
445
-        if ($result === $this->query) {
446
-            return $this;
447
-        }
448
-
449
-        return $result;
450
-    }
22
+	/**
23
+	 * The mapper instance for the related entity
24
+	 *
25
+	 * @var Mapper
26
+	 */
27
+	protected $relatedMapper;
28
+
29
+	/**
30
+	 * The Analogue Query Builder instance.
31
+	 *
32
+	 * @var Query
33
+	 */
34
+	protected $query;
35
+
36
+	/**
37
+	 * The parent entity proxy instance.
38
+	 *
39
+	 * @var InternallyMappable
40
+	 */
41
+	protected $parent;
42
+
43
+	/**
44
+	 * The parent entity map
45
+	 * @var \Analogue\ORM\EntityMap
46
+	 */
47
+	protected $parentMap;
48
+
49
+	/**
50
+	 * The Parent Mapper instance
51
+	 *
52
+	 * @var Mapper
53
+	 */
54
+	protected $parentMapper;
55
+
56
+	/**
57
+	 * The related entity instance.
58
+	 *
59
+	 * @var object
60
+	 */
61
+	protected $related;
62
+
63
+	/**
64
+	 * The related entity Map
65
+	 * @var \Analogue\ORM\EntityMap
66
+	 */
67
+	protected $relatedMap;
68
+
69
+	/**
70
+	 * Indicate if the parent entity hold the key for the relation.
71
+	 *
72
+	 * @var boolean
73
+	 */
74
+	protected static $ownForeignKey = false;
75
+
76
+	/**
77
+	 * Indicate if the relationships use a pivot table.*
78
+	 *
79
+	 * @var boolean
80
+	 */
81
+	protected static $hasPivot = false;
82
+
83
+	/**
84
+	 * Indicates if the relation is adding constraints.
85
+	 *
86
+	 * @var bool
87
+	 */
88
+	protected static $constraints = true;
89
+
90
+	/**
91
+	 * Wrapper factory
92
+	 *
93
+	 * @var \Analogue\ORM\System\Wrappers\Factory
94
+	 */
95
+	protected $factory;
96
+
97
+	/**
98
+	 * Create a new relation instance.
99
+	 *
100
+	 * @param  Mapper   $mapper
101
+	 * @param  Mappable $parent
102
+	 * @throws \Analogue\ORM\Exceptions\MappingException
103
+	 */
104
+	public function __construct(Mapper $mapper, $parent)
105
+	{
106
+		$this->relatedMapper = $mapper;
107
+
108
+		$this->query = $mapper->getQuery();
109
+
110
+		$this->factory = new Factory;
111
+
112
+		$this->parent = $this->factory->make($parent);
113
+
114
+		$this->parentMapper = $mapper->getManager()->getMapper($parent);
115
+
116
+		$this->parentMap = $this->parentMapper->getEntityMap();
117
+
118
+		$this->related = $this->query->getEntityInstance();
119
+
120
+		$this->relatedMap = $mapper->getEntityMap();
121
+
122
+		$this->addConstraints();
123
+	}
124
+
125
+	/**
126
+	 * @param $related
127
+	 * @return mixed
128
+	 */
129
+	abstract public function attachTo($related);
130
+
131
+	/**
132
+	 * @param $related
133
+	 * @return mixed
134
+	 */
135
+	abstract public function detachFrom($related);
136
+
137
+	/**
138
+	 * Indicate if the parent entity hold the foreign key for relation.
139
+	 *
140
+	 * @return boolean
141
+	 */
142
+	public function ownForeignKey()
143
+	{
144
+		return static::$ownForeignKey;
145
+	}
146
+
147
+	/**
148
+	 * Indicate if the relationship uses a pivot table
149
+	 *
150
+	 * @return boolean
151
+	 */
152
+	public function hasPivot()
153
+	{
154
+		return static::$hasPivot;
155
+	}
156
+
157
+	/**
158
+	 * Set the base constraints on the relation query.
159
+	 *
160
+	 * @return void
161
+	 */
162
+	abstract public function addConstraints();
163
+
164
+	/**
165
+	 * Set the constraints for an eager load of the relation.
166
+	 *
167
+	 * @param  array $models
168
+	 * @return void
169
+	 */
170
+	abstract public function addEagerConstraints(array $models);
171
+
172
+	/**
173
+	 * Initialize the relation on a set of models.
174
+	 *
175
+	 * @param  array  $models
176
+	 * @param  string $relation
177
+	 * @return array
178
+	 */
179
+	abstract public function initRelation(array $models, $relation);
180
+
181
+	/**
182
+	 * Match the eagerly loaded results to their parents.
183
+	 *
184
+	 * @param  array            $entities
185
+	 * @param  EntityCollection $results
186
+	 * @param  string           $relation
187
+	 * @return array
188
+	 */
189
+	abstract public function match(array $entities, EntityCollection $results, $relation);
190
+
191
+	/**
192
+	 * Get the results of the relationship.
193
+	 *
194
+	 * @param string $relation relation name in parent's entity map
195
+	 * @return mixed
196
+	 */
197
+	abstract public function getResults($relation);
198
+
199
+	/**
200
+	 * Get the relationship for eager loading.
201
+	 *
202
+	 * @return EntityCollection
203
+	 */
204
+	public function getEager()
205
+	{
206
+		return $this->get();
207
+	}
208
+
209
+	/**
210
+	 * Add the constraints for a relationship count query.
211
+	 *
212
+	 * @param  Query $query
213
+	 * @param  Query $parent
214
+	 * @return Query
215
+	 */
216
+	public function getRelationCountQuery(Query $query, Query $parent)
217
+	{
218
+		$query->select(new Expression('count(*)'));
219
+
220
+		$key = $this->wrap($this->getQualifiedParentKeyName());
221
+
222
+		return $query->where($this->getHasCompareKey(), '=', new Expression($key));
223
+	}
224
+
225
+	/**
226
+	 * Run a callback with constraints disabled on the relation.
227
+	 *
228
+	 * @param  Closure $callback
229
+	 * @return mixed
230
+	 */
231
+	public static function noConstraints(Closure $callback)
232
+	{
233
+		static::$constraints = false;
234
+
235
+		// When resetting the relation where clause, we want to shift the first element
236
+		// off of the bindings, leaving only the constraints that the developers put
237
+		// as "extra" on the relationships, and not original relation constraints.
238
+		$results = call_user_func($callback);
239
+
240
+		static::$constraints = true;
241
+
242
+		return $results;
243
+	}
244
+
245
+	/**
246
+	 * Get all of the primary keys for an array of entities.
247
+	 *
248
+	 * @param  array  $entities
249
+	 * @param  string $key
250
+	 * @return array
251
+	 */
252
+	protected function getKeys(array $entities, $key = null)
253
+	{
254
+		if (is_null($key)) {
255
+			$key = $this->relatedMap->getKeyName();
256
+		}
257
+
258
+		$host = $this;
259
+
260
+		return array_unique(array_values(array_map(function ($value) use ($key, $host) {
261
+			if (!$value instanceof InternallyMappable) {
262
+				$value = $host->factory->make($value);
263
+			}
264
+
265
+			return $value->getEntityAttribute($key);
266
+
267
+		}, $entities)));
268
+	}
269
+
270
+	/**
271
+	 * Get the underlying query for the relation.
272
+	 *
273
+	 * @return Query
274
+	 */
275
+	public function getQuery()
276
+	{
277
+		return $this->query;
278
+	}
279
+
280
+	/**
281
+	 * Get the base query builder
282
+	 *
283
+	 * @return \Analogue\ORM\Drivers\QueryAdapter
284
+	 */
285
+	public function getBaseQuery()
286
+	{
287
+		return $this->query->getQuery();
288
+	}
289
+
290
+	/**
291
+	 * Get the parent model of the relation.
292
+	 *
293
+	 * @return InternallyMappable
294
+	 */
295
+	public function getParent()
296
+	{
297
+		return $this->parent;
298
+	}
299
+
300
+	/**
301
+	 * Get the fully qualified parent key name.
302
+	 *
303
+	 * @return string
304
+	 */
305
+	protected function getQualifiedParentKeyName()
306
+	{
307
+		return $this->parent->getQualifiedKeyName();
308
+	}
309
+
310
+	/**
311
+	 * Get the related entity of the relation.
312
+	 *
313
+	 * @return \Analogue\ORM\Entity
314
+	 */
315
+	public function getRelated()
316
+	{
317
+		return $this->related;
318
+	}
319
+
320
+	/**
321
+	 * Get the related mapper for the relation
322
+	 *
323
+	 * @return Mapper
324
+	 */
325
+	public function getRelatedMapper()
326
+	{
327
+		return $this->relatedMapper;
328
+	}
329
+
330
+
331
+	/**
332
+	 * Get the name of the "created at" column.
333
+	 *
334
+	 * @return string
335
+	 */
336
+	public function createdAt()
337
+	{
338
+		return $this->parentMap->getCreatedAtColumn();
339
+	}
340
+
341
+	/**
342
+	 * Get the name of the "updated at" column.
343
+	 *
344
+	 * @return string
345
+	 */
346
+	public function updatedAt()
347
+	{
348
+		return $this->parentMap->getUpdatedAtColumn();
349
+	}
350
+
351
+	/**
352
+	 * Get the name of the related model's "updated at" column.
353
+	 *
354
+	 * @return string
355
+	 */
356
+	public function relatedUpdatedAt()
357
+	{
358
+		return $this->related->getUpdatedAtColumn();
359
+	}
360
+
361
+	/**
362
+	 * Wrap the given value with the parent query's grammar.
363
+	 *
364
+	 * @param  string $value
365
+	 * @return string
366
+	 */
367
+	public function wrap($value)
368
+	{
369
+		return $this->parentMapper->getQuery()->getQuery()->getGrammar()->wrap($value);
370
+	}
371
+
372
+	/**
373
+	 * Get a fresh timestamp
374
+	 *
375
+	 * @return Carbon
376
+	 */
377
+	protected function freshTimestamp()
378
+	{
379
+		return new Carbon;
380
+	}
381
+
382
+	/**
383
+	 * Cache the link between parent and related
384
+	 * into the mapper's Entity Cache.
385
+	 *
386
+	 * @param  EntityCollection|Mappable $results  result of the relation query
387
+	 * @param  string                    $relation name of the relation method on the parent entity
388
+	 * @return void
389
+	 */
390
+	protected function cacheRelation($results, $relation)
391
+	{
392
+		$cache = $this->parentMapper->getEntityCache();
393
+
394
+		$cache->cacheLoadedRelationResult($this->parent, $relation, $results, $this);
395
+	}
396
+
397
+	/**
398
+	 * Return Pivot attributes when available on a relationship
399
+	 *
400
+	 * @return array
401
+	 */
402
+	public function getPivotAttributes()
403
+	{
404
+		return [];
405
+	}
406
+
407
+	/**
408
+	 * Get a combo type.primaryKey
409
+	 *
410
+	 * @param  Mappable $entity
411
+	 * @return string
412
+	 */
413
+	protected function getEntityHash(Mappable $entity)
414
+	{
415
+		$class = get_class($entity);
416
+
417
+		$keyName = Mapper::getMapper($class)->getEntityMap()->getKeyName();
418
+
419
+		return $class . '.' . $entity->getEntityAttribute($keyName);
420
+	}
421
+
422
+	/**
423
+	 * Run synchronization content if needed by the
424
+	 * relation type.
425
+	 *
426
+	 * @param  array $actualContent
427
+	 * @return void
428
+	 */
429
+	public function sync(array $actualContent)
430
+	{
431
+		//
432
+	}
433
+
434
+	/**
435
+	 * Handle dynamic method calls to the relationship.
436
+	 *
437
+	 * @param  string $method
438
+	 * @param  array  $parameters
439
+	 * @return mixed
440
+	 */
441
+	public function __call($method, $parameters)
442
+	{
443
+		$result = call_user_func_array([$this->query, $method], $parameters);
444
+
445
+		if ($result === $this->query) {
446
+			return $this;
447
+		}
448
+
449
+		return $result;
450
+	}
451 451
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -257,7 +257,7 @@  discard block
 block discarded – undo
257 257
 
258 258
         $host = $this;
259 259
 
260
-        return array_unique(array_values(array_map(function ($value) use ($key, $host) {
260
+        return array_unique(array_values(array_map(function($value) use ($key, $host) {
261 261
             if (!$value instanceof InternallyMappable) {
262 262
                 $value = $host->factory->make($value);
263 263
             }
@@ -416,7 +416,7 @@  discard block
 block discarded – undo
416 416
 
417 417
         $keyName = Mapper::getMapper($class)->getEntityMap()->getKeyName();
418 418
 
419
-        return $class . '.' . $entity->getEntityAttribute($keyName);
419
+        return $class.'.'.$entity->getEntityAttribute($keyName);
420 420
     }
421 421
 
422 422
     /**
Please login to merge, or discard this patch.
src/Commands/Store.php 1 patch
Indentation   +221 added lines, -221 removed lines patch added patch discarded remove patch
@@ -14,235 +14,235 @@
 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
-        }
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 48
         
49
-        /**
50
-         * We'll only run an update if the entity
51
-         * is actually dirty
52
-         */
53
-        if ($this->aggregate->isDirty()) {
54
-            if ($mapper->fireEvent('updating', $entity) === false) {
55
-                return false;
56
-            }
57
-            $this->update();
58
-
59
-            $mapper->fireEvent('updated', $entity, false);
60
-        }
61
-
62
-        $this->postStoreProcess();
63
-
64
-        $mapper->fireEvent('stored', $entity, false);
65
-
66
-        return $entity;
67
-    }
68
-
69
-    /**
70
-     * Run all operations that have to occur before actually
71
-     * storing the entity
72
-     *
73
-     * @throws \InvalidArgumentException
74
-     * @return void
75
-     */
76
-    protected function preStoreProcess()
77
-    {
78
-        // Create any related object that doesn't exist in the database.
79
-        $localRelationships = $this->aggregate->getEntityMap()->getLocalRelationships();
80
-
81
-        $this->createRelatedEntities($localRelationships);
82
-    }
83
-
84
-    /**
85
-     * Check for existence and create non-existing related entities
86
-     *
87
-     * @param  array
88
-     * @throws \InvalidArgumentException
89
-     * @return void
90
-     */
91
-    protected function createRelatedEntities($relations)
92
-    {
93
-        $entitiesToCreate = $this->aggregate->getNonExistingRelated($relations);
94
-
95
-        foreach ($entitiesToCreate as $aggregate) {
96
-            $this->createStoreCommand($aggregate)->execute();
97
-        }
98
-    }
99
-
100
-    /**
101
-     * Create a new store command
102
-     *
103
-     * @param  Aggregate $aggregate
104
-     * @return Store
105
-     */
106
-    protected function createStoreCommand(Aggregate $aggregate)
107
-    {
108
-        // We gotta retrieve the corresponding query adapter to use.
109
-        $mapper = $aggregate->getMapper();
110
-
111
-        return new Store($aggregate, $mapper->newQueryBuilder());
112
-    }
113
-
114
-    /**
115
-     * Run all operations that have to occur after the entity
116
-     * is stored.
117
-     *
118
-     * @throws \InvalidArgumentException
119
-     * @return void
120
-     */
121
-    protected function postStoreProcess()
122
-    {
123
-        $aggregate = $this->aggregate;
124
-
125
-        // Create any related object that doesn't exist in the database.
126
-        $foreignRelationships = $aggregate->getEntityMap()->getForeignRelationships();
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
-
136
-        foreach ($dirtyRelatedAggregates as $related) {
137
-            $this->createStoreCommand($related)->execute();
138
-        }
139
-
140
-        if ($this->aggregate->exists()) {
141
-            $this->aggregate->syncRelationships();
142
-        }
143
-        // This should be move to the wrapper class
144
-        // so it's the same code for the entity builder
145
-        $aggregate->setProxies();
146
-
147
-        // Update Entity Cache
148
-        $aggregate->getMapper()->getEntityCache()->refresh($aggregate);
149
-    }
150
-
151
-    /**
152
-     * Update Related Entities which attributes have
153
-     * been modified.
154
-     *
155
-     * @return void
156
-     */
157
-    protected function updateDirtyRelated()
158
-    {
159
-        $relations = $this->entityMap->getRelationships();
160
-        $attributes = $this->getAttributes();
161
-
162
-        foreach ($relations as $relation) {
163
-            if (!array_key_exists($relation, $attributes)) {
164
-                continue;
165
-            }
166
-
167
-            $value = $attributes[$relation];
168
-
169
-            if ($value == null) {
170
-                continue;
171
-            }
172
-
173
-            if ($value instanceof EntityProxy) {
174
-                continue;
175
-            }
176
-
177
-            if ($value instanceof CollectionProxy && $value->isLoaded()) {
178
-                $value = $value->getUnderlyingCollection();
179
-            }
180
-            if ($value instanceof CollectionProxy && !$value->isLoaded()) {
181
-                foreach ($value->getAddedItems() as $entity) {
182
-                    $this->updateEntityIfDirty($entity);
183
-                }
184
-                continue;
185
-            }
186
-
187
-            if ($value instanceof EntityCollection) {
188
-                foreach ($value as $entity) {
189
-                    if (!$this->createEntityIfNotExists($entity)) {
190
-                        $this->updateEntityIfDirty($entity);
191
-                    }
192
-                }
193
-                continue;
194
-            }
195
-            if ($value instanceof Mappable) {
196
-                $this->updateEntityIfDirty($value);
197
-                continue;
198
-            }
199
-        }
200
-    }
201
-
202
-    /**
203
-     * Execute an insert statement on the database
204
-     *
205
-     * @return void
206
-     */
207
-    protected function insert()
208
-    {
209
-        $aggregate = $this->aggregate;
210
-
211
-        $attributes = $aggregate->getRawAttributes();
49
+		/**
50
+		 * We'll only run an update if the entity
51
+		 * is actually dirty
52
+		 */
53
+		if ($this->aggregate->isDirty()) {
54
+			if ($mapper->fireEvent('updating', $entity) === false) {
55
+				return false;
56
+			}
57
+			$this->update();
58
+
59
+			$mapper->fireEvent('updated', $entity, false);
60
+		}
61
+
62
+		$this->postStoreProcess();
63
+
64
+		$mapper->fireEvent('stored', $entity, false);
65
+
66
+		return $entity;
67
+	}
68
+
69
+	/**
70
+	 * Run all operations that have to occur before actually
71
+	 * storing the entity
72
+	 *
73
+	 * @throws \InvalidArgumentException
74
+	 * @return void
75
+	 */
76
+	protected function preStoreProcess()
77
+	{
78
+		// Create any related object that doesn't exist in the database.
79
+		$localRelationships = $this->aggregate->getEntityMap()->getLocalRelationships();
80
+
81
+		$this->createRelatedEntities($localRelationships);
82
+	}
83
+
84
+	/**
85
+	 * Check for existence and create non-existing related entities
86
+	 *
87
+	 * @param  array
88
+	 * @throws \InvalidArgumentException
89
+	 * @return void
90
+	 */
91
+	protected function createRelatedEntities($relations)
92
+	{
93
+		$entitiesToCreate = $this->aggregate->getNonExistingRelated($relations);
94
+
95
+		foreach ($entitiesToCreate as $aggregate) {
96
+			$this->createStoreCommand($aggregate)->execute();
97
+		}
98
+	}
99
+
100
+	/**
101
+	 * Create a new store command
102
+	 *
103
+	 * @param  Aggregate $aggregate
104
+	 * @return Store
105
+	 */
106
+	protected function createStoreCommand(Aggregate $aggregate)
107
+	{
108
+		// We gotta retrieve the corresponding query adapter to use.
109
+		$mapper = $aggregate->getMapper();
110
+
111
+		return new Store($aggregate, $mapper->newQueryBuilder());
112
+	}
113
+
114
+	/**
115
+	 * Run all operations that have to occur after the entity
116
+	 * is stored.
117
+	 *
118
+	 * @throws \InvalidArgumentException
119
+	 * @return void
120
+	 */
121
+	protected function postStoreProcess()
122
+	{
123
+		$aggregate = $this->aggregate;
124
+
125
+		// Create any related object that doesn't exist in the database.
126
+		$foreignRelationships = $aggregate->getEntityMap()->getForeignRelationships();
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
+
136
+		foreach ($dirtyRelatedAggregates as $related) {
137
+			$this->createStoreCommand($related)->execute();
138
+		}
139
+
140
+		if ($this->aggregate->exists()) {
141
+			$this->aggregate->syncRelationships();
142
+		}
143
+		// This should be move to the wrapper class
144
+		// so it's the same code for the entity builder
145
+		$aggregate->setProxies();
146
+
147
+		// Update Entity Cache
148
+		$aggregate->getMapper()->getEntityCache()->refresh($aggregate);
149
+	}
150
+
151
+	/**
152
+	 * Update Related Entities which attributes have
153
+	 * been modified.
154
+	 *
155
+	 * @return void
156
+	 */
157
+	protected function updateDirtyRelated()
158
+	{
159
+		$relations = $this->entityMap->getRelationships();
160
+		$attributes = $this->getAttributes();
161
+
162
+		foreach ($relations as $relation) {
163
+			if (!array_key_exists($relation, $attributes)) {
164
+				continue;
165
+			}
166
+
167
+			$value = $attributes[$relation];
168
+
169
+			if ($value == null) {
170
+				continue;
171
+			}
172
+
173
+			if ($value instanceof EntityProxy) {
174
+				continue;
175
+			}
176
+
177
+			if ($value instanceof CollectionProxy && $value->isLoaded()) {
178
+				$value = $value->getUnderlyingCollection();
179
+			}
180
+			if ($value instanceof CollectionProxy && !$value->isLoaded()) {
181
+				foreach ($value->getAddedItems() as $entity) {
182
+					$this->updateEntityIfDirty($entity);
183
+				}
184
+				continue;
185
+			}
186
+
187
+			if ($value instanceof EntityCollection) {
188
+				foreach ($value as $entity) {
189
+					if (!$this->createEntityIfNotExists($entity)) {
190
+						$this->updateEntityIfDirty($entity);
191
+					}
192
+				}
193
+				continue;
194
+			}
195
+			if ($value instanceof Mappable) {
196
+				$this->updateEntityIfDirty($value);
197
+				continue;
198
+			}
199
+		}
200
+	}
201
+
202
+	/**
203
+	 * Execute an insert statement on the database
204
+	 *
205
+	 * @return void
206
+	 */
207
+	protected function insert()
208
+	{
209
+		$aggregate = $this->aggregate;
210
+
211
+		$attributes = $aggregate->getRawAttributes();
212 212
         
213
-        $keyName = $aggregate->getEntityMap()->getKeyName();
213
+		$keyName = $aggregate->getEntityMap()->getKeyName();
214 214
 
215
-        // Check if the primary key is defined in the attributes
216
-        if (array_key_exists($keyName, $attributes) && $attributes[$keyName] != null) {
217
-            $this->query->insert($attributes);
218
-        } else {
219
-            $sequence = $aggregate->getEntityMap()->getSequence();
215
+		// Check if the primary key is defined in the attributes
216
+		if (array_key_exists($keyName, $attributes) && $attributes[$keyName] != null) {
217
+			$this->query->insert($attributes);
218
+		} else {
219
+			$sequence = $aggregate->getEntityMap()->getSequence();
220 220
 
221
-            $id = $this->query->insertGetId($attributes, $sequence);
221
+			$id = $this->query->insertGetId($attributes, $sequence);
222 222
 
223
-            $aggregate->setEntityAttribute($keyName, $id);
224
-        }
225
-    }
223
+			$aggregate->setEntityAttribute($keyName, $id);
224
+		}
225
+	}
226 226
 
227
-    /**
228
-     * Run an update statement on the entity
229
-     *
230
-     * @throws \InvalidArgumentException
231
-     *
232
-     * @return void
233
-     */
234
-    protected function update()
235
-    {
236
-        $query = $this->query;
227
+	/**
228
+	 * Run an update statement on the entity
229
+	 *
230
+	 * @throws \InvalidArgumentException
231
+	 *
232
+	 * @return void
233
+	 */
234
+	protected function update()
235
+	{
236
+		$query = $this->query;
237 237
 
238
-        $keyName = $this->aggregate->getEntityKey();
238
+		$keyName = $this->aggregate->getEntityKey();
239 239
 
240
-        $query = $query->where($keyName, '=', $this->aggregate->getEntityId());
240
+		$query = $query->where($keyName, '=', $this->aggregate->getEntityId());
241 241
 
242
-        $dirtyAttributes = $this->aggregate->getDirtyRawAttributes();
242
+		$dirtyAttributes = $this->aggregate->getDirtyRawAttributes();
243 243
 
244
-        if (count($dirtyAttributes) > 0) {
245
-            $query->update($dirtyAttributes);
246
-        }
247
-    }
244
+		if (count($dirtyAttributes) > 0) {
245
+			$query->update($dirtyAttributes);
246
+		}
247
+	}
248 248
 }
Please login to merge, or discard this patch.
src/Commands/Delete.php 1 patch
Indentation   +29 added lines, -29 removed lines patch added patch discarded remove patch
@@ -6,38 +6,38 @@
 block discarded – undo
6 6
 
7 7
 class Delete extends Command
8 8
 {
9
-    /**
10
-     * Execute the Delete Statement
11
-     *
12
-     * @throws MappingException
13
-     * @throws \InvalidArgumentException
14
-     * @return false|void
15
-     */
16
-    public function execute()
17
-    {
18
-        $aggregate = $this->aggregate;
19
-
20
-        $entity = $aggregate->getEntityObject();
21
-
22
-        $mapper = $aggregate->getMapper();
23
-
24
-        if ($mapper->fireEvent('deleting', $entity) === false) {
25
-            return false;
26
-        }
27
-
28
-        $keyName = $aggregate->getEntityMap()->getKeyName();
9
+	/**
10
+	 * Execute the Delete Statement
11
+	 *
12
+	 * @throws MappingException
13
+	 * @throws \InvalidArgumentException
14
+	 * @return false|void
15
+	 */
16
+	public function execute()
17
+	{
18
+		$aggregate = $this->aggregate;
19
+
20
+		$entity = $aggregate->getEntityObject();
21
+
22
+		$mapper = $aggregate->getMapper();
23
+
24
+		if ($mapper->fireEvent('deleting', $entity) === false) {
25
+			return false;
26
+		}
27
+
28
+		$keyName = $aggregate->getEntityMap()->getKeyName();
29 29
         
30
-        $id = $this->aggregate->getEntityId();
30
+		$id = $this->aggregate->getEntityId();
31 31
 
32
-        if (is_null($id)) {
33
-            throw new MappingException('Executed a delete command on an entity with "null" as primary key');
34
-        }
32
+		if (is_null($id)) {
33
+			throw new MappingException('Executed a delete command on an entity with "null" as primary key');
34
+		}
35 35
 
36
-        $this->query->where($keyName, '=', $id)->delete();
36
+		$this->query->where($keyName, '=', $id)->delete();
37 37
 
38
-        $mapper->fireEvent('deleted', $entity, false);
38
+		$mapper->fireEvent('deleted', $entity, false);
39 39
 
40
-        // Once the Entity is successfully deleted, we'll just set the primary key to null.
41
-        $aggregate->setEntityAttribute($keyName, null);
42
-    }
40
+		// Once the Entity is successfully deleted, we'll just set the primary key to null.
41
+		$aggregate->setEntityAttribute($keyName, null);
42
+	}
43 43
 }
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, QueryAdapter $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, QueryAdapter $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/ValueObject.php 1 patch
Indentation   +152 added lines, -152 removed lines patch added patch discarded remove patch
@@ -11,158 +11,158 @@
 block discarded – undo
11 11
 
12 12
 class ValueObject implements Mappable, ArrayAccess, Jsonable, JsonSerializable, Arrayable
13 13
 {
14
-    use MappableTrait;
15
-
16
-    /**
17
-     * Dynamically retrieve attributes on the entity.
18
-     *
19
-     * @param  string $key
20
-     * @return mixed
21
-     */
22
-    public function __get($key)
23
-    {
24
-        return $this->attributes[$key];
25
-    }
26
-
27
-    /**
28
-     * Dynamically set attributes on the entity.
29
-     *
30
-     * @param  string $key
31
-     * @param  mixed  $value
32
-     * @return void
33
-     */
34
-    public function __set($key, $value)
35
-    {
36
-        $this->attributes[$key] = $value;
37
-    }
38
-
39
-    /**
40
-     * Determine if an attribute exists on the entity.
41
-     *
42
-     * @param  string $key
43
-     * @return bool
44
-     */
45
-    public function __isset($key)
46
-    {
47
-        return array_key_exists($key, $this->attributes);
48
-    }
49
-
50
-    /**
51
-     * Unset an attribute on the entity.
52
-     *
53
-     * @param  string $key
54
-     * @return void
55
-     */
56
-    public function __unset($key)
57
-    {
58
-        unset($this->attributes[$key]);
59
-    }
14
+	use MappableTrait;
15
+
16
+	/**
17
+	 * Dynamically retrieve attributes on the entity.
18
+	 *
19
+	 * @param  string $key
20
+	 * @return mixed
21
+	 */
22
+	public function __get($key)
23
+	{
24
+		return $this->attributes[$key];
25
+	}
26
+
27
+	/**
28
+	 * Dynamically set attributes on the entity.
29
+	 *
30
+	 * @param  string $key
31
+	 * @param  mixed  $value
32
+	 * @return void
33
+	 */
34
+	public function __set($key, $value)
35
+	{
36
+		$this->attributes[$key] = $value;
37
+	}
38
+
39
+	/**
40
+	 * Determine if an attribute exists on the entity.
41
+	 *
42
+	 * @param  string $key
43
+	 * @return bool
44
+	 */
45
+	public function __isset($key)
46
+	{
47
+		return array_key_exists($key, $this->attributes);
48
+	}
49
+
50
+	/**
51
+	 * Unset an attribute on the entity.
52
+	 *
53
+	 * @param  string $key
54
+	 * @return void
55
+	 */
56
+	public function __unset($key)
57
+	{
58
+		unset($this->attributes[$key]);
59
+	}
60 60
 
61 61
     
62
-    /**
63
-     * Determine if the given attribute exists.
64
-     *
65
-     * @param  mixed $offset
66
-     * @return bool
67
-     */
68
-    public function offsetExists($offset)
69
-    {
70
-        return isset($this->$offset);
71
-    }
72
-
73
-    /**
74
-     * Get the value for a given offset.
75
-     *
76
-     * @param  mixed $offset
77
-     * @return mixed
78
-     */
79
-    public function offsetGet($offset)
80
-    {
81
-        return $this->$offset;
82
-    }
83
-
84
-    /**
85
-     * Set the value for a given offset.
86
-     *
87
-     * @param  mixed $offset
88
-     * @param  mixed $value
89
-     * @return void
90
-     */
91
-    public function offsetSet($offset, $value)
92
-    {
93
-        $this->$offset = $value;
94
-    }
95
-
96
-    /**
97
-     * Unset the value for a given offset.
98
-     *
99
-     * @param  mixed $offset
100
-     * @return void
101
-     */
102
-    public function offsetUnset($offset)
103
-    {
104
-        unset($this->$offset);
105
-    }
106
-
107
-    /**
108
-     * Convert the object into something JSON serializable.
109
-     *
110
-     * @return array
111
-     */
112
-    public function jsonSerialize()
113
-    {
114
-        return $this->toArray();
115
-    }
116
-
117
-    /**
118
-     * Convert the entity instance to JSON.
119
-     *
120
-     * @param  int $options
121
-     * @return string
122
-     */
123
-    public function toJson($options = 0)
124
-    {
125
-        return json_encode($this->toArray(), $options);
126
-    }
127
-
128
-    /**
129
-     * Convert Mappable object to array;
130
-     *
131
-     * @return array
132
-     */
133
-    public function toArray()
134
-    {
135
-        return $this->attributesToArray($this->attributes);
136
-    }
137
-
138
-    /**
139
-     * Transform the Object to array/json,
140
-     *
141
-     * @param  array $sourceAttributes
142
-     * @return array
143
-     */
144
-    protected function attributesToArray(array $sourceAttributes)
145
-    {
146
-        $attributes = [];
147
-
148
-        foreach ($sourceAttributes as $key => $attribute) {
149
-            // If the attribute is a proxy, and hasn't be loaded, we discard
150
-            // it from the returned set.
151
-            if ($attribute instanceof ProxyInterface && !$attribute->isLoaded()) {
152
-                continue;
153
-            }
154
-
155
-            if ($attribute instanceof Carbon) {
156
-                $attributes[$key] = $attribute->__toString();
157
-                continue;
158
-            }
159
-
160
-            if ($attribute instanceof Arrayable) {
161
-                $attributes[$key] = $attribute->toArray();
162
-            } else {
163
-                $attributes[$key] = $attribute;
164
-            }
165
-        }
166
-        return $attributes;
167
-    }
62
+	/**
63
+	 * Determine if the given attribute exists.
64
+	 *
65
+	 * @param  mixed $offset
66
+	 * @return bool
67
+	 */
68
+	public function offsetExists($offset)
69
+	{
70
+		return isset($this->$offset);
71
+	}
72
+
73
+	/**
74
+	 * Get the value for a given offset.
75
+	 *
76
+	 * @param  mixed $offset
77
+	 * @return mixed
78
+	 */
79
+	public function offsetGet($offset)
80
+	{
81
+		return $this->$offset;
82
+	}
83
+
84
+	/**
85
+	 * Set the value for a given offset.
86
+	 *
87
+	 * @param  mixed $offset
88
+	 * @param  mixed $value
89
+	 * @return void
90
+	 */
91
+	public function offsetSet($offset, $value)
92
+	{
93
+		$this->$offset = $value;
94
+	}
95
+
96
+	/**
97
+	 * Unset the value for a given offset.
98
+	 *
99
+	 * @param  mixed $offset
100
+	 * @return void
101
+	 */
102
+	public function offsetUnset($offset)
103
+	{
104
+		unset($this->$offset);
105
+	}
106
+
107
+	/**
108
+	 * Convert the object into something JSON serializable.
109
+	 *
110
+	 * @return array
111
+	 */
112
+	public function jsonSerialize()
113
+	{
114
+		return $this->toArray();
115
+	}
116
+
117
+	/**
118
+	 * Convert the entity instance to JSON.
119
+	 *
120
+	 * @param  int $options
121
+	 * @return string
122
+	 */
123
+	public function toJson($options = 0)
124
+	{
125
+		return json_encode($this->toArray(), $options);
126
+	}
127
+
128
+	/**
129
+	 * Convert Mappable object to array;
130
+	 *
131
+	 * @return array
132
+	 */
133
+	public function toArray()
134
+	{
135
+		return $this->attributesToArray($this->attributes);
136
+	}
137
+
138
+	/**
139
+	 * Transform the Object to array/json,
140
+	 *
141
+	 * @param  array $sourceAttributes
142
+	 * @return array
143
+	 */
144
+	protected function attributesToArray(array $sourceAttributes)
145
+	{
146
+		$attributes = [];
147
+
148
+		foreach ($sourceAttributes as $key => $attribute) {
149
+			// If the attribute is a proxy, and hasn't be loaded, we discard
150
+			// it from the returned set.
151
+			if ($attribute instanceof ProxyInterface && !$attribute->isLoaded()) {
152
+				continue;
153
+			}
154
+
155
+			if ($attribute instanceof Carbon) {
156
+				$attributes[$key] = $attribute->__toString();
157
+				continue;
158
+			}
159
+
160
+			if ($attribute instanceof Arrayable) {
161
+				$attributes[$key] = $attribute->toArray();
162
+			} else {
163
+				$attributes[$key] = $attribute;
164
+			}
165
+		}
166
+		return $attributes;
167
+	}
168 168
 }
Please login to merge, or discard this patch.
src/Plugins/SoftDeletes/Restore.php 1 patch
Indentation   +25 added lines, -25 removed lines patch added patch discarded remove patch
@@ -6,36 +6,36 @@
 block discarded – undo
6 6
 
7 7
 class Restore extends Command
8 8
 {
9
-    /**
10
-     * @throws \InvalidArgumentException
11
-     *
12
-     * @return false|mixed
13
-     */
14
-    public function execute()
15
-    {
16
-        $aggregate = $this->aggregate;
17
-        $entity = $aggregate->getEntityObject();
18
-        $mapper = $aggregate->getMapper();
19
-        $entityMap = $mapper->getEntityMap();
20
-
21
-        if ($mapper->fireEvent('restoring', $entity) === false) {
22
-            return false;
23
-        }
24
-
25
-        $keyName = $entityMap->getKeyName();
9
+	/**
10
+	 * @throws \InvalidArgumentException
11
+	 *
12
+	 * @return false|mixed
13
+	 */
14
+	public function execute()
15
+	{
16
+		$aggregate = $this->aggregate;
17
+		$entity = $aggregate->getEntityObject();
18
+		$mapper = $aggregate->getMapper();
19
+		$entityMap = $mapper->getEntityMap();
20
+
21
+		if ($mapper->fireEvent('restoring', $entity) === false) {
22
+			return false;
23
+		}
24
+
25
+		$keyName = $entityMap->getKeyName();
26 26
         
27
-        $query = $this->query->where($keyName, '=', $aggregate->getEntityAttribute($keyName));
27
+		$query = $this->query->where($keyName, '=', $aggregate->getEntityAttribute($keyName));
28 28
 
29
-        $deletedAtColumn = $entityMap->getQualifiedDeletedAtColumn();
29
+		$deletedAtColumn = $entityMap->getQualifiedDeletedAtColumn();
30 30
 
31
-        $query->update([$deletedAtColumn => null]);
31
+		$query->update([$deletedAtColumn => null]);
32 32
         
33
-        $aggregate->setEntityAttribute($deletedAtColumn, null);
33
+		$aggregate->setEntityAttribute($deletedAtColumn, null);
34 34
         
35
-        $mapper->fireEvent('restored', $entity, false);
35
+		$mapper->fireEvent('restored', $entity, false);
36 36
 
37
-        $mapper->getEntityCache()->refresh($aggregate);
37
+		$mapper->getEntityCache()->refresh($aggregate);
38 38
 
39
-        return $entity;
40
-    }
39
+		return $entity;
40
+	}
41 41
 }
Please login to merge, or discard this patch.
src/Plugins/SoftDeletes/SoftDeletesPlugin.php 2 patches
Indentation   +66 added lines, -66 removed lines patch added patch discarded remove patch
@@ -13,80 +13,80 @@
 block discarded – undo
13 13
  */
14 14
 class SoftDeletesPlugin extends AnaloguePlugin
15 15
 {
16
-    /**
17
-     * Register the plugin
18
-     *
19
-     * @throws \Exception
20
-     * @return void
21
-     */
22
-    public function register()
23
-    {
24
-        $host = $this;
25
-
26
-        // Hook any mapper init and check the mapping include soft deletes.
27
-        $this->manager->registerGlobalEvent('initialized', function (Mapper $mapper) use ($host) {
28
-            $entityMap = $mapper->getEntityMap();
29
-
30
-            if ($entityMap->usesSoftDeletes()) {
31
-                $host->registerSoftDelete($mapper);
32
-            }
33
-
34
-        });
35
-    }
36
-
37
-    /**
38
-     * By hooking to the mapper initialization event, we can extend it
39
-     * with the softDelete capacity.
40
-     *
41
-     * @param  \Analogue\ORM\System\Mapper $mapper
42
-     * @throws \Analogue\ORM\Exceptions\MappingException
43
-     * @return bool|void
44
-     */
45
-    protected function registerSoftDelete(Mapper $mapper)
46
-    {
47
-        $entityMap = $mapper->getEntityMap();
48
-
49
-        // Add Scopes
50
-        $mapper->addGlobalScope(new SoftDeletingScope);
51
-
52
-        $host = $this;
53
-
54
-        // Register 'deleting' events
55
-        $mapper->registerEvent('deleting', function ($entity) use ($entityMap, $host) {
16
+	/**
17
+	 * Register the plugin
18
+	 *
19
+	 * @throws \Exception
20
+	 * @return void
21
+	 */
22
+	public function register()
23
+	{
24
+		$host = $this;
25
+
26
+		// Hook any mapper init and check the mapping include soft deletes.
27
+		$this->manager->registerGlobalEvent('initialized', function (Mapper $mapper) use ($host) {
28
+			$entityMap = $mapper->getEntityMap();
29
+
30
+			if ($entityMap->usesSoftDeletes()) {
31
+				$host->registerSoftDelete($mapper);
32
+			}
33
+
34
+		});
35
+	}
36
+
37
+	/**
38
+	 * By hooking to the mapper initialization event, we can extend it
39
+	 * with the softDelete capacity.
40
+	 *
41
+	 * @param  \Analogue\ORM\System\Mapper $mapper
42
+	 * @throws \Analogue\ORM\Exceptions\MappingException
43
+	 * @return bool|void
44
+	 */
45
+	protected function registerSoftDelete(Mapper $mapper)
46
+	{
47
+		$entityMap = $mapper->getEntityMap();
48
+
49
+		// Add Scopes
50
+		$mapper->addGlobalScope(new SoftDeletingScope);
51
+
52
+		$host = $this;
53
+
54
+		// Register 'deleting' events
55
+		$mapper->registerEvent('deleting', function ($entity) use ($entityMap, $host) {
56 56
             
57
-            // Convert Entity into an EntityWrapper
58
-            $factory = new Factory;
57
+			// Convert Entity into an EntityWrapper
58
+			$factory = new Factory;
59 59
 
60
-            $wrappedEntity = $factory->make($entity);
60
+			$wrappedEntity = $factory->make($entity);
61 61
 
62
-            $deletedAtField = $entityMap->getQualifiedDeletedAtColumn();
62
+			$deletedAtField = $entityMap->getQualifiedDeletedAtColumn();
63 63
 
64
-            if (!is_null($wrappedEntity->getEntityAttribute($deletedAtField))) {
65
-                return true;
66
-            } else {
67
-                $time = new Carbon;
64
+			if (!is_null($wrappedEntity->getEntityAttribute($deletedAtField))) {
65
+				return true;
66
+			} else {
67
+				$time = new Carbon;
68 68
 
69
-                $wrappedEntity->setEntityAttribute($deletedAtField, $time);
69
+				$wrappedEntity->setEntityAttribute($deletedAtField, $time);
70 70
 
71
-                $plainObject = $wrappedEntity->getObject();
72
-                $host->manager->mapper(get_class($plainObject))->store($plainObject);
71
+				$plainObject = $wrappedEntity->getObject();
72
+				$host->manager->mapper(get_class($plainObject))->store($plainObject);
73 73
 
74
-                return false;
75
-            }
74
+				return false;
75
+			}
76 76
 
77
-        });
77
+		});
78 78
 
79
-        // Register RestoreCommand
80
-        $mapper->addCustomCommand('Analogue\ORM\Plugins\SoftDeletes\Restore');
81
-    }
79
+		// Register RestoreCommand
80
+		$mapper->addCustomCommand('Analogue\ORM\Plugins\SoftDeletes\Restore');
81
+	}
82 82
 
83
-    /**
84
-     * Get custom events provided by the plugin
85
-     *
86
-     * @return string[]
87
-     */
88
-    public function getCustomEvents()
89
-    {
90
-        return ['restoring', 'restored'];
91
-    }
83
+	/**
84
+	 * Get custom events provided by the plugin
85
+	 *
86
+	 * @return string[]
87
+	 */
88
+	public function getCustomEvents()
89
+	{
90
+		return ['restoring', 'restored'];
91
+	}
92 92
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -24,7 +24,7 @@  discard block
 block discarded – undo
24 24
         $host = $this;
25 25
 
26 26
         // Hook any mapper init and check the mapping include soft deletes.
27
-        $this->manager->registerGlobalEvent('initialized', function (Mapper $mapper) use ($host) {
27
+        $this->manager->registerGlobalEvent('initialized', function(Mapper $mapper) use ($host) {
28 28
             $entityMap = $mapper->getEntityMap();
29 29
 
30 30
             if ($entityMap->usesSoftDeletes()) {
@@ -52,7 +52,7 @@  discard block
 block discarded – undo
52 52
         $host = $this;
53 53
 
54 54
         // Register 'deleting' events
55
-        $mapper->registerEvent('deleting', function ($entity) use ($entityMap, $host) {
55
+        $mapper->registerEvent('deleting', function($entity) use ($entityMap, $host) {
56 56
             
57 57
             // Convert Entity into an EntityWrapper
58 58
             $factory = new Factory;
Please login to merge, or discard this patch.
src/Plugins/Timestamps/TimestampsPlugin.php 2 patches
Indentation   +39 added lines, -39 removed lines patch added patch discarded remove patch
@@ -12,55 +12,55 @@
 block discarded – undo
12 12
  */
13 13
 class TimestampsPlugin extends AnaloguePlugin
14 14
 {
15
-    /**
16
-     * Register the plugin
17
-     *
18
-     * @throws \Exception
19
-     * @return void
20
-     */
21
-    public function register()
22
-    {
23
-        $this->manager->registerGlobalEvent('initialized', function (Mapper $mapper) {
24
-            $entityMap = $mapper->getEntityMap();
15
+	/**
16
+	 * Register the plugin
17
+	 *
18
+	 * @throws \Exception
19
+	 * @return void
20
+	 */
21
+	public function register()
22
+	{
23
+		$this->manager->registerGlobalEvent('initialized', function (Mapper $mapper) {
24
+			$entityMap = $mapper->getEntityMap();
25 25
 
26
-            if ($entityMap->usesTimestamps()) {
27
-                $mapper->registerEvent('creating', function ($entity) use ($entityMap) {
26
+			if ($entityMap->usesTimestamps()) {
27
+				$mapper->registerEvent('creating', function ($entity) use ($entityMap) {
28 28
 
29
-                    $factory = new Factory;
30
-                    $wrappedEntity = $factory->make($entity);
29
+					$factory = new Factory;
30
+					$wrappedEntity = $factory->make($entity);
31 31
 
32
-                    $createdAtField = $entityMap->getCreatedAtColumn();
33
-                    $updatedAtField = $entityMap->getUpdatedAtColumn();
32
+					$createdAtField = $entityMap->getCreatedAtColumn();
33
+					$updatedAtField = $entityMap->getUpdatedAtColumn();
34 34
 
35
-                    $time = new Carbon;
35
+					$time = new Carbon;
36 36
 
37
-                    $wrappedEntity->setEntityAttribute($createdAtField, $time);
38
-                    $wrappedEntity->setEntityAttribute($updatedAtField, $time);
37
+					$wrappedEntity->setEntityAttribute($createdAtField, $time);
38
+					$wrappedEntity->setEntityAttribute($updatedAtField, $time);
39 39
 
40
-                });
40
+				});
41 41
 
42
-                $mapper->registerEvent('updating', function ($entity) use ($entityMap) {
42
+				$mapper->registerEvent('updating', function ($entity) use ($entityMap) {
43 43
 
44
-                    $factory = new Factory;
45
-                    $wrappedEntity = $factory->make($entity);
44
+					$factory = new Factory;
45
+					$wrappedEntity = $factory->make($entity);
46 46
 
47
-                    $updatedAtField = $entityMap->getUpdatedAtColumn();
47
+					$updatedAtField = $entityMap->getUpdatedAtColumn();
48 48
 
49
-                    $time = new Carbon;
49
+					$time = new Carbon;
50 50
 
51
-                    $wrappedEntity->setEntityAttribute($updatedAtField, $time);
52
-                });
53
-            }
54
-        });
55
-    }
51
+					$wrappedEntity->setEntityAttribute($updatedAtField, $time);
52
+				});
53
+			}
54
+		});
55
+	}
56 56
 
57
-    /**
58
-     * Get custom events provided by the plugin
59
-     *
60
-     * @return array
61
-     */
62
-    public function getCustomEvents()
63
-    {
64
-        return [];
65
-    }
57
+	/**
58
+	 * Get custom events provided by the plugin
59
+	 *
60
+	 * @return array
61
+	 */
62
+	public function getCustomEvents()
63
+	{
64
+		return [];
65
+	}
66 66
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -20,11 +20,11 @@  discard block
 block discarded – undo
20 20
      */
21 21
     public function register()
22 22
     {
23
-        $this->manager->registerGlobalEvent('initialized', function (Mapper $mapper) {
23
+        $this->manager->registerGlobalEvent('initialized', function(Mapper $mapper) {
24 24
             $entityMap = $mapper->getEntityMap();
25 25
 
26 26
             if ($entityMap->usesTimestamps()) {
27
-                $mapper->registerEvent('creating', function ($entity) use ($entityMap) {
27
+                $mapper->registerEvent('creating', function($entity) use ($entityMap) {
28 28
 
29 29
                     $factory = new Factory;
30 30
                     $wrappedEntity = $factory->make($entity);
@@ -39,7 +39,7 @@  discard block
 block discarded – undo
39 39
 
40 40
                 });
41 41
 
42
-                $mapper->registerEvent('updating', function ($entity) use ($entityMap) {
42
+                $mapper->registerEvent('updating', function($entity) use ($entityMap) {
43 43
 
44 44
                     $factory = new Factory;
45 45
                     $wrappedEntity = $factory->make($entity);
Please login to merge, or discard this patch.
src/Plugins/AnaloguePlugin.php 1 patch
Indentation   +26 added lines, -26 removed lines patch added patch discarded remove patch
@@ -6,33 +6,33 @@
 block discarded – undo
6 6
 
7 7
 abstract class AnaloguePlugin implements AnaloguePluginInterface
8 8
 {
9
-    /**
10
-     * Manager instance
11
-     *
12
-     * @var Manager
13
-     */
14
-    protected $manager;
9
+	/**
10
+	 * Manager instance
11
+	 *
12
+	 * @var Manager
13
+	 */
14
+	protected $manager;
15 15
 
16
-    /**
17
-     * AnaloguePlugin constructor.
18
-     * @param Manager $manager
19
-     */
20
-    public function __construct(Manager $manager)
21
-    {
22
-        $this->manager = $manager;
23
-    }
16
+	/**
17
+	 * AnaloguePlugin constructor.
18
+	 * @param Manager $manager
19
+	 */
20
+	public function __construct(Manager $manager)
21
+	{
22
+		$this->manager = $manager;
23
+	}
24 24
 
25
-    /**
26
-     * Boot the plugin
27
-     *
28
-     * @return void
29
-     */
30
-    abstract public function register();
25
+	/**
26
+	 * Boot the plugin
27
+	 *
28
+	 * @return void
29
+	 */
30
+	abstract public function register();
31 31
 
32
-    /**
33
-     * Get custom events provided by the plugin
34
-     *
35
-     * @return array
36
-     */
37
-    abstract public function getCustomEvents();
32
+	/**
33
+	 * Get custom events provided by the plugin
34
+	 *
35
+	 * @return array
36
+	 */
37
+	abstract public function getCustomEvents();
38 38
 }
Please login to merge, or discard this patch.