Completed
Pull Request — 5.1 (#84)
by Maxim
08:05
created
src/MappableTrait.php 1 patch
Indentation   +55 added lines, -55 removed lines patch added patch discarded remove patch
@@ -8,63 +8,63 @@
 block discarded – undo
8 8
  */
9 9
 trait MappableTrait
10 10
 {
11
-    /**
12
-     * The Entity's Attributes
13
-     * @var array
14
-     */
15
-    protected $attributes = [];
11
+	/**
12
+	 * The Entity's Attributes
13
+	 * @var array
14
+	 */
15
+	protected $attributes = [];
16 16
 
17
-    /**
18
-     * Method used by the mapper to set the object
19
-     * attribute raw values (hydration)
20
-     *
21
-     * @param array $attributes
22
-     *
23
-     * @return void
24
-     */
25
-    public function setEntityAttributes(array $attributes)
26
-    {
27
-        $this->attributes = $attributes;
28
-    }
17
+	/**
18
+	 * Method used by the mapper to set the object
19
+	 * attribute raw values (hydration)
20
+	 *
21
+	 * @param array $attributes
22
+	 *
23
+	 * @return void
24
+	 */
25
+	public function setEntityAttributes(array $attributes)
26
+	{
27
+		$this->attributes = $attributes;
28
+	}
29 29
 
30
-    /**
31
-     * Method used by the mapper to get the
32
-     * raw object's values.
33
-     *
34
-     * @return array
35
-     */
36
-    public function getEntityAttributes()
37
-    {
38
-        return $this->attributes;
39
-    }
30
+	/**
31
+	 * Method used by the mapper to get the
32
+	 * raw object's values.
33
+	 *
34
+	 * @return array
35
+	 */
36
+	public function getEntityAttributes()
37
+	{
38
+		return $this->attributes;
39
+	}
40 40
 
41
-    /**
42
-     * Method used by the mapper to set raw
43
-     * key-value pair
44
-     *
45
-     * @param string $key
46
-     * @param string $value
47
-     *
48
-     * @return void
49
-     */
50
-    public function setEntityAttribute($key, $value)
51
-    {
52
-        $this->attributes[$key] = $value;
53
-    }
41
+	/**
42
+	 * Method used by the mapper to set raw
43
+	 * key-value pair
44
+	 *
45
+	 * @param string $key
46
+	 * @param string $value
47
+	 *
48
+	 * @return void
49
+	 */
50
+	public function setEntityAttribute($key, $value)
51
+	{
52
+		$this->attributes[$key] = $value;
53
+	}
54 54
 
55
-    /**
56
-     * Method used by the mapper to get single
57
-     * key-value pair
58
-     *
59
-     * @param  string $key
60
-     * @return mixed|null
61
-     */
62
-    public function getEntityAttribute($key)
63
-    {
64
-        if (array_key_exists($key, $this->attributes)) {
65
-            return $this->attributes[$key];
66
-        } else {
67
-            return null;
68
-        }
69
-    }
55
+	/**
56
+	 * Method used by the mapper to get single
57
+	 * key-value pair
58
+	 *
59
+	 * @param  string $key
60
+	 * @return mixed|null
61
+	 */
62
+	public function getEntityAttribute($key)
63
+	{
64
+		if (array_key_exists($key, $this->attributes)) {
65
+			return $this->attributes[$key];
66
+		} else {
67
+			return null;
68
+		}
69
+	}
70 70
 }
Please login to merge, or discard this patch.
src/Entity.php 2 patches
Indentation   +97 added lines, -97 removed lines patch added patch discarded remove patch
@@ -6,112 +6,112 @@
 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
-        return ucfirst($key) . 'Attribute';
92
-    }
85
+	/**
86
+	 * @param $key
87
+	 * @return string
88
+	 */
89
+	protected function getMutatorMethod($key)
90
+	{
91
+		return ucfirst($key) . 'Attribute';
92
+	}
93 93
 
94
-    /**
95
-     * Convert every attributes to value / arrays
96
-     *
97
-     * @return array
98
-     */
99
-    public function toArray()
100
-    {
101
-        // First, call the trait method before filtering
102
-        // with Entity specific methods
103
-        $attributes = $this->attributesToArray($this->attributes);
94
+	/**
95
+	 * Convert every attributes to value / arrays
96
+	 *
97
+	 * @return array
98
+	 */
99
+	public function toArray()
100
+	{
101
+		// First, call the trait method before filtering
102
+		// with Entity specific methods
103
+		$attributes = $this->attributesToArray($this->attributes);
104 104
         
105
-        foreach ($this->attributes as $key => $attribute) {
106
-            if (in_array($key, $this->hidden)) {
107
-                unset($attributes[$key]);
108
-                continue;
109
-            }
110
-            if ($this->hasGetMutator($key)) {
111
-                $method = 'get' . $this->getMutatorMethod($key);
112
-                $attributes[$key] = $this->$method($attribute);
113
-            }
114
-        }
115
-        return $attributes;
116
-    }
105
+		foreach ($this->attributes as $key => $attribute) {
106
+			if (in_array($key, $this->hidden)) {
107
+				unset($attributes[$key]);
108
+				continue;
109
+			}
110
+			if ($this->hasGetMutator($key)) {
111
+				$method = 'get' . $this->getMutatorMethod($key);
112
+				$attributes[$key] = $this->$method($attribute);
113
+			}
114
+		}
115
+		return $attributes;
116
+	}
117 117
 }
Please login to merge, or discard this patch.
Spacing   +6 added lines, -6 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
     /**
@@ -88,7 +88,7 @@  discard block
 block discarded – undo
88 88
      */
89 89
     protected function getMutatorMethod($key)
90 90
     {
91
-        return ucfirst($key) . 'Attribute';
91
+        return ucfirst($key).'Attribute';
92 92
     }
93 93
 
94 94
     /**
@@ -108,7 +108,7 @@  discard block
 block discarded – undo
108 108
                 continue;
109 109
             }
110 110
             if ($this->hasGetMutator($key)) {
111
-                $method = 'get' . $this->getMutatorMethod($key);
111
+                $method = 'get'.$this->getMutatorMethod($key);
112 112
                 $attributes[$key] = $this->$method($attribute);
113 113
             }
114 114
         }
Please login to merge, or discard this patch.
src/Exceptions/EntityNotFoundException.php 1 patch
Indentation   +27 added lines, -27 removed lines patch added patch discarded remove patch
@@ -6,35 +6,35 @@
 block discarded – undo
6 6
 
7 7
 class EntityNotFoundException extends RuntimeException
8 8
 {
9
-    /**
10
-     * Name of the affected Entity Map.
11
-     *
12
-     * @var string
13
-     */
14
-    protected $entity;
9
+	/**
10
+	 * Name of the affected Entity Map.
11
+	 *
12
+	 * @var string
13
+	 */
14
+	protected $entity;
15 15
 
16
-    /**
17
-     * Set the affected Entity Map.
18
-     *
19
-     * @param  string   $entity
20
-     * @return $this
21
-     */
22
-    public function setEntity($entity)
23
-    {
24
-        $this->entity = $entity;
16
+	/**
17
+	 * Set the affected Entity Map.
18
+	 *
19
+	 * @param  string   $entity
20
+	 * @return $this
21
+	 */
22
+	public function setEntity($entity)
23
+	{
24
+		$this->entity = $entity;
25 25
 
26
-        $this->message = "No query results for entity [{$entity}].";
26
+		$this->message = "No query results for entity [{$entity}].";
27 27
 
28
-        return $this;
29
-    }
28
+		return $this;
29
+	}
30 30
 
31
-    /**
32
-     * Get the affected Entity.
33
-     *
34
-     * @return string
35
-     */
36
-    public function getEntity()
37
-    {
38
-        return $this->entity;
39
-    }
31
+	/**
32
+	 * Get the affected Entity.
33
+	 *
34
+	 * @return string
35
+	 */
36
+	public function getEntity()
37
+	{
38
+		return $this->entity;
39
+	}
40 40
 }
Please login to merge, or discard this patch.
src/EntityMap.php 2 patches
Indentation   +1147 added lines, -1147 removed lines patch added patch discarded remove patch
@@ -23,1165 +23,1165 @@
 block discarded – undo
23 23
  */
24 24
 class EntityMap
25 25
 {
26
-    /**
27
-     * The mapping driver to use with this entity
28
-     */
29
-    protected $driver = 'illuminate';
30
-
31
-    /**
32
-     * The Database Connection name for the model.
33
-     *
34
-     * @var string
35
-     */
36
-    protected $connection;
37
-
38
-    /**
39
-     * The table associated with the entity.
40
-     *
41
-     * @var string|null
42
-     */
43
-    protected $table = null;
44
-
45
-    /**
46
-     * The primary key for the model.
47
-     *
48
-     * @var string
49
-     */
50
-    protected $primaryKey = 'id';
51
-
52
-    /**
53
-     * Array containing a list of class attributes. Mandatory if the
54
-     * mapped entity is a Plain PHP Object.
55
-     *
56
-     * @var array
57
-     */
58
-    protected $attributes = [];
59
-
60
-    /**
61
-     * The Custom Domain Class to use with this mapping
62
-     *
63
-     * @var string|null
64
-     */
65
-    protected $class = null;
66
-
67
-    /**
68
-     * Attributes that should be treated as Value Objects
69
-     *
70
-     * @var array
71
-     */
72
-    protected $embeddables = [];
26
+	/**
27
+	 * The mapping driver to use with this entity
28
+	 */
29
+	protected $driver = 'illuminate';
30
+
31
+	/**
32
+	 * The Database Connection name for the model.
33
+	 *
34
+	 * @var string
35
+	 */
36
+	protected $connection;
37
+
38
+	/**
39
+	 * The table associated with the entity.
40
+	 *
41
+	 * @var string|null
42
+	 */
43
+	protected $table = null;
44
+
45
+	/**
46
+	 * The primary key for the model.
47
+	 *
48
+	 * @var string
49
+	 */
50
+	protected $primaryKey = 'id';
51
+
52
+	/**
53
+	 * Array containing a list of class attributes. Mandatory if the
54
+	 * mapped entity is a Plain PHP Object.
55
+	 *
56
+	 * @var array
57
+	 */
58
+	protected $attributes = [];
59
+
60
+	/**
61
+	 * The Custom Domain Class to use with this mapping
62
+	 *
63
+	 * @var string|null
64
+	 */
65
+	protected $class = null;
66
+
67
+	/**
68
+	 * Attributes that should be treated as Value Objects
69
+	 *
70
+	 * @var array
71
+	 */
72
+	protected $embeddables = [];
73 73
         
74
-    /**
75
-     * Determine the relationships method used on the entity.
76
-     * If not set, mapper will autodetect them
77
-     *
78
-     * @var array
79
-     */
80
-    private $relationships = [];
81
-
82
-    /**
83
-     * Relationships that should be treated as collection.
84
-     *
85
-     * @var array
86
-     */
87
-    private $manyRelations = [];
88
-
89
-    /**
90
-     * Relationships that should be treated as single entity.
91
-     *
92
-     * @var array
93
-     */
94
-    private $singleRelations = [];
95
-
96
-    /**
97
-     * Relationships for which the key is stored in the Entity itself
98
-     *
99
-     * @var array
100
-     */
101
-    private $localRelations = [];
102
-
103
-    /**
104
-     * Relationships for which the key is stored in the Related Entity
105
-     *
106
-     * @var array
107
-     */
108
-    private $foreignRelations = [];
109
-
110
-    /**
111
-     * Relationships which use a pivot record.
112
-     *
113
-     * @var array
114
-     */
115
-    private $pivotRelations = [];
116
-
117
-    /**
118
-     * Dynamic relationships
119
-     *
120
-     * @var array
121
-     */
122
-    private $dynamicRelationships = [];
123
-
124
-    /**
125
-     * The number of models to return for pagination.
126
-     *
127
-     * @var int
128
-     */
129
-    protected $perPage = 15;
130
-
131
-    /**
132
-     * The relations to eager load on every query.
133
-     *
134
-     * @var array
135
-     */
136
-    protected $with = [];
137
-
138
-    /**
139
-     * The class name to be used in polymorphic relations.
140
-     *
141
-     * @var string
142
-     */
143
-    protected $morphClass;
144
-
145
-    /**
146
-     * Sequence name, to be used with postgreSql
147
-     * defaults to %table_name%_id_seq
148
-     *
149
-     * @var string|null
150
-     */
151
-    protected $sequence = null;
152
-
153
-    /**
154
-     * Indicates if the entity should be timestamped.
155
-     *
156
-     * @var bool
157
-     */
158
-    public $timestamps = false;
159
-
160
-    /**
161
-     * The name of the "created at" column.
162
-     *
163
-     * @var string
164
-     */
165
-    protected $createdAtColumn = 'created_at';
166
-
167
-    /**
168
-     * The name of the "updated at" column.
169
-     *
170
-     * @var string
171
-     */
172
-    protected $updatedAtColumn = 'updated_at';
173
-
174
-    /**
175
-     * Indicates if the entity uses softdeletes
176
-     *
177
-     * @var boolean
178
-     */
179
-    public $softDeletes = false;
180
-
181
-    /**
182
-     * The name of the "deleted at" column.
183
-     *
184
-     * @var string
185
-     */
186
-    protected $deletedAtColumn = 'deleted_at';
187
-
188
-    /**
189
-     * The many to many relationship methods.
190
-     *
191
-     * @var array
192
-     */
193
-    protected static $manyMethods = ['belongsToMany', 'morphToMany', 'morphedByMany'];
194
-
195
-    /**
196
-     * The 'Many' relationships classes, which related Entity attribute should be
197
-     * an array/entityCollection
198
-     *
199
-     * @var array
200
-     */
201
-    protected static $manyClasses = ['BelongsToMany', 'HasMany', 'HasManyThrough', 'MorphMany', 'MorphToMany'];
202
-
203
-    /**
204
-     * The 'Single' relationships classes, which related Entity attribute should be
205
-     * another Entity.
206
-     *
207
-     * @var array
208
-     */
209
-    protected static $singleClasses = ['BelongsTo', 'HasOne', 'MorphOne', 'MorphTo'];
210
-
211
-    /**
212
-     * Relationships with a pivot record
213
-     *
214
-     * @var array
215
-     */
216
-    protected static $pivotClasses = ['BelongsToMany', 'MorphToMany'];
217
-
218
-    /**
219
-     * Relationships on which key is stored in the Entity itself
220
-     *
221
-     * @var array
222
-     */
223
-    protected static $localClasses = ['BelongsTo', 'MorphTo'];
224
-
225
-    /**
226
-     * Relationships on which key is stored in the related Entity record or in a pivot record
227
-     *
228
-     * @var array
229
-     */
230
-    protected static $foreignClasses = [
231
-        'BelongsToMany',
232
-        'HasMany',
233
-        'HasManyThrough',
234
-        'MorphMany',
235
-        'MorphToMany',
236
-        'HasOne',
237
-        'MorphOne',
238
-    ];
239
-
240
-    /**
241
-     * The date format to use with the current database connection
242
-     *
243
-     * @var string
244
-     */
245
-    protected $dateFormat;
246
-
247
-    /**
248
-     * The Analogue's manager instance.
249
-     *
250
-     * @var \Analogue\ORM\System\Manager
251
-     */
252
-    private $manager;
253
-
254
-    /**
255
-     * Set the Manager that will be used for relationship's mapper instantiations.
256
-     *
257
-     * @param Manager $manager
258
-     */
259
-    public function setManager(Manager $manager)
260
-    {
261
-        $this->manager = $manager;
262
-    }
263
-
264
-    /**
265
-     * Return Domain class attributes, useful when mapping to a Plain PHP Object
266
-     *
267
-     * @return array
268
-     */
269
-    public function getAttributes()
270
-    {
271
-        return $this->attributes;
272
-    }
273
-
274
-    /**
275
-     * Set the domain class attributes
276
-     *
277
-     * @param array $attributeNames
278
-     */
279
-    public function setAttributes(array $attributeNames)
280
-    {
281
-        $this->attributes = $attributeNames;
282
-    }
283
-
284
-    /**
285
-     * Get all the attribute names for the class, including relationships, embeddables and primary key.
286
-     *
287
-     * @return array
288
-     */
289
-    public function getCompiledAttributes()
290
-    {
291
-        $key = $this->getKeyName();
292
-
293
-        $embeddables = array_keys($this->getEmbeddables());
294
-
295
-        $relationships = $this->getRelationships();
296
-
297
-        $attributes = $this->getAttributes();
298
-
299
-        return array_merge([$key], $embeddables, $relationships, $attributes);
300
-    }
301
-
302
-    /**
303
-     * Set the date format to use with the current database connection
304
-     *
305
-     * @param string $format
306
-     */
307
-    public function setDateFormat($format)
308
-    {
309
-        $this->dateFormat = $format;
310
-    }
74
+	/**
75
+	 * Determine the relationships method used on the entity.
76
+	 * If not set, mapper will autodetect them
77
+	 *
78
+	 * @var array
79
+	 */
80
+	private $relationships = [];
81
+
82
+	/**
83
+	 * Relationships that should be treated as collection.
84
+	 *
85
+	 * @var array
86
+	 */
87
+	private $manyRelations = [];
88
+
89
+	/**
90
+	 * Relationships that should be treated as single entity.
91
+	 *
92
+	 * @var array
93
+	 */
94
+	private $singleRelations = [];
95
+
96
+	/**
97
+	 * Relationships for which the key is stored in the Entity itself
98
+	 *
99
+	 * @var array
100
+	 */
101
+	private $localRelations = [];
102
+
103
+	/**
104
+	 * Relationships for which the key is stored in the Related Entity
105
+	 *
106
+	 * @var array
107
+	 */
108
+	private $foreignRelations = [];
109
+
110
+	/**
111
+	 * Relationships which use a pivot record.
112
+	 *
113
+	 * @var array
114
+	 */
115
+	private $pivotRelations = [];
116
+
117
+	/**
118
+	 * Dynamic relationships
119
+	 *
120
+	 * @var array
121
+	 */
122
+	private $dynamicRelationships = [];
123
+
124
+	/**
125
+	 * The number of models to return for pagination.
126
+	 *
127
+	 * @var int
128
+	 */
129
+	protected $perPage = 15;
130
+
131
+	/**
132
+	 * The relations to eager load on every query.
133
+	 *
134
+	 * @var array
135
+	 */
136
+	protected $with = [];
137
+
138
+	/**
139
+	 * The class name to be used in polymorphic relations.
140
+	 *
141
+	 * @var string
142
+	 */
143
+	protected $morphClass;
144
+
145
+	/**
146
+	 * Sequence name, to be used with postgreSql
147
+	 * defaults to %table_name%_id_seq
148
+	 *
149
+	 * @var string|null
150
+	 */
151
+	protected $sequence = null;
152
+
153
+	/**
154
+	 * Indicates if the entity should be timestamped.
155
+	 *
156
+	 * @var bool
157
+	 */
158
+	public $timestamps = false;
159
+
160
+	/**
161
+	 * The name of the "created at" column.
162
+	 *
163
+	 * @var string
164
+	 */
165
+	protected $createdAtColumn = 'created_at';
166
+
167
+	/**
168
+	 * The name of the "updated at" column.
169
+	 *
170
+	 * @var string
171
+	 */
172
+	protected $updatedAtColumn = 'updated_at';
173
+
174
+	/**
175
+	 * Indicates if the entity uses softdeletes
176
+	 *
177
+	 * @var boolean
178
+	 */
179
+	public $softDeletes = false;
180
+
181
+	/**
182
+	 * The name of the "deleted at" column.
183
+	 *
184
+	 * @var string
185
+	 */
186
+	protected $deletedAtColumn = 'deleted_at';
187
+
188
+	/**
189
+	 * The many to many relationship methods.
190
+	 *
191
+	 * @var array
192
+	 */
193
+	protected static $manyMethods = ['belongsToMany', 'morphToMany', 'morphedByMany'];
194
+
195
+	/**
196
+	 * The 'Many' relationships classes, which related Entity attribute should be
197
+	 * an array/entityCollection
198
+	 *
199
+	 * @var array
200
+	 */
201
+	protected static $manyClasses = ['BelongsToMany', 'HasMany', 'HasManyThrough', 'MorphMany', 'MorphToMany'];
202
+
203
+	/**
204
+	 * The 'Single' relationships classes, which related Entity attribute should be
205
+	 * another Entity.
206
+	 *
207
+	 * @var array
208
+	 */
209
+	protected static $singleClasses = ['BelongsTo', 'HasOne', 'MorphOne', 'MorphTo'];
210
+
211
+	/**
212
+	 * Relationships with a pivot record
213
+	 *
214
+	 * @var array
215
+	 */
216
+	protected static $pivotClasses = ['BelongsToMany', 'MorphToMany'];
217
+
218
+	/**
219
+	 * Relationships on which key is stored in the Entity itself
220
+	 *
221
+	 * @var array
222
+	 */
223
+	protected static $localClasses = ['BelongsTo', 'MorphTo'];
224
+
225
+	/**
226
+	 * Relationships on which key is stored in the related Entity record or in a pivot record
227
+	 *
228
+	 * @var array
229
+	 */
230
+	protected static $foreignClasses = [
231
+		'BelongsToMany',
232
+		'HasMany',
233
+		'HasManyThrough',
234
+		'MorphMany',
235
+		'MorphToMany',
236
+		'HasOne',
237
+		'MorphOne',
238
+	];
239
+
240
+	/**
241
+	 * The date format to use with the current database connection
242
+	 *
243
+	 * @var string
244
+	 */
245
+	protected $dateFormat;
246
+
247
+	/**
248
+	 * The Analogue's manager instance.
249
+	 *
250
+	 * @var \Analogue\ORM\System\Manager
251
+	 */
252
+	private $manager;
253
+
254
+	/**
255
+	 * Set the Manager that will be used for relationship's mapper instantiations.
256
+	 *
257
+	 * @param Manager $manager
258
+	 */
259
+	public function setManager(Manager $manager)
260
+	{
261
+		$this->manager = $manager;
262
+	}
263
+
264
+	/**
265
+	 * Return Domain class attributes, useful when mapping to a Plain PHP Object
266
+	 *
267
+	 * @return array
268
+	 */
269
+	public function getAttributes()
270
+	{
271
+		return $this->attributes;
272
+	}
273
+
274
+	/**
275
+	 * Set the domain class attributes
276
+	 *
277
+	 * @param array $attributeNames
278
+	 */
279
+	public function setAttributes(array $attributeNames)
280
+	{
281
+		$this->attributes = $attributeNames;
282
+	}
283
+
284
+	/**
285
+	 * Get all the attribute names for the class, including relationships, embeddables and primary key.
286
+	 *
287
+	 * @return array
288
+	 */
289
+	public function getCompiledAttributes()
290
+	{
291
+		$key = $this->getKeyName();
292
+
293
+		$embeddables = array_keys($this->getEmbeddables());
294
+
295
+		$relationships = $this->getRelationships();
296
+
297
+		$attributes = $this->getAttributes();
298
+
299
+		return array_merge([$key], $embeddables, $relationships, $attributes);
300
+	}
301
+
302
+	/**
303
+	 * Set the date format to use with the current database connection
304
+	 *
305
+	 * @param string $format
306
+	 */
307
+	public function setDateFormat($format)
308
+	{
309
+		$this->dateFormat = $format;
310
+	}
311 311
     
312
-    /**
313
-     * Get the date format to use with the current database connection
314
-     *
315
-     *  @return string
316
-     */
317
-    public function getDateFormat()
318
-    {
319
-        return $this->dateFormat;
320
-    }
321
-
322
-    /**
323
-     * Set the Driver for this mapping
324
-     *
325
-     * @param string $driver
326
-     */
327
-    public function setDriver($driver)
328
-    {
329
-        $this->driver = $driver;
330
-    }
331
-
332
-    /**
333
-     * Get the Driver for this mapping.
334
-     *
335
-     * @return string
336
-     */
337
-    public function getDriver()
338
-    {
339
-        return $this->driver;
340
-    }
341
-
342
-    /**
343
-     * Set the db connection to use on the table
344
-     *
345
-     * @param $connection
346
-     */
347
-    public function setConnection($connection)
348
-    {
349
-        $this->connection = $connection;
350
-    }
351
-
352
-    /**
353
-     * Get the Database connection the Entity is stored on.
354
-     *
355
-     * @return string
356
-     */
357
-    public function getConnection()
358
-    {
359
-        return $this->connection;
360
-    }
361
-
362
-    /**
363
-     * Get the table associated with the entity.
364
-     *
365
-     * @return string
366
-     */
367
-    public function getTable()
368
-    {
369
-        if (!is_null($this->table)) {
370
-            return $this->table;
371
-        }
312
+	/**
313
+	 * Get the date format to use with the current database connection
314
+	 *
315
+	 *  @return string
316
+	 */
317
+	public function getDateFormat()
318
+	{
319
+		return $this->dateFormat;
320
+	}
321
+
322
+	/**
323
+	 * Set the Driver for this mapping
324
+	 *
325
+	 * @param string $driver
326
+	 */
327
+	public function setDriver($driver)
328
+	{
329
+		$this->driver = $driver;
330
+	}
331
+
332
+	/**
333
+	 * Get the Driver for this mapping.
334
+	 *
335
+	 * @return string
336
+	 */
337
+	public function getDriver()
338
+	{
339
+		return $this->driver;
340
+	}
341
+
342
+	/**
343
+	 * Set the db connection to use on the table
344
+	 *
345
+	 * @param $connection
346
+	 */
347
+	public function setConnection($connection)
348
+	{
349
+		$this->connection = $connection;
350
+	}
351
+
352
+	/**
353
+	 * Get the Database connection the Entity is stored on.
354
+	 *
355
+	 * @return string
356
+	 */
357
+	public function getConnection()
358
+	{
359
+		return $this->connection;
360
+	}
361
+
362
+	/**
363
+	 * Get the table associated with the entity.
364
+	 *
365
+	 * @return string
366
+	 */
367
+	public function getTable()
368
+	{
369
+		if (!is_null($this->table)) {
370
+			return $this->table;
371
+		}
372 372
         
373
-        return str_replace('\\', '', snake_case(str_plural(class_basename($this->getClass()))));
374
-    }
375
-
376
-    /**
377
-     * Set the database table name
378
-     *
379
-     * @param  string $table
380
-     */
381
-    public function setTable($table)
382
-    {
383
-        $this->table = $table;
384
-    }
385
-
386
-    /**
387
-     * Get the pgSql sequence name
388
-     *
389
-     * @return string
390
-     */
391
-    public function getSequence()
392
-    {
393
-        if (!is_null($this->sequence)) {
394
-            return $this->sequence;
395
-        } else {
396
-            return $this->getTable() . '_id_seq';
397
-        }
398
-    }
399
-
400
-    /**
401
-     * Get the custom entity class
402
-     *
403
-     * @return string namespaced class name
404
-     */
405
-    public function getClass()
406
-    {
407
-        return isset($this->class) ? $this->class : null;
408
-    }
409
-
410
-    /**
411
-     * Set the custom entity class
412
-     *
413
-     * @param string $class namespaced class name
414
-     */
415
-    public function setClass($class)
416
-    {
417
-        // Throw exception if class not exists
418
-
419
-        $this->class = $class;
420
-    }
421
-
422
-    /**
423
-     * Get the embedded Value Objects
424
-     *
425
-     * @return array
426
-     */
427
-    public function getEmbeddables()
428
-    {
429
-        return $this->embeddables;
430
-    }
431
-
432
-    /**
433
-     * Set the embedded Value Objects
434
-     *
435
-     * @param array $embeddables
436
-     */
437
-    public function setEmbeddables(array $embeddables)
438
-    {
439
-        $this->embeddables = $embeddables;
440
-    }
441
-
442
-    /**
443
-     * Get the relationships to map on a custom domain
444
-     * class.
445
-     *
446
-     * @return array
447
-     */
448
-    public function getRelationships()
449
-    {
450
-        return $this->relationships;
451
-    }
452
-
453
-    /**
454
-     * Relationships of the Entity type
455
-     *
456
-     * @return array
457
-     */
458
-    public function getSingleRelationships()
459
-    {
460
-        return $this->singleRelations;
461
-    }
462
-
463
-    /**
464
-     * Relationships of type Collection
465
-     *
466
-     * @return array
467
-     */
468
-    public function getManyRelationships()
469
-    {
470
-        return $this->manyRelations;
471
-    }
472
-
473
-    /**
474
-     * Relationships with foreign key in the mapped entity record.
475
-     *
476
-     * @return array
477
-     */
478
-    public function getLocalRelationships()
479
-    {
480
-        return $this->localRelations;
481
-    }
482
-
483
-    /**
484
-     * Relationships with foreign key in the related Entity record
485
-     *
486
-     * @return array
487
-     */
488
-    public function getForeignRelationships()
489
-    {
490
-        return $this->foreignRelations;
491
-    }
492
-
493
-    /**
494
-     * Relationships which keys are stored in a pivot record
495
-     *
496
-     * @return array
497
-     */
498
-    public function getPivotRelationships()
499
-    {
500
-        return $this->pivotRelations;
501
-    }
502
-
503
-    /**
504
-     * Add a Dynamic Relationship method at runtime. This has to be done
505
-     * by hooking the 'initializing' event, before entityMap is initialized.
506
-     *
507
-     * @param string  $name         Relation name
508
-     * @param \Closure $relationship
509
-     *
510
-     * @return void
511
-     */
512
-    public function addRelationshipMethod($name, \Closure $relationship)
513
-    {
514
-        $this->dynamicRelationships[$name] = $relationship;
515
-    }
516
-
517
-    /**
518
-     * Get the dynamic relationship method names.
519
-     *
520
-     * @return array
521
-     */
522
-    public function getDynamicRelationships()
523
-    {
524
-        return array_keys($this->dynamicRelationships);
525
-    }
526
-
527
-    /**
528
-     * Get the relationships that have to be eager loaded
529
-     * on each request.
530
-     *
531
-     * @return array
532
-     */
533
-    public function getEagerloadedRelationships()
534
-    {
535
-        return $this->with;
536
-    }
537
-
538
-    /**
539
-     * Get the primary key for the entity.
540
-     *
541
-     * @return string
542
-     */
543
-    public function getKeyName()
544
-    {
545
-        return $this->primaryKey;
546
-    }
373
+		return str_replace('\\', '', snake_case(str_plural(class_basename($this->getClass()))));
374
+	}
375
+
376
+	/**
377
+	 * Set the database table name
378
+	 *
379
+	 * @param  string $table
380
+	 */
381
+	public function setTable($table)
382
+	{
383
+		$this->table = $table;
384
+	}
385
+
386
+	/**
387
+	 * Get the pgSql sequence name
388
+	 *
389
+	 * @return string
390
+	 */
391
+	public function getSequence()
392
+	{
393
+		if (!is_null($this->sequence)) {
394
+			return $this->sequence;
395
+		} else {
396
+			return $this->getTable() . '_id_seq';
397
+		}
398
+	}
399
+
400
+	/**
401
+	 * Get the custom entity class
402
+	 *
403
+	 * @return string namespaced class name
404
+	 */
405
+	public function getClass()
406
+	{
407
+		return isset($this->class) ? $this->class : null;
408
+	}
409
+
410
+	/**
411
+	 * Set the custom entity class
412
+	 *
413
+	 * @param string $class namespaced class name
414
+	 */
415
+	public function setClass($class)
416
+	{
417
+		// Throw exception if class not exists
418
+
419
+		$this->class = $class;
420
+	}
421
+
422
+	/**
423
+	 * Get the embedded Value Objects
424
+	 *
425
+	 * @return array
426
+	 */
427
+	public function getEmbeddables()
428
+	{
429
+		return $this->embeddables;
430
+	}
431
+
432
+	/**
433
+	 * Set the embedded Value Objects
434
+	 *
435
+	 * @param array $embeddables
436
+	 */
437
+	public function setEmbeddables(array $embeddables)
438
+	{
439
+		$this->embeddables = $embeddables;
440
+	}
441
+
442
+	/**
443
+	 * Get the relationships to map on a custom domain
444
+	 * class.
445
+	 *
446
+	 * @return array
447
+	 */
448
+	public function getRelationships()
449
+	{
450
+		return $this->relationships;
451
+	}
452
+
453
+	/**
454
+	 * Relationships of the Entity type
455
+	 *
456
+	 * @return array
457
+	 */
458
+	public function getSingleRelationships()
459
+	{
460
+		return $this->singleRelations;
461
+	}
462
+
463
+	/**
464
+	 * Relationships of type Collection
465
+	 *
466
+	 * @return array
467
+	 */
468
+	public function getManyRelationships()
469
+	{
470
+		return $this->manyRelations;
471
+	}
472
+
473
+	/**
474
+	 * Relationships with foreign key in the mapped entity record.
475
+	 *
476
+	 * @return array
477
+	 */
478
+	public function getLocalRelationships()
479
+	{
480
+		return $this->localRelations;
481
+	}
482
+
483
+	/**
484
+	 * Relationships with foreign key in the related Entity record
485
+	 *
486
+	 * @return array
487
+	 */
488
+	public function getForeignRelationships()
489
+	{
490
+		return $this->foreignRelations;
491
+	}
492
+
493
+	/**
494
+	 * Relationships which keys are stored in a pivot record
495
+	 *
496
+	 * @return array
497
+	 */
498
+	public function getPivotRelationships()
499
+	{
500
+		return $this->pivotRelations;
501
+	}
502
+
503
+	/**
504
+	 * Add a Dynamic Relationship method at runtime. This has to be done
505
+	 * by hooking the 'initializing' event, before entityMap is initialized.
506
+	 *
507
+	 * @param string  $name         Relation name
508
+	 * @param \Closure $relationship
509
+	 *
510
+	 * @return void
511
+	 */
512
+	public function addRelationshipMethod($name, \Closure $relationship)
513
+	{
514
+		$this->dynamicRelationships[$name] = $relationship;
515
+	}
516
+
517
+	/**
518
+	 * Get the dynamic relationship method names.
519
+	 *
520
+	 * @return array
521
+	 */
522
+	public function getDynamicRelationships()
523
+	{
524
+		return array_keys($this->dynamicRelationships);
525
+	}
526
+
527
+	/**
528
+	 * Get the relationships that have to be eager loaded
529
+	 * on each request.
530
+	 *
531
+	 * @return array
532
+	 */
533
+	public function getEagerloadedRelationships()
534
+	{
535
+		return $this->with;
536
+	}
537
+
538
+	/**
539
+	 * Get the primary key for the entity.
540
+	 *
541
+	 * @return string
542
+	 */
543
+	public function getKeyName()
544
+	{
545
+		return $this->primaryKey;
546
+	}
547 547
     
548
-    /**
549
-     * Set the primary key for the entity.
550
-     *
551
-     * @param $key
552
-     * @return void
553
-     */
554
-    public function setKeyName($key)
555
-    {
556
-        $this->primaryKey = $key;
557
-    }
548
+	/**
549
+	 * Set the primary key for the entity.
550
+	 *
551
+	 * @param $key
552
+	 * @return void
553
+	 */
554
+	public function setKeyName($key)
555
+	{
556
+		$this->primaryKey = $key;
557
+	}
558 558
     
559
-    /**
560
-     * Get the table qualified key name.
561
-     *
562
-     * @return string
563
-     */
564
-    public function getQualifiedKeyName()
565
-    {
566
-        return $this->getTable() . '.' . $this->getKeyName();
567
-    }
568
-
569
-    /**
570
-     * Get the number of models to return per page.
571
-     *
572
-     * @return int
573
-     */
574
-    public function getPerPage()
575
-    {
576
-        return $this->perPage;
577
-    }
578
-
579
-    /**
580
-     * Set the number of models to return per page.
581
-     *
582
-     * @param  int $perPage
583
-     * @return void
584
-     */
585
-    public function setPerPage($perPage)
586
-    {
587
-        $this->perPage = $perPage;
588
-    }
589
-
590
-    /**
591
-     * Determine if the entity uses get.
592
-     *
593
-     * @return bool
594
-     */
595
-    public function usesTimestamps()
596
-    {
597
-        return $this->timestamps;
598
-    }
599
-
600
-    /**
601
-     * Determine if the entity uses soft deletes
602
-     *
603
-     * @return bool
604
-     */
605
-    public function usesSoftDeletes()
606
-    {
607
-        return $this->softDeletes;
608
-    }
609
-
610
-    /**
611
-     * Get the 'created_at' column name
612
-     *
613
-     * @return string
614
-     */
615
-    public function getCreatedAtColumn()
616
-    {
617
-        return $this->createdAtColumn;
618
-    }
619
-
620
-    /**
621
-     * Get the 'updated_at' column name
622
-     *
623
-     * @return string
624
-     */
625
-    public function getUpdatedAtColumn()
626
-    {
627
-        return $this->updatedAtColumn;
628
-    }
629
-
630
-    /**
631
-     * Get the deleted_at column
632
-     *
633
-     * @return string
634
-     */
635
-    public function getQualifiedDeletedAtColumn()
636
-    {
637
-        return $this->deletedAtColumn;
638
-    }
639
-
640
-    /**
641
-     * Get the default foreign key name for the model.
642
-     *
643
-     * @return string
644
-     */
645
-    public function getForeignKey()
646
-    {
647
-        return snake_case(class_basename($this->getClass())) . '_id';
648
-    }
649
-
650
-    /**
651
-     * Define a one-to-one relationship.
652
-     *
653
-     * @param         $entity
654
-     * @param  string $relatedClass entity class
655
-     * @param  string $foreignKey
656
-     * @param  string $localKey
657
-     * @throws MappingException
658
-     * @return \Analogue\ORM\Relationships\HasOne
659
-     */
660
-    public function hasOne($entity, $relatedClass, $foreignKey = null, $localKey = null)
661
-    {
662
-        $foreignKey = $foreignKey ?: $this->getForeignKey();
663
-
664
-        $relatedMapper = $this->manager->mapper($relatedClass);
665
-
666
-        $relatedMap = $relatedMapper->getEntityMap();
667
-
668
-        $localKey = $localKey ?: $this->getKeyName();
669
-
670
-        return new HasOne($relatedMapper, $entity, $relatedMap->getTable() . '.' . $foreignKey, $localKey);
671
-    }
672
-
673
-    /**
674
-     * Define a polymorphic one-to-one relationship.
675
-     *
676
-     * @param  mixed       $entity
677
-     * @param  string      $related
678
-     * @param  string      $name
679
-     * @param  string|null $type
680
-     * @param  string|null $id
681
-     * @param  string|null $localKey
682
-     * @throws MappingException
683
-     * @return \Analogue\ORM\Relationships\MorphOne
684
-     */
685
-    public function morphOne($entity, $related, $name, $type = null, $id = null, $localKey = null)
686
-    {
687
-        list($type, $id) = $this->getMorphs($name, $type, $id);
688
-
689
-        $localKey = $localKey ?: $this->getKeyName();
690
-
691
-        $relatedMapper = $this->manager->mapper($related);
692
-
693
-        $table = $relatedMapper->getEntityMap()->getTable();
559
+	/**
560
+	 * Get the table qualified key name.
561
+	 *
562
+	 * @return string
563
+	 */
564
+	public function getQualifiedKeyName()
565
+	{
566
+		return $this->getTable() . '.' . $this->getKeyName();
567
+	}
568
+
569
+	/**
570
+	 * Get the number of models to return per page.
571
+	 *
572
+	 * @return int
573
+	 */
574
+	public function getPerPage()
575
+	{
576
+		return $this->perPage;
577
+	}
578
+
579
+	/**
580
+	 * Set the number of models to return per page.
581
+	 *
582
+	 * @param  int $perPage
583
+	 * @return void
584
+	 */
585
+	public function setPerPage($perPage)
586
+	{
587
+		$this->perPage = $perPage;
588
+	}
589
+
590
+	/**
591
+	 * Determine if the entity uses get.
592
+	 *
593
+	 * @return bool
594
+	 */
595
+	public function usesTimestamps()
596
+	{
597
+		return $this->timestamps;
598
+	}
599
+
600
+	/**
601
+	 * Determine if the entity uses soft deletes
602
+	 *
603
+	 * @return bool
604
+	 */
605
+	public function usesSoftDeletes()
606
+	{
607
+		return $this->softDeletes;
608
+	}
609
+
610
+	/**
611
+	 * Get the 'created_at' column name
612
+	 *
613
+	 * @return string
614
+	 */
615
+	public function getCreatedAtColumn()
616
+	{
617
+		return $this->createdAtColumn;
618
+	}
619
+
620
+	/**
621
+	 * Get the 'updated_at' column name
622
+	 *
623
+	 * @return string
624
+	 */
625
+	public function getUpdatedAtColumn()
626
+	{
627
+		return $this->updatedAtColumn;
628
+	}
629
+
630
+	/**
631
+	 * Get the deleted_at column
632
+	 *
633
+	 * @return string
634
+	 */
635
+	public function getQualifiedDeletedAtColumn()
636
+	{
637
+		return $this->deletedAtColumn;
638
+	}
639
+
640
+	/**
641
+	 * Get the default foreign key name for the model.
642
+	 *
643
+	 * @return string
644
+	 */
645
+	public function getForeignKey()
646
+	{
647
+		return snake_case(class_basename($this->getClass())) . '_id';
648
+	}
649
+
650
+	/**
651
+	 * Define a one-to-one relationship.
652
+	 *
653
+	 * @param         $entity
654
+	 * @param  string $relatedClass entity class
655
+	 * @param  string $foreignKey
656
+	 * @param  string $localKey
657
+	 * @throws MappingException
658
+	 * @return \Analogue\ORM\Relationships\HasOne
659
+	 */
660
+	public function hasOne($entity, $relatedClass, $foreignKey = null, $localKey = null)
661
+	{
662
+		$foreignKey = $foreignKey ?: $this->getForeignKey();
663
+
664
+		$relatedMapper = $this->manager->mapper($relatedClass);
665
+
666
+		$relatedMap = $relatedMapper->getEntityMap();
667
+
668
+		$localKey = $localKey ?: $this->getKeyName();
669
+
670
+		return new HasOne($relatedMapper, $entity, $relatedMap->getTable() . '.' . $foreignKey, $localKey);
671
+	}
672
+
673
+	/**
674
+	 * Define a polymorphic one-to-one relationship.
675
+	 *
676
+	 * @param  mixed       $entity
677
+	 * @param  string      $related
678
+	 * @param  string      $name
679
+	 * @param  string|null $type
680
+	 * @param  string|null $id
681
+	 * @param  string|null $localKey
682
+	 * @throws MappingException
683
+	 * @return \Analogue\ORM\Relationships\MorphOne
684
+	 */
685
+	public function morphOne($entity, $related, $name, $type = null, $id = null, $localKey = null)
686
+	{
687
+		list($type, $id) = $this->getMorphs($name, $type, $id);
688
+
689
+		$localKey = $localKey ?: $this->getKeyName();
690
+
691
+		$relatedMapper = $this->manager->mapper($related);
692
+
693
+		$table = $relatedMapper->getEntityMap()->getTable();
694 694
         
695
-        return new MorphOne($relatedMapper, $entity, $table . '.' . $type, $table . '.' . $id, $localKey);
696
-    }
697
-
698
-    /**
699
-     * Define an inverse one-to-one or many relationship.
700
-     *
701
-     * @param  mixed       $entity
702
-     * @param  string      $related
703
-     * @param  string|null $foreignKey
704
-     * @param  string|null $otherKey
705
-     * @param  string|null $relation
706
-     * @throws MappingException
707
-     * @return \Analogue\ORM\Relationships\BelongsTo
708
-     */
709
-    public function belongsTo($entity, $related, $foreignKey = null, $otherKey = null, $relation = null)
710
-    {
711
-        // If no relation name was given, we will use this debug backtrace to extract
712
-        // the calling method's name and use that as the relationship name as most
713
-        // of the time this will be what we desire to use for the relationships.
714
-        if (is_null($relation)) {
715
-            list(, $caller) = debug_backtrace(false);
716
-
717
-            $relation = $caller['function'];
718
-        }
719
-
720
-        // If no foreign key was supplied, we can use a backtrace to guess the proper
721
-        // foreign key name by using the name of the relationship function, which
722
-        // when combined with an "_id" should conventionally match the columns.
723
-        if (is_null($foreignKey)) {
724
-            $foreignKey = snake_case($relation) . '_id';
725
-        }
726
-
727
-        $relatedMapper = $this->manager->mapper($related);
728
-
729
-        $otherKey = $otherKey ?: $relatedMapper->getEntityMap()->getKeyName();
730
-
731
-        return new BelongsTo($relatedMapper, $entity, $foreignKey, $otherKey, $relation);
732
-    }
733
-
734
-    /**
735
-     * Define a polymorphic, inverse one-to-one or many relationship.
736
-     *
737
-     * @param  mixed       $entity
738
-     * @param  string|null $name
739
-     * @param  string|null $type
740
-     * @param  string|null $id
741
-     * @throws MappingException
742
-     * @return \Analogue\ORM\Relationships\MorphTo
743
-     */
744
-    public function morphTo($entity, $name = null, $type = null, $id = null)
745
-    {
746
-        // If no name is provided, we will use the backtrace to get the function name
747
-        // since that is most likely the name of the polymorphic interface. We can
748
-        // use that to get both the class and foreign key that will be utilized.
749
-        if (is_null($name)) {
750
-            list(, $caller) = debug_backtrace(false);
751
-
752
-            $name = snake_case($caller['function']);
753
-        }
754
-
755
-        list($type, $id) = $this->getMorphs($name, $type, $id);
695
+		return new MorphOne($relatedMapper, $entity, $table . '.' . $type, $table . '.' . $id, $localKey);
696
+	}
697
+
698
+	/**
699
+	 * Define an inverse one-to-one or many relationship.
700
+	 *
701
+	 * @param  mixed       $entity
702
+	 * @param  string      $related
703
+	 * @param  string|null $foreignKey
704
+	 * @param  string|null $otherKey
705
+	 * @param  string|null $relation
706
+	 * @throws MappingException
707
+	 * @return \Analogue\ORM\Relationships\BelongsTo
708
+	 */
709
+	public function belongsTo($entity, $related, $foreignKey = null, $otherKey = null, $relation = null)
710
+	{
711
+		// If no relation name was given, we will use this debug backtrace to extract
712
+		// the calling method's name and use that as the relationship name as most
713
+		// of the time this will be what we desire to use for the relationships.
714
+		if (is_null($relation)) {
715
+			list(, $caller) = debug_backtrace(false);
716
+
717
+			$relation = $caller['function'];
718
+		}
719
+
720
+		// If no foreign key was supplied, we can use a backtrace to guess the proper
721
+		// foreign key name by using the name of the relationship function, which
722
+		// when combined with an "_id" should conventionally match the columns.
723
+		if (is_null($foreignKey)) {
724
+			$foreignKey = snake_case($relation) . '_id';
725
+		}
726
+
727
+		$relatedMapper = $this->manager->mapper($related);
728
+
729
+		$otherKey = $otherKey ?: $relatedMapper->getEntityMap()->getKeyName();
730
+
731
+		return new BelongsTo($relatedMapper, $entity, $foreignKey, $otherKey, $relation);
732
+	}
733
+
734
+	/**
735
+	 * Define a polymorphic, inverse one-to-one or many relationship.
736
+	 *
737
+	 * @param  mixed       $entity
738
+	 * @param  string|null $name
739
+	 * @param  string|null $type
740
+	 * @param  string|null $id
741
+	 * @throws MappingException
742
+	 * @return \Analogue\ORM\Relationships\MorphTo
743
+	 */
744
+	public function morphTo($entity, $name = null, $type = null, $id = null)
745
+	{
746
+		// If no name is provided, we will use the backtrace to get the function name
747
+		// since that is most likely the name of the polymorphic interface. We can
748
+		// use that to get both the class and foreign key that will be utilized.
749
+		if (is_null($name)) {
750
+			list(, $caller) = debug_backtrace(false);
751
+
752
+			$name = snake_case($caller['function']);
753
+		}
754
+
755
+		list($type, $id) = $this->getMorphs($name, $type, $id);
756 756
         
757
-        $mapper = $this->manager->mapper(get_class($entity));
757
+		$mapper = $this->manager->mapper(get_class($entity));
758 758
 
759
-        // If the type value is null it is probably safe to assume we're eager loading
760
-        // the relationship. When that is the case we will pass in a dummy query as
761
-        // there are multiple types in the morph and we can't use single queries.
762
-        $factory = new Factory;
763
-        $wrapper = $factory->make($entity);
759
+		// If the type value is null it is probably safe to assume we're eager loading
760
+		// the relationship. When that is the case we will pass in a dummy query as
761
+		// there are multiple types in the morph and we can't use single queries.
762
+		$factory = new Factory;
763
+		$wrapper = $factory->make($entity);
764 764
             
765
-        if (is_null($class = $wrapper->getEntityAttribute($type))) {
766
-            return new MorphTo(
767
-                $mapper, $entity, $id, null, $type, $name
768
-            );
769
-        }
770
-
771
-        // If we are not eager loading the relationship we will essentially treat this
772
-        // as a belongs-to style relationship since morph-to extends that class and
773
-        // we will pass in the appropriate values so that it behaves as expected.
774
-        else {
775
-            $relatedMapper = $this->manager->mapper($class);
776
-
777
-            $foreignKey = $relatedMapper->getEntityMap()->getKeyName();
765
+		if (is_null($class = $wrapper->getEntityAttribute($type))) {
766
+			return new MorphTo(
767
+				$mapper, $entity, $id, null, $type, $name
768
+			);
769
+		}
770
+
771
+		// If we are not eager loading the relationship we will essentially treat this
772
+		// as a belongs-to style relationship since morph-to extends that class and
773
+		// we will pass in the appropriate values so that it behaves as expected.
774
+		else {
775
+			$relatedMapper = $this->manager->mapper($class);
776
+
777
+			$foreignKey = $relatedMapper->getEntityMap()->getKeyName();
778 778
             
779
-            return new MorphTo(
780
-                $mapper, $entity, $id, $foreignKey, $type, $name
781
-            );
782
-        }
783
-    }
784
-
785
-    /**
786
-     * Define a one-to-many relationship.
787
-     *
788
-     * @param  mixed       $entity
789
-     * @param  string      $related
790
-     * @param  string|null $foreignKey
791
-     * @param  string|null $localKey
792
-     * @throws MappingException
793
-     * @return \Analogue\ORM\Relationships\HasMany
794
-     */
795
-    public function hasMany($entity, $related, $foreignKey = null, $localKey = null)
796
-    {
797
-        $foreignKey = $foreignKey ?: $this->getForeignKey();
798
-
799
-        $relatedMapper = $this->manager->mapper($related);
800
-
801
-        $table = $relatedMapper->getEntityMap()->getTable() . '.' . $foreignKey;
802
-
803
-        $localKey = $localKey ?: $this->getKeyName();
804
-
805
-        return new HasMany($relatedMapper, $entity, $table, $localKey);
806
-    }
807
-
808
-    /**
809
-     * Define a has-many-through relationship.
810
-     *
811
-     * @param  mixed       $entity
812
-     * @param  string      $related
813
-     * @param  string      $through
814
-     * @param  string|null $firstKey
815
-     * @param  string|null $secondKey
816
-     * @throws MappingException
817
-     * @return \Analogue\ORM\Relationships\HasManyThrough
818
-     */
819
-    public function hasManyThrough($entity, $related, $through, $firstKey = null, $secondKey = null)
820
-    {
821
-        $relatedMapper = $this->manager->mapper($related);
822
-
823
-        $throughMapper = $this->manager->mapper($through);
824
-
825
-
826
-        $firstKey = $firstKey ?: $this->getForeignKey();
827
-
828
-        $throughMap = $throughMapper->getEntityMap();
829
-
830
-        $secondKey = $secondKey ?: $throughMap->getForeignKey();
831
-
832
-        return new HasManyThrough($relatedMapper, $entity, $throughMap, $firstKey, $secondKey);
833
-    }
834
-
835
-    /**
836
-     * Define a polymorphic one-to-many relationship.
837
-     *
838
-     * @param  mixed       $entity
839
-     * @param  string      $related
840
-     * @param  string      $name
841
-     * @param  string|null $type
842
-     * @param  string|null $id
843
-     * @param  string|null $localKey
844
-     * @return \Analogue\ORM\Relationships\MorphMany
845
-     */
846
-    public function morphMany($entity, $related, $name, $type = null, $id = null, $localKey = null)
847
-    {
848
-        // Here we will gather up the morph type and ID for the relationship so that we
849
-        // can properly query the intermediate table of a relation. Finally, we will
850
-        // get the table and create the relationship instances for the developers.
851
-        list($type, $id) = $this->getMorphs($name, $type, $id);
852
-
853
-        $relatedMapper = $this->manager->mapper($related);
854
-
855
-        $table = $relatedMapper->getEntityMap()->getTable();
856
-
857
-        $localKey = $localKey ?: $this->getKeyName();
779
+			return new MorphTo(
780
+				$mapper, $entity, $id, $foreignKey, $type, $name
781
+			);
782
+		}
783
+	}
784
+
785
+	/**
786
+	 * Define a one-to-many relationship.
787
+	 *
788
+	 * @param  mixed       $entity
789
+	 * @param  string      $related
790
+	 * @param  string|null $foreignKey
791
+	 * @param  string|null $localKey
792
+	 * @throws MappingException
793
+	 * @return \Analogue\ORM\Relationships\HasMany
794
+	 */
795
+	public function hasMany($entity, $related, $foreignKey = null, $localKey = null)
796
+	{
797
+		$foreignKey = $foreignKey ?: $this->getForeignKey();
798
+
799
+		$relatedMapper = $this->manager->mapper($related);
800
+
801
+		$table = $relatedMapper->getEntityMap()->getTable() . '.' . $foreignKey;
802
+
803
+		$localKey = $localKey ?: $this->getKeyName();
804
+
805
+		return new HasMany($relatedMapper, $entity, $table, $localKey);
806
+	}
807
+
808
+	/**
809
+	 * Define a has-many-through relationship.
810
+	 *
811
+	 * @param  mixed       $entity
812
+	 * @param  string      $related
813
+	 * @param  string      $through
814
+	 * @param  string|null $firstKey
815
+	 * @param  string|null $secondKey
816
+	 * @throws MappingException
817
+	 * @return \Analogue\ORM\Relationships\HasManyThrough
818
+	 */
819
+	public function hasManyThrough($entity, $related, $through, $firstKey = null, $secondKey = null)
820
+	{
821
+		$relatedMapper = $this->manager->mapper($related);
822
+
823
+		$throughMapper = $this->manager->mapper($through);
824
+
825
+
826
+		$firstKey = $firstKey ?: $this->getForeignKey();
827
+
828
+		$throughMap = $throughMapper->getEntityMap();
829
+
830
+		$secondKey = $secondKey ?: $throughMap->getForeignKey();
831
+
832
+		return new HasManyThrough($relatedMapper, $entity, $throughMap, $firstKey, $secondKey);
833
+	}
834
+
835
+	/**
836
+	 * Define a polymorphic one-to-many relationship.
837
+	 *
838
+	 * @param  mixed       $entity
839
+	 * @param  string      $related
840
+	 * @param  string      $name
841
+	 * @param  string|null $type
842
+	 * @param  string|null $id
843
+	 * @param  string|null $localKey
844
+	 * @return \Analogue\ORM\Relationships\MorphMany
845
+	 */
846
+	public function morphMany($entity, $related, $name, $type = null, $id = null, $localKey = null)
847
+	{
848
+		// Here we will gather up the morph type and ID for the relationship so that we
849
+		// can properly query the intermediate table of a relation. Finally, we will
850
+		// get the table and create the relationship instances for the developers.
851
+		list($type, $id) = $this->getMorphs($name, $type, $id);
852
+
853
+		$relatedMapper = $this->manager->mapper($related);
854
+
855
+		$table = $relatedMapper->getEntityMap()->getTable();
856
+
857
+		$localKey = $localKey ?: $this->getKeyName();
858 858
         
859
-        return new MorphMany($relatedMapper, $entity, $table . '.' . $type, $table . '.' . $id, $localKey);
860
-    }
861
-
862
-    /**
863
-     * Define a many-to-many relationship.
864
-     *
865
-     * @param  mixed       $entity
866
-     * @param  string      $related
867
-     * @param  string|null $table
868
-     * @param  string|null $foreignKey
869
-     * @param  string|null $otherKey
870
-     * @param  string|null $relation
871
-     * @throws MappingException
872
-     * @return \Analogue\ORM\Relationships\BelongsToMany
873
-     */
874
-    public function belongsToMany($entity, $related, $table = null, $foreignKey = null, $otherKey = null, $relation = null)
875
-    {
876
-        // If no relationship name was passed, we will pull backtraces to get the
877
-        // name of the calling function. We will use that function name as the
878
-        // title of this relation since that is a great convention to apply.
879
-        if (is_null($relation)) {
880
-            $relation = $this->getBelongsToManyCaller();
881
-        }
882
-
883
-        // First, we'll need to determine the foreign key and "other key" for the
884
-        // relationship. Once we have determined the keys we'll make the query
885
-        // instances as well as the relationship instances we need for this.
886
-        $foreignKey = $foreignKey ?: $this->getForeignKey();
887
-
888
-        $relatedMapper = $this->manager->mapper($related);
889
-
890
-        $relatedMap = $relatedMapper->getEntityMap();
891
-
892
-        $otherKey = $otherKey ?: $relatedMap->getForeignKey();
893
-
894
-        // If no table name was provided, we can guess it by concatenating the two
895
-        // models using underscores in alphabetical order. The two model names
896
-        // are transformed to snake case from their default CamelCase also.
897
-        if (is_null($table)) {
898
-            $table = $this->joiningTable($relatedMap);
899
-        }
900
-
901
-        return new BelongsToMany($relatedMapper, $entity, $table, $foreignKey, $otherKey, $relation);
902
-    }
903
-
904
-    /**
905
-     * Define a polymorphic many-to-many relationship.
906
-     *
907
-     * @param  mixed       $entity
908
-     * @param  string      $related
909
-     * @param  string      $name
910
-     * @param  string|null $table
911
-     * @param  string|null $foreignKey
912
-     * @param  string|null $otherKey
913
-     * @param  bool        $inverse
914
-     * @throws MappingException
915
-     * @return \Analogue\ORM\Relationships\MorphToMany
916
-     */
917
-    public function morphToMany($entity, $related, $name, $table = null, $foreignKey = null, $otherKey = null, $inverse = false)
918
-    {
919
-        $caller = $this->getBelongsToManyCaller();
920
-
921
-        // First, we will need to determine the foreign key and "other key" for the
922
-        // relationship. Once we have determined the keys we will make the query
923
-        // instances, as well as the relationship instances we need for these.
924
-        $foreignKey = $foreignKey ?: $name . '_id';
925
-
926
-        $relatedMapper = $this->manager->mapper($related);
927
-
928
-        $otherKey = $otherKey ?: $relatedMapper->getEntityMap()->getForeignKey();
929
-
930
-        $table = $table ?: str_plural($name);
931
-
932
-        return new MorphToMany($relatedMapper, $entity, $name, $table, $foreignKey, $otherKey, $caller, $inverse);
933
-    }
934
-
935
-    /**
936
-     * Define a polymorphic, inverse many-to-many relationship.
937
-     *
938
-     * @param  mixed       $entity
939
-     * @param  string      $related
940
-     * @param  string      $name
941
-     * @param  string|null $table
942
-     * @param  string|null $foreignKey
943
-     * @param  string|null $otherKey
944
-     * @throws MappingException
945
-     * @return \Analogue\ORM\Relationships\MorphToMany
946
-     */
947
-    public function morphedByMany($entity, $related, $name, $table = null, $foreignKey = null, $otherKey = null)
948
-    {
949
-        $foreignKey = $foreignKey ?: $this->getForeignKey();
950
-
951
-        // For the inverse of the polymorphic many-to-many relations, we will change
952
-        // the way we determine the foreign and other keys, as it is the opposite
953
-        // of the morph-to-many method since we're figuring out these inverses.
954
-        $otherKey = $otherKey ?: $name . '_id';
955
-
956
-        return $this->morphToMany($entity, $related, $name, $table, $foreignKey, $otherKey, true);
957
-    }
958
-
959
-    /**
960
-     * Get the relationship name of the belongs to many.
961
-     *
962
-     * @return string
963
-     */
964
-    protected function getBelongsToManyCaller()
965
-    {
966
-        $self = __FUNCTION__;
967
-
968
-        $caller = array_first(debug_backtrace(false), function ($key, $trace) use ($self) {
969
-            $caller = $trace['function'];
970
-
971
-            return (!in_array($caller, EntityMap::$manyMethods) && $caller != $self);
972
-        });
973
-
974
-        return !is_null($caller) ? $caller['function'] : null;
975
-    }
976
-
977
-    /**
978
-     * Get the joining table name for a many-to-many relation.
979
-     *
980
-     * @param  EntityMap $relatedMap
981
-     * @return string
982
-     */
983
-    public function joiningTable($relatedMap)
984
-    {
985
-        // The joining table name, by convention, is simply the snake cased models
986
-        // sorted alphabetically and concatenated with an underscore, so we can
987
-        // just sort the models and join them together to get the table name.
988
-        $base = $this->getTable();
989
-
990
-        $related = $relatedMap->getTable();
991
-
992
-        $tables = [$related, $base];
993
-
994
-        // Now that we have the model names in an array we can just sort them and
995
-        // use the implode function to join them together with an underscores,
996
-        // which is typically used by convention within the database system.
997
-        sort($tables);
998
-
999
-        return strtolower(implode('_', $tables));
1000
-    }
1001
-
1002
-    /**
1003
-     * Get the polymorphic relationship columns.
1004
-     *
1005
-     * @param  string $name
1006
-     * @param  string $type
1007
-     * @param  string $id
1008
-     * @return string[]
1009
-     */
1010
-    protected function getMorphs($name, $type, $id)
1011
-    {
1012
-        $type = $type ?: $name . '_type';
1013
-
1014
-        $id = $id ?: $name . '_id';
1015
-
1016
-        return [$type, $id];
1017
-    }
1018
-
1019
-    /**
1020
-     * Get the class name for polymorphic relations.
1021
-     *
1022
-     * @return string
1023
-     */
1024
-    public function getMorphClass()
1025
-    {
1026
-        return $this->morphClass ?: $this->getClass();
1027
-    }
859
+		return new MorphMany($relatedMapper, $entity, $table . '.' . $type, $table . '.' . $id, $localKey);
860
+	}
861
+
862
+	/**
863
+	 * Define a many-to-many relationship.
864
+	 *
865
+	 * @param  mixed       $entity
866
+	 * @param  string      $related
867
+	 * @param  string|null $table
868
+	 * @param  string|null $foreignKey
869
+	 * @param  string|null $otherKey
870
+	 * @param  string|null $relation
871
+	 * @throws MappingException
872
+	 * @return \Analogue\ORM\Relationships\BelongsToMany
873
+	 */
874
+	public function belongsToMany($entity, $related, $table = null, $foreignKey = null, $otherKey = null, $relation = null)
875
+	{
876
+		// If no relationship name was passed, we will pull backtraces to get the
877
+		// name of the calling function. We will use that function name as the
878
+		// title of this relation since that is a great convention to apply.
879
+		if (is_null($relation)) {
880
+			$relation = $this->getBelongsToManyCaller();
881
+		}
882
+
883
+		// First, we'll need to determine the foreign key and "other key" for the
884
+		// relationship. Once we have determined the keys we'll make the query
885
+		// instances as well as the relationship instances we need for this.
886
+		$foreignKey = $foreignKey ?: $this->getForeignKey();
887
+
888
+		$relatedMapper = $this->manager->mapper($related);
889
+
890
+		$relatedMap = $relatedMapper->getEntityMap();
891
+
892
+		$otherKey = $otherKey ?: $relatedMap->getForeignKey();
893
+
894
+		// If no table name was provided, we can guess it by concatenating the two
895
+		// models using underscores in alphabetical order. The two model names
896
+		// are transformed to snake case from their default CamelCase also.
897
+		if (is_null($table)) {
898
+			$table = $this->joiningTable($relatedMap);
899
+		}
900
+
901
+		return new BelongsToMany($relatedMapper, $entity, $table, $foreignKey, $otherKey, $relation);
902
+	}
903
+
904
+	/**
905
+	 * Define a polymorphic many-to-many relationship.
906
+	 *
907
+	 * @param  mixed       $entity
908
+	 * @param  string      $related
909
+	 * @param  string      $name
910
+	 * @param  string|null $table
911
+	 * @param  string|null $foreignKey
912
+	 * @param  string|null $otherKey
913
+	 * @param  bool        $inverse
914
+	 * @throws MappingException
915
+	 * @return \Analogue\ORM\Relationships\MorphToMany
916
+	 */
917
+	public function morphToMany($entity, $related, $name, $table = null, $foreignKey = null, $otherKey = null, $inverse = false)
918
+	{
919
+		$caller = $this->getBelongsToManyCaller();
920
+
921
+		// First, we will need to determine the foreign key and "other key" for the
922
+		// relationship. Once we have determined the keys we will make the query
923
+		// instances, as well as the relationship instances we need for these.
924
+		$foreignKey = $foreignKey ?: $name . '_id';
925
+
926
+		$relatedMapper = $this->manager->mapper($related);
927
+
928
+		$otherKey = $otherKey ?: $relatedMapper->getEntityMap()->getForeignKey();
929
+
930
+		$table = $table ?: str_plural($name);
931
+
932
+		return new MorphToMany($relatedMapper, $entity, $name, $table, $foreignKey, $otherKey, $caller, $inverse);
933
+	}
934
+
935
+	/**
936
+	 * Define a polymorphic, inverse many-to-many relationship.
937
+	 *
938
+	 * @param  mixed       $entity
939
+	 * @param  string      $related
940
+	 * @param  string      $name
941
+	 * @param  string|null $table
942
+	 * @param  string|null $foreignKey
943
+	 * @param  string|null $otherKey
944
+	 * @throws MappingException
945
+	 * @return \Analogue\ORM\Relationships\MorphToMany
946
+	 */
947
+	public function morphedByMany($entity, $related, $name, $table = null, $foreignKey = null, $otherKey = null)
948
+	{
949
+		$foreignKey = $foreignKey ?: $this->getForeignKey();
950
+
951
+		// For the inverse of the polymorphic many-to-many relations, we will change
952
+		// the way we determine the foreign and other keys, as it is the opposite
953
+		// of the morph-to-many method since we're figuring out these inverses.
954
+		$otherKey = $otherKey ?: $name . '_id';
955
+
956
+		return $this->morphToMany($entity, $related, $name, $table, $foreignKey, $otherKey, true);
957
+	}
958
+
959
+	/**
960
+	 * Get the relationship name of the belongs to many.
961
+	 *
962
+	 * @return string
963
+	 */
964
+	protected function getBelongsToManyCaller()
965
+	{
966
+		$self = __FUNCTION__;
967
+
968
+		$caller = array_first(debug_backtrace(false), function ($key, $trace) use ($self) {
969
+			$caller = $trace['function'];
970
+
971
+			return (!in_array($caller, EntityMap::$manyMethods) && $caller != $self);
972
+		});
973
+
974
+		return !is_null($caller) ? $caller['function'] : null;
975
+	}
976
+
977
+	/**
978
+	 * Get the joining table name for a many-to-many relation.
979
+	 *
980
+	 * @param  EntityMap $relatedMap
981
+	 * @return string
982
+	 */
983
+	public function joiningTable($relatedMap)
984
+	{
985
+		// The joining table name, by convention, is simply the snake cased models
986
+		// sorted alphabetically and concatenated with an underscore, so we can
987
+		// just sort the models and join them together to get the table name.
988
+		$base = $this->getTable();
989
+
990
+		$related = $relatedMap->getTable();
991
+
992
+		$tables = [$related, $base];
993
+
994
+		// Now that we have the model names in an array we can just sort them and
995
+		// use the implode function to join them together with an underscores,
996
+		// which is typically used by convention within the database system.
997
+		sort($tables);
998
+
999
+		return strtolower(implode('_', $tables));
1000
+	}
1001
+
1002
+	/**
1003
+	 * Get the polymorphic relationship columns.
1004
+	 *
1005
+	 * @param  string $name
1006
+	 * @param  string $type
1007
+	 * @param  string $id
1008
+	 * @return string[]
1009
+	 */
1010
+	protected function getMorphs($name, $type, $id)
1011
+	{
1012
+		$type = $type ?: $name . '_type';
1013
+
1014
+		$id = $id ?: $name . '_id';
1015
+
1016
+		return [$type, $id];
1017
+	}
1018
+
1019
+	/**
1020
+	 * Get the class name for polymorphic relations.
1021
+	 *
1022
+	 * @return string
1023
+	 */
1024
+	public function getMorphClass()
1025
+	{
1026
+		return $this->morphClass ?: $this->getClass();
1027
+	}
1028 1028
     
1029
-    /**
1030
-     * Create a new Entity Collection instance.
1031
-     *
1032
-     * @param  array $entities
1033
-     * @return \Analogue\ORM\EntityCollection
1034
-     */
1035
-    public function newCollection(array $entities = [])
1036
-    {
1037
-        return new EntityCollection($entities, $this);
1038
-    }
1039
-
1040
-    /**
1041
-     * Process EntityMap parsing at initialization time
1042
-     *
1043
-     * @return void
1044
-     */
1045
-    public function initialize()
1046
-    {
1047
-        $userMethods = $this->getCustomMethods();
1048
-
1049
-        // Parse EntityMap for method based relationship
1050
-        if (count($userMethods) > 0) {
1051
-            $this->relationships = $this->parseMethodsForRelationship($userMethods);
1052
-        }
1053
-
1054
-        // Parse EntityMap for dynamic relationships
1055
-        if (count($this->dynamicRelationships) > 0) {
1056
-            $this->relationships = $this->relationships + $this->getDynamicRelationships();
1057
-        }
1058
-    }
1059
-
1060
-    /**
1061
-     * Parse every relationships on the EntityMap and sort
1062
-     * them by type.
1063
-     *
1064
-     * @return void
1065
-     */
1066
-    public function boot()
1067
-    {
1068
-        if (count($this->relationships > 0)) {
1069
-            $this->sortRelationshipsByType();
1070
-        }
1071
-    }
1072
-
1073
-    /**
1074
-     * Get Methods that has been added in the child class.
1075
-     *
1076
-     * @return array
1077
-     */
1078
-    protected function getCustomMethods()
1079
-    {
1080
-        $mapMethods = get_class_methods($this);
1081
-
1082
-        $parentsMethods = get_class_methods('Analogue\ORM\EntityMap');
1029
+	/**
1030
+	 * Create a new Entity Collection instance.
1031
+	 *
1032
+	 * @param  array $entities
1033
+	 * @return \Analogue\ORM\EntityCollection
1034
+	 */
1035
+	public function newCollection(array $entities = [])
1036
+	{
1037
+		return new EntityCollection($entities, $this);
1038
+	}
1039
+
1040
+	/**
1041
+	 * Process EntityMap parsing at initialization time
1042
+	 *
1043
+	 * @return void
1044
+	 */
1045
+	public function initialize()
1046
+	{
1047
+		$userMethods = $this->getCustomMethods();
1048
+
1049
+		// Parse EntityMap for method based relationship
1050
+		if (count($userMethods) > 0) {
1051
+			$this->relationships = $this->parseMethodsForRelationship($userMethods);
1052
+		}
1053
+
1054
+		// Parse EntityMap for dynamic relationships
1055
+		if (count($this->dynamicRelationships) > 0) {
1056
+			$this->relationships = $this->relationships + $this->getDynamicRelationships();
1057
+		}
1058
+	}
1059
+
1060
+	/**
1061
+	 * Parse every relationships on the EntityMap and sort
1062
+	 * them by type.
1063
+	 *
1064
+	 * @return void
1065
+	 */
1066
+	public function boot()
1067
+	{
1068
+		if (count($this->relationships > 0)) {
1069
+			$this->sortRelationshipsByType();
1070
+		}
1071
+	}
1072
+
1073
+	/**
1074
+	 * Get Methods that has been added in the child class.
1075
+	 *
1076
+	 * @return array
1077
+	 */
1078
+	protected function getCustomMethods()
1079
+	{
1080
+		$mapMethods = get_class_methods($this);
1081
+
1082
+		$parentsMethods = get_class_methods('Analogue\ORM\EntityMap');
1083 1083
         
1084
-        return array_diff($mapMethods, $parentsMethods);
1085
-    }
1086
-
1087
-    /**
1088
-     * Parse user's class methods for relationships
1089
-     *
1090
-     * @param  array $customMethods
1091
-     * @return array
1092
-     */
1093
-    protected function parseMethodsForRelationship(array $customMethods)
1094
-    {
1095
-        $relationships = [];
1096
-
1097
-        $class = new ReflectionClass(get_class($this));
1098
-
1099
-        // Get the mapped Entity class, as we will detect relationships
1100
-        // methods by testing that the first argument is type-hinted to
1101
-        // the same class as the mapped Entity.
1102
-        $entityClass = $this->getClass();
1103
-
1104
-        foreach ($customMethods as $methodName) {
1105
-            $method = $class->getMethod($methodName);
1106
-
1107
-            if ($method->getNumberOfParameters() > 0) {
1108
-                $params = $method->getParameters();
1109
-
1110
-                if ($params[0]->getClass() && $params[0]->getClass()->name == $entityClass) {
1111
-                    $relationships[] = $methodName;
1112
-                }
1113
-            }
1114
-        }
1115
-
1116
-        return $relationships;
1117
-    }
1118
-
1119
-    /**
1120
-     * Sort Relationships methods by type
1121
-     *
1122
-     * @return void
1123
-     */
1124
-    protected function sortRelationshipsByType()
1125
-    {
1126
-        $entityClass = $this->getClass();
1127
-
1128
-        // Instantiate a dummy entity which we will pass to relationship methods.
1129
-        $entity = unserialize(sprintf('O:%d:"%s":0:{}', strlen($entityClass), $entityClass));
1084
+		return array_diff($mapMethods, $parentsMethods);
1085
+	}
1086
+
1087
+	/**
1088
+	 * Parse user's class methods for relationships
1089
+	 *
1090
+	 * @param  array $customMethods
1091
+	 * @return array
1092
+	 */
1093
+	protected function parseMethodsForRelationship(array $customMethods)
1094
+	{
1095
+		$relationships = [];
1096
+
1097
+		$class = new ReflectionClass(get_class($this));
1098
+
1099
+		// Get the mapped Entity class, as we will detect relationships
1100
+		// methods by testing that the first argument is type-hinted to
1101
+		// the same class as the mapped Entity.
1102
+		$entityClass = $this->getClass();
1103
+
1104
+		foreach ($customMethods as $methodName) {
1105
+			$method = $class->getMethod($methodName);
1106
+
1107
+			if ($method->getNumberOfParameters() > 0) {
1108
+				$params = $method->getParameters();
1109
+
1110
+				if ($params[0]->getClass() && $params[0]->getClass()->name == $entityClass) {
1111
+					$relationships[] = $methodName;
1112
+				}
1113
+			}
1114
+		}
1115
+
1116
+		return $relationships;
1117
+	}
1118
+
1119
+	/**
1120
+	 * Sort Relationships methods by type
1121
+	 *
1122
+	 * @return void
1123
+	 */
1124
+	protected function sortRelationshipsByType()
1125
+	{
1126
+		$entityClass = $this->getClass();
1127
+
1128
+		// Instantiate a dummy entity which we will pass to relationship methods.
1129
+		$entity = unserialize(sprintf('O:%d:"%s":0:{}', strlen($entityClass), $entityClass));
1130 1130
         
1131
-        foreach ($this->relationships as $relation) {
1132
-            $relationObject = $this->$relation($entity);
1133
-
1134
-            $class = class_basename(get_class($relationObject));
1135
-
1136
-            if (in_array($class, static::$singleClasses)) {
1137
-                $this->singleRelations[] = $relation;
1138
-            }
1139
-
1140
-            if (in_array($class, static::$manyClasses)) {
1141
-                $this->manyRelations[] = $relation;
1142
-            }
1143
-
1144
-            if (in_array($class, static::$localClasses)) {
1145
-                $this->localRelations[] = $relation;
1146
-            }
1147
-
1148
-            if (in_array($class, static::$foreignClasses)) {
1149
-                $this->foreignRelations[] = $relation;
1150
-            }
1151
-
1152
-            if (in_array($class, static::$pivotClasses)) {
1153
-                $this->pivotRelations[] = $relation;
1154
-            }
1155
-        }
1156
-    }
1157
-
1158
-    /**
1159
-     * Override this method for custom entity instantiation
1160
-     *
1161
-     * @return null
1162
-     */
1163
-    public function activator()
1164
-    {
1165
-        return null;
1166
-    }
1167
-
1168
-    /**
1169
-     * Call dynamic relationship, if it exists
1170
-     *
1171
-     * @param  string $method
1172
-     * @param  array  $parameters
1173
-     * @throws Exception
1174
-     * @return mixed
1175
-     */
1176
-    public function __call($method, $parameters)
1177
-    {
1178
-        if (!array_key_exists($method, $this->dynamicRelationships)) {
1179
-            throw new Exception(get_class($this) . " has no method $method");
1180
-        }
1181
-
1182
-        // Add $this to parameters so the closure can call relationship method on the map.
1183
-        $parameters[] = $this;
1184
-
1185
-        return  call_user_func_array([$this->dynamicRelationships[$method], $parameters]);
1186
-    }
1131
+		foreach ($this->relationships as $relation) {
1132
+			$relationObject = $this->$relation($entity);
1133
+
1134
+			$class = class_basename(get_class($relationObject));
1135
+
1136
+			if (in_array($class, static::$singleClasses)) {
1137
+				$this->singleRelations[] = $relation;
1138
+			}
1139
+
1140
+			if (in_array($class, static::$manyClasses)) {
1141
+				$this->manyRelations[] = $relation;
1142
+			}
1143
+
1144
+			if (in_array($class, static::$localClasses)) {
1145
+				$this->localRelations[] = $relation;
1146
+			}
1147
+
1148
+			if (in_array($class, static::$foreignClasses)) {
1149
+				$this->foreignRelations[] = $relation;
1150
+			}
1151
+
1152
+			if (in_array($class, static::$pivotClasses)) {
1153
+				$this->pivotRelations[] = $relation;
1154
+			}
1155
+		}
1156
+	}
1157
+
1158
+	/**
1159
+	 * Override this method for custom entity instantiation
1160
+	 *
1161
+	 * @return null
1162
+	 */
1163
+	public function activator()
1164
+	{
1165
+		return null;
1166
+	}
1167
+
1168
+	/**
1169
+	 * Call dynamic relationship, if it exists
1170
+	 *
1171
+	 * @param  string $method
1172
+	 * @param  array  $parameters
1173
+	 * @throws Exception
1174
+	 * @return mixed
1175
+	 */
1176
+	public function __call($method, $parameters)
1177
+	{
1178
+		if (!array_key_exists($method, $this->dynamicRelationships)) {
1179
+			throw new Exception(get_class($this) . " has no method $method");
1180
+		}
1181
+
1182
+		// Add $this to parameters so the closure can call relationship method on the map.
1183
+		$parameters[] = $this;
1184
+
1185
+		return  call_user_func_array([$this->dynamicRelationships[$method], $parameters]);
1186
+	}
1187 1187
 }
Please login to merge, or discard this patch.
Spacing   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -393,7 +393,7 @@  discard block
 block discarded – undo
393 393
         if (!is_null($this->sequence)) {
394 394
             return $this->sequence;
395 395
         } else {
396
-            return $this->getTable() . '_id_seq';
396
+            return $this->getTable().'_id_seq';
397 397
         }
398 398
     }
399 399
 
@@ -563,7 +563,7 @@  discard block
 block discarded – undo
563 563
      */
564 564
     public function getQualifiedKeyName()
565 565
     {
566
-        return $this->getTable() . '.' . $this->getKeyName();
566
+        return $this->getTable().'.'.$this->getKeyName();
567 567
     }
568 568
 
569 569
     /**
@@ -644,7 +644,7 @@  discard block
 block discarded – undo
644 644
      */
645 645
     public function getForeignKey()
646 646
     {
647
-        return snake_case(class_basename($this->getClass())) . '_id';
647
+        return snake_case(class_basename($this->getClass())).'_id';
648 648
     }
649 649
 
650 650
     /**
@@ -667,7 +667,7 @@  discard block
 block discarded – undo
667 667
 
668 668
         $localKey = $localKey ?: $this->getKeyName();
669 669
 
670
-        return new HasOne($relatedMapper, $entity, $relatedMap->getTable() . '.' . $foreignKey, $localKey);
670
+        return new HasOne($relatedMapper, $entity, $relatedMap->getTable().'.'.$foreignKey, $localKey);
671 671
     }
672 672
 
673 673
     /**
@@ -692,7 +692,7 @@  discard block
 block discarded – undo
692 692
 
693 693
         $table = $relatedMapper->getEntityMap()->getTable();
694 694
         
695
-        return new MorphOne($relatedMapper, $entity, $table . '.' . $type, $table . '.' . $id, $localKey);
695
+        return new MorphOne($relatedMapper, $entity, $table.'.'.$type, $table.'.'.$id, $localKey);
696 696
     }
697 697
 
698 698
     /**
@@ -721,7 +721,7 @@  discard block
 block discarded – undo
721 721
         // foreign key name by using the name of the relationship function, which
722 722
         // when combined with an "_id" should conventionally match the columns.
723 723
         if (is_null($foreignKey)) {
724
-            $foreignKey = snake_case($relation) . '_id';
724
+            $foreignKey = snake_case($relation).'_id';
725 725
         }
726 726
 
727 727
         $relatedMapper = $this->manager->mapper($related);
@@ -798,7 +798,7 @@  discard block
 block discarded – undo
798 798
 
799 799
         $relatedMapper = $this->manager->mapper($related);
800 800
 
801
-        $table = $relatedMapper->getEntityMap()->getTable() . '.' . $foreignKey;
801
+        $table = $relatedMapper->getEntityMap()->getTable().'.'.$foreignKey;
802 802
 
803 803
         $localKey = $localKey ?: $this->getKeyName();
804 804
 
@@ -856,7 +856,7 @@  discard block
 block discarded – undo
856 856
 
857 857
         $localKey = $localKey ?: $this->getKeyName();
858 858
         
859
-        return new MorphMany($relatedMapper, $entity, $table . '.' . $type, $table . '.' . $id, $localKey);
859
+        return new MorphMany($relatedMapper, $entity, $table.'.'.$type, $table.'.'.$id, $localKey);
860 860
     }
861 861
 
862 862
     /**
@@ -921,7 +921,7 @@  discard block
 block discarded – undo
921 921
         // First, we will need to determine the foreign key and "other key" for the
922 922
         // relationship. Once we have determined the keys we will make the query
923 923
         // instances, as well as the relationship instances we need for these.
924
-        $foreignKey = $foreignKey ?: $name . '_id';
924
+        $foreignKey = $foreignKey ?: $name.'_id';
925 925
 
926 926
         $relatedMapper = $this->manager->mapper($related);
927 927
 
@@ -951,7 +951,7 @@  discard block
 block discarded – undo
951 951
         // For the inverse of the polymorphic many-to-many relations, we will change
952 952
         // the way we determine the foreign and other keys, as it is the opposite
953 953
         // of the morph-to-many method since we're figuring out these inverses.
954
-        $otherKey = $otherKey ?: $name . '_id';
954
+        $otherKey = $otherKey ?: $name.'_id';
955 955
 
956 956
         return $this->morphToMany($entity, $related, $name, $table, $foreignKey, $otherKey, true);
957 957
     }
@@ -965,7 +965,7 @@  discard block
 block discarded – undo
965 965
     {
966 966
         $self = __FUNCTION__;
967 967
 
968
-        $caller = array_first(debug_backtrace(false), function ($key, $trace) use ($self) {
968
+        $caller = array_first(debug_backtrace(false), function($key, $trace) use ($self) {
969 969
             $caller = $trace['function'];
970 970
 
971 971
             return (!in_array($caller, EntityMap::$manyMethods) && $caller != $self);
@@ -1009,9 +1009,9 @@  discard block
 block discarded – undo
1009 1009
      */
1010 1010
     protected function getMorphs($name, $type, $id)
1011 1011
     {
1012
-        $type = $type ?: $name . '_type';
1012
+        $type = $type ?: $name.'_type';
1013 1013
 
1014
-        $id = $id ?: $name . '_id';
1014
+        $id = $id ?: $name.'_id';
1015 1015
 
1016 1016
         return [$type, $id];
1017 1017
     }
@@ -1176,7 +1176,7 @@  discard block
 block discarded – undo
1176 1176
     public function __call($method, $parameters)
1177 1177
     {
1178 1178
         if (!array_key_exists($method, $this->dynamicRelationships)) {
1179
-            throw new Exception(get_class($this) . " has no method $method");
1179
+            throw new Exception(get_class($this)." has no method $method");
1180 1180
         }
1181 1181
 
1182 1182
         // Add $this to parameters so the closure can call relationship method on the map.
Please login to merge, or discard this patch.
src/System/Aggregate.php 2 patches
Indentation   +992 added lines, -992 removed lines patch added patch discarded remove patch
@@ -15,1009 +15,1009 @@
 block discarded – undo
15 15
  */
16 16
 class Aggregate implements InternallyMappable
17 17
 {
18
-    /**
19
-     * The Root Entity
20
-     *
21
-     * @var \Analogue\ORM\System\Wrappers\Wrapper
22
-     */
23
-    protected $wrappedEntity;
24
-
25
-    /**
26
-     * Parent Root Aggregate
27
-     *
28
-     * @var \Analogue\ORM\System\Aggregate
29
-     */
30
-    protected $parent;
31
-
32
-    /**
33
-     * Parent's relationship method
34
-     *
35
-     * @var string
36
-     */
37
-    protected $parentRelationship;
38
-
39
-    /**
40
-     * Root Entity
41
-     *
42
-     * @var \Analogue\ORM\System\Aggregate
43
-     */
44
-    protected $root;
45
-
46
-    /**
47
-     * An associative array containing entity's
48
-     * relationships converted to Aggregates
49
-     *
50
-     * @var array
51
-     */
52
-    protected $relationships = [];
53
-
54
-    /**
55
-     * Relationship that need post-command synchronization
56
-     *
57
-     * @var array
58
-     */
59
-    protected $needSync = [];
60
-
61
-    /**
62
-     * Mapper
63
-     *
64
-     * @var \Analogue\ORM\System\Mapper;
65
-     */
66
-    protected $mapper;
67
-
68
-    /**
69
-     * Entity Map
70
-     *
71
-     * @var \Analogue\ORM\EntityMap;
72
-     */
73
-    protected $entityMap;
74
-
75
-    /**
76
-     * Create a new Aggregated Entity instance
77
-     *
78
-     * @param mixed          $entity
79
-     * @param Aggregate|null $parent
80
-     * @param string         $parentRelationship
81
-     * @param Aggregate|null $root
82
-     * @throws MappingException
83
-     */
84
-    public function __construct($entity, Aggregate $parent = null, $parentRelationship = null, Aggregate $root = null)
85
-    {
86
-        $factory = new Factory;
87
-
88
-        $this->wrappedEntity = $factory->make($entity);
89
-
90
-        $this->parent = $parent;
91
-
92
-        $this->parentRelationship = $parentRelationship;
93
-
94
-        $this->root = $root;
95
-
96
-        $this->mapper = Manager::getMapper($entity);
97
-
98
-        $this->entityMap = $this->mapper->getEntityMap();
18
+	/**
19
+	 * The Root Entity
20
+	 *
21
+	 * @var \Analogue\ORM\System\Wrappers\Wrapper
22
+	 */
23
+	protected $wrappedEntity;
24
+
25
+	/**
26
+	 * Parent Root Aggregate
27
+	 *
28
+	 * @var \Analogue\ORM\System\Aggregate
29
+	 */
30
+	protected $parent;
31
+
32
+	/**
33
+	 * Parent's relationship method
34
+	 *
35
+	 * @var string
36
+	 */
37
+	protected $parentRelationship;
38
+
39
+	/**
40
+	 * Root Entity
41
+	 *
42
+	 * @var \Analogue\ORM\System\Aggregate
43
+	 */
44
+	protected $root;
45
+
46
+	/**
47
+	 * An associative array containing entity's
48
+	 * relationships converted to Aggregates
49
+	 *
50
+	 * @var array
51
+	 */
52
+	protected $relationships = [];
53
+
54
+	/**
55
+	 * Relationship that need post-command synchronization
56
+	 *
57
+	 * @var array
58
+	 */
59
+	protected $needSync = [];
60
+
61
+	/**
62
+	 * Mapper
63
+	 *
64
+	 * @var \Analogue\ORM\System\Mapper;
65
+	 */
66
+	protected $mapper;
67
+
68
+	/**
69
+	 * Entity Map
70
+	 *
71
+	 * @var \Analogue\ORM\EntityMap;
72
+	 */
73
+	protected $entityMap;
74
+
75
+	/**
76
+	 * Create a new Aggregated Entity instance
77
+	 *
78
+	 * @param mixed          $entity
79
+	 * @param Aggregate|null $parent
80
+	 * @param string         $parentRelationship
81
+	 * @param Aggregate|null $root
82
+	 * @throws MappingException
83
+	 */
84
+	public function __construct($entity, Aggregate $parent = null, $parentRelationship = null, Aggregate $root = null)
85
+	{
86
+		$factory = new Factory;
87
+
88
+		$this->wrappedEntity = $factory->make($entity);
89
+
90
+		$this->parent = $parent;
91
+
92
+		$this->parentRelationship = $parentRelationship;
93
+
94
+		$this->root = $root;
95
+
96
+		$this->mapper = Manager::getMapper($entity);
97
+
98
+		$this->entityMap = $this->mapper->getEntityMap();
99 99
              
100
-        $this->parseRelationships();
101
-    }
102
-
103
-    /**
104
-     * Parse Every relationships defined on the entity
105
-     *
106
-     * @throws MappingException
107
-     * @return void
108
-     */
109
-    protected function parseRelationships()
110
-    {
111
-        foreach ($this->entityMap->getSingleRelationships() as $relation) {
112
-            $this->parseSingleRelationship($relation);
113
-        }
114
-
115
-        foreach ($this->entityMap->getManyRelationships() as $relation) {
116
-            $this->parseManyRelationship($relation);
117
-        }
118
-    }
119
-
120
-    /**
121
-     * Parse for values common to single & many relations
122
-     *
123
-     * @param  string $relation
124
-     * @throws MappingException
125
-     * @return mixed|boolean
126
-     */
127
-    protected function parseForCommonValues($relation)
128
-    {
129
-        if (!$this->hasAttribute($relation)) {
130
-            // If no attribute exists for this relationships
131
-            // we'll make it a simple empty array. This will
132
-            // save us from constantly checking for the attributes
133
-            // actual existence.
134
-            $this->relationships[$relation] = [];
135
-            return false;
136
-        }
137
-
138
-        $value = $this->getRelationshipValue($relation);
139
-
140
-        if (is_null($value)) {
141
-            $this->relationships[$relation] = [];
142
-
143
-            // If the relationship's content is the null value
144
-            // and the Entity's exist in DB, we'll interpret this
145
-            // as the need to detach all related Entities,
146
-            // therefore a sync operation is needed.
147
-            $this->needSync[] = $relation;
148
-            return false;
149
-        }
150
-
151
-        return $value;
152
-    }
153
-
154
-    /**
155
-     * Parse a 'single' relationship
156
-     *
157
-     * @param  string $relation
158
-     * @throws MappingException
159
-     * @return boolean
160
-     */
161
-    protected function parseSingleRelationship($relation)
162
-    {
163
-        if (!$value = $this->parseForCommonValues($relation)) {
164
-            return true;
165
-        }
100
+		$this->parseRelationships();
101
+	}
102
+
103
+	/**
104
+	 * Parse Every relationships defined on the entity
105
+	 *
106
+	 * @throws MappingException
107
+	 * @return void
108
+	 */
109
+	protected function parseRelationships()
110
+	{
111
+		foreach ($this->entityMap->getSingleRelationships() as $relation) {
112
+			$this->parseSingleRelationship($relation);
113
+		}
114
+
115
+		foreach ($this->entityMap->getManyRelationships() as $relation) {
116
+			$this->parseManyRelationship($relation);
117
+		}
118
+	}
119
+
120
+	/**
121
+	 * Parse for values common to single & many relations
122
+	 *
123
+	 * @param  string $relation
124
+	 * @throws MappingException
125
+	 * @return mixed|boolean
126
+	 */
127
+	protected function parseForCommonValues($relation)
128
+	{
129
+		if (!$this->hasAttribute($relation)) {
130
+			// If no attribute exists for this relationships
131
+			// we'll make it a simple empty array. This will
132
+			// save us from constantly checking for the attributes
133
+			// actual existence.
134
+			$this->relationships[$relation] = [];
135
+			return false;
136
+		}
137
+
138
+		$value = $this->getRelationshipValue($relation);
139
+
140
+		if (is_null($value)) {
141
+			$this->relationships[$relation] = [];
142
+
143
+			// If the relationship's content is the null value
144
+			// and the Entity's exist in DB, we'll interpret this
145
+			// as the need to detach all related Entities,
146
+			// therefore a sync operation is needed.
147
+			$this->needSync[] = $relation;
148
+			return false;
149
+		}
150
+
151
+		return $value;
152
+	}
153
+
154
+	/**
155
+	 * Parse a 'single' relationship
156
+	 *
157
+	 * @param  string $relation
158
+	 * @throws MappingException
159
+	 * @return boolean
160
+	 */
161
+	protected function parseSingleRelationship($relation)
162
+	{
163
+		if (!$value = $this->parseForCommonValues($relation)) {
164
+			return true;
165
+		}
166 166
         
167
-        if ($value instanceof Collection || is_array($value) || $value instanceof CollectionProxy) {
168
-            throw new MappingException("Entity's attribute $relation should not be array, or collection");
169
-        }
170
-
171
-        if ($value instanceof EntityProxy && !$value->isLoaded()) {
172
-            $this->relationships[$relation] = [];
173
-            return true;
174
-        }
175
-
176
-        // If the attribute is a loaded proxy, swap it for its
177
-        // loaded entity.
178
-        if ($value instanceof EntityProxy && $value->isLoaded()) {
179
-            $value = $value->getUnderlyingObject();
180
-        }
181
-
182
-        if ($this->isParentOrRoot($value)) {
183
-            $this->relationships[$relation] = [];
184
-            return true;
185
-        }
186
-
187
-        // At this point, we can assume the attribute is an Entity instance
188
-        // so we'll treat it as such.
189
-        $subAggregate = $this->createSubAggregate($value, $relation);
190
-
191
-        // Even if it's a single entity, we'll store it as an array
192
-        // just for consistency with other relationships
193
-        $this->relationships[$relation] = [$subAggregate];
167
+		if ($value instanceof Collection || is_array($value) || $value instanceof CollectionProxy) {
168
+			throw new MappingException("Entity's attribute $relation should not be array, or collection");
169
+		}
170
+
171
+		if ($value instanceof EntityProxy && !$value->isLoaded()) {
172
+			$this->relationships[$relation] = [];
173
+			return true;
174
+		}
175
+
176
+		// If the attribute is a loaded proxy, swap it for its
177
+		// loaded entity.
178
+		if ($value instanceof EntityProxy && $value->isLoaded()) {
179
+			$value = $value->getUnderlyingObject();
180
+		}
181
+
182
+		if ($this->isParentOrRoot($value)) {
183
+			$this->relationships[$relation] = [];
184
+			return true;
185
+		}
186
+
187
+		// At this point, we can assume the attribute is an Entity instance
188
+		// so we'll treat it as such.
189
+		$subAggregate = $this->createSubAggregate($value, $relation);
190
+
191
+		// Even if it's a single entity, we'll store it as an array
192
+		// just for consistency with other relationships
193
+		$this->relationships[$relation] = [$subAggregate];
194 194
  
195
-        return true;
196
-    }
197
-
198
-    /**
199
-     * Check if value isn't parent or root in the aggregate
200
-     *
201
-     * @param  mixed
202
-     * @return boolean|null
203
-     */
204
-    protected function isParentOrRoot($value)
205
-    {
206
-        if (!is_null($this->root)) {
207
-            $rootClass = get_class($this->root->getEntityObject());
208
-            if ($rootClass == get_class($value)) {
209
-                return true;
210
-            }
211
-        }
212
-
213
-        if (!is_null($this->parent)) {
214
-            $parentClass = get_class($this->parent->getEntityObject());
215
-            if ($parentClass == get_class($value)) {
216
-                return true;
217
-            }
218
-        }
219
-    }
220
-
221
-    /**
222
-     * Parse a 'many' relationship
223
-     *
224
-     * @param  string $relation
225
-     * @throws MappingException
226
-     * @return boolean
227
-     */
228
-    protected function parseManyRelationship($relation)
229
-    {
230
-        if (!$value = $this->parseForCommonValues($relation)) {
231
-            return true;
232
-        }
233
-
234
-        if (is_array($value) || $value instanceof Collection) {
235
-            $this->needSync[] = $relation;
236
-        }
237
-        // If the relation is a proxy, we test is the relation
238
-        // has been lazy loaded, otherwise we'll just treat
239
-        // the subset of newly added items.
240
-        if ($value instanceof CollectionProxy && $value->isLoaded()) {
241
-            $this->needSync[] = $relation;
242
-            $value = $value->getUnderlyingCollection();
243
-        }
244
-
245
-        if ($value instanceof CollectionProxy && !$value->isLoaded()) {
246
-            $value = $value->getAddedItems();
247
-        }
248
-
249
-        // At this point $value should be either an array or an instance
250
-        // of a collection class.
251
-        if (!is_array($value) && !$value instanceof Collection) {
252
-            throw new MappingException("'$relation' attribute should be array() or Collection");
253
-        }
254
-
255
-        $this->relationships[$relation] = $this->createSubAggregates($value, $relation);
195
+		return true;
196
+	}
197
+
198
+	/**
199
+	 * Check if value isn't parent or root in the aggregate
200
+	 *
201
+	 * @param  mixed
202
+	 * @return boolean|null
203
+	 */
204
+	protected function isParentOrRoot($value)
205
+	{
206
+		if (!is_null($this->root)) {
207
+			$rootClass = get_class($this->root->getEntityObject());
208
+			if ($rootClass == get_class($value)) {
209
+				return true;
210
+			}
211
+		}
212
+
213
+		if (!is_null($this->parent)) {
214
+			$parentClass = get_class($this->parent->getEntityObject());
215
+			if ($parentClass == get_class($value)) {
216
+				return true;
217
+			}
218
+		}
219
+	}
220
+
221
+	/**
222
+	 * Parse a 'many' relationship
223
+	 *
224
+	 * @param  string $relation
225
+	 * @throws MappingException
226
+	 * @return boolean
227
+	 */
228
+	protected function parseManyRelationship($relation)
229
+	{
230
+		if (!$value = $this->parseForCommonValues($relation)) {
231
+			return true;
232
+		}
233
+
234
+		if (is_array($value) || $value instanceof Collection) {
235
+			$this->needSync[] = $relation;
236
+		}
237
+		// If the relation is a proxy, we test is the relation
238
+		// has been lazy loaded, otherwise we'll just treat
239
+		// the subset of newly added items.
240
+		if ($value instanceof CollectionProxy && $value->isLoaded()) {
241
+			$this->needSync[] = $relation;
242
+			$value = $value->getUnderlyingCollection();
243
+		}
244
+
245
+		if ($value instanceof CollectionProxy && !$value->isLoaded()) {
246
+			$value = $value->getAddedItems();
247
+		}
248
+
249
+		// At this point $value should be either an array or an instance
250
+		// of a collection class.
251
+		if (!is_array($value) && !$value instanceof Collection) {
252
+			throw new MappingException("'$relation' attribute should be array() or Collection");
253
+		}
254
+
255
+		$this->relationships[$relation] = $this->createSubAggregates($value, $relation);
256 256
         
257
-        return true;
258
-    }
259
-
260
-    /**
261
-     * Return Entity's relationship attribute
262
-     *
263
-     * @param  string $relation
264
-     * @throws MappingException
265
-     * @return mixed
266
-     */
267
-    protected function getRelationshipValue($relation)
268
-    {
269
-        $value = $this->getEntityAttribute($relation);
270
-
271
-        if (is_bool($value) || is_float($value) || is_int($value) || is_string($value)) {
272
-            throw new MappingException("Entity's attribute $relation should be array, object, collection or null");
273
-        }
274
-
275
-        return $value;
276
-    }
277
-
278
-    /**
279
-     * Create a child, aggregated entity
280
-     *
281
-     * @param  mixed $entities
282
-     * @param string $relation
283
-     * @return array
284
-     */
285
-    protected function createSubAggregates($entities, $relation)
286
-    {
287
-        $aggregates = [];
288
-
289
-        foreach ($entities as $entity) {
290
-            $aggregates[] = $this->createSubAggregate($entity, $relation);
291
-        }
292
-
293
-        return $aggregates;
294
-    }
295
-
296
-    /**
297
-     * Create a related subAggregate
298
-     *
299
-     * @param  mixed $entity
300
-     * @param  string $relation
301
-     * @throws MappingException
302
-     * @return self
303
-     */
304
-    protected function createSubAggregate($entity, $relation)
305
-    {
306
-        // If root isn't defined, then this is the Aggregate Root
307
-        if (is_null($this->root)) {
308
-            $root = $this;
309
-        } else {
310
-            $root = $this->root;
311
-        }
312
-
313
-        return new self($entity, $this, $relation, $root);
314
-    }
315
-
316
-    /**
317
-     * Get the Entity's primary key attribute
318
-     *
319
-     * @return string|integer
320
-     */
321
-    public function getEntityId()
322
-    {
323
-        return $this->wrappedEntity->getEntityAttribute($this->entityMap->getKeyName());
324
-    }
325
-
326
-    /**
327
-     * Get the name of the primary key
328
-     *
329
-     * @return string
330
-     */
331
-    public function getEntityKey()
332
-    {
333
-        return $this->entityMap->getKeyName();
334
-    }
335
-
336
-    /**
337
-     * Return the entity map for the current entity
338
-     *
339
-     * @return \Analogue\ORM\EntityMap
340
-     */
341
-    public function getEntityMap()
342
-    {
343
-        return $this->entityMap;
344
-    }
345
-
346
-    /**
347
-     * Return the Entity's hash $class.$id
348
-     *
349
-     * @return string
350
-     */
351
-    public function getEntityHash()
352
-    {
353
-        return $this->getEntityClass() . '.' . $this->getEntityId();
354
-    }
355
-
356
-    /**
357
-     * Get wrapped entity class
358
-     *
359
-     * @return string
360
-     */
361
-    public function getEntityClass()
362
-    {
363
-        return $this->entityMap->getClass();
364
-    }
365
-
366
-    /**
367
-     * Return the Mapper's entity cache
368
-     *
369
-     * @return \Analogue\ORM\System\EntityCache
370
-     */
371
-    protected function getEntityCache()
372
-    {
373
-        return $this->mapper->getEntityCache();
374
-    }
375
-
376
-    /**
377
-     * Get a relationship as an aggregated entities' array
378
-     *
379
-     * @param  string $name
380
-     * @return array
381
-     */
382
-    public function getRelationship($name)
383
-    {
384
-        if (array_key_exists($name, $this->relationships)) {
385
-            return $this->relationships[$name];
386
-        } else {
387
-            return [];
388
-        }
389
-    }
390
-
391
-    /**
392
-     * [TO IMPLEMENT]
393
-     *
394
-     * @return array
395
-     */
396
-    public function getPivotAttributes()
397
-    {
398
-        return [];
399
-    }
400
-
401
-    /**
402
-     * Get Non existing related entities from several relationships
403
-     *
404
-     * @param  array $relationships
405
-     * @return array
406
-     */
407
-    public function getNonExistingRelated(array $relationships)
408
-    {
409
-        $nonExisting = [];
410
-
411
-        foreach ($relationships as $relation) {
412
-            if ($this->hasAttribute($relation) && array_key_exists($relation, $this->relationships)) {
413
-                $nonExisting = array_merge($nonExisting, $this->getNonExistingFromRelation($relation));
414
-            }
415
-        }
416
-
417
-        return $nonExisting;
418
-    }
419
-
420
-    /**
421
-     * Get non-existing related entities from a single relation
422
-     *
423
-     * @param  string $relation
424
-     * @return array
425
-     */
426
-    protected function getNonExistingFromRelation($relation)
427
-    {
428
-        $nonExisting = [];
429
-
430
-        foreach ($this->relationships[$relation] as $aggregate) {
431
-            if (!$aggregate->exists()) {
432
-                $nonExisting[] = $aggregate;
433
-            }
434
-        }
435
-
436
-        return $nonExisting;
437
-    }
438
-
439
-    /**
440
-     * Synchronize relationships if needed
441
-     */
442
-    public function syncRelationships()
443
-    {
444
-        if ($this->exists()) {
445
-            foreach ($this->needSync as $relation) {
446
-                $this->synchronize($relation);
447
-            }
448
-        }
449
-    }
450
-
451
-    /**
452
-     * Synchronize a relationship attribute
453
-     *
454
-     * @param $relation
455
-     */
456
-    protected function synchronize($relation)
457
-    {
458
-        $actualContent = $this->relationships[$relation];
459
-
460
-        $this->entityMap->$relation($this->getEntityObject())->sync($actualContent);
461
-    }
462
-
463
-    /**
464
-     * Returns an array of Missing related Entities for the
465
-     * given $relation
466
-     *
467
-     * @param  string $relation
468
-     * @return array
469
-     */
470
-    public function getMissingEntities($relation)
471
-    {
472
-        $cachedRelations = $this->getCachedAttribute($relation);
473
-
474
-        if (!is_null($cachedRelations)) {
475
-            $missing = [];
476
-
477
-            foreach ($cachedRelations as $hash) {
478
-                if (!$this->getRelatedAggregateFromHash($hash, $relation)) {
479
-                    $missing[] = $hash;
480
-                }
481
-            }
482
-
483
-            return $missing;
484
-        } else {
485
-            return [];
486
-        }
487
-    }
257
+		return true;
258
+	}
259
+
260
+	/**
261
+	 * Return Entity's relationship attribute
262
+	 *
263
+	 * @param  string $relation
264
+	 * @throws MappingException
265
+	 * @return mixed
266
+	 */
267
+	protected function getRelationshipValue($relation)
268
+	{
269
+		$value = $this->getEntityAttribute($relation);
270
+
271
+		if (is_bool($value) || is_float($value) || is_int($value) || is_string($value)) {
272
+			throw new MappingException("Entity's attribute $relation should be array, object, collection or null");
273
+		}
274
+
275
+		return $value;
276
+	}
277
+
278
+	/**
279
+	 * Create a child, aggregated entity
280
+	 *
281
+	 * @param  mixed $entities
282
+	 * @param string $relation
283
+	 * @return array
284
+	 */
285
+	protected function createSubAggregates($entities, $relation)
286
+	{
287
+		$aggregates = [];
288
+
289
+		foreach ($entities as $entity) {
290
+			$aggregates[] = $this->createSubAggregate($entity, $relation);
291
+		}
292
+
293
+		return $aggregates;
294
+	}
295
+
296
+	/**
297
+	 * Create a related subAggregate
298
+	 *
299
+	 * @param  mixed $entity
300
+	 * @param  string $relation
301
+	 * @throws MappingException
302
+	 * @return self
303
+	 */
304
+	protected function createSubAggregate($entity, $relation)
305
+	{
306
+		// If root isn't defined, then this is the Aggregate Root
307
+		if (is_null($this->root)) {
308
+			$root = $this;
309
+		} else {
310
+			$root = $this->root;
311
+		}
312
+
313
+		return new self($entity, $this, $relation, $root);
314
+	}
315
+
316
+	/**
317
+	 * Get the Entity's primary key attribute
318
+	 *
319
+	 * @return string|integer
320
+	 */
321
+	public function getEntityId()
322
+	{
323
+		return $this->wrappedEntity->getEntityAttribute($this->entityMap->getKeyName());
324
+	}
325
+
326
+	/**
327
+	 * Get the name of the primary key
328
+	 *
329
+	 * @return string
330
+	 */
331
+	public function getEntityKey()
332
+	{
333
+		return $this->entityMap->getKeyName();
334
+	}
335
+
336
+	/**
337
+	 * Return the entity map for the current entity
338
+	 *
339
+	 * @return \Analogue\ORM\EntityMap
340
+	 */
341
+	public function getEntityMap()
342
+	{
343
+		return $this->entityMap;
344
+	}
345
+
346
+	/**
347
+	 * Return the Entity's hash $class.$id
348
+	 *
349
+	 * @return string
350
+	 */
351
+	public function getEntityHash()
352
+	{
353
+		return $this->getEntityClass() . '.' . $this->getEntityId();
354
+	}
355
+
356
+	/**
357
+	 * Get wrapped entity class
358
+	 *
359
+	 * @return string
360
+	 */
361
+	public function getEntityClass()
362
+	{
363
+		return $this->entityMap->getClass();
364
+	}
365
+
366
+	/**
367
+	 * Return the Mapper's entity cache
368
+	 *
369
+	 * @return \Analogue\ORM\System\EntityCache
370
+	 */
371
+	protected function getEntityCache()
372
+	{
373
+		return $this->mapper->getEntityCache();
374
+	}
375
+
376
+	/**
377
+	 * Get a relationship as an aggregated entities' array
378
+	 *
379
+	 * @param  string $name
380
+	 * @return array
381
+	 */
382
+	public function getRelationship($name)
383
+	{
384
+		if (array_key_exists($name, $this->relationships)) {
385
+			return $this->relationships[$name];
386
+		} else {
387
+			return [];
388
+		}
389
+	}
390
+
391
+	/**
392
+	 * [TO IMPLEMENT]
393
+	 *
394
+	 * @return array
395
+	 */
396
+	public function getPivotAttributes()
397
+	{
398
+		return [];
399
+	}
400
+
401
+	/**
402
+	 * Get Non existing related entities from several relationships
403
+	 *
404
+	 * @param  array $relationships
405
+	 * @return array
406
+	 */
407
+	public function getNonExistingRelated(array $relationships)
408
+	{
409
+		$nonExisting = [];
410
+
411
+		foreach ($relationships as $relation) {
412
+			if ($this->hasAttribute($relation) && array_key_exists($relation, $this->relationships)) {
413
+				$nonExisting = array_merge($nonExisting, $this->getNonExistingFromRelation($relation));
414
+			}
415
+		}
416
+
417
+		return $nonExisting;
418
+	}
419
+
420
+	/**
421
+	 * Get non-existing related entities from a single relation
422
+	 *
423
+	 * @param  string $relation
424
+	 * @return array
425
+	 */
426
+	protected function getNonExistingFromRelation($relation)
427
+	{
428
+		$nonExisting = [];
429
+
430
+		foreach ($this->relationships[$relation] as $aggregate) {
431
+			if (!$aggregate->exists()) {
432
+				$nonExisting[] = $aggregate;
433
+			}
434
+		}
435
+
436
+		return $nonExisting;
437
+	}
438
+
439
+	/**
440
+	 * Synchronize relationships if needed
441
+	 */
442
+	public function syncRelationships()
443
+	{
444
+		if ($this->exists()) {
445
+			foreach ($this->needSync as $relation) {
446
+				$this->synchronize($relation);
447
+			}
448
+		}
449
+	}
450
+
451
+	/**
452
+	 * Synchronize a relationship attribute
453
+	 *
454
+	 * @param $relation
455
+	 */
456
+	protected function synchronize($relation)
457
+	{
458
+		$actualContent = $this->relationships[$relation];
459
+
460
+		$this->entityMap->$relation($this->getEntityObject())->sync($actualContent);
461
+	}
462
+
463
+	/**
464
+	 * Returns an array of Missing related Entities for the
465
+	 * given $relation
466
+	 *
467
+	 * @param  string $relation
468
+	 * @return array
469
+	 */
470
+	public function getMissingEntities($relation)
471
+	{
472
+		$cachedRelations = $this->getCachedAttribute($relation);
473
+
474
+		if (!is_null($cachedRelations)) {
475
+			$missing = [];
476
+
477
+			foreach ($cachedRelations as $hash) {
478
+				if (!$this->getRelatedAggregateFromHash($hash, $relation)) {
479
+					$missing[] = $hash;
480
+				}
481
+			}
482
+
483
+			return $missing;
484
+		} else {
485
+			return [];
486
+		}
487
+	}
488 488
        
489
-    /**
490
-     * Get Relationships who have dirty attributes / dirty relationships
491
-     *
492
-     * @return array
493
-     */
494
-    public function getDirtyRelationships()
495
-    {
496
-        $dirtyAggregates = [];
497
-
498
-        foreach ($this->relationships as $relation) {
499
-            foreach ($relation as $aggregate) {
500
-                if (!$aggregate->exists() || $aggregate->isDirty() || count($aggregate->getDirtyRelationships() > 0)) {
501
-                    $dirtyAggregates[] = $aggregate;
502
-                }
503
-            }
504
-        }
505
-
506
-        return $dirtyAggregates;
507
-    }
489
+	/**
490
+	 * Get Relationships who have dirty attributes / dirty relationships
491
+	 *
492
+	 * @return array
493
+	 */
494
+	public function getDirtyRelationships()
495
+	{
496
+		$dirtyAggregates = [];
497
+
498
+		foreach ($this->relationships as $relation) {
499
+			foreach ($relation as $aggregate) {
500
+				if (!$aggregate->exists() || $aggregate->isDirty() || count($aggregate->getDirtyRelationships() > 0)) {
501
+					$dirtyAggregates[] = $aggregate;
502
+				}
503
+			}
504
+		}
505
+
506
+		return $dirtyAggregates;
507
+	}
508 508
     
509
-    /**
510
-     * Compare the object's raw attributes with the record in cache
511
-     *
512
-     * @return boolean
513
-     */
514
-    public function isDirty()
515
-    {
516
-        if (count($this->getDirtyRawAttributes()) > 0) {
517
-            return true;
518
-        } else {
519
-            return false;
520
-        }
521
-    }
522
-
523
-    /**
524
-     * Get Raw Entity's attributes, as they are represented
525
-     * in the database, including value objects & foreign keys
526
-     *
527
-     * @return array
528
-     */
529
-    public function getRawAttributes()
530
-    {
531
-        $attributes = $this->wrappedEntity->getEntityAttributes();
532
-
533
-        foreach ($this->entityMap->getRelationships() as $relation) {
534
-            unset($attributes[$relation]);
535
-        }
536
-
537
-        $attributes = $this->flattenEmbeddables($attributes);
538
-
539
-        $foreignKeys = $this->getForeignKeyAttributes();
540
-
541
-        return $attributes + $foreignKeys;
542
-    }
543
-
544
-    /**
545
-     * Convert Value Objects to raw db attributes
546
-     *
547
-     * @param  array $attributes
548
-     * @return array
549
-     */
550
-    protected function flattenEmbeddables($attributes)
551
-    {
552
-        $embeddables = $this->entityMap->getEmbeddables();
509
+	/**
510
+	 * Compare the object's raw attributes with the record in cache
511
+	 *
512
+	 * @return boolean
513
+	 */
514
+	public function isDirty()
515
+	{
516
+		if (count($this->getDirtyRawAttributes()) > 0) {
517
+			return true;
518
+		} else {
519
+			return false;
520
+		}
521
+	}
522
+
523
+	/**
524
+	 * Get Raw Entity's attributes, as they are represented
525
+	 * in the database, including value objects & foreign keys
526
+	 *
527
+	 * @return array
528
+	 */
529
+	public function getRawAttributes()
530
+	{
531
+		$attributes = $this->wrappedEntity->getEntityAttributes();
532
+
533
+		foreach ($this->entityMap->getRelationships() as $relation) {
534
+			unset($attributes[$relation]);
535
+		}
536
+
537
+		$attributes = $this->flattenEmbeddables($attributes);
538
+
539
+		$foreignKeys = $this->getForeignKeyAttributes();
540
+
541
+		return $attributes + $foreignKeys;
542
+	}
543
+
544
+	/**
545
+	 * Convert Value Objects to raw db attributes
546
+	 *
547
+	 * @param  array $attributes
548
+	 * @return array
549
+	 */
550
+	protected function flattenEmbeddables($attributes)
551
+	{
552
+		$embeddables = $this->entityMap->getEmbeddables();
553 553
         
554
-        foreach ($embeddables as $localKey => $embed) {
555
-            // Retrieve the value object from the entity's attributes
556
-            $valueObject = $attributes[$localKey];
554
+		foreach ($embeddables as $localKey => $embed) {
555
+			// Retrieve the value object from the entity's attributes
556
+			$valueObject = $attributes[$localKey];
557 557
 
558
-            // Unset the corresponding key
559
-            unset($attributes[$localKey]);
558
+			// Unset the corresponding key
559
+			unset($attributes[$localKey]);
560 560
 
561
-            // TODO Make wrapper object compatible with value objects
562
-            $valueObjectAttributes = $valueObject->getEntityAttributes();
561
+			// TODO Make wrapper object compatible with value objects
562
+			$valueObjectAttributes = $valueObject->getEntityAttributes();
563 563
 
564
-            // Now (if setup in the entity map) we prefix the value object's
565
-            // attributes with the snake_case name of the embedded class.
566
-            $prefix = snake_case(class_basename($embed));
564
+			// Now (if setup in the entity map) we prefix the value object's
565
+			// attributes with the snake_case name of the embedded class.
566
+			$prefix = snake_case(class_basename($embed));
567 567
 
568
-            foreach ($valueObjectAttributes as $key=>$value) {
569
-                $valueObjectAttributes[$prefix . '_' . $key] = $value;
570
-                unset($valueObjectAttributes[$key]);
571
-            }
568
+			foreach ($valueObjectAttributes as $key=>$value) {
569
+				$valueObjectAttributes[$prefix . '_' . $key] = $value;
570
+				unset($valueObjectAttributes[$key]);
571
+			}
572 572
 
573
-            $attributes = array_merge($attributes, $valueObjectAttributes);
574
-        }
573
+			$attributes = array_merge($attributes, $valueObjectAttributes);
574
+		}
575 575
         
576
-        return $attributes;
577
-    }
578
-
579
-    /**
580
-     * Return's entity raw attributes in the state they were at last
581
-     * query.
582
-     *
583
-     * @param  array|null $columns
584
-     * @return array
585
-     */
586
-    protected function getCachedRawAttributes(array $columns = null)
587
-    {
588
-        $cachedAttributes = $this->getCache()->get($this->getEntityId());
589
-
590
-        if (is_null($columns)) {
591
-            return $cachedAttributes;
592
-        } else {
593
-            return array_only($cachedAttributes, $columns);
594
-        }
595
-    }
596
-
597
-    /**
598
-     * Return a single attribute from the cache
599
-     * @param  string $key
600
-     * @return mixed
601
-     */
602
-    protected function getCachedAttribute($key)
603
-    {
604
-        $cachedAttributes = $this->getCache()->get($this->getEntityId());
605
-
606
-        if (!array_key_exists($key, $cachedAttributes)) {
607
-            return null;
608
-        } else {
609
-            return $cachedAttributes[$key];
610
-        }
611
-    }
612
-
613
-    /**
614
-     * Convert related Entity's attributes to foreign keys
615
-     *
616
-     * @return array
617
-     */
618
-    protected function getForeignKeyAttributes()
619
-    {
620
-        $foreignKeys = [];
621
-
622
-        foreach ($this->entityMap->getLocalRelationships() as $relation) {
623
-            // check if relationship has been parsed, meaning it has an actual object
624
-            // in the entity's attributes
625
-            if ($this->isActualRelationships($relation)) {
626
-                $foreignKeys = $foreignKeys + $this->getForeignKeyAttributesFromRelation($relation);
627
-            }
628
-        }
629
-
630
-        if (!is_null($this->parent)) {
631
-            $foreignKeys = $foreignKeys + $this->getForeignKeyAttributesFromParent();
632
-        }
633
-
634
-        return $foreignKeys;
635
-    }
636
-
637
-    /**
638
-     * Return an associative array containing the key-value pair(s) from
639
-     * the related entity.
640
-     *
641
-     * @param  string $relation
642
-     * @return array
643
-     */
644
-    protected function getForeignKeyAttributesFromRelation($relation)
645
-    {
646
-        $localRelations = $this->entityMap->getLocalRelationships();
647
-
648
-        if (in_array($relation, $localRelations)) {
649
-            // Call Relationship's method
650
-            $relationship = $this->entityMap->$relation($this->getEntityObject());
651
-
652
-            $relatedAggregate = $this->relationships[$relation][0];
653
-
654
-            return $relationship->getForeignKeyValuePair($relatedAggregate->getEntityObject());
655
-        } else {
656
-            return [];
657
-        }
658
-    }
659
-
660
-    /**
661
-     * Get foreign key attribute(s) from a parent entity in this
662
-     * aggregate context
663
-     *
664
-     * @return array
665
-     */
666
-    protected function getForeignKeyAttributesFromParent()
667
-    {
668
-        $parentMap = $this->parent->getEntityMap();
669
-
670
-        $parentForeignRelations = $parentMap->getForeignRelationships();
671
-        $parentPivotRelations = $parentMap->getPivotRelationships();
672
-
673
-        $parentRelation = $this->parentRelationship;
674
-
675
-        if (in_array($parentRelation, $parentForeignRelations)
676
-            && !in_array($parentRelation, $parentPivotRelations)
677
-        ) {
678
-            $parentObject = $this->parent->getEntityObject();
679
-
680
-            // Call Relationship's method on parent map
681
-            $relationship = $parentMap->$parentRelation($parentObject);
682
-
683
-            return $relationship->getForeignKeyValuePair();
684
-        } else {
685
-            return [];
686
-        }
687
-    }
688
-
689
-    /**
690
-     * Update Pivot records on loaded relationships, by comparing the
691
-     * values from the Entity Cache to the actual relationship inside
692
-     * the aggregated entity.
693
-     *
694
-     * @return void
695
-     */
696
-    public function updatePivotRecords()
697
-    {
698
-        $pivots = $this->entityMap->getPivotRelationships();
699
-
700
-        foreach ($pivots as $pivot) {
701
-            if (array_key_exists($pivot, $this->relationships)) {
702
-                $this->updatePivotRelation($pivot);
703
-            }
704
-        }
705
-    }
706
-
707
-    /**
708
-     * Update Single pivot relationship
709
-     *
710
-     * @param  string $relation
711
-     * @return void
712
-     */
713
-    protected function updatePivotRelation($relation)
714
-    {
715
-        $hashes = $this->getEntityHashesFromRelation($relation);
716
-
717
-        $cachedAttributes = $this->getCachedRawAttributes();
718
-
719
-        if (array_key_exists($relation, $cachedAttributes)) {
720
-            // Compare the two array of hashes to find out existing
721
-            // pivot records, and the ones to be created.
722
-            $new = array_diff($hashes, array_keys($cachedAttributes[$relation]));
723
-            $existing = array_intersect($hashes, array_keys($cachedAttributes[$relation]));
724
-        } else {
725
-            $existing = [];
726
-            $new = $hashes;
727
-        }
728
-
729
-        if (count($new) > 0) {
730
-            $pivots = $this->getRelatedAggregatesFromHashes($new, $relation);
731
-
732
-            $this->entityMap->$relation($this->getEntityObject())->createPivots($pivots);
733
-        }
734
-
735
-        if (count($existing) > 0) {
736
-            foreach ($existing as $pivotHash) {
737
-                $this->updatePivotIfDirty($pivotHash, $relation);
738
-            }
739
-        }
740
-    }
741
-
742
-    /**
743
-     * Compare existing pivot record in cache and update it
744
-     * if the pivot attributes are dirty
745
-     *
746
-     * @param  string $pivotHash
747
-     * @param  string $relation
748
-     * @return void
749
-     */
750
-    protected function updatePivotIfDirty($pivotHash, $relation)
751
-    {
752
-        $aggregate = $this->getRelatedAggregateFromHash($pivotHash, $relation);
753
-
754
-        if ($aggregate->hasAttribute('pivot')) {
755
-            $pivot = $aggregate->getEntityAttribute('pivot')->getEntityAttributes();
756
-
757
-            $cachedPivotAttributes = $this->getPivotAttributesFromCache($pivotHash, $relation);
758
-
759
-            $actualPivotAttributes = array_only($pivot, array_keys($cachedPivotAttributes));
760
-
761
-            $dirty = $this->getDirtyAttributes($actualPivotAttributes, $cachedPivotAttributes);
762
-
763
-            if (count($dirty) > 0) {
764
-                $id = $aggregate->getEntityId();
765
-
766
-                $this->entityMap->$relation($this->getEntityObject())->updateExistingPivot($id, $dirty);
767
-            }
768
-        }
769
-    }
770
-
771
-    /**
772
-     * Compare two attributes array and return dirty attributes
773
-     *
774
-     * @param  array $actual
775
-     * @param  array $cached
776
-     * @return array
777
-     */
778
-    protected function getDirtyAttributes(array $actual, array $cached)
779
-    {
780
-        $dirty = [];
781
-
782
-        foreach ($actual as $key => $value) {
783
-            if (!$this->originalIsNumericallyEquivalent($value, $cached[$key])) {
784
-                $dirty[$key] = $actual[$key];
785
-            }
786
-        }
787
-
788
-        return $dirty;
789
-    }
790
-
791
-    /**
792
-     *
793
-     * @param  string $pivotHash
794
-     * @param  string $relation
795
-     * @return array
796
-     */
797
-    protected function getPivotAttributesFromCache($pivotHash, $relation)
798
-    {
799
-        $cachedAttributes = $this->getCachedRawAttributes();
800
-
801
-        $cachedRelations = $cachedAttributes[$relation];
802
-
803
-        foreach ($cachedRelations as $cachedRelation) {
804
-            if ($cachedRelation == $pivotHash) {
805
-                return $cachedRelation->getPivotAttributes();
806
-            }
807
-        }
808
-    }
809
-
810
-    /**
811
-     * Returns an array of related Aggregates from its entity hashes
812
-     *
813
-     * @param  array  $hashes
814
-     * @param  string $relation
815
-     * @return array
816
-     */
817
-    protected function getRelatedAggregatesFromHashes(array $hashes, $relation)
818
-    {
819
-        $related = [];
820
-
821
-        foreach ($hashes as $hash) {
822
-            $aggregate = $this->getRelatedAggregateFromHash($hash, $relation);
823
-
824
-            if (!is_null($aggregate)) {
825
-                $related[] = $aggregate;
826
-            }
827
-        }
828
-
829
-        return $related;
830
-    }
831
-
832
-    /**
833
-     * Get related aggregate from its hash
834
-     *
835
-     * @param  string $hash
836
-     * @param  string $relation
837
-     * @return \Analogue\ORM\System\Aggregate|null
838
-     */
839
-    protected function getRelatedAggregateFromHash($hash, $relation)
840
-    {
841
-        foreach ($this->relationships[$relation] as $aggregate) {
842
-            if ($aggregate->getEntityHash() == $hash) {
843
-                return $aggregate;
844
-            }
845
-        }
846
-        return null;
847
-    }
848
-
849
-    /**
850
-     * Return an array of Entity Hashes from a specific relation
851
-     *
852
-     * @param  string $relation
853
-     * @return array
854
-     */
855
-    protected function getEntityHashesFromRelation($relation)
856
-    {
857
-        return array_map(function ($aggregate) {
858
-            return $aggregate->getEntityHash();
859
-        }, $this->relationships[$relation]);
860
-    }
861
-
862
-    /**
863
-     * Check the existence of an actual relationship
864
-     *
865
-     * @param  string $relation
866
-     * @return boolean
867
-     */
868
-    protected function isActualRelationships($relation)
869
-    {
870
-        return array_key_exists($relation, $this->relationships)
871
-            && count($this->relationships[$relation]) > 0;
872
-    }
873
-
874
-    /**
875
-     * Return cache instance for the current entity type
876
-     *
877
-     * @return \Analogue\ORM\System\EntityCache
878
-     */
879
-    protected function getCache()
880
-    {
881
-        return $this->mapper->getEntityCache();
882
-    }
883
-
884
-    /**
885
-     * Get Only Raw Entiy's attributes which have been modified
886
-     * since last query
887
-     *
888
-     * @return array
889
-     */
890
-    public function getDirtyRawAttributes()
891
-    {
892
-        $attributes = $this->getRawAttributes();
893
-        $cachedAttributes = $this->getCachedRawAttributes(array_keys($attributes));
894
-
895
-        $dirty = [];
896
-
897
-        foreach ($attributes as $key => $value) {
898
-            if ($this->isRelation($key) || $key == 'pivot') {
899
-                continue;
900
-            }
901
-
902
-            if (!array_key_exists($key, $cachedAttributes) && !$value instanceof Pivot) {
903
-                $dirty[$key] = $value;
904
-            } elseif ($value !== $cachedAttributes[$key] &&
905
-                !$this->originalIsNumericallyEquivalent($value, $cachedAttributes[$key])) {
906
-                $dirty[$key] = $value;
907
-            }
908
-        }
909
-
910
-        return $dirty;
911
-    }
912
-
913
-    /**
914
-     * @param $key
915
-     * @return bool
916
-     */
917
-    protected function isRelation($key)
918
-    {
919
-        return in_array($key, $this->entityMap->getRelationships());
920
-    }
921
-
922
-    /**
923
-     * Determine if the new and old values for a given key are numerically equivalent.
924
-     *
925
-     * @param $current
926
-     * @param $original
927
-     * @return boolean
928
-     */
929
-    protected function originalIsNumericallyEquivalent($current, $original)
930
-    {
931
-        return is_numeric($current) && is_numeric($original) && strcmp((string) $current, (string) $original) === 0;
932
-    }
933
-
934
-    /**
935
-     * Get the underlying entity object
936
-     *
937
-     * @return mixed
938
-     */
939
-    public function getEntityObject()
940
-    {
941
-        return $this->wrappedEntity->getObject();
942
-    }
943
-
944
-    /**
945
-     * Return the Mapper instance for the current Entity Type
946
-     *
947
-     * @return \Analogue\ORM\System\Mapper
948
-     */
949
-    public function getMapper()
950
-    {
951
-        return $this->mapper;
952
-    }
953
-
954
-    /**
955
-     * Check that the entity already exists in the database, by checking
956
-     * if it has an EntityCache record
957
-     *
958
-     * @return boolean
959
-     */
960
-    public function exists()
961
-    {
962
-        return $this->getCache()->has($this->getEntityId());
963
-    }
964
-
965
-    /**
966
-     * Set the object attribute raw values (hydration)
967
-     *
968
-     * @param array $attributes
969
-     */
970
-    public function setEntityAttributes(array $attributes)
971
-    {
972
-        $this->wrappedEntity->setEntityAttributes($attributes);
973
-    }
974
-
975
-    /**
976
-     * Get the raw object's values.
977
-     *
978
-     * @return array
979
-     */
980
-    public function getEntityAttributes()
981
-    {
982
-        return $this->wrappedEntity->getEntityAttributes();
983
-    }
984
-
985
-    /**
986
-     * Set the raw entity attributes
987
-     * @param string $key
988
-     * @param string $value
989
-     */
990
-    public function setEntityAttribute($key, $value)
991
-    {
992
-        $this->wrappedEntity->setEntityAttribute($key, $value);
993
-    }
994
-
995
-    /**
996
-     * Return the entity's attribute
997
-     * @param  string $key
998
-     * @return mixed
999
-     */
1000
-    public function getEntityAttribute($key)
1001
-    {
1002
-        return $this->wrappedEntity->getEntityAttribute($key);
1003
-    }
1004
-
1005
-    /**
1006
-     * Does the attribute exists on the entity
1007
-     *
1008
-     * @param  string  $key
1009
-     * @return boolean
1010
-     */
1011
-    public function hasAttribute($key)
1012
-    {
1013
-        return $this->wrappedEntity->hasAttribute($key);
1014
-    }
1015
-
1016
-    /**
1017
-     * Set the lazyloading proxies on the wrapped entity
1018
-     */
1019
-    public function setProxies()
1020
-    {
1021
-        $this->wrappedEntity->setProxies();
1022
-    }
576
+		return $attributes;
577
+	}
578
+
579
+	/**
580
+	 * Return's entity raw attributes in the state they were at last
581
+	 * query.
582
+	 *
583
+	 * @param  array|null $columns
584
+	 * @return array
585
+	 */
586
+	protected function getCachedRawAttributes(array $columns = null)
587
+	{
588
+		$cachedAttributes = $this->getCache()->get($this->getEntityId());
589
+
590
+		if (is_null($columns)) {
591
+			return $cachedAttributes;
592
+		} else {
593
+			return array_only($cachedAttributes, $columns);
594
+		}
595
+	}
596
+
597
+	/**
598
+	 * Return a single attribute from the cache
599
+	 * @param  string $key
600
+	 * @return mixed
601
+	 */
602
+	protected function getCachedAttribute($key)
603
+	{
604
+		$cachedAttributes = $this->getCache()->get($this->getEntityId());
605
+
606
+		if (!array_key_exists($key, $cachedAttributes)) {
607
+			return null;
608
+		} else {
609
+			return $cachedAttributes[$key];
610
+		}
611
+	}
612
+
613
+	/**
614
+	 * Convert related Entity's attributes to foreign keys
615
+	 *
616
+	 * @return array
617
+	 */
618
+	protected function getForeignKeyAttributes()
619
+	{
620
+		$foreignKeys = [];
621
+
622
+		foreach ($this->entityMap->getLocalRelationships() as $relation) {
623
+			// check if relationship has been parsed, meaning it has an actual object
624
+			// in the entity's attributes
625
+			if ($this->isActualRelationships($relation)) {
626
+				$foreignKeys = $foreignKeys + $this->getForeignKeyAttributesFromRelation($relation);
627
+			}
628
+		}
629
+
630
+		if (!is_null($this->parent)) {
631
+			$foreignKeys = $foreignKeys + $this->getForeignKeyAttributesFromParent();
632
+		}
633
+
634
+		return $foreignKeys;
635
+	}
636
+
637
+	/**
638
+	 * Return an associative array containing the key-value pair(s) from
639
+	 * the related entity.
640
+	 *
641
+	 * @param  string $relation
642
+	 * @return array
643
+	 */
644
+	protected function getForeignKeyAttributesFromRelation($relation)
645
+	{
646
+		$localRelations = $this->entityMap->getLocalRelationships();
647
+
648
+		if (in_array($relation, $localRelations)) {
649
+			// Call Relationship's method
650
+			$relationship = $this->entityMap->$relation($this->getEntityObject());
651
+
652
+			$relatedAggregate = $this->relationships[$relation][0];
653
+
654
+			return $relationship->getForeignKeyValuePair($relatedAggregate->getEntityObject());
655
+		} else {
656
+			return [];
657
+		}
658
+	}
659
+
660
+	/**
661
+	 * Get foreign key attribute(s) from a parent entity in this
662
+	 * aggregate context
663
+	 *
664
+	 * @return array
665
+	 */
666
+	protected function getForeignKeyAttributesFromParent()
667
+	{
668
+		$parentMap = $this->parent->getEntityMap();
669
+
670
+		$parentForeignRelations = $parentMap->getForeignRelationships();
671
+		$parentPivotRelations = $parentMap->getPivotRelationships();
672
+
673
+		$parentRelation = $this->parentRelationship;
674
+
675
+		if (in_array($parentRelation, $parentForeignRelations)
676
+			&& !in_array($parentRelation, $parentPivotRelations)
677
+		) {
678
+			$parentObject = $this->parent->getEntityObject();
679
+
680
+			// Call Relationship's method on parent map
681
+			$relationship = $parentMap->$parentRelation($parentObject);
682
+
683
+			return $relationship->getForeignKeyValuePair();
684
+		} else {
685
+			return [];
686
+		}
687
+	}
688
+
689
+	/**
690
+	 * Update Pivot records on loaded relationships, by comparing the
691
+	 * values from the Entity Cache to the actual relationship inside
692
+	 * the aggregated entity.
693
+	 *
694
+	 * @return void
695
+	 */
696
+	public function updatePivotRecords()
697
+	{
698
+		$pivots = $this->entityMap->getPivotRelationships();
699
+
700
+		foreach ($pivots as $pivot) {
701
+			if (array_key_exists($pivot, $this->relationships)) {
702
+				$this->updatePivotRelation($pivot);
703
+			}
704
+		}
705
+	}
706
+
707
+	/**
708
+	 * Update Single pivot relationship
709
+	 *
710
+	 * @param  string $relation
711
+	 * @return void
712
+	 */
713
+	protected function updatePivotRelation($relation)
714
+	{
715
+		$hashes = $this->getEntityHashesFromRelation($relation);
716
+
717
+		$cachedAttributes = $this->getCachedRawAttributes();
718
+
719
+		if (array_key_exists($relation, $cachedAttributes)) {
720
+			// Compare the two array of hashes to find out existing
721
+			// pivot records, and the ones to be created.
722
+			$new = array_diff($hashes, array_keys($cachedAttributes[$relation]));
723
+			$existing = array_intersect($hashes, array_keys($cachedAttributes[$relation]));
724
+		} else {
725
+			$existing = [];
726
+			$new = $hashes;
727
+		}
728
+
729
+		if (count($new) > 0) {
730
+			$pivots = $this->getRelatedAggregatesFromHashes($new, $relation);
731
+
732
+			$this->entityMap->$relation($this->getEntityObject())->createPivots($pivots);
733
+		}
734
+
735
+		if (count($existing) > 0) {
736
+			foreach ($existing as $pivotHash) {
737
+				$this->updatePivotIfDirty($pivotHash, $relation);
738
+			}
739
+		}
740
+	}
741
+
742
+	/**
743
+	 * Compare existing pivot record in cache and update it
744
+	 * if the pivot attributes are dirty
745
+	 *
746
+	 * @param  string $pivotHash
747
+	 * @param  string $relation
748
+	 * @return void
749
+	 */
750
+	protected function updatePivotIfDirty($pivotHash, $relation)
751
+	{
752
+		$aggregate = $this->getRelatedAggregateFromHash($pivotHash, $relation);
753
+
754
+		if ($aggregate->hasAttribute('pivot')) {
755
+			$pivot = $aggregate->getEntityAttribute('pivot')->getEntityAttributes();
756
+
757
+			$cachedPivotAttributes = $this->getPivotAttributesFromCache($pivotHash, $relation);
758
+
759
+			$actualPivotAttributes = array_only($pivot, array_keys($cachedPivotAttributes));
760
+
761
+			$dirty = $this->getDirtyAttributes($actualPivotAttributes, $cachedPivotAttributes);
762
+
763
+			if (count($dirty) > 0) {
764
+				$id = $aggregate->getEntityId();
765
+
766
+				$this->entityMap->$relation($this->getEntityObject())->updateExistingPivot($id, $dirty);
767
+			}
768
+		}
769
+	}
770
+
771
+	/**
772
+	 * Compare two attributes array and return dirty attributes
773
+	 *
774
+	 * @param  array $actual
775
+	 * @param  array $cached
776
+	 * @return array
777
+	 */
778
+	protected function getDirtyAttributes(array $actual, array $cached)
779
+	{
780
+		$dirty = [];
781
+
782
+		foreach ($actual as $key => $value) {
783
+			if (!$this->originalIsNumericallyEquivalent($value, $cached[$key])) {
784
+				$dirty[$key] = $actual[$key];
785
+			}
786
+		}
787
+
788
+		return $dirty;
789
+	}
790
+
791
+	/**
792
+	 *
793
+	 * @param  string $pivotHash
794
+	 * @param  string $relation
795
+	 * @return array
796
+	 */
797
+	protected function getPivotAttributesFromCache($pivotHash, $relation)
798
+	{
799
+		$cachedAttributes = $this->getCachedRawAttributes();
800
+
801
+		$cachedRelations = $cachedAttributes[$relation];
802
+
803
+		foreach ($cachedRelations as $cachedRelation) {
804
+			if ($cachedRelation == $pivotHash) {
805
+				return $cachedRelation->getPivotAttributes();
806
+			}
807
+		}
808
+	}
809
+
810
+	/**
811
+	 * Returns an array of related Aggregates from its entity hashes
812
+	 *
813
+	 * @param  array  $hashes
814
+	 * @param  string $relation
815
+	 * @return array
816
+	 */
817
+	protected function getRelatedAggregatesFromHashes(array $hashes, $relation)
818
+	{
819
+		$related = [];
820
+
821
+		foreach ($hashes as $hash) {
822
+			$aggregate = $this->getRelatedAggregateFromHash($hash, $relation);
823
+
824
+			if (!is_null($aggregate)) {
825
+				$related[] = $aggregate;
826
+			}
827
+		}
828
+
829
+		return $related;
830
+	}
831
+
832
+	/**
833
+	 * Get related aggregate from its hash
834
+	 *
835
+	 * @param  string $hash
836
+	 * @param  string $relation
837
+	 * @return \Analogue\ORM\System\Aggregate|null
838
+	 */
839
+	protected function getRelatedAggregateFromHash($hash, $relation)
840
+	{
841
+		foreach ($this->relationships[$relation] as $aggregate) {
842
+			if ($aggregate->getEntityHash() == $hash) {
843
+				return $aggregate;
844
+			}
845
+		}
846
+		return null;
847
+	}
848
+
849
+	/**
850
+	 * Return an array of Entity Hashes from a specific relation
851
+	 *
852
+	 * @param  string $relation
853
+	 * @return array
854
+	 */
855
+	protected function getEntityHashesFromRelation($relation)
856
+	{
857
+		return array_map(function ($aggregate) {
858
+			return $aggregate->getEntityHash();
859
+		}, $this->relationships[$relation]);
860
+	}
861
+
862
+	/**
863
+	 * Check the existence of an actual relationship
864
+	 *
865
+	 * @param  string $relation
866
+	 * @return boolean
867
+	 */
868
+	protected function isActualRelationships($relation)
869
+	{
870
+		return array_key_exists($relation, $this->relationships)
871
+			&& count($this->relationships[$relation]) > 0;
872
+	}
873
+
874
+	/**
875
+	 * Return cache instance for the current entity type
876
+	 *
877
+	 * @return \Analogue\ORM\System\EntityCache
878
+	 */
879
+	protected function getCache()
880
+	{
881
+		return $this->mapper->getEntityCache();
882
+	}
883
+
884
+	/**
885
+	 * Get Only Raw Entiy's attributes which have been modified
886
+	 * since last query
887
+	 *
888
+	 * @return array
889
+	 */
890
+	public function getDirtyRawAttributes()
891
+	{
892
+		$attributes = $this->getRawAttributes();
893
+		$cachedAttributes = $this->getCachedRawAttributes(array_keys($attributes));
894
+
895
+		$dirty = [];
896
+
897
+		foreach ($attributes as $key => $value) {
898
+			if ($this->isRelation($key) || $key == 'pivot') {
899
+				continue;
900
+			}
901
+
902
+			if (!array_key_exists($key, $cachedAttributes) && !$value instanceof Pivot) {
903
+				$dirty[$key] = $value;
904
+			} elseif ($value !== $cachedAttributes[$key] &&
905
+				!$this->originalIsNumericallyEquivalent($value, $cachedAttributes[$key])) {
906
+				$dirty[$key] = $value;
907
+			}
908
+		}
909
+
910
+		return $dirty;
911
+	}
912
+
913
+	/**
914
+	 * @param $key
915
+	 * @return bool
916
+	 */
917
+	protected function isRelation($key)
918
+	{
919
+		return in_array($key, $this->entityMap->getRelationships());
920
+	}
921
+
922
+	/**
923
+	 * Determine if the new and old values for a given key are numerically equivalent.
924
+	 *
925
+	 * @param $current
926
+	 * @param $original
927
+	 * @return boolean
928
+	 */
929
+	protected function originalIsNumericallyEquivalent($current, $original)
930
+	{
931
+		return is_numeric($current) && is_numeric($original) && strcmp((string) $current, (string) $original) === 0;
932
+	}
933
+
934
+	/**
935
+	 * Get the underlying entity object
936
+	 *
937
+	 * @return mixed
938
+	 */
939
+	public function getEntityObject()
940
+	{
941
+		return $this->wrappedEntity->getObject();
942
+	}
943
+
944
+	/**
945
+	 * Return the Mapper instance for the current Entity Type
946
+	 *
947
+	 * @return \Analogue\ORM\System\Mapper
948
+	 */
949
+	public function getMapper()
950
+	{
951
+		return $this->mapper;
952
+	}
953
+
954
+	/**
955
+	 * Check that the entity already exists in the database, by checking
956
+	 * if it has an EntityCache record
957
+	 *
958
+	 * @return boolean
959
+	 */
960
+	public function exists()
961
+	{
962
+		return $this->getCache()->has($this->getEntityId());
963
+	}
964
+
965
+	/**
966
+	 * Set the object attribute raw values (hydration)
967
+	 *
968
+	 * @param array $attributes
969
+	 */
970
+	public function setEntityAttributes(array $attributes)
971
+	{
972
+		$this->wrappedEntity->setEntityAttributes($attributes);
973
+	}
974
+
975
+	/**
976
+	 * Get the raw object's values.
977
+	 *
978
+	 * @return array
979
+	 */
980
+	public function getEntityAttributes()
981
+	{
982
+		return $this->wrappedEntity->getEntityAttributes();
983
+	}
984
+
985
+	/**
986
+	 * Set the raw entity attributes
987
+	 * @param string $key
988
+	 * @param string $value
989
+	 */
990
+	public function setEntityAttribute($key, $value)
991
+	{
992
+		$this->wrappedEntity->setEntityAttribute($key, $value);
993
+	}
994
+
995
+	/**
996
+	 * Return the entity's attribute
997
+	 * @param  string $key
998
+	 * @return mixed
999
+	 */
1000
+	public function getEntityAttribute($key)
1001
+	{
1002
+		return $this->wrappedEntity->getEntityAttribute($key);
1003
+	}
1004
+
1005
+	/**
1006
+	 * Does the attribute exists on the entity
1007
+	 *
1008
+	 * @param  string  $key
1009
+	 * @return boolean
1010
+	 */
1011
+	public function hasAttribute($key)
1012
+	{
1013
+		return $this->wrappedEntity->hasAttribute($key);
1014
+	}
1015
+
1016
+	/**
1017
+	 * Set the lazyloading proxies on the wrapped entity
1018
+	 */
1019
+	public function setProxies()
1020
+	{
1021
+		$this->wrappedEntity->setProxies();
1022
+	}
1023 1023
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -350,7 +350,7 @@  discard block
 block discarded – undo
350 350
      */
351 351
     public function getEntityHash()
352 352
     {
353
-        return $this->getEntityClass() . '.' . $this->getEntityId();
353
+        return $this->getEntityClass().'.'.$this->getEntityId();
354 354
     }
355 355
 
356 356
     /**
@@ -566,7 +566,7 @@  discard block
 block discarded – undo
566 566
             $prefix = snake_case(class_basename($embed));
567 567
 
568 568
             foreach ($valueObjectAttributes as $key=>$value) {
569
-                $valueObjectAttributes[$prefix . '_' . $key] = $value;
569
+                $valueObjectAttributes[$prefix.'_'.$key] = $value;
570 570
                 unset($valueObjectAttributes[$key]);
571 571
             }
572 572
 
@@ -854,7 +854,7 @@  discard block
 block discarded – undo
854 854
      */
855 855
     protected function getEntityHashesFromRelation($relation)
856 856
     {
857
-        return array_map(function ($aggregate) {
857
+        return array_map(function($aggregate) {
858 858
             return $aggregate->getEntityHash();
859 859
         }, $this->relationships[$relation]);
860 860
     }
Please login to merge, or discard this patch.
src/System/ScopeInterface.php 1 patch
Indentation   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -4,19 +4,19 @@
 block discarded – undo
4 4
 
5 5
 interface ScopeInterface
6 6
 {
7
-    /**
8
-     * Apply the scope to a given Query builder.
9
-     *
10
-     * @param  \Analogue\ORM\System\Query $builder
11
-     * @return void
12
-     */
13
-    public function apply(Query $builder);
7
+	/**
8
+	 * Apply the scope to a given Query builder.
9
+	 *
10
+	 * @param  \Analogue\ORM\System\Query $builder
11
+	 * @return void
12
+	 */
13
+	public function apply(Query $builder);
14 14
 
15
-    /**
16
-     * Remove the scope from the Query builder.
17
-     *
18
-     * @param  \Analogue\ORM\System\Query $builder
19
-     * @return void
20
-     */
21
-    public function remove(Query $builder);
15
+	/**
16
+	 * Remove the scope from the Query builder.
17
+	 *
18
+	 * @param  \Analogue\ORM\System\Query $builder
19
+	 * @return void
20
+	 */
21
+	public function remove(Query $builder);
22 22
 }
Please login to merge, or discard this patch.
src/System/CachedRelationship.php 1 patch
Indentation   +58 added lines, -58 removed lines patch added patch discarded remove patch
@@ -8,68 +8,68 @@
 block discarded – undo
8 8
  */
9 9
 class CachedRelationship
10 10
 {
11
-    /**
12
-     * The Hash of the related entity
13
-     *
14
-     * @var string
15
-     */
16
-    protected $hash;
11
+	/**
12
+	 * The Hash of the related entity
13
+	 *
14
+	 * @var string
15
+	 */
16
+	protected $hash;
17 17
 
18
-    /**
19
-     * Pivot attributes, if any
20
-     *
21
-     * @var array
22
-     */
23
-    protected $pivotAttributes;
18
+	/**
19
+	 * Pivot attributes, if any
20
+	 *
21
+	 * @var array
22
+	 */
23
+	protected $pivotAttributes;
24 24
 
25
-    /**
26
-     * CachedRelationship constructor.
27
-     * @param $hash
28
-     * @param array $pivotAttributes
29
-     */
30
-    public function __construct($hash, $pivotAttributes = [])
31
-    {
32
-        $this->hash = $hash;
33
-        $this->pivotAttributes = $pivotAttributes;
34
-    }
25
+	/**
26
+	 * CachedRelationship constructor.
27
+	 * @param $hash
28
+	 * @param array $pivotAttributes
29
+	 */
30
+	public function __construct($hash, $pivotAttributes = [])
31
+	{
32
+		$this->hash = $hash;
33
+		$this->pivotAttributes = $pivotAttributes;
34
+	}
35 35
 
36
-    /**
37
-     * Return true if any pivot attributes are present
38
-     *
39
-     * @return boolean
40
-     */
41
-    public function hasPivotAttributes()
42
-    {
43
-        return count($this->pivotAttributes) > 0;
44
-    }
36
+	/**
37
+	 * Return true if any pivot attributes are present
38
+	 *
39
+	 * @return boolean
40
+	 */
41
+	public function hasPivotAttributes()
42
+	{
43
+		return count($this->pivotAttributes) > 0;
44
+	}
45 45
 
46
-    /**
47
-     * Returns the hash of the related entity
48
-     *
49
-     * @return string
50
-     */
51
-    public function getHash()
52
-    {
53
-        return $this->hash;
54
-    }
46
+	/**
47
+	 * Returns the hash of the related entity
48
+	 *
49
+	 * @return string
50
+	 */
51
+	public function getHash()
52
+	{
53
+		return $this->hash;
54
+	}
55 55
 
56
-    /**
57
-     * Get the cached values for the pivot attributes
58
-     *
59
-     * @return array
60
-     */
61
-    public function getPivotAttributes()
62
-    {
63
-        return $this->pivotAttributes;
64
-    }
56
+	/**
57
+	 * Get the cached values for the pivot attributes
58
+	 *
59
+	 * @return array
60
+	 */
61
+	public function getPivotAttributes()
62
+	{
63
+		return $this->pivotAttributes;
64
+	}
65 65
 
66
-    /**
67
-     * Access to the hash for fast cache comparison
68
-     *
69
-     * @return string
70
-     */
71
-    public function __toString()
72
-    {
73
-        return $this->hash;
74
-    }
66
+	/**
67
+	 * Access to the hash for fast cache comparison
68
+	 *
69
+	 * @return string
70
+	 */
71
+	public function __toString()
72
+	{
73
+		return $this->hash;
74
+	}
75 75
 }
Please login to merge, or discard this patch.
src/System/Manager.php 2 patches
Indentation   +431 added lines, -431 removed lines patch added patch discarded remove patch
@@ -15,439 +15,439 @@
 block discarded – undo
15 15
  */
16 16
 class Manager
17 17
 {
18
-    /**
19
-     * Driver Manager
20
-     *
21
-     * @var \Analogue\ORM\Drivers\Manager
22
-     */
23
-    protected $drivers;
24
-
25
-    /**
26
-     * Registered entity classes and corresponding map objects.
27
-     *
28
-     * @var array
29
-     */
30
-    protected $entityClasses = [];
31
-
32
-    /**
33
-     * Key value store of ValueObject Classes and corresponding map classes
34
-     *
35
-     * @var array
36
-     */
37
-    protected $valueClasses = [];
38
-
39
-    /**
40
-     * Loaded Mappers
41
-     *
42
-     * @var array
43
-     */
44
-    protected $mappers = [];
45
-
46
-    /**
47
-     * Loaded Repositories
48
-     *
49
-     * @var array
50
-     */
51
-    protected $repositories = [];
52
-
53
-    /**
54
-     * Event dispatcher instance
55
-     *
56
-     * @var \Illuminate\Contracts\Events\Dispatcher
57
-     */
58
-    protected $eventDispatcher;
59
-
60
-    /**
61
-     * Manager instance
62
-     *
63
-     * @var Manager
64
-     */
65
-    protected static $instance;
66
-
67
-    /**
68
-     * Available Analogue Events
69
-     *
70
-     * @var array
71
-     */
72
-    protected $events = [
73
-        'initializing',
74
-        'initialized',
75
-        'store',
76
-        'stored',
77
-        'creating',
78
-        'created',
79
-        'updating',
80
-        'updated',
81
-        'deleting',
82
-        'deleted',
83
-    ];
84
-
85
-    /**
86
-     * @param \Analogue\ORM\Drivers\Manager $driverManager
87
-     * @param Dispatcher $event
88
-     */
89
-    public function __construct(DriverManager $driverManager, Dispatcher $event)
90
-    {
91
-        $this->drivers = $driverManager;
92
-
93
-        $this->eventDispatcher = $event;
94
-
95
-        static::$instance = $this;
96
-    }
97
-
98
-    /**
99
-     * Return the Driver Manager's instance
100
-     *
101
-     * @return \Analogue\ORM\Drivers\Manager
102
-     */
103
-    public function getDriverManager()
104
-    {
105
-        return $this->drivers;
106
-    }
107
-
108
-    /**
109
-     * Create a mapper for a given entity
110
-     *
111
-     * @param  \Analogue\ORM\Mappable|string $entity
112
-     * @param  mixed                         $entityMap
113
-     * @throws MappingException
114
-     * @return Mapper
115
-     */
116
-    public function mapper($entity, $entityMap = null)
117
-    {
118
-        if ($entity instanceof Wrapper) {
119
-            throw new MappingException('Tried to instantiate mapper on wrapped Entity');
120
-        }
121
-
122
-        if (!is_string($entity)) {
123
-            $entity = get_class($entity);
124
-        }
125
-
126
-        // Return existing mapper instance if exists.
127
-        if (array_key_exists($entity, $this->mappers)) {
128
-            return $this->mappers[$entity];
129
-        } else {
130
-            return $this->buildMapper($entity, $entityMap);
131
-        }
132
-    }
133
-
134
-    /**
135
-     * Build a new Mapper instance for a given Entity
136
-     *
137
-     * @param  string $entity
138
-     * @param         $entityMap
139
-     * @throws MappingException
140
-     * @return Mapper
141
-     */
142
-    protected function buildMapper($entity, $entityMap)
143
-    {
144
-        // If an EntityMap hasn't been manually registered by the user
145
-        // register it at runtime.
146
-        if (!$this->isRegisteredEntity($entity)) {
147
-            $this->register($entity, $entityMap);
148
-        }
149
-
150
-        $entityMap = $this->entityClasses[$entity];
151
-
152
-        $factory = new MapperFactory($this->drivers, $this->eventDispatcher, $this);
153
-
154
-        $mapper = $factory->make($entity, $entityMap);
155
-
156
-        $this->mappers[$entity] = $mapper;
157
-
158
-        // At this point we can safely call the boot() method on the entityMap as
159
-        // the mapper is now instantiated & registered within the manager.
160
-
161
-        $mapper->getEntityMap()->boot();
18
+	/**
19
+	 * Driver Manager
20
+	 *
21
+	 * @var \Analogue\ORM\Drivers\Manager
22
+	 */
23
+	protected $drivers;
24
+
25
+	/**
26
+	 * Registered entity classes and corresponding map objects.
27
+	 *
28
+	 * @var array
29
+	 */
30
+	protected $entityClasses = [];
31
+
32
+	/**
33
+	 * Key value store of ValueObject Classes and corresponding map classes
34
+	 *
35
+	 * @var array
36
+	 */
37
+	protected $valueClasses = [];
38
+
39
+	/**
40
+	 * Loaded Mappers
41
+	 *
42
+	 * @var array
43
+	 */
44
+	protected $mappers = [];
45
+
46
+	/**
47
+	 * Loaded Repositories
48
+	 *
49
+	 * @var array
50
+	 */
51
+	protected $repositories = [];
52
+
53
+	/**
54
+	 * Event dispatcher instance
55
+	 *
56
+	 * @var \Illuminate\Contracts\Events\Dispatcher
57
+	 */
58
+	protected $eventDispatcher;
59
+
60
+	/**
61
+	 * Manager instance
62
+	 *
63
+	 * @var Manager
64
+	 */
65
+	protected static $instance;
66
+
67
+	/**
68
+	 * Available Analogue Events
69
+	 *
70
+	 * @var array
71
+	 */
72
+	protected $events = [
73
+		'initializing',
74
+		'initialized',
75
+		'store',
76
+		'stored',
77
+		'creating',
78
+		'created',
79
+		'updating',
80
+		'updated',
81
+		'deleting',
82
+		'deleted',
83
+	];
84
+
85
+	/**
86
+	 * @param \Analogue\ORM\Drivers\Manager $driverManager
87
+	 * @param Dispatcher $event
88
+	 */
89
+	public function __construct(DriverManager $driverManager, Dispatcher $event)
90
+	{
91
+		$this->drivers = $driverManager;
92
+
93
+		$this->eventDispatcher = $event;
94
+
95
+		static::$instance = $this;
96
+	}
97
+
98
+	/**
99
+	 * Return the Driver Manager's instance
100
+	 *
101
+	 * @return \Analogue\ORM\Drivers\Manager
102
+	 */
103
+	public function getDriverManager()
104
+	{
105
+		return $this->drivers;
106
+	}
107
+
108
+	/**
109
+	 * Create a mapper for a given entity
110
+	 *
111
+	 * @param  \Analogue\ORM\Mappable|string $entity
112
+	 * @param  mixed                         $entityMap
113
+	 * @throws MappingException
114
+	 * @return Mapper
115
+	 */
116
+	public function mapper($entity, $entityMap = null)
117
+	{
118
+		if ($entity instanceof Wrapper) {
119
+			throw new MappingException('Tried to instantiate mapper on wrapped Entity');
120
+		}
121
+
122
+		if (!is_string($entity)) {
123
+			$entity = get_class($entity);
124
+		}
125
+
126
+		// Return existing mapper instance if exists.
127
+		if (array_key_exists($entity, $this->mappers)) {
128
+			return $this->mappers[$entity];
129
+		} else {
130
+			return $this->buildMapper($entity, $entityMap);
131
+		}
132
+	}
133
+
134
+	/**
135
+	 * Build a new Mapper instance for a given Entity
136
+	 *
137
+	 * @param  string $entity
138
+	 * @param         $entityMap
139
+	 * @throws MappingException
140
+	 * @return Mapper
141
+	 */
142
+	protected function buildMapper($entity, $entityMap)
143
+	{
144
+		// If an EntityMap hasn't been manually registered by the user
145
+		// register it at runtime.
146
+		if (!$this->isRegisteredEntity($entity)) {
147
+			$this->register($entity, $entityMap);
148
+		}
149
+
150
+		$entityMap = $this->entityClasses[$entity];
151
+
152
+		$factory = new MapperFactory($this->drivers, $this->eventDispatcher, $this);
153
+
154
+		$mapper = $factory->make($entity, $entityMap);
155
+
156
+		$this->mappers[$entity] = $mapper;
157
+
158
+		// At this point we can safely call the boot() method on the entityMap as
159
+		// the mapper is now instantiated & registered within the manager.
160
+
161
+		$mapper->getEntityMap()->boot();
162 162
         
163
-        return $mapper;
164
-    }
165
-
166
-    /**
167
-     * Create a mapper for a given entity (static alias)
168
-     *
169
-     * @param  \Analogue\ORM\Mappable|string $entity
170
-     * @param  null|EntityMap                $entityMap
171
-     * @throws MappingException
172
-     * @return Mapper
173
-     */
174
-    public static function getMapper($entity, $entityMap = null)
175
-    {
176
-        return static::$instance->mapper($entity, $entityMap);
177
-    }
178
-
179
-    /**
180
-     * Get the Repository instance for the given Entity
181
-     *
182
-     * @param  \Analogue\ORM\Mappable|string $entity
183
-     * @throws \InvalidArgumentException
184
-     * @throws MappingException
185
-     * @return \Analogue\ORM\Repository
186
-     */
187
-    public function repository($entity)
188
-    {
189
-        if (!is_string($entity)) {
190
-            $entity = get_class($entity);
191
-        }
192
-
193
-        // First we check if the repository is not already created.
194
-        if (array_key_exists($entity, $this->repositories)) {
195
-            return $this->repositories[$entity];
196
-        }
197
-
198
-        $this->repositories[$entity] = new Repository($this->mapper($entity));
163
+		return $mapper;
164
+	}
165
+
166
+	/**
167
+	 * Create a mapper for a given entity (static alias)
168
+	 *
169
+	 * @param  \Analogue\ORM\Mappable|string $entity
170
+	 * @param  null|EntityMap                $entityMap
171
+	 * @throws MappingException
172
+	 * @return Mapper
173
+	 */
174
+	public static function getMapper($entity, $entityMap = null)
175
+	{
176
+		return static::$instance->mapper($entity, $entityMap);
177
+	}
178
+
179
+	/**
180
+	 * Get the Repository instance for the given Entity
181
+	 *
182
+	 * @param  \Analogue\ORM\Mappable|string $entity
183
+	 * @throws \InvalidArgumentException
184
+	 * @throws MappingException
185
+	 * @return \Analogue\ORM\Repository
186
+	 */
187
+	public function repository($entity)
188
+	{
189
+		if (!is_string($entity)) {
190
+			$entity = get_class($entity);
191
+		}
192
+
193
+		// First we check if the repository is not already created.
194
+		if (array_key_exists($entity, $this->repositories)) {
195
+			return $this->repositories[$entity];
196
+		}
197
+
198
+		$this->repositories[$entity] = new Repository($this->mapper($entity));
199 199
         
200
-        return $this->repositories[$entity];
201
-    }
202
-
203
-    /**
204
-     * Register an entity
205
-     *
206
-     * @param  string|\Analogue\ORM\Mappable $entity    entity's class name
207
-     * @param  string|EntityMap              $entityMap map's class name
208
-     * @throws MappingException
209
-     * @return void
210
-     */
211
-    public function register($entity, $entityMap = null)
212
-    {
213
-        // If an object is provider, get the class name from it
214
-        if (!is_string($entity)) {
215
-            $entity = get_class($entity);
216
-        }
217
-
218
-        if ($this->isRegisteredEntity($entity)) {
219
-            throw new MappingException("Entity $entity is already registered.");
220
-        }
221
-
222
-        if (!class_exists($entity)) {
223
-            throw new MappingException("Class $entity does not exists");
224
-        }
225
-
226
-        if (is_null($entityMap)) {
227
-            $entityMap = $this->getEntityMapInstanceFor($entity);
228
-        }
229
-
230
-        if (is_string($entityMap)) {
231
-            $entityMap = new $entityMap;
232
-        }
233
-
234
-        if (!$entityMap instanceof EntityMap) {
235
-            throw new MappingException(get_class($entityMap) . ' must be an instance of EntityMap.');
236
-        }
237
-
238
-        $entityMap->setClass($entity);
239
-
240
-        $entityMap->setManager($this);
241
-
242
-        $this->entityClasses[$entity] = $entityMap;
243
-    }
244
-
245
-    /**
246
-     * Get the entity map instance for a custom entity
247
-     *
248
-     * @param  string   $entity
249
-     * @return \Analogue\ORM\Mappable
250
-     */
251
-    protected function getEntityMapInstanceFor($entity)
252
-    {
253
-        if (class_exists($entity . 'Map')) {
254
-            $map = $entity . 'Map';
255
-            $map = new $map;
256
-        } else {
257
-            // Generate an EntityMap object
258
-            $map = $this->getNewEntityMap();
259
-        }
200
+		return $this->repositories[$entity];
201
+	}
202
+
203
+	/**
204
+	 * Register an entity
205
+	 *
206
+	 * @param  string|\Analogue\ORM\Mappable $entity    entity's class name
207
+	 * @param  string|EntityMap              $entityMap map's class name
208
+	 * @throws MappingException
209
+	 * @return void
210
+	 */
211
+	public function register($entity, $entityMap = null)
212
+	{
213
+		// If an object is provider, get the class name from it
214
+		if (!is_string($entity)) {
215
+			$entity = get_class($entity);
216
+		}
217
+
218
+		if ($this->isRegisteredEntity($entity)) {
219
+			throw new MappingException("Entity $entity is already registered.");
220
+		}
221
+
222
+		if (!class_exists($entity)) {
223
+			throw new MappingException("Class $entity does not exists");
224
+		}
225
+
226
+		if (is_null($entityMap)) {
227
+			$entityMap = $this->getEntityMapInstanceFor($entity);
228
+		}
229
+
230
+		if (is_string($entityMap)) {
231
+			$entityMap = new $entityMap;
232
+		}
233
+
234
+		if (!$entityMap instanceof EntityMap) {
235
+			throw new MappingException(get_class($entityMap) . ' must be an instance of EntityMap.');
236
+		}
237
+
238
+		$entityMap->setClass($entity);
239
+
240
+		$entityMap->setManager($this);
241
+
242
+		$this->entityClasses[$entity] = $entityMap;
243
+	}
244
+
245
+	/**
246
+	 * Get the entity map instance for a custom entity
247
+	 *
248
+	 * @param  string   $entity
249
+	 * @return \Analogue\ORM\Mappable
250
+	 */
251
+	protected function getEntityMapInstanceFor($entity)
252
+	{
253
+		if (class_exists($entity . 'Map')) {
254
+			$map = $entity . 'Map';
255
+			$map = new $map;
256
+		} else {
257
+			// Generate an EntityMap object
258
+			$map = $this->getNewEntityMap();
259
+		}
260 260
         
261
-        return $map;
262
-    }
263
-
264
-    /**
265
-     * Dynamically create an entity map for a custom entity class
266
-     *
267
-     * @return EntityMap
268
-     */
269
-    protected function getNewEntityMap()
270
-    {
271
-        return new EntityMap;
272
-    }
273
-
274
-    /**
275
-     * Register a Value Object
276
-     *
277
-     * @param  string $valueObject
278
-     * @param  string $valueMap
279
-     * @throws MappingException
280
-     * @return void
281
-     */
282
-    public function registerValueObject($valueObject, $valueMap = null)
283
-    {
284
-        if (!is_string($valueObject)) {
285
-            $valueObject = get_class($valueObject);
286
-        }
287
-
288
-        if (is_null($valueMap)) {
289
-            $valueMap = $valueObject . 'Map';
290
-        }
291
-
292
-        if (!class_exists($valueMap)) {
293
-            throw new MappingException("$valueMap doesn't exists");
294
-        }
295
-
296
-        $this->valueClasses[$valueObject] = $valueMap;
297
-    }
298
-
299
-    /**
300
-     * Return true is the object is registered as value object
301
-     *
302
-     * @param  mixed $object
303
-     * @return boolean
304
-     */
305
-    public function isValueObject($object)
306
-    {
307
-        if (!is_string($object)) {
308
-            $object = get_class($object);
309
-        }
310
-
311
-        return array_key_exists($object, $this->valueClasses);
312
-    }
313
-
314
-    /**
315
-     * Get the Value Map for a given Value Object Class
316
-     *
317
-     * @param  string $valueObject
318
-     * @throws MappingException
319
-     * @return \Analogue\ORM\ValueMap
320
-     */
321
-    public function getValueMap($valueObject)
322
-    {
323
-        if (!is_string($valueObject)) {
324
-            $valueObject = get_class($valueObject);
325
-        }
326
-
327
-        if (!array_key_exists($valueObject, $this->valueClasses)) {
328
-            $this->registerValueObject($valueObject);
329
-        }
330
-        $valueMap = new $this->valueClasses[$valueObject];
331
-
332
-        $valueMap->setClass($valueObject);
333
-
334
-        return $valueMap;
335
-    }
336
-
337
-    /**
338
-     * Instantiate a new Value Object instance
339
-     *
340
-     * @param  string $valueObject
341
-     * @return \Analogue\ORM\ValueObject
342
-     */
343
-    public function getValueObjectInstance($valueObject)
344
-    {
345
-        $prototype = unserialize(sprintf('O:%d:"%s":0:{}', strlen($valueObject), $valueObject));
346
-        return $prototype;
347
-    }
348
-
349
-    /**
350
-     * Register Analogue Plugin
351
-     *
352
-     * @param  string $plugin class
353
-     * @return void
354
-     */
355
-    public function registerPlugin($plugin)
356
-    {
357
-        $plugin = new $plugin($this);
358
-
359
-        $this->events = array_merge($this->events, $plugin->getCustomEvents());
360
-
361
-        $plugin->register();
362
-    }
363
-
364
-    /**
365
-     * Check if the entity is already registered
366
-     *
367
-     * @param  string|object $entity
368
-     * @return boolean
369
-     */
370
-    public function isRegisteredEntity($entity)
371
-    {
372
-        if (!is_string($entity)) {
373
-            $entity = get_class($entity);
374
-        }
375
-
376
-        return array_key_exists($entity, $this->entityClasses);
377
-    }
378
-
379
-    /**
380
-     * Register event listeners that will be fired regardless the type
381
-     * of the entity.
382
-     *
383
-     * @param  string $event
384
-     * @param  \Closure $callback
385
-     * @throws \Exception
386
-     * @return void
387
-     */
388
-    public function registerGlobalEvent($event, $callback)
389
-    {
390
-        if (!in_array($event, $this->events)) {
391
-            throw new \Exception("Analogue : Event $event doesn't exist");
392
-        }
393
-        $this->eventDispatcher->listen("analogue.{$event}.*", $callback);
394
-    }
395
-
396
-    /**
397
-     * Shortcut to Mapper store
398
-     *
399
-     * @param  mixed $entity
400
-     * @throws MappingException
401
-     * @return mixed
402
-     */
403
-    public function store($entity)
404
-    {
405
-        return $this->mapper($entity)->store($entity);
406
-    }
407
-
408
-    /**
409
-     * Shortcut to Mapper delete
410
-     *
411
-     * @param  mixed $entity
412
-     * @throws MappingException
413
-     * @return \Illuminate\Support\Collection|null
414
-     */
415
-    public function delete($entity)
416
-    {
417
-        return $this->mapper($entity)->delete($entity);
418
-    }
419
-
420
-    /**
421
-     * Shortcut to Mapper query
422
-     *
423
-     * @param  mixed $entity
424
-     * @throws MappingException
425
-     * @return Query
426
-     */
427
-    public function query($entity)
428
-    {
429
-        return $this->mapper($entity)->query();
430
-    }
431
-
432
-    /**
433
-     * Shortcut to Mapper Global Query
434
-     *
435
-     * @param  mixed $entity
436
-     * @throws MappingException
437
-     * @return Query
438
-     */
439
-    public function globalQuery($entity)
440
-    {
441
-        return $this->mapper($entity)->globalQuery();
442
-    }
261
+		return $map;
262
+	}
263
+
264
+	/**
265
+	 * Dynamically create an entity map for a custom entity class
266
+	 *
267
+	 * @return EntityMap
268
+	 */
269
+	protected function getNewEntityMap()
270
+	{
271
+		return new EntityMap;
272
+	}
273
+
274
+	/**
275
+	 * Register a Value Object
276
+	 *
277
+	 * @param  string $valueObject
278
+	 * @param  string $valueMap
279
+	 * @throws MappingException
280
+	 * @return void
281
+	 */
282
+	public function registerValueObject($valueObject, $valueMap = null)
283
+	{
284
+		if (!is_string($valueObject)) {
285
+			$valueObject = get_class($valueObject);
286
+		}
287
+
288
+		if (is_null($valueMap)) {
289
+			$valueMap = $valueObject . 'Map';
290
+		}
291
+
292
+		if (!class_exists($valueMap)) {
293
+			throw new MappingException("$valueMap doesn't exists");
294
+		}
295
+
296
+		$this->valueClasses[$valueObject] = $valueMap;
297
+	}
298
+
299
+	/**
300
+	 * Return true is the object is registered as value object
301
+	 *
302
+	 * @param  mixed $object
303
+	 * @return boolean
304
+	 */
305
+	public function isValueObject($object)
306
+	{
307
+		if (!is_string($object)) {
308
+			$object = get_class($object);
309
+		}
310
+
311
+		return array_key_exists($object, $this->valueClasses);
312
+	}
313
+
314
+	/**
315
+	 * Get the Value Map for a given Value Object Class
316
+	 *
317
+	 * @param  string $valueObject
318
+	 * @throws MappingException
319
+	 * @return \Analogue\ORM\ValueMap
320
+	 */
321
+	public function getValueMap($valueObject)
322
+	{
323
+		if (!is_string($valueObject)) {
324
+			$valueObject = get_class($valueObject);
325
+		}
326
+
327
+		if (!array_key_exists($valueObject, $this->valueClasses)) {
328
+			$this->registerValueObject($valueObject);
329
+		}
330
+		$valueMap = new $this->valueClasses[$valueObject];
331
+
332
+		$valueMap->setClass($valueObject);
333
+
334
+		return $valueMap;
335
+	}
336
+
337
+	/**
338
+	 * Instantiate a new Value Object instance
339
+	 *
340
+	 * @param  string $valueObject
341
+	 * @return \Analogue\ORM\ValueObject
342
+	 */
343
+	public function getValueObjectInstance($valueObject)
344
+	{
345
+		$prototype = unserialize(sprintf('O:%d:"%s":0:{}', strlen($valueObject), $valueObject));
346
+		return $prototype;
347
+	}
348
+
349
+	/**
350
+	 * Register Analogue Plugin
351
+	 *
352
+	 * @param  string $plugin class
353
+	 * @return void
354
+	 */
355
+	public function registerPlugin($plugin)
356
+	{
357
+		$plugin = new $plugin($this);
358
+
359
+		$this->events = array_merge($this->events, $plugin->getCustomEvents());
360
+
361
+		$plugin->register();
362
+	}
363
+
364
+	/**
365
+	 * Check if the entity is already registered
366
+	 *
367
+	 * @param  string|object $entity
368
+	 * @return boolean
369
+	 */
370
+	public function isRegisteredEntity($entity)
371
+	{
372
+		if (!is_string($entity)) {
373
+			$entity = get_class($entity);
374
+		}
375
+
376
+		return array_key_exists($entity, $this->entityClasses);
377
+	}
378
+
379
+	/**
380
+	 * Register event listeners that will be fired regardless the type
381
+	 * of the entity.
382
+	 *
383
+	 * @param  string $event
384
+	 * @param  \Closure $callback
385
+	 * @throws \Exception
386
+	 * @return void
387
+	 */
388
+	public function registerGlobalEvent($event, $callback)
389
+	{
390
+		if (!in_array($event, $this->events)) {
391
+			throw new \Exception("Analogue : Event $event doesn't exist");
392
+		}
393
+		$this->eventDispatcher->listen("analogue.{$event}.*", $callback);
394
+	}
395
+
396
+	/**
397
+	 * Shortcut to Mapper store
398
+	 *
399
+	 * @param  mixed $entity
400
+	 * @throws MappingException
401
+	 * @return mixed
402
+	 */
403
+	public function store($entity)
404
+	{
405
+		return $this->mapper($entity)->store($entity);
406
+	}
407
+
408
+	/**
409
+	 * Shortcut to Mapper delete
410
+	 *
411
+	 * @param  mixed $entity
412
+	 * @throws MappingException
413
+	 * @return \Illuminate\Support\Collection|null
414
+	 */
415
+	public function delete($entity)
416
+	{
417
+		return $this->mapper($entity)->delete($entity);
418
+	}
419
+
420
+	/**
421
+	 * Shortcut to Mapper query
422
+	 *
423
+	 * @param  mixed $entity
424
+	 * @throws MappingException
425
+	 * @return Query
426
+	 */
427
+	public function query($entity)
428
+	{
429
+		return $this->mapper($entity)->query();
430
+	}
431
+
432
+	/**
433
+	 * Shortcut to Mapper Global Query
434
+	 *
435
+	 * @param  mixed $entity
436
+	 * @throws MappingException
437
+	 * @return Query
438
+	 */
439
+	public function globalQuery($entity)
440
+	{
441
+		return $this->mapper($entity)->globalQuery();
442
+	}
443 443
     
444
-    /**
445
-     * Return the Singleton instance of the manager
446
-     *
447
-     * @return Manager
448
-     */
449
-    public static function getInstance()
450
-    {
451
-        return static::$instance;
452
-    }
444
+	/**
445
+	 * Return the Singleton instance of the manager
446
+	 *
447
+	 * @return Manager
448
+	 */
449
+	public static function getInstance()
450
+	{
451
+		return static::$instance;
452
+	}
453 453
 }
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -232,7 +232,7 @@  discard block
 block discarded – undo
232 232
         }
233 233
 
234 234
         if (!$entityMap instanceof EntityMap) {
235
-            throw new MappingException(get_class($entityMap) . ' must be an instance of EntityMap.');
235
+            throw new MappingException(get_class($entityMap).' must be an instance of EntityMap.');
236 236
         }
237 237
 
238 238
         $entityMap->setClass($entity);
@@ -250,8 +250,8 @@  discard block
 block discarded – undo
250 250
      */
251 251
     protected function getEntityMapInstanceFor($entity)
252 252
     {
253
-        if (class_exists($entity . 'Map')) {
254
-            $map = $entity . 'Map';
253
+        if (class_exists($entity.'Map')) {
254
+            $map = $entity.'Map';
255 255
             $map = new $map;
256 256
         } else {
257 257
             // Generate an EntityMap object
@@ -286,7 +286,7 @@  discard block
 block discarded – undo
286 286
         }
287 287
 
288 288
         if (is_null($valueMap)) {
289
-            $valueMap = $valueObject . 'Map';
289
+            $valueMap = $valueObject.'Map';
290 290
         }
291 291
 
292 292
         if (!class_exists($valueMap)) {
Please login to merge, or discard this patch.
src/System/Wrappers/EntityWrapper.php 1 patch
Indentation   +68 added lines, -68 removed lines patch added patch discarded remove patch
@@ -7,79 +7,79 @@
 block discarded – undo
7 7
  */
8 8
 class EntityWrapper extends Wrapper
9 9
 {
10
-    /**
11
-     * Method used by the mapper to set the object
12
-     * attribute raw values (hydration)
13
-     *
14
-     * @param array $attributes
15
-     *
16
-     * @return void
17
-     */
18
-    public function setEntityAttributes(array $attributes)
19
-    {
20
-        $this->entity->setEntityAttributes($attributes);
21
-    }
10
+	/**
11
+	 * Method used by the mapper to set the object
12
+	 * attribute raw values (hydration)
13
+	 *
14
+	 * @param array $attributes
15
+	 *
16
+	 * @return void
17
+	 */
18
+	public function setEntityAttributes(array $attributes)
19
+	{
20
+		$this->entity->setEntityAttributes($attributes);
21
+	}
22 22
 
23
-    /**
24
-     * Method used by the mapper to get the
25
-     * raw object's values.
26
-     *
27
-     * @return array
28
-     */
29
-    public function getEntityAttributes()
30
-    {
31
-        return $this->entity->getEntityAttributes();
32
-    }
23
+	/**
24
+	 * Method used by the mapper to get the
25
+	 * raw object's values.
26
+	 *
27
+	 * @return array
28
+	 */
29
+	public function getEntityAttributes()
30
+	{
31
+		return $this->entity->getEntityAttributes();
32
+	}
33 33
 
34
-    /**
35
-     * Method used by the mapper to set raw
36
-     * key-value pair
37
-     *
38
-     * @param string $key
39
-     * @param string $value
40
-     *
41
-     * @return void
42
-     */
43
-    public function setEntityAttribute($key, $value)
44
-    {
45
-        $attributes = $this->entity->getEntityAttributes();
34
+	/**
35
+	 * Method used by the mapper to set raw
36
+	 * key-value pair
37
+	 *
38
+	 * @param string $key
39
+	 * @param string $value
40
+	 *
41
+	 * @return void
42
+	 */
43
+	public function setEntityAttribute($key, $value)
44
+	{
45
+		$attributes = $this->entity->getEntityAttributes();
46 46
 
47
-        $attributes[$key] = $value;
47
+		$attributes[$key] = $value;
48 48
 
49
-        $this->entity->setEntityAttributes($attributes);
50
-    }
49
+		$this->entity->setEntityAttributes($attributes);
50
+	}
51 51
 
52
-    /**
53
-     * Method used by the mapper to get single
54
-     * key-value pair
55
-     *
56
-     * @param  string $key
57
-     * @return mixed|null
58
-     */
59
-    public function getEntityAttribute($key)
60
-    {
61
-        if ($this->hasAttribute($key)) {
62
-            $attributes = $this->entity->getEntityAttributes();
63
-            return $attributes[$key];
64
-        } else {
65
-            return null;
66
-        }
67
-    }
52
+	/**
53
+	 * Method used by the mapper to get single
54
+	 * key-value pair
55
+	 *
56
+	 * @param  string $key
57
+	 * @return mixed|null
58
+	 */
59
+	public function getEntityAttribute($key)
60
+	{
61
+		if ($this->hasAttribute($key)) {
62
+			$attributes = $this->entity->getEntityAttributes();
63
+			return $attributes[$key];
64
+		} else {
65
+			return null;
66
+		}
67
+	}
68 68
 
69
-    /**
70
-     * Test if a given attribute exists
71
-     *
72
-     * @param  string $key
73
-     * @return boolean
74
-     */
75
-    public function hasAttribute($key)
76
-    {
77
-        $attributes = $this->entity->getEntityAttributes();
69
+	/**
70
+	 * Test if a given attribute exists
71
+	 *
72
+	 * @param  string $key
73
+	 * @return boolean
74
+	 */
75
+	public function hasAttribute($key)
76
+	{
77
+		$attributes = $this->entity->getEntityAttributes();
78 78
 
79
-        if (array_key_exists($key, $attributes)) {
80
-            return true;
81
-        } else {
82
-            return false;
83
-        }
84
-    }
79
+		if (array_key_exists($key, $attributes)) {
80
+			return true;
81
+		} else {
82
+			return false;
83
+		}
84
+	}
85 85
 }
Please login to merge, or discard this patch.