Completed
Pull Request — 5.1 (#82)
by Maxim
02:47
created
src/Relationships/MorphPivot.php 2 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -26,7 +26,7 @@
 block discarded – undo
26 26
     /**
27 27
      * Set the keys for a save update query.
28 28
      *
29
-     * @param  \Analogue\ORM\Query  $query
29
+     * @param  Query  $query
30 30
      * @return \Analogue\ORM\Query
31 31
      */
32 32
     protected function setKeysForSaveQuery(Query $query)
Please login to merge, or discard this patch.
Indentation   +49 added lines, -49 removed lines patch added patch discarded remove patch
@@ -5,60 +5,60 @@
 block discarded – undo
5 5
 class MorphPivot extends Pivot
6 6
 {
7 7
 
8
-    /**
9
-     * The type of the polymorphic relation.
10
-     *
11
-     * Explicitly define this so it's not included in saved attributes.
12
-     *
13
-     * @var string
14
-     */
15
-    protected $morphType;
8
+	/**
9
+	 * The type of the polymorphic relation.
10
+	 *
11
+	 * Explicitly define this so it's not included in saved attributes.
12
+	 *
13
+	 * @var string
14
+	 */
15
+	protected $morphType;
16 16
 
17
-    /**
18
-     * The value of the polymorphic relation.
19
-     *
20
-     * Explicitly define this so it's not included in saved attributes.
21
-     *
22
-     * @var string
23
-     */
24
-    protected $morphClass;
17
+	/**
18
+	 * The value of the polymorphic relation.
19
+	 *
20
+	 * Explicitly define this so it's not included in saved attributes.
21
+	 *
22
+	 * @var string
23
+	 */
24
+	protected $morphClass;
25 25
 
26
-    /**
27
-     * Set the keys for a save update query.
28
-     *
29
-     * @param  \Analogue\ORM\Query  $query
30
-     * @return \Analogue\ORM\Query
31
-     */
32
-    protected function setKeysForSaveQuery(Query $query)
33
-    {
34
-        $query->where($this->morphType, $this->morphClass);
26
+	/**
27
+	 * Set the keys for a save update query.
28
+	 *
29
+	 * @param  \Analogue\ORM\Query  $query
30
+	 * @return \Analogue\ORM\Query
31
+	 */
32
+	protected function setKeysForSaveQuery(Query $query)
33
+	{
34
+		$query->where($this->morphType, $this->morphClass);
35 35
 
36
-        return parent::setKeysForSaveQuery($query);
37
-    }
36
+		return parent::setKeysForSaveQuery($query);
37
+	}
38 38
 
39
-    /**
40
-     * Set the morph type for the pivot.
41
-     *
42
-     * @param  string  $morphType
43
-     * @return $this
44
-     */
45
-    public function setMorphType($morphType)
46
-    {
47
-        $this->morphType = $morphType;
39
+	/**
40
+	 * Set the morph type for the pivot.
41
+	 *
42
+	 * @param  string  $morphType
43
+	 * @return $this
44
+	 */
45
+	public function setMorphType($morphType)
46
+	{
47
+		$this->morphType = $morphType;
48 48
 
49
-        return $this;
50
-    }
49
+		return $this;
50
+	}
51 51
 
52
-    /**
53
-     * Set the morph class for the pivot.
54
-     *
55
-     * @param  string  $morphClass
56
-     * @return \Analogue\ORM\Relationships\MorphPivot
57
-     */
58
-    public function setMorphClass($morphClass)
59
-    {
60
-        $this->morphClass = $morphClass;
52
+	/**
53
+	 * Set the morph class for the pivot.
54
+	 *
55
+	 * @param  string  $morphClass
56
+	 * @return \Analogue\ORM\Relationships\MorphPivot
57
+	 */
58
+	public function setMorphClass($morphClass)
59
+	{
60
+		$this->morphClass = $morphClass;
61 61
 
62
-        return $this;
63
-    }
62
+		return $this;
63
+	}
64 64
 }
Please login to merge, or discard this patch.
src/Relationships/MorphTo.php 4 patches
Doc Comments   +1 added lines, -2 removed lines patch added patch discarded remove patch
@@ -46,7 +46,6 @@  discard block
 block discarded – undo
46 46
     /**
47 47
      * Create a new belongs to relationship instance.
48 48
      *
49
-     * @param  \Analogue\ORM\System\Query  $query
50 49
      * @param  Mappable  $parent
51 50
      * @param  string  $foreignKey
52 51
      * @param  string  $otherKey
@@ -157,7 +156,7 @@  discard block
 block discarded – undo
157 156
      * Gather all of the foreign keys for a given type.
158 157
      *
159 158
      * @param  string  $type
160
-     * @return array
159
+     * @return BaseCollection
161 160
      */
162 161
     protected function gatherKeysByType($type)
163 162
     {
Please login to merge, or discard this patch.
Indentation   +207 added lines, -207 removed lines patch added patch discarded remove patch
@@ -8,224 +8,224 @@
 block discarded – undo
8 8
 class MorphTo extends BelongsTo
9 9
 {
10 10
 
11
-    /**
12
-     * The type of the polymorphic relation.
13
-     *
14
-     * @var string
15
-     */
16
-    protected $morphType;
17
-
18
-    /**
19
-     * The entities whose relations are being eager loaded.
20
-     *
21
-     * @var \Analogue\ORM\EntityCollection
22
-     */
23
-    protected $entities;
24
-
25
-    /**
26
-     * All of the models keyed by ID.
27
-     *
28
-     * @var array
29
-     */
30
-    protected $dictionary = array();
31
-
32
-    /*
11
+	/**
12
+	 * The type of the polymorphic relation.
13
+	 *
14
+	 * @var string
15
+	 */
16
+	protected $morphType;
17
+
18
+	/**
19
+	 * The entities whose relations are being eager loaded.
20
+	 *
21
+	 * @var \Analogue\ORM\EntityCollection
22
+	 */
23
+	protected $entities;
24
+
25
+	/**
26
+	 * All of the models keyed by ID.
27
+	 *
28
+	 * @var array
29
+	 */
30
+	protected $dictionary = array();
31
+
32
+	/*
33 33
      * Indicates if soft-deleted model instances should be fetched.
34 34
      *
35 35
      * @var bool
36 36
      */
37
-    protected $withTrashed = false;
38
-
39
-    /**
40
-     * Indicate if the parent entity hold the key for the relation.
41
-     *
42
-     * @var boolean
43
-     */
44
-    protected static $ownForeignKey = true;
45
-
46
-    /**
47
-     * Create a new belongs to relationship instance.
48
-     *
49
-     * @param  \Analogue\ORM\System\Query  $query
50
-     * @param  Mappable  $parent
51
-     * @param  string  $foreignKey
52
-     * @param  string  $otherKey
53
-     * @param  string  $type
54
-     * @param  string  $relation
55
-     * @return void
56
-     */
57
-    public function __construct(Mapper $mapper, $parent, $foreignKey, $otherKey, $type, $relation)
58
-    {
59
-        $this->morphType = $type;
60
-
61
-        parent::__construct($mapper, $parent, $foreignKey, $otherKey, $relation);
62
-    }
63
-
64
-    /**
65
-     * Set the constraints for an eager load of the relation.
66
-     *
67
-     * @param  array  $entities
68
-     * @return void
69
-     */
70
-    public function addEagerConstraints(array $entities)
71
-    {
72
-        $this->buildDictionary($this->entities = EntityCollection::make($entities));
73
-    }
74
-
75
-    /**
76
-     * Build a dictionary with the entities
77
-     *
78
-     * @param  \Analogue\ORM\EntityCollection  $entities
79
-     * @return void
80
-     */
81
-    protected function buildDictionary(EntityCollection $entities)
82
-    {
83
-        foreach ($entities as $entity) {
84
-            if ($entity->getEntityAttribute($this->morphType)) {
85
-                $this->dictionary[$entity->getEntityAttribute($this->morphType)][$entity->getEntityAttribute($this->foreignKey)][] = $entity;
86
-            }
87
-        }
88
-    }
89
-
90
-    /**
91
-     * Match the eagerly loaded results to their parents.
92
-     *
93
-     * @param  array   $entities
94
-     * @param  \Analogue\ORM\EntityCollection  $results
95
-     * @param  string  $relation
96
-     * @return array
97
-     */
98
-    public function match(array $entities, EntityCollection $results, $relation)
99
-    {
100
-        return $entities;
101
-    }
102
-
103
-    /*
37
+	protected $withTrashed = false;
38
+
39
+	/**
40
+	 * Indicate if the parent entity hold the key for the relation.
41
+	 *
42
+	 * @var boolean
43
+	 */
44
+	protected static $ownForeignKey = true;
45
+
46
+	/**
47
+	 * Create a new belongs to relationship instance.
48
+	 *
49
+	 * @param  \Analogue\ORM\System\Query  $query
50
+	 * @param  Mappable  $parent
51
+	 * @param  string  $foreignKey
52
+	 * @param  string  $otherKey
53
+	 * @param  string  $type
54
+	 * @param  string  $relation
55
+	 * @return void
56
+	 */
57
+	public function __construct(Mapper $mapper, $parent, $foreignKey, $otherKey, $type, $relation)
58
+	{
59
+		$this->morphType = $type;
60
+
61
+		parent::__construct($mapper, $parent, $foreignKey, $otherKey, $relation);
62
+	}
63
+
64
+	/**
65
+	 * Set the constraints for an eager load of the relation.
66
+	 *
67
+	 * @param  array  $entities
68
+	 * @return void
69
+	 */
70
+	public function addEagerConstraints(array $entities)
71
+	{
72
+		$this->buildDictionary($this->entities = EntityCollection::make($entities));
73
+	}
74
+
75
+	/**
76
+	 * Build a dictionary with the entities
77
+	 *
78
+	 * @param  \Analogue\ORM\EntityCollection  $entities
79
+	 * @return void
80
+	 */
81
+	protected function buildDictionary(EntityCollection $entities)
82
+	{
83
+		foreach ($entities as $entity) {
84
+			if ($entity->getEntityAttribute($this->morphType)) {
85
+				$this->dictionary[$entity->getEntityAttribute($this->morphType)][$entity->getEntityAttribute($this->foreignKey)][] = $entity;
86
+			}
87
+		}
88
+	}
89
+
90
+	/**
91
+	 * Match the eagerly loaded results to their parents.
92
+	 *
93
+	 * @param  array   $entities
94
+	 * @param  \Analogue\ORM\EntityCollection  $results
95
+	 * @param  string  $relation
96
+	 * @return array
97
+	 */
98
+	public function match(array $entities, EntityCollection $results, $relation)
99
+	{
100
+		return $entities;
101
+	}
102
+
103
+	/*
104 104
      * Get the results of the relationship.
105 105
      *
106 106
      * @return mixed
107 107
      */
108
-    public function getEager()
109
-    {
110
-        foreach (array_keys($this->dictionary) as $type) {
111
-            $this->matchToMorphParents($type, $this->getResultsByType($type));
112
-        }
113
-
114
-        return $this->entities;
115
-    }
116
-
117
-    /**
118
-     * Match the results for a given type to their parents.
119
-     *
120
-     * @param  string  $type
121
-     * @param  \Analogue\ORM\EntityCollection  $results
122
-     * @return void
123
-     */
124
-    protected function matchToMorphParents($type, EntityCollection $results)
125
-    {
126
-        foreach ($results as $result) {
127
-            if (isset($this->dictionary[$type][$result->getEntityKey()])) {
128
-                foreach ($this->dictionary[$type][$result->getEntityKey()] as $entity) {
129
-                    $entity->setEntityAttribute($this->relation, $result);
130
-                }
131
-            }
132
-        }
133
-    }
134
-
135
-    /**
136
-     * Get all of the relation results for a type.
137
-     *
138
-     * @param  string  $type
139
-     * @return \Analogue\ORM\EntityCollection
140
-     */
141
-    protected function getResultsByType($type)
142
-    {
143
-        $mapper = $this->relatedMapper->getManager()->mapper($type);
144
-
145
-        $map = $mapper->getEntityMap();
146
-
147
-        $key = $map->getKeyName();
148
-
149
-        $query = $mapper->getQuery();
150
-
151
-        //$query = $this->useWithTrashed($query);
152
-
153
-        return $query->whereIn($key, $this->gatherKeysByType($type)->all())->get();
154
-    }
155
-
156
-    /**
157
-     * Gather all of the foreign keys for a given type.
158
-     *
159
-     * @param  string  $type
160
-     * @return array
161
-     */
162
-    protected function gatherKeysByType($type)
163
-    {
164
-        $foreign = $this->foreignKey;
165
-
166
-        return BaseCollection::make($this->dictionary[$type])->map(function ($entities) use ($foreign) {
167
-            return head($entities)->{$foreign};
168
-
169
-        })->unique();
170
-    }
171
-
172
-    /**
173
-     * Get the foreign key of the relationship.
174
-     *
175
-     * @return string
176
-     */
177
-    /*public function getForeignKey()
108
+	public function getEager()
109
+	{
110
+		foreach (array_keys($this->dictionary) as $type) {
111
+			$this->matchToMorphParents($type, $this->getResultsByType($type));
112
+		}
113
+
114
+		return $this->entities;
115
+	}
116
+
117
+	/**
118
+	 * Match the results for a given type to their parents.
119
+	 *
120
+	 * @param  string  $type
121
+	 * @param  \Analogue\ORM\EntityCollection  $results
122
+	 * @return void
123
+	 */
124
+	protected function matchToMorphParents($type, EntityCollection $results)
125
+	{
126
+		foreach ($results as $result) {
127
+			if (isset($this->dictionary[$type][$result->getEntityKey()])) {
128
+				foreach ($this->dictionary[$type][$result->getEntityKey()] as $entity) {
129
+					$entity->setEntityAttribute($this->relation, $result);
130
+				}
131
+			}
132
+		}
133
+	}
134
+
135
+	/**
136
+	 * Get all of the relation results for a type.
137
+	 *
138
+	 * @param  string  $type
139
+	 * @return \Analogue\ORM\EntityCollection
140
+	 */
141
+	protected function getResultsByType($type)
142
+	{
143
+		$mapper = $this->relatedMapper->getManager()->mapper($type);
144
+
145
+		$map = $mapper->getEntityMap();
146
+
147
+		$key = $map->getKeyName();
148
+
149
+		$query = $mapper->getQuery();
150
+
151
+		//$query = $this->useWithTrashed($query);
152
+
153
+		return $query->whereIn($key, $this->gatherKeysByType($type)->all())->get();
154
+	}
155
+
156
+	/**
157
+	 * Gather all of the foreign keys for a given type.
158
+	 *
159
+	 * @param  string  $type
160
+	 * @return array
161
+	 */
162
+	protected function gatherKeysByType($type)
163
+	{
164
+		$foreign = $this->foreignKey;
165
+
166
+		return BaseCollection::make($this->dictionary[$type])->map(function ($entities) use ($foreign) {
167
+			return head($entities)->{$foreign};
168
+
169
+		})->unique();
170
+	}
171
+
172
+	/**
173
+	 * Get the foreign key of the relationship.
174
+	 *
175
+	 * @return string
176
+	 */
177
+	/*public function getForeignKey()
178 178
     {
179 179
         return $this->foreignKey;
180 180
     }*/
181 181
 
182
-    /**
183
-     * Associate the model instance to the given parent.
184
-     *
185
-     * @param  mixed $entity
186
-     * @return void
187
-     */
188
-    public function associate($entity)
189
-    {
190
-        // The Mapper will retrieve this association within the object model, we won't be using
191
-        // the foreign key attribute inside the parent Entity.
192
-        //
193
-        //$this->parent->setEntityAttribute($this->foreignKey, $entity->getEntityAttribute($this->otherKey));
194
-        //
195
-        // Instead, we'll just add the object to the Entity's attribute
182
+	/**
183
+	 * Associate the model instance to the given parent.
184
+	 *
185
+	 * @param  mixed $entity
186
+	 * @return void
187
+	 */
188
+	public function associate($entity)
189
+	{
190
+		// The Mapper will retrieve this association within the object model, we won't be using
191
+		// the foreign key attribute inside the parent Entity.
192
+		//
193
+		//$this->parent->setEntityAttribute($this->foreignKey, $entity->getEntityAttribute($this->otherKey));
194
+		//
195
+		// Instead, we'll just add the object to the Entity's attribute
196 196
         
197
-        $this->parent->setEntityAttribute($this->relation, $entity);
198
-    }
199
-
200
-    /**
201
-     * Get the foreign key value pair for a related object
202
-     *
203
-     * @var mixed $related
204
-     *
205
-     * @return array
206
-     */
207
-    public function getForeignKeyValuePair($related)
208
-    {
209
-        $foreignKey = $this->getForeignKey();
210
-
211
-        if ($related) {
212
-            $wrapper = $this->factory->make($related);
213
-
214
-            $relatedKey = $this->relatedMap->getKeyName();
215
-
216
-            return [$foreignKey => $wrapper->getEntityAttribute($relatedKey)];
217
-        } else {
218
-            return [$foreignKey => null];
219
-        }
220
-    }
221
-
222
-    /**
223
-     * Get the dictionary used by the relationship.
224
-     *
225
-     * @return array
226
-     */
227
-    public function getDictionary()
228
-    {
229
-        return $this->dictionary;
230
-    }
197
+		$this->parent->setEntityAttribute($this->relation, $entity);
198
+	}
199
+
200
+	/**
201
+	 * Get the foreign key value pair for a related object
202
+	 *
203
+	 * @var mixed $related
204
+	 *
205
+	 * @return array
206
+	 */
207
+	public function getForeignKeyValuePair($related)
208
+	{
209
+		$foreignKey = $this->getForeignKey();
210
+
211
+		if ($related) {
212
+			$wrapper = $this->factory->make($related);
213
+
214
+			$relatedKey = $this->relatedMap->getKeyName();
215
+
216
+			return [$foreignKey => $wrapper->getEntityAttribute($relatedKey)];
217
+		} else {
218
+			return [$foreignKey => null];
219
+		}
220
+	}
221
+
222
+	/**
223
+	 * Get the dictionary used by the relationship.
224
+	 *
225
+	 * @return array
226
+	 */
227
+	public function getDictionary()
228
+	{
229
+		return $this->dictionary;
230
+	}
231 231
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -163,7 +163,7 @@
 block discarded – undo
163 163
     {
164 164
         $foreign = $this->foreignKey;
165 165
 
166
-        return BaseCollection::make($this->dictionary[$type])->map(function ($entities) use ($foreign) {
166
+        return BaseCollection::make($this->dictionary[$type])->map(function($entities) use ($foreign) {
167 167
             return head($entities)->{$foreign};
168 168
 
169 169
         })->unique();
Please login to merge, or discard this patch.
Unused Use Statements   -1 removed lines patch added patch discarded remove patch
@@ -5,7 +5,6 @@
 block discarded – undo
5 5
 use Analogue\ORM\Mappable;
6 6
 use Analogue\ORM\System\Query;
7 7
 use Analogue\ORM\System\Mapper;
8
-use Analogue\ORM\System\Manager;
9 8
 use Analogue\ORM\EntityCollection;
10 9
 use Analogue\ORM\System\InternallyMappable;
11 10
 use Analogue\ORM\System\Wrappers\Factory;
Please login to merge, or discard this patch.
src/Relationships/MorphToMany.php 2 patches
Doc Comments   +3 added lines, -4 removed lines patch added patch discarded remove patch
@@ -34,7 +34,6 @@  discard block
 block discarded – undo
34 34
     /**
35 35
      * Create a new has many relationship instance.
36 36
      *
37
-     * @param  \Analogue\ORM\Query  $query
38 37
      * @param  \Analogue\ORM\Entityl  $parent
39 38
      * @param  string  $name
40 39
      * @param  string  $table
@@ -72,9 +71,9 @@  discard block
 block discarded – undo
72 71
     /**
73 72
      * Add the constraints for a relationship count query.
74 73
      *
75
-     * @param  \Analogue\ORM\Query  $query
76
-     * @param  \Analogue\ORM\Query  $parent
77
-     * @return \Analogue\ORM\Query
74
+     * @param  Query  $query
75
+     * @param  Query  $parent
76
+     * @return Query
78 77
      */
79 78
     public function getRelationCountQuery(Query $query, Query $parent)
80 79
     {
Please login to merge, or discard this patch.
Indentation   +162 added lines, -162 removed lines patch added patch discarded remove patch
@@ -6,167 +6,167 @@
 block discarded – undo
6 6
 class MorphToMany extends BelongsToMany
7 7
 {
8 8
 
9
-    /**
10
-     * The type of the polymorphic relation.
11
-     *
12
-     * @var string
13
-     */
14
-    protected $morphType;
15
-
16
-    /**
17
-     * The class name of the morph type constraint.
18
-     *
19
-     * @var string
20
-     */
21
-    protected $morphClass;
22
-
23
-    /**
24
-     * Indicates if we are connecting the inverse of the relation.
25
-     *
26
-     * This primarily affects the morphClass constraint.
27
-     *
28
-     * @var bool
29
-     */
30
-    protected $inverse;
31
-
32
-    protected static $hasPivot = true;
33
-
34
-    /**
35
-     * Create a new has many relationship instance.
36
-     *
37
-     * @param  \Analogue\ORM\Query  $query
38
-     * @param  \Analogue\ORM\Entityl  $parent
39
-     * @param  string  $name
40
-     * @param  string  $table
41
-     * @param  string  $foreignKey
42
-     * @param  string  $otherKey
43
-     * @param  string  $relationName
44
-     * @param  bool   $inverse
45
-     * @return void
46
-     */
47
-    public function __construct(Mapper $mapper, $parent, $name, $table, $foreignKey, $otherKey, $relationName = null, $inverse = false)
48
-    {
49
-        $this->inverse = $inverse;
9
+	/**
10
+	 * The type of the polymorphic relation.
11
+	 *
12
+	 * @var string
13
+	 */
14
+	protected $morphType;
15
+
16
+	/**
17
+	 * The class name of the morph type constraint.
18
+	 *
19
+	 * @var string
20
+	 */
21
+	protected $morphClass;
22
+
23
+	/**
24
+	 * Indicates if we are connecting the inverse of the relation.
25
+	 *
26
+	 * This primarily affects the morphClass constraint.
27
+	 *
28
+	 * @var bool
29
+	 */
30
+	protected $inverse;
31
+
32
+	protected static $hasPivot = true;
33
+
34
+	/**
35
+	 * Create a new has many relationship instance.
36
+	 *
37
+	 * @param  \Analogue\ORM\Query  $query
38
+	 * @param  \Analogue\ORM\Entityl  $parent
39
+	 * @param  string  $name
40
+	 * @param  string  $table
41
+	 * @param  string  $foreignKey
42
+	 * @param  string  $otherKey
43
+	 * @param  string  $relationName
44
+	 * @param  bool   $inverse
45
+	 * @return void
46
+	 */
47
+	public function __construct(Mapper $mapper, $parent, $name, $table, $foreignKey, $otherKey, $relationName = null, $inverse = false)
48
+	{
49
+		$this->inverse = $inverse;
50 50
         
51
-        $this->morphType = $name.'_type';
52
-
53
-        $this->morphClass = $inverse ? $mapper->getEntityMap()->getClass() : get_class($parent);
54
-
55
-        parent::__construct($mapper, $parent, $table, $foreignKey, $otherKey, $relationName);
56
-    }
57
-
58
-    /**
59
-     * Set the where clause for the relation query.
60
-     *
61
-     * @return $this
62
-     */
63
-    protected function setWhere()
64
-    {
65
-        parent::setWhere();
66
-
67
-        $this->query->where($this->table.'.'.$this->morphType, $this->morphClass);
68
-
69
-        return $this;
70
-    }
71
-
72
-    /**
73
-     * Add the constraints for a relationship count query.
74
-     *
75
-     * @param  \Analogue\ORM\Query  $query
76
-     * @param  \Analogue\ORM\Query  $parent
77
-     * @return \Analogue\ORM\Query
78
-     */
79
-    public function getRelationCountQuery(Query $query, Query $parent)
80
-    {
81
-        $query = parent::getRelationCountQuery($query, $parent);
82
-
83
-        return $query->where($this->table.'.'.$this->morphType, $this->morphClass);
84
-    }
85
-
86
-    /**
87
-     * Set the constraints for an eager load of the relation.
88
-     *
89
-     * @param  array  $entities
90
-     * @return void
91
-     */
92
-    public function addEagerConstraints(array $entities)
93
-    {
94
-        parent::addEagerConstraints($entities);
95
-
96
-        $this->query->where($this->table.'.'.$this->morphType, $this->morphClass);
97
-    }
98
-
99
-    /**
100
-     * Create a new pivot attachment record.
101
-     *
102
-     * @param  int   $id
103
-     * @param  bool  $timed
104
-     * @return array
105
-     */
106
-    protected function createAttachRecord($id, $timed)
107
-    {
108
-        $record = parent::createAttachRecord($id, $timed);
109
-
110
-        return array_add($record, $this->morphType, $this->morphClass);
111
-    }
112
-
113
-    /**
114
-     * Create a new query builder for the pivot table.
115
-     *
116
-     * @return \Illuminate\Database\Query\Builder
117
-     */
118
-    protected function newPivotQuery()
119
-    {
120
-        $query = parent::newPivotQuery();
121
-
122
-        return $query->where($this->morphType, $this->morphClass);
123
-    }
124
-
125
-    /**
126
-     * Create a new pivot model instance.
127
-     *
128
-     * @param  array  $attributes
129
-     * @param  bool   $exists
130
-     * @return \Analogue\ORM\Relationships\Pivot
131
-     */
132
-    public function newPivot(array $attributes = array(), $exists = false)
133
-    {
134
-        $pivot = new MorphPivot($this->parent, $this->parentMap, $attributes, $this->table, $exists);
135
-
136
-        $pivot->setPivotKeys($this->foreignKey, $this->otherKey)
137
-              ->setMorphType($this->morphType)
138
-              ->setMorphClass($this->morphClass);
139
-
140
-        return $pivot;
141
-    }
142
-
143
-    /**
144
-     * Return Pivot attributes when available on a relationship
145
-     *
146
-     * @return array
147
-     */
148
-    public function getPivotAttributes()
149
-    {
150
-        return $this->pivotColumns;
151
-    }
152
-
153
-    /**
154
-     * Get the foreign key "type" name.
155
-     *
156
-     * @return string
157
-     */
158
-    public function getMorphType()
159
-    {
160
-        return $this->morphType;
161
-    }
162
-
163
-    /**
164
-     * Get the class name of the parent model.
165
-     *
166
-     * @return string
167
-     */
168
-    public function getMorphClass()
169
-    {
170
-        return $this->morphClass;
171
-    }
51
+		$this->morphType = $name.'_type';
52
+
53
+		$this->morphClass = $inverse ? $mapper->getEntityMap()->getClass() : get_class($parent);
54
+
55
+		parent::__construct($mapper, $parent, $table, $foreignKey, $otherKey, $relationName);
56
+	}
57
+
58
+	/**
59
+	 * Set the where clause for the relation query.
60
+	 *
61
+	 * @return $this
62
+	 */
63
+	protected function setWhere()
64
+	{
65
+		parent::setWhere();
66
+
67
+		$this->query->where($this->table.'.'.$this->morphType, $this->morphClass);
68
+
69
+		return $this;
70
+	}
71
+
72
+	/**
73
+	 * Add the constraints for a relationship count query.
74
+	 *
75
+	 * @param  \Analogue\ORM\Query  $query
76
+	 * @param  \Analogue\ORM\Query  $parent
77
+	 * @return \Analogue\ORM\Query
78
+	 */
79
+	public function getRelationCountQuery(Query $query, Query $parent)
80
+	{
81
+		$query = parent::getRelationCountQuery($query, $parent);
82
+
83
+		return $query->where($this->table.'.'.$this->morphType, $this->morphClass);
84
+	}
85
+
86
+	/**
87
+	 * Set the constraints for an eager load of the relation.
88
+	 *
89
+	 * @param  array  $entities
90
+	 * @return void
91
+	 */
92
+	public function addEagerConstraints(array $entities)
93
+	{
94
+		parent::addEagerConstraints($entities);
95
+
96
+		$this->query->where($this->table.'.'.$this->morphType, $this->morphClass);
97
+	}
98
+
99
+	/**
100
+	 * Create a new pivot attachment record.
101
+	 *
102
+	 * @param  int   $id
103
+	 * @param  bool  $timed
104
+	 * @return array
105
+	 */
106
+	protected function createAttachRecord($id, $timed)
107
+	{
108
+		$record = parent::createAttachRecord($id, $timed);
109
+
110
+		return array_add($record, $this->morphType, $this->morphClass);
111
+	}
112
+
113
+	/**
114
+	 * Create a new query builder for the pivot table.
115
+	 *
116
+	 * @return \Illuminate\Database\Query\Builder
117
+	 */
118
+	protected function newPivotQuery()
119
+	{
120
+		$query = parent::newPivotQuery();
121
+
122
+		return $query->where($this->morphType, $this->morphClass);
123
+	}
124
+
125
+	/**
126
+	 * Create a new pivot model instance.
127
+	 *
128
+	 * @param  array  $attributes
129
+	 * @param  bool   $exists
130
+	 * @return \Analogue\ORM\Relationships\Pivot
131
+	 */
132
+	public function newPivot(array $attributes = array(), $exists = false)
133
+	{
134
+		$pivot = new MorphPivot($this->parent, $this->parentMap, $attributes, $this->table, $exists);
135
+
136
+		$pivot->setPivotKeys($this->foreignKey, $this->otherKey)
137
+			  ->setMorphType($this->morphType)
138
+			  ->setMorphClass($this->morphClass);
139
+
140
+		return $pivot;
141
+	}
142
+
143
+	/**
144
+	 * Return Pivot attributes when available on a relationship
145
+	 *
146
+	 * @return array
147
+	 */
148
+	public function getPivotAttributes()
149
+	{
150
+		return $this->pivotColumns;
151
+	}
152
+
153
+	/**
154
+	 * Get the foreign key "type" name.
155
+	 *
156
+	 * @return string
157
+	 */
158
+	public function getMorphType()
159
+	{
160
+		return $this->morphType;
161
+	}
162
+
163
+	/**
164
+	 * Get the class name of the parent model.
165
+	 *
166
+	 * @return string
167
+	 */
168
+	public function getMorphClass()
169
+	{
170
+		return $this->morphClass;
171
+	}
172 172
 }
Please login to merge, or discard this patch.
src/Relationships/Pivot.php 2 patches
Doc Comments   +2 added lines, -1 removed lines patch added patch discarded remove patch
@@ -67,10 +67,11 @@
 block discarded – undo
67 67
     /**
68 68
      * Create a new pivot model instance.
69 69
      *
70
-     * @param  $parent
70
+     * @param  \Analogue\ORM\System\InternallyMappable $parent
71 71
      * @param  array   $attributes
72 72
      * @param  string  $table
73 73
      * @param  bool    $exists
74
+     * @param \Analogue\ORM\EntityMap $parentMap
74 75
      * @return void
75 76
      */
76 77
     public function __construct($parent, $parentMap, $attributes, $table, $exists = false)
Please login to merge, or discard this patch.
Indentation   +154 added lines, -154 removed lines patch added patch discarded remove patch
@@ -5,158 +5,158 @@
 block discarded – undo
5 5
 class Pivot extends Entity
6 6
 {
7 7
 
8
-    protected $exists;
9
-
10
-    /**
11
-     * Pivot's table
12
-     *
13
-     * @var string
14
-     */
15
-    protected $table;
16
-
17
-    /**
18
-     * The parent entity of the relationship.
19
-     *
20
-     * @var object
21
-     */
22
-    protected $parent;
23
-
24
-    /**
25
-     * The parent entity of the relationship.
26
-     *
27
-     * @var \Analogue\ORM\EntityMap
28
-     */
29
-    protected $parentMap;
30
-
31
-    /**
32
-     * We may define a pivot mapping to deal with
33
-     * soft deletes, timestamps, etc.
34
-     *
35
-     * @var \Analogue\ORM\EntityMap
36
-     */
37
-    protected $pivotMap;
38
-
39
-    /**
40
-     * The name of the foreign key column.
41
-     *
42
-     * @var string
43
-     */
44
-    protected $foreignKey;
45
-
46
-    /**
47
-     * The name of the "other key" column.
48
-     *
49
-     * @var string
50
-     */
51
-    protected $otherKey;
52
-
53
-    /**
54
-     * The attributes that aren't mass assignable.
55
-     *
56
-     * @var array
57
-     */
58
-    protected $guarded = array();
59
-
60
-    /**
61
-     * Pivot uses timestamps ?
62
-     *
63
-     * @var boolean
64
-     */
65
-    protected $timestamps;
66
-
67
-    /**
68
-     * Create a new pivot model instance.
69
-     *
70
-     * @param  $parent
71
-     * @param  array   $attributes
72
-     * @param  string  $table
73
-     * @param  bool    $exists
74
-     * @return void
75
-     */
76
-    public function __construct($parent, $parentMap, $attributes, $table, $exists = false)
77
-    {
78
-        // The pivot model is a "dynamic" model since we will set the tables dynamically
79
-        // for the instance. This allows it work for any intermediate tables for the
80
-        // many to many relationship that are defined by this developer's classes.
81
-        $this->setEntityAttributes($attributes, true);
82
-
83
-        $this->table = $table;
84
-
85
-        // We store off the parent instance so we will access the timestamp column names
86
-        // for the model, since the pivot model timestamps aren't easily configurable
87
-        // from the developer's point of view. We can use the parents to get these.
88
-        $this->parent = $parent;
89
-
90
-        $this->parentMap = $parentMap;
91
-
92
-        $this->exists = $exists;
93
-
94
-        $this->timestamps = $this->hasTimestampAttributes();
95
-    }
96
-
97
-    /**
98
-     * Get the foreign key column name.
99
-     *
100
-     * @return string
101
-     */
102
-    public function getForeignKey()
103
-    {
104
-        return $this->foreignKey;
105
-    }
106
-
107
-    /**
108
-     * Get the "other key" column name.
109
-     *
110
-     * @return string
111
-     */
112
-    public function getOtherKey()
113
-    {
114
-        return $this->otherKey;
115
-    }
116
-
117
-    /**
118
-     * Set the key names for the pivot model instance.
119
-     *
120
-     * @param  string  $foreignKey
121
-     * @param  string  $otherKey
122
-     * @return $this
123
-     */
124
-    public function setPivotKeys($foreignKey, $otherKey)
125
-    {
126
-        $this->foreignKey = $foreignKey;
127
-
128
-        $this->otherKey = $otherKey;
129
-
130
-        return $this;
131
-    }
132
-
133
-    /**
134
-     * Determine if the pivot model has timestamp attributes.
135
-     *
136
-     * @return bool
137
-     */
138
-    public function hasTimestampAttributes()
139
-    {
140
-        return array_key_exists($this->getCreatedAtColumn(), $this->attributes);
141
-    }
142
-
143
-    /**
144
-     * Get the name of the "created at" column.
145
-     *
146
-     * @return string
147
-     */
148
-    public function getCreatedAtColumn()
149
-    {
150
-        return $this->parentMap->getCreatedAtColumn();
151
-    }
152
-
153
-    /**
154
-     * Get the name of the "updated at" column.
155
-     *
156
-     * @return string
157
-     */
158
-    public function getUpdatedAtColumn()
159
-    {
160
-        return $this->parentMap->getUpdatedAtColumn();
161
-    }
8
+	protected $exists;
9
+
10
+	/**
11
+	 * Pivot's table
12
+	 *
13
+	 * @var string
14
+	 */
15
+	protected $table;
16
+
17
+	/**
18
+	 * The parent entity of the relationship.
19
+	 *
20
+	 * @var object
21
+	 */
22
+	protected $parent;
23
+
24
+	/**
25
+	 * The parent entity of the relationship.
26
+	 *
27
+	 * @var \Analogue\ORM\EntityMap
28
+	 */
29
+	protected $parentMap;
30
+
31
+	/**
32
+	 * We may define a pivot mapping to deal with
33
+	 * soft deletes, timestamps, etc.
34
+	 *
35
+	 * @var \Analogue\ORM\EntityMap
36
+	 */
37
+	protected $pivotMap;
38
+
39
+	/**
40
+	 * The name of the foreign key column.
41
+	 *
42
+	 * @var string
43
+	 */
44
+	protected $foreignKey;
45
+
46
+	/**
47
+	 * The name of the "other key" column.
48
+	 *
49
+	 * @var string
50
+	 */
51
+	protected $otherKey;
52
+
53
+	/**
54
+	 * The attributes that aren't mass assignable.
55
+	 *
56
+	 * @var array
57
+	 */
58
+	protected $guarded = array();
59
+
60
+	/**
61
+	 * Pivot uses timestamps ?
62
+	 *
63
+	 * @var boolean
64
+	 */
65
+	protected $timestamps;
66
+
67
+	/**
68
+	 * Create a new pivot model instance.
69
+	 *
70
+	 * @param  $parent
71
+	 * @param  array   $attributes
72
+	 * @param  string  $table
73
+	 * @param  bool    $exists
74
+	 * @return void
75
+	 */
76
+	public function __construct($parent, $parentMap, $attributes, $table, $exists = false)
77
+	{
78
+		// The pivot model is a "dynamic" model since we will set the tables dynamically
79
+		// for the instance. This allows it work for any intermediate tables for the
80
+		// many to many relationship that are defined by this developer's classes.
81
+		$this->setEntityAttributes($attributes, true);
82
+
83
+		$this->table = $table;
84
+
85
+		// We store off the parent instance so we will access the timestamp column names
86
+		// for the model, since the pivot model timestamps aren't easily configurable
87
+		// from the developer's point of view. We can use the parents to get these.
88
+		$this->parent = $parent;
89
+
90
+		$this->parentMap = $parentMap;
91
+
92
+		$this->exists = $exists;
93
+
94
+		$this->timestamps = $this->hasTimestampAttributes();
95
+	}
96
+
97
+	/**
98
+	 * Get the foreign key column name.
99
+	 *
100
+	 * @return string
101
+	 */
102
+	public function getForeignKey()
103
+	{
104
+		return $this->foreignKey;
105
+	}
106
+
107
+	/**
108
+	 * Get the "other key" column name.
109
+	 *
110
+	 * @return string
111
+	 */
112
+	public function getOtherKey()
113
+	{
114
+		return $this->otherKey;
115
+	}
116
+
117
+	/**
118
+	 * Set the key names for the pivot model instance.
119
+	 *
120
+	 * @param  string  $foreignKey
121
+	 * @param  string  $otherKey
122
+	 * @return $this
123
+	 */
124
+	public function setPivotKeys($foreignKey, $otherKey)
125
+	{
126
+		$this->foreignKey = $foreignKey;
127
+
128
+		$this->otherKey = $otherKey;
129
+
130
+		return $this;
131
+	}
132
+
133
+	/**
134
+	 * Determine if the pivot model has timestamp attributes.
135
+	 *
136
+	 * @return bool
137
+	 */
138
+	public function hasTimestampAttributes()
139
+	{
140
+		return array_key_exists($this->getCreatedAtColumn(), $this->attributes);
141
+	}
142
+
143
+	/**
144
+	 * Get the name of the "created at" column.
145
+	 *
146
+	 * @return string
147
+	 */
148
+	public function getCreatedAtColumn()
149
+	{
150
+		return $this->parentMap->getCreatedAtColumn();
151
+	}
152
+
153
+	/**
154
+	 * Get the name of the "updated at" column.
155
+	 *
156
+	 * @return string
157
+	 */
158
+	public function getUpdatedAtColumn()
159
+	{
160
+		return $this->parentMap->getUpdatedAtColumn();
161
+	}
162 162
 }
Please login to merge, or discard this patch.
src/Relationships/Relationship.php 4 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -287,7 +287,7 @@
 block discarded – undo
287 287
     /**
288 288
      * Get the parent model of the relation.
289 289
      *
290
-     * @return Mappable
290
+     * @return InternallyMappable
291 291
      */
292 292
     public function getParent()
293 293
     {
Please login to merge, or discard this patch.
Unused Use Statements   -1 removed lines patch added patch discarded remove patch
@@ -5,7 +5,6 @@
 block discarded – undo
5 5
 use Analogue\ORM\Mappable;
6 6
 use Analogue\ORM\System\Query;
7 7
 use Analogue\ORM\System\Mapper;
8
-use Analogue\ORM\System\Manager;
9 8
 use Analogue\ORM\EntityCollection;
10 9
 use Analogue\ORM\System\InternallyMappable;
11 10
 use Analogue\ORM\System\Wrappers\Factory;
Please login to merge, or discard this patch.
Indentation   +432 added lines, -432 removed lines patch added patch discarded remove patch
@@ -14,438 +14,438 @@
 block discarded – undo
14 14
 abstract class Relationship
15 15
 {
16 16
 
17
-    /**
18
-     * The mapper instance for the related entity
19
-     *
20
-     * @var \Analogue\ORM\System\Mapper
21
-     */
22
-    protected $relatedMapper;
23
-
24
-    /**
25
-     * The Analogue Query Builder instance.
26
-     *
27
-     * @var \Analogue\ORM\Query
28
-     */
29
-    protected $query;
30
-
31
-    /**
32
-     * The parent entity proxy instance.
33
-     *
34
-     * @var \Analogue\ORM\System\InternallyMappable
35
-     */
36
-    protected $parent;
37
-
38
-    /**
39
-     * The parent entity map
40
-     * @var \Analogue\ORM\EntityMap
41
-     */
42
-    protected $parentMap;
43
-
44
-    /**
45
-     * The Parent Mapper instance
46
-     *
47
-     * @var \Analogue\ORM\System\Mapper
48
-     */
49
-    protected $parentMapper;
50
-
51
-    /**
52
-     * The related entity instance.
53
-     *
54
-     * @var object
55
-     */
56
-    protected $related;
57
-
58
-    /**
59
-     * The related entity Map
60
-     * @var \Analogue\ORM\EntityMap
61
-     */
62
-    protected $relatedMap;
63
-
64
-    /**
65
-     * Indicate if the parent entity hold the key for the relation.
66
-     *
67
-     * @var boolean
68
-     */
69
-    protected static $ownForeignKey = false;
70
-
71
-    /**
72
-     * Indicate if the relationships use a pivot table.*
73
-     *
74
-     * @var boolean
75
-     */
76
-    protected static $hasPivot = false;
77
-
78
-    /**
79
-     * Indicates if the relation is adding constraints.
80
-     *
81
-     * @var bool
82
-     */
83
-    protected static $constraints = true;
84
-
85
-    /**
86
-     * Wrapper factory
87
-     *
88
-     * @var \Analogue\ORM\System\Wrappers\Factory
89
-     */
90
-    protected $factory;
91
-
92
-    /**
93
-     * Create a new relation instance.
94
-     *
95
-     * @param  \Analogue\ORM\System\Mapper  $mapper
96
-     * @param  Mappable  $parent
97
-     * @return void
98
-     */
99
-    public function __construct(Mapper $mapper, $parent)
100
-    {
101
-        $this->relatedMapper = $mapper;
102
-
103
-        $this->query = $mapper->getQuery();
104
-
105
-        $this->factory = new Factory;
106
-
107
-        $this->parent = $this->factory->make($parent);
108
-
109
-        $this->parentMapper = $mapper->getManager()->getMapper($parent);
110
-
111
-        $this->parentMap = $this->parentMapper->getEntityMap();
112
-
113
-        $this->related = $this->query->getEntityInstance();
114
-
115
-        $this->relatedMap = $mapper->getEntityMap();
116
-
117
-        $this->addConstraints();
118
-    }
119
-
120
-    /**
121
-     *
122
-     * @param  [type] $related [description]
123
-     * @return [type]         [description]
124
-     */
125
-    abstract public function attachTo($related);
126
-
127
-    /**
128
-     *
129
-     * @param  [type] $related [description]
130
-     * @return [type]         [description]
131
-     */
132
-    abstract public function detachFrom($related);
133
-
134
-    /**
135
-     * Indicate if the parent entity hold the foreign key for relation.
136
-     *
137
-     * @return boolean
138
-     */
139
-    public function ownForeignKey()
140
-    {
141
-        return static::$ownForeignKey;
142
-    }
143
-
144
-    /**
145
-     * Indicate if the relationship uses a pivot table
146
-     *
147
-     * @return boolean
148
-     */
149
-    public function hasPivot()
150
-    {
151
-        return static::$hasPivot;
152
-    }
153
-
154
-    /**
155
-     * Set the base constraints on the relation query.
156
-     *
157
-     * @return void
158
-     */
159
-    abstract public function addConstraints();
160
-
161
-    /**
162
-     * Set the constraints for an eager load of the relation.
163
-     *
164
-     * @param  array  $models
165
-     * @return void
166
-     */
167
-    abstract public function addEagerConstraints(array $models);
168
-
169
-    /**
170
-     * Initialize the relation on a set of models.
171
-     *
172
-     * @param  array   $models
173
-     * @param  string  $relation
174
-     * @return array
175
-     */
176
-    abstract public function initRelation(array $models, $relation);
177
-
178
-    /**
179
-     * Match the eagerly loaded results to their parents.
180
-     *
181
-     * @param  array   $entities
182
-     * @param  \Analogue\ORM\EntityCollection  $results
183
-     * @param  string  $relation
184
-     * @return array
185
-     */
186
-    abstract public function match(array $entities, EntityCollection $results, $relation);
187
-
188
-    /**
189
-     * Get the results of the relationship.
190
-     *
191
-     * @param string 	$relation 	relation name in parent's entity map
192
-     * @return mixed
193
-     */
194
-    abstract public function getResults($relation);
195
-
196
-    /**
197
-     * Get the relationship for eager loading.
198
-     *
199
-     * @return \Analogue\ORM\EntityCollection
200
-     */
201
-    public function getEager()
202
-    {
203
-        return $this->get();
204
-    }
205
-
206
-    /**
207
-     * Add the constraints for a relationship count query.
208
-     *
209
-     * @param  \Analogue\ORM\System\Query  $query
210
-     * @param  \Analogue\ORM\System\Query  $parent
211
-     * @return \Analogue\ORM\System\Query
212
-     */
213
-    public function getRelationCountQuery(Query $query, Query $parent)
214
-    {
215
-        $query->select(new Expression('count(*)'));
216
-
217
-        $key = $this->wrap($this->getQualifiedParentKeyName());
218
-
219
-        return $query->where($this->getHasCompareKey(), '=', new Expression($key));
220
-    }
221
-
222
-    /**
223
-     * Run a callback with constraints disabled on the relation.
224
-     *
225
-     * @param  \Closure  $callback
226
-     * @return mixed
227
-     */
228
-    public static function noConstraints(Closure $callback)
229
-    {
230
-        static::$constraints = false;
231
-
232
-        // When resetting the relation where clause, we want to shift the first element
233
-        // off of the bindings, leaving only the constraints that the developers put
234
-        // as "extra" on the relationships, and not original relation constraints.
235
-        $results = call_user_func($callback);
236
-
237
-        static::$constraints = true;
238
-
239
-        return $results;
240
-    }
241
-
242
-    /**
243
-     * Get all of the primary keys for an array of entities.
244
-     *
245
-     * @param  array   $entities
246
-     * @param  string  $key
247
-     * @return array
248
-     */
249
-    protected function getKeys(array $entities, $key = null)
250
-    {
251
-        if (is_null($key)) {
252
-            $key = $this->relatedMap->getKeyName();
253
-        }
254
-
255
-        $host = $this;
256
-
257
-        return array_unique(array_values(array_map(function ($value) use ($key, $host) {
258
-            if (! $value instanceof InternallyMappable) {
259
-                $value = $host->factory->make($value);
260
-            }
261
-
262
-            return $value->getEntityAttribute($key);
263
-
264
-        }, $entities)));
265
-    }
266
-
267
-    /**
268
-     * Get the underlying query for the relation.
269
-     *
270
-     * @return \Analogue\ORM\System\Query
271
-     */
272
-    public function getQuery()
273
-    {
274
-        return $this->query;
275
-    }
276
-
277
-    /**
278
-     * Get the base query builder
279
-     *
280
-     * @return \Illuminate\Database\Query\Builder
281
-     */
282
-    public function getBaseQuery()
283
-    {
284
-        return $this->query->getQuery();
285
-    }
286
-
287
-    /**
288
-     * Get the parent model of the relation.
289
-     *
290
-     * @return Mappable
291
-     */
292
-    public function getParent()
293
-    {
294
-        return $this->parent;
295
-    }
296
-
297
-    /**
298
-     * Get the fully qualified parent key name.
299
-     *
300
-     * @return string
301
-     */
302
-    protected function getQualifiedParentKeyName()
303
-    {
304
-        return $this->parent->getQualifiedKeyName();
305
-    }
306
-
307
-    /**
308
-     * Get the related entity of the relation.
309
-     *
310
-     * @return \Analogue\ORM\Entity
311
-     */
312
-    public function getRelated()
313
-    {
314
-        return $this->related;
315
-    }
316
-
317
-    /**
318
-     * Get the related mapper for the relation
319
-     *
320
-     * @return \Analogue\ORM\System\Mapper
321
-     */
322
-    public function getRelatedMapper()
323
-    {
324
-        return $this->relatedMapper;
325
-    }
326
-
327
-
328
-    /**
329
-     * Get the name of the "created at" column.
330
-     *
331
-     * @return string
332
-     */
333
-    public function createdAt()
334
-    {
335
-        return $this->parentMap->getCreatedAtColumn();
336
-    }
337
-
338
-    /**
339
-     * Get the name of the "updated at" column.
340
-     *
341
-     * @return string
342
-     */
343
-    public function updatedAt()
344
-    {
345
-        return $this->parentMap->getUpdatedAtColumn();
346
-    }
347
-
348
-    /**
349
-     * Get the name of the related model's "updated at" column.
350
-     *
351
-     * @return string
352
-     */
353
-    public function relatedUpdatedAt()
354
-    {
355
-        return $this->related->getUpdatedAtColumn();
356
-    }
357
-
358
-    /**
359
-     * Wrap the given value with the parent query's grammar.
360
-     *
361
-     * @param  string  $value
362
-     * @return string
363
-     */
364
-    public function wrap($value)
365
-    {
366
-        return $this->parentMapper->getQuery()->getQuery()->getGrammar()->wrap($value);
367
-    }
368
-
369
-    /**
370
-     * Get a fresh timestamp
371
-     *
372
-     * @return \Carbon\Carbon
373
-     */
374
-    protected function freshTimestamp()
375
-    {
376
-        return new Carbon;
377
-    }
378
-
379
-    /**
380
-     * Cache the link between parent and related
381
-     * into the mapper's Entity Cache.
382
-     *
383
-     * @param  EntityCollection|Mappable $results 	result of the relation query
384
-     * @param  string  $relation 					Name of the relation method on the parent entity
385
-     *
386
-     * @return void
387
-     */
388
-    protected function cacheRelation($results, $relation)
389
-    {
390
-        $cache = $this->parentMapper->getEntityCache();
391
-
392
-        $cache->cacheLoadedRelationResult($this->parent, $relation, $results, $this);
393
-    }
394
-
395
-    /**
396
-     * Return Pivot attributes when available on a relationship
397
-     *
398
-     * @return array
399
-     */
400
-    public function getPivotAttributes()
401
-    {
402
-        return [];
403
-    }
404
-
405
-    /**
406
-     * Get a combo type.primaryKey
407
-     *
408
-     * @param  Mappable $entity
409
-     * @return string
410
-     */
411
-    protected function getEntityHash(Mappable $entity)
412
-    {
413
-        $class = get_class($entity);
17
+	/**
18
+	 * The mapper instance for the related entity
19
+	 *
20
+	 * @var \Analogue\ORM\System\Mapper
21
+	 */
22
+	protected $relatedMapper;
23
+
24
+	/**
25
+	 * The Analogue Query Builder instance.
26
+	 *
27
+	 * @var \Analogue\ORM\Query
28
+	 */
29
+	protected $query;
30
+
31
+	/**
32
+	 * The parent entity proxy instance.
33
+	 *
34
+	 * @var \Analogue\ORM\System\InternallyMappable
35
+	 */
36
+	protected $parent;
37
+
38
+	/**
39
+	 * The parent entity map
40
+	 * @var \Analogue\ORM\EntityMap
41
+	 */
42
+	protected $parentMap;
43
+
44
+	/**
45
+	 * The Parent Mapper instance
46
+	 *
47
+	 * @var \Analogue\ORM\System\Mapper
48
+	 */
49
+	protected $parentMapper;
50
+
51
+	/**
52
+	 * The related entity instance.
53
+	 *
54
+	 * @var object
55
+	 */
56
+	protected $related;
57
+
58
+	/**
59
+	 * The related entity Map
60
+	 * @var \Analogue\ORM\EntityMap
61
+	 */
62
+	protected $relatedMap;
63
+
64
+	/**
65
+	 * Indicate if the parent entity hold the key for the relation.
66
+	 *
67
+	 * @var boolean
68
+	 */
69
+	protected static $ownForeignKey = false;
70
+
71
+	/**
72
+	 * Indicate if the relationships use a pivot table.*
73
+	 *
74
+	 * @var boolean
75
+	 */
76
+	protected static $hasPivot = false;
77
+
78
+	/**
79
+	 * Indicates if the relation is adding constraints.
80
+	 *
81
+	 * @var bool
82
+	 */
83
+	protected static $constraints = true;
84
+
85
+	/**
86
+	 * Wrapper factory
87
+	 *
88
+	 * @var \Analogue\ORM\System\Wrappers\Factory
89
+	 */
90
+	protected $factory;
91
+
92
+	/**
93
+	 * Create a new relation instance.
94
+	 *
95
+	 * @param  \Analogue\ORM\System\Mapper  $mapper
96
+	 * @param  Mappable  $parent
97
+	 * @return void
98
+	 */
99
+	public function __construct(Mapper $mapper, $parent)
100
+	{
101
+		$this->relatedMapper = $mapper;
102
+
103
+		$this->query = $mapper->getQuery();
104
+
105
+		$this->factory = new Factory;
106
+
107
+		$this->parent = $this->factory->make($parent);
108
+
109
+		$this->parentMapper = $mapper->getManager()->getMapper($parent);
110
+
111
+		$this->parentMap = $this->parentMapper->getEntityMap();
112
+
113
+		$this->related = $this->query->getEntityInstance();
114
+
115
+		$this->relatedMap = $mapper->getEntityMap();
116
+
117
+		$this->addConstraints();
118
+	}
119
+
120
+	/**
121
+	 *
122
+	 * @param  [type] $related [description]
123
+	 * @return [type]         [description]
124
+	 */
125
+	abstract public function attachTo($related);
126
+
127
+	/**
128
+	 *
129
+	 * @param  [type] $related [description]
130
+	 * @return [type]         [description]
131
+	 */
132
+	abstract public function detachFrom($related);
133
+
134
+	/**
135
+	 * Indicate if the parent entity hold the foreign key for relation.
136
+	 *
137
+	 * @return boolean
138
+	 */
139
+	public function ownForeignKey()
140
+	{
141
+		return static::$ownForeignKey;
142
+	}
143
+
144
+	/**
145
+	 * Indicate if the relationship uses a pivot table
146
+	 *
147
+	 * @return boolean
148
+	 */
149
+	public function hasPivot()
150
+	{
151
+		return static::$hasPivot;
152
+	}
153
+
154
+	/**
155
+	 * Set the base constraints on the relation query.
156
+	 *
157
+	 * @return void
158
+	 */
159
+	abstract public function addConstraints();
160
+
161
+	/**
162
+	 * Set the constraints for an eager load of the relation.
163
+	 *
164
+	 * @param  array  $models
165
+	 * @return void
166
+	 */
167
+	abstract public function addEagerConstraints(array $models);
168
+
169
+	/**
170
+	 * Initialize the relation on a set of models.
171
+	 *
172
+	 * @param  array   $models
173
+	 * @param  string  $relation
174
+	 * @return array
175
+	 */
176
+	abstract public function initRelation(array $models, $relation);
177
+
178
+	/**
179
+	 * Match the eagerly loaded results to their parents.
180
+	 *
181
+	 * @param  array   $entities
182
+	 * @param  \Analogue\ORM\EntityCollection  $results
183
+	 * @param  string  $relation
184
+	 * @return array
185
+	 */
186
+	abstract public function match(array $entities, EntityCollection $results, $relation);
187
+
188
+	/**
189
+	 * Get the results of the relationship.
190
+	 *
191
+	 * @param string 	$relation 	relation name in parent's entity map
192
+	 * @return mixed
193
+	 */
194
+	abstract public function getResults($relation);
195
+
196
+	/**
197
+	 * Get the relationship for eager loading.
198
+	 *
199
+	 * @return \Analogue\ORM\EntityCollection
200
+	 */
201
+	public function getEager()
202
+	{
203
+		return $this->get();
204
+	}
205
+
206
+	/**
207
+	 * Add the constraints for a relationship count query.
208
+	 *
209
+	 * @param  \Analogue\ORM\System\Query  $query
210
+	 * @param  \Analogue\ORM\System\Query  $parent
211
+	 * @return \Analogue\ORM\System\Query
212
+	 */
213
+	public function getRelationCountQuery(Query $query, Query $parent)
214
+	{
215
+		$query->select(new Expression('count(*)'));
216
+
217
+		$key = $this->wrap($this->getQualifiedParentKeyName());
218
+
219
+		return $query->where($this->getHasCompareKey(), '=', new Expression($key));
220
+	}
221
+
222
+	/**
223
+	 * Run a callback with constraints disabled on the relation.
224
+	 *
225
+	 * @param  \Closure  $callback
226
+	 * @return mixed
227
+	 */
228
+	public static function noConstraints(Closure $callback)
229
+	{
230
+		static::$constraints = false;
231
+
232
+		// When resetting the relation where clause, we want to shift the first element
233
+		// off of the bindings, leaving only the constraints that the developers put
234
+		// as "extra" on the relationships, and not original relation constraints.
235
+		$results = call_user_func($callback);
236
+
237
+		static::$constraints = true;
238
+
239
+		return $results;
240
+	}
241
+
242
+	/**
243
+	 * Get all of the primary keys for an array of entities.
244
+	 *
245
+	 * @param  array   $entities
246
+	 * @param  string  $key
247
+	 * @return array
248
+	 */
249
+	protected function getKeys(array $entities, $key = null)
250
+	{
251
+		if (is_null($key)) {
252
+			$key = $this->relatedMap->getKeyName();
253
+		}
254
+
255
+		$host = $this;
256
+
257
+		return array_unique(array_values(array_map(function ($value) use ($key, $host) {
258
+			if (! $value instanceof InternallyMappable) {
259
+				$value = $host->factory->make($value);
260
+			}
261
+
262
+			return $value->getEntityAttribute($key);
263
+
264
+		}, $entities)));
265
+	}
266
+
267
+	/**
268
+	 * Get the underlying query for the relation.
269
+	 *
270
+	 * @return \Analogue\ORM\System\Query
271
+	 */
272
+	public function getQuery()
273
+	{
274
+		return $this->query;
275
+	}
276
+
277
+	/**
278
+	 * Get the base query builder
279
+	 *
280
+	 * @return \Illuminate\Database\Query\Builder
281
+	 */
282
+	public function getBaseQuery()
283
+	{
284
+		return $this->query->getQuery();
285
+	}
286
+
287
+	/**
288
+	 * Get the parent model of the relation.
289
+	 *
290
+	 * @return Mappable
291
+	 */
292
+	public function getParent()
293
+	{
294
+		return $this->parent;
295
+	}
296
+
297
+	/**
298
+	 * Get the fully qualified parent key name.
299
+	 *
300
+	 * @return string
301
+	 */
302
+	protected function getQualifiedParentKeyName()
303
+	{
304
+		return $this->parent->getQualifiedKeyName();
305
+	}
306
+
307
+	/**
308
+	 * Get the related entity of the relation.
309
+	 *
310
+	 * @return \Analogue\ORM\Entity
311
+	 */
312
+	public function getRelated()
313
+	{
314
+		return $this->related;
315
+	}
316
+
317
+	/**
318
+	 * Get the related mapper for the relation
319
+	 *
320
+	 * @return \Analogue\ORM\System\Mapper
321
+	 */
322
+	public function getRelatedMapper()
323
+	{
324
+		return $this->relatedMapper;
325
+	}
326
+
327
+
328
+	/**
329
+	 * Get the name of the "created at" column.
330
+	 *
331
+	 * @return string
332
+	 */
333
+	public function createdAt()
334
+	{
335
+		return $this->parentMap->getCreatedAtColumn();
336
+	}
337
+
338
+	/**
339
+	 * Get the name of the "updated at" column.
340
+	 *
341
+	 * @return string
342
+	 */
343
+	public function updatedAt()
344
+	{
345
+		return $this->parentMap->getUpdatedAtColumn();
346
+	}
347
+
348
+	/**
349
+	 * Get the name of the related model's "updated at" column.
350
+	 *
351
+	 * @return string
352
+	 */
353
+	public function relatedUpdatedAt()
354
+	{
355
+		return $this->related->getUpdatedAtColumn();
356
+	}
357
+
358
+	/**
359
+	 * Wrap the given value with the parent query's grammar.
360
+	 *
361
+	 * @param  string  $value
362
+	 * @return string
363
+	 */
364
+	public function wrap($value)
365
+	{
366
+		return $this->parentMapper->getQuery()->getQuery()->getGrammar()->wrap($value);
367
+	}
368
+
369
+	/**
370
+	 * Get a fresh timestamp
371
+	 *
372
+	 * @return \Carbon\Carbon
373
+	 */
374
+	protected function freshTimestamp()
375
+	{
376
+		return new Carbon;
377
+	}
378
+
379
+	/**
380
+	 * Cache the link between parent and related
381
+	 * into the mapper's Entity Cache.
382
+	 *
383
+	 * @param  EntityCollection|Mappable $results 	result of the relation query
384
+	 * @param  string  $relation 					Name of the relation method on the parent entity
385
+	 *
386
+	 * @return void
387
+	 */
388
+	protected function cacheRelation($results, $relation)
389
+	{
390
+		$cache = $this->parentMapper->getEntityCache();
391
+
392
+		$cache->cacheLoadedRelationResult($this->parent, $relation, $results, $this);
393
+	}
394
+
395
+	/**
396
+	 * Return Pivot attributes when available on a relationship
397
+	 *
398
+	 * @return array
399
+	 */
400
+	public function getPivotAttributes()
401
+	{
402
+		return [];
403
+	}
404
+
405
+	/**
406
+	 * Get a combo type.primaryKey
407
+	 *
408
+	 * @param  Mappable $entity
409
+	 * @return string
410
+	 */
411
+	protected function getEntityHash(Mappable $entity)
412
+	{
413
+		$class = get_class($entity);
414 414
         
415
-        $keyName = Mapper::getMapper($class)->getEntityMap()->getKeyName();
415
+		$keyName = Mapper::getMapper($class)->getEntityMap()->getKeyName();
416 416
         
417
-        $hash = $class.'.'.$entity->getEntityAttribute($keyName);
418
-
419
-        return $hash;
420
-    }
421
-
422
-    /**
423
-     * Run synchronization content if needed by the
424
-     * relation type.
425
-     *
426
-     * @param  array  $actualContent
427
-     * @return void
428
-     */
429
-    public function sync(array $actualContent)
430
-    {
431
-        //
432
-    }
433
-
434
-    /**
435
-     * Handle dynamic method calls to the relationship.
436
-     *
437
-     * @param  string  $method
438
-     * @param  array   $parameters
439
-     * @return mixed
440
-     */
441
-    public function __call($method, $parameters)
442
-    {
443
-        $result = call_user_func_array(array($this->query, $method), $parameters);
444
-
445
-        if ($result === $this->query) {
446
-            return $this;
447
-        }
448
-
449
-        return $result;
450
-    }
417
+		$hash = $class.'.'.$entity->getEntityAttribute($keyName);
418
+
419
+		return $hash;
420
+	}
421
+
422
+	/**
423
+	 * Run synchronization content if needed by the
424
+	 * relation type.
425
+	 *
426
+	 * @param  array  $actualContent
427
+	 * @return void
428
+	 */
429
+	public function sync(array $actualContent)
430
+	{
431
+		//
432
+	}
433
+
434
+	/**
435
+	 * Handle dynamic method calls to the relationship.
436
+	 *
437
+	 * @param  string  $method
438
+	 * @param  array   $parameters
439
+	 * @return mixed
440
+	 */
441
+	public function __call($method, $parameters)
442
+	{
443
+		$result = call_user_func_array(array($this->query, $method), $parameters);
444
+
445
+		if ($result === $this->query) {
446
+			return $this;
447
+		}
448
+
449
+		return $result;
450
+	}
451 451
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -254,8 +254,8 @@
 block discarded – undo
254 254
 
255 255
         $host = $this;
256 256
 
257
-        return array_unique(array_values(array_map(function ($value) use ($key, $host) {
258
-            if (! $value instanceof InternallyMappable) {
257
+        return array_unique(array_values(array_map(function($value) use ($key, $host) {
258
+            if (!$value instanceof InternallyMappable) {
259 259
                 $value = $host->factory->make($value);
260 260
             }
261 261
 
Please login to merge, or discard this patch.
src/Repository.php 2 patches
Doc Comments   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -24,7 +24,7 @@  discard block
 block discarded – undo
24 24
      * - Mappable object instance
25 25
      * - Instance of mapper
26 26
      *
27
-     * @param Mapper|Mappable|string $mapper
27
+     * @param Mapper $mapper
28 28
      * @param EntityMap 			 $entityMap (optionnal)
29 29
      *
30 30
      * @throws \InvalidArgumentException
@@ -97,7 +97,7 @@  discard block
 block discarded – undo
97 97
      * Delete an entity or an entity collection from the database
98 98
      *
99 99
      * @param  Mappable|Collection $entity
100
-     * @return null
100
+     * @return \Illuminate\Support\Collection|null
101 101
      */
102 102
     public function delete($entity)
103 103
     {
Please login to merge, or discard this patch.
Indentation   +111 added lines, -111 removed lines patch added patch discarded remove patch
@@ -10,124 +10,124 @@
 block discarded – undo
10 10
 class Repository
11 11
 {
12 12
 
13
-    /**
14
-     * The mapper object for the corresponding entity
15
-     *
16
-     * @var \Analogue\ORM\System\Mapper
17
-     */
18
-    protected $mapper;
13
+	/**
14
+	 * The mapper object for the corresponding entity
15
+	 *
16
+	 * @var \Analogue\ORM\System\Mapper
17
+	 */
18
+	protected $mapper;
19 19
 
20
-    /**
21
-     * To build a repository, either provide :
22
-     *
23
-     * - Mappable object's class name as a string
24
-     * - Mappable object instance
25
-     * - Instance of mapper
26
-     *
27
-     * @param Mapper|Mappable|string $mapper
28
-     * @param EntityMap 			 $entityMap (optionnal)
29
-     *
30
-     * @throws \InvalidArgumentException
31
-     */
32
-    public function __construct($mapper, EntityMap $entityMap = null)
33
-    {
34
-        if ($mapper instanceof Mappable || is_string($mapper)) {
35
-            $this->mapper = Manager::getMapper($mapper, $entityMap);
36
-        } elseif ($mapper instanceof Mapper) {
37
-            $this->mapper = $mapper;
38
-        } else {
39
-            new InvalidArgumentException('Repository class constuctor need a valid Mapper or Mappable object.');
40
-        }
41
-    }
20
+	/**
21
+	 * To build a repository, either provide :
22
+	 *
23
+	 * - Mappable object's class name as a string
24
+	 * - Mappable object instance
25
+	 * - Instance of mapper
26
+	 *
27
+	 * @param Mapper|Mappable|string $mapper
28
+	 * @param EntityMap 			 $entityMap (optionnal)
29
+	 *
30
+	 * @throws \InvalidArgumentException
31
+	 */
32
+	public function __construct($mapper, EntityMap $entityMap = null)
33
+	{
34
+		if ($mapper instanceof Mappable || is_string($mapper)) {
35
+			$this->mapper = Manager::getMapper($mapper, $entityMap);
36
+		} elseif ($mapper instanceof Mapper) {
37
+			$this->mapper = $mapper;
38
+		} else {
39
+			new InvalidArgumentException('Repository class constuctor need a valid Mapper or Mappable object.');
40
+		}
41
+	}
42 42
 
43
-    /**
44
-     * Return all Entities from database
45
-     *
46
-     * @return \Analogue\ORM\EntityCollection
47
-     */
48
-    public function all()
49
-    {
50
-        return $this->mapper->get();
51
-    }
43
+	/**
44
+	 * Return all Entities from database
45
+	 *
46
+	 * @return \Analogue\ORM\EntityCollection
47
+	 */
48
+	public function all()
49
+	{
50
+		return $this->mapper->get();
51
+	}
52 52
     
53
-    /**
54
-     * Fetch a record from the database
55
-     * @param  integer $id
56
-     * @return \Analogue\ORM\Mappable
57
-     */
58
-    public function find($id)
59
-    {
60
-        return $this->mapper->find($id);
61
-    }
53
+	/**
54
+	 * Fetch a record from the database
55
+	 * @param  integer $id
56
+	 * @return \Analogue\ORM\Mappable
57
+	 */
58
+	public function find($id)
59
+	{
60
+		return $this->mapper->find($id);
61
+	}
62 62
 
63
-    /**
64
-     * Get the first entity matching the given attributes.
65
-     *
66
-     * @param  array  $attributes
67
-     * @return \Analogue\ORM\Mappable|null
68
-     */
69
-    public function firstMatching(array $attributes)
70
-    {
71
-        return $this->mapper->where($attributes)->first();
72
-    }
63
+	/**
64
+	 * Get the first entity matching the given attributes.
65
+	 *
66
+	 * @param  array  $attributes
67
+	 * @return \Analogue\ORM\Mappable|null
68
+	 */
69
+	public function firstMatching(array $attributes)
70
+	{
71
+		return $this->mapper->where($attributes)->first();
72
+	}
73 73
 
74
-    /**
75
-     * Return all the entities matching the given attributes
76
-     *
77
-     * @param array $attributes
78
-     * @return \Analogue\ORM\EntityCollection
79
-     */
80
-    public function allMatching(array $attributes)
81
-    {
82
-        return $this->mapper->where($attributes)->get();
83
-    }
74
+	/**
75
+	 * Return all the entities matching the given attributes
76
+	 *
77
+	 * @param array $attributes
78
+	 * @return \Analogue\ORM\EntityCollection
79
+	 */
80
+	public function allMatching(array $attributes)
81
+	{
82
+		return $this->mapper->where($attributes)->get();
83
+	}
84 84
 
85
-    /**
86
-     * Return a paginator instance on the EntityCollection
87
-     *
88
-     * @param  int $perPage number of item per page (fallback on default setup in entity map)
89
-     * @return
90
-     */
91
-    public function paginate($perPage = null)
92
-    {
93
-        return $this->mapper->paginate($perPage);
94
-    }
85
+	/**
86
+	 * Return a paginator instance on the EntityCollection
87
+	 *
88
+	 * @param  int $perPage number of item per page (fallback on default setup in entity map)
89
+	 * @return
90
+	 */
91
+	public function paginate($perPage = null)
92
+	{
93
+		return $this->mapper->paginate($perPage);
94
+	}
95 95
 
96
-    /**
97
-     * Delete an entity or an entity collection from the database
98
-     *
99
-     * @param  Mappable|Collection $entity
100
-     * @return null
101
-     */
102
-    public function delete($entity)
103
-    {
104
-        return $this->mapper->delete($entity);
105
-    }
96
+	/**
97
+	 * Delete an entity or an entity collection from the database
98
+	 *
99
+	 * @param  Mappable|Collection $entity
100
+	 * @return null
101
+	 */
102
+	public function delete($entity)
103
+	{
104
+		return $this->mapper->delete($entity);
105
+	}
106 106
 
107
-    /**
108
-     * Persist an entity or an entity collection in the database.
109
-     *
110
-     * @param  Mappable|Collection|array $entity
111
-     * @return Mappable|Collection|array
112
-     */
113
-    public function store($entity)
114
-    {
115
-        return $this->mapper->store($entity);
116
-    }
107
+	/**
108
+	 * Persist an entity or an entity collection in the database.
109
+	 *
110
+	 * @param  Mappable|Collection|array $entity
111
+	 * @return Mappable|Collection|array
112
+	 */
113
+	public function store($entity)
114
+	{
115
+		return $this->mapper->store($entity);
116
+	}
117 117
 
118
-    /**
119
-     * Make custom mapper custom commands available in repository
120
-     *
121
-     * @param  string  $method
122
-     * @param  array   $parameters
123
-     * @return mixed
124
-     */
125
-    public function __call($method, $parameters)
126
-    {
127
-        if ($this->mapper->hasCustomCommand($method)) {
128
-            call_user_func_array(array($this->mapper, $method), $parameters);
129
-        } else {
130
-            throw new Exception("No method $method on ".get_class($this));
131
-        }
132
-    }
118
+	/**
119
+	 * Make custom mapper custom commands available in repository
120
+	 *
121
+	 * @param  string  $method
122
+	 * @param  array   $parameters
123
+	 * @return mixed
124
+	 */
125
+	public function __call($method, $parameters)
126
+	{
127
+		if ($this->mapper->hasCustomCommand($method)) {
128
+			call_user_func_array(array($this->mapper, $method), $parameters);
129
+		} else {
130
+			throw new Exception("No method $method on ".get_class($this));
131
+		}
132
+	}
133 133
 }
Please login to merge, or discard this patch.
src/System/Aggregate.php 4 patches
Doc Comments   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -151,7 +151,7 @@  discard block
 block discarded – undo
151 151
      * Parse a 'single' relationship
152 152
      *
153 153
      * @param  string $relation
154
-     * @return void|boolean
154
+     * @return boolean
155 155
      */
156 156
     protected function parseSingleRelationship($relation)
157 157
     {
@@ -194,7 +194,7 @@  discard block
 block discarded – undo
194 194
      * Check if value isn't parent or root in the aggregate
195 195
      *
196 196
      * @param  mixed
197
-     * @return boolean
197
+     * @return boolean|null
198 198
      */
199 199
     protected function isParentOrRoot($value)
200 200
     {
@@ -274,6 +274,7 @@  discard block
 block discarded – undo
274 274
      * Create a child, aggregated entity
275 275
      *
276 276
      * @param  mixed $entities
277
+     * @param string $relation
277 278
      * @return
278 279
      */
279 280
     protected function createSubAggregates($entities, $relation)
@@ -656,7 +657,6 @@  discard block
 block discarded – undo
656 657
      * Get foreign key attribute(s) from a parent entity in this
657 658
      * aggregate context
658 659
      *
659
-     * @param  string $relation
660 660
      * @return array
661 661
      */
662 662
     protected function getForeignKeyAttributesFromParent()
Please login to merge, or discard this patch.
Indentation   +984 added lines, -984 removed lines patch added patch discarded remove patch
@@ -14,1003 +14,1003 @@
 block discarded – undo
14 14
 class Aggregate implements InternallyMappable
15 15
 {
16 16
 
17
-    /**
18
-     * The Root Entity
19
-     *
20
-     * @var \Analogue\ORM\System\Wrappers\Wrapper
21
-     */
22
-    protected $wrappedEntity;
23
-
24
-    /**
25
-     * Parent Root Aggregate
26
-     *
27
-     * @var \Analogue\ORM\System\Aggregate
28
-     */
29
-    protected $parent;
30
-
31
-    /**
32
-     * Parent's relationship method
33
-     *
34
-     * @var string
35
-     */
36
-    protected $parentRelationship;
37
-
38
-    /**
39
-     * Root Entity
40
-     *
41
-     * @var \Analogue\ORM\System\Aggregate
42
-     */
43
-    protected $root;
44
-
45
-    /**
46
-     * An associative array containing entity's
47
-     * relationships converted to Aggregates
48
-     *
49
-     * @var array
50
-     */
51
-    protected $relationships = [];
52
-
53
-    /**
54
-     * Relationship that need post-command synchronization
55
-     *
56
-     * @var array
57
-     */
58
-    protected $needSync = [];
59
-
60
-    /**
61
-     * Mapper
62
-     *
63
-     * @var \Analogue\ORM\System\Mapper;
64
-     */
65
-    protected $mapper;
66
-
67
-    /**
68
-     * Entity Map
69
-     *
70
-     * @var \Analogue\ORM\EntityMap;
71
-     */
72
-    protected $entityMap;
73
-
74
-    /**
75
-     * Create a new Aggregated Entity instance
76
-     *
77
-     * @param mixed          $entity             [description]
78
-     * @param Aggregate|null $parent             [description]
79
-     * @param string         $parentRelationship [description]
80
-     * @param Aggregate|null $root               [description]
81
-     */
82
-    public function __construct($entity, Aggregate $parent = null, $parentRelationship = null, Aggregate $root = null)
83
-    {
84
-        $factory = new Factory;
85
-
86
-        $this->wrappedEntity = $factory->make($entity);
87
-
88
-        $this->parent = $parent;
89
-
90
-        $this->parentRelationship = $parentRelationship;
91
-
92
-        $this->root = $root;
93
-
94
-        $this->mapper = Manager::getMapper($entity);
95
-
96
-        $this->entityMap = $this->mapper->getEntityMap();
17
+	/**
18
+	 * The Root Entity
19
+	 *
20
+	 * @var \Analogue\ORM\System\Wrappers\Wrapper
21
+	 */
22
+	protected $wrappedEntity;
23
+
24
+	/**
25
+	 * Parent Root Aggregate
26
+	 *
27
+	 * @var \Analogue\ORM\System\Aggregate
28
+	 */
29
+	protected $parent;
30
+
31
+	/**
32
+	 * Parent's relationship method
33
+	 *
34
+	 * @var string
35
+	 */
36
+	protected $parentRelationship;
37
+
38
+	/**
39
+	 * Root Entity
40
+	 *
41
+	 * @var \Analogue\ORM\System\Aggregate
42
+	 */
43
+	protected $root;
44
+
45
+	/**
46
+	 * An associative array containing entity's
47
+	 * relationships converted to Aggregates
48
+	 *
49
+	 * @var array
50
+	 */
51
+	protected $relationships = [];
52
+
53
+	/**
54
+	 * Relationship that need post-command synchronization
55
+	 *
56
+	 * @var array
57
+	 */
58
+	protected $needSync = [];
59
+
60
+	/**
61
+	 * Mapper
62
+	 *
63
+	 * @var \Analogue\ORM\System\Mapper;
64
+	 */
65
+	protected $mapper;
66
+
67
+	/**
68
+	 * Entity Map
69
+	 *
70
+	 * @var \Analogue\ORM\EntityMap;
71
+	 */
72
+	protected $entityMap;
73
+
74
+	/**
75
+	 * Create a new Aggregated Entity instance
76
+	 *
77
+	 * @param mixed          $entity             [description]
78
+	 * @param Aggregate|null $parent             [description]
79
+	 * @param string         $parentRelationship [description]
80
+	 * @param Aggregate|null $root               [description]
81
+	 */
82
+	public function __construct($entity, Aggregate $parent = null, $parentRelationship = null, Aggregate $root = null)
83
+	{
84
+		$factory = new Factory;
85
+
86
+		$this->wrappedEntity = $factory->make($entity);
87
+
88
+		$this->parent = $parent;
89
+
90
+		$this->parentRelationship = $parentRelationship;
91
+
92
+		$this->root = $root;
93
+
94
+		$this->mapper = Manager::getMapper($entity);
95
+
96
+		$this->entityMap = $this->mapper->getEntityMap();
97 97
              
98
-        $this->parseRelationships();
99
-    }
100
-
101
-    /**
102
-     * Parse Every relationships defined on the entity
103
-     *
104
-     * @return void
105
-     */
106
-    protected function parseRelationships()
107
-    {
108
-        foreach ($this->entityMap->getSingleRelationships() as $relation) {
109
-            $this->parseSingleRelationship($relation);
110
-        }
111
-
112
-        foreach ($this->entityMap->getManyRelationships() as $relation) {
113
-            $this->parseManyRelationship($relation);
114
-        }
115
-    }
116
-
117
-    /**
118
-     * Parse for values common to single & many relations
119
-     *
120
-     * @param  string    $relation
121
-     * @return mixed|boolean
122
-     */
123
-    protected function parseForCommonValues($relation)
124
-    {
125
-        if (! $this->hasAttribute($relation)) {
126
-            // If no attribute exists for this relationships
127
-            // we'll make it a simple empty array. This will
128
-            // save us from constantly checking for the attributes
129
-            // actual existence.
130
-            $this->relationships[$relation] = [];
131
-            return false;
132
-        }
133
-
134
-        $value = $this->getRelationshipValue($relation);
135
-
136
-        if (is_null($value)) {
137
-            $this->relationships[$relation] = [];
138
-
139
-            // If the relationship's content is the null value
140
-            // and the Entity's exist in DB, we'll interpret this
141
-            // as the need to detach all related Entities,
142
-            // therefore a sync operation is needed.
143
-            $this->needSync[] = $relation;
144
-            return false;
145
-        }
146
-
147
-        return $value;
148
-    }
149
-
150
-    /**
151
-     * Parse a 'single' relationship
152
-     *
153
-     * @param  string $relation
154
-     * @return void|boolean
155
-     */
156
-    protected function parseSingleRelationship($relation)
157
-    {
158
-        if (! $value = $this->parseForCommonValues($relation)) {
159
-            return true;
160
-        }
98
+		$this->parseRelationships();
99
+	}
100
+
101
+	/**
102
+	 * Parse Every relationships defined on the entity
103
+	 *
104
+	 * @return void
105
+	 */
106
+	protected function parseRelationships()
107
+	{
108
+		foreach ($this->entityMap->getSingleRelationships() as $relation) {
109
+			$this->parseSingleRelationship($relation);
110
+		}
111
+
112
+		foreach ($this->entityMap->getManyRelationships() as $relation) {
113
+			$this->parseManyRelationship($relation);
114
+		}
115
+	}
116
+
117
+	/**
118
+	 * Parse for values common to single & many relations
119
+	 *
120
+	 * @param  string    $relation
121
+	 * @return mixed|boolean
122
+	 */
123
+	protected function parseForCommonValues($relation)
124
+	{
125
+		if (! $this->hasAttribute($relation)) {
126
+			// If no attribute exists for this relationships
127
+			// we'll make it a simple empty array. This will
128
+			// save us from constantly checking for the attributes
129
+			// actual existence.
130
+			$this->relationships[$relation] = [];
131
+			return false;
132
+		}
133
+
134
+		$value = $this->getRelationshipValue($relation);
135
+
136
+		if (is_null($value)) {
137
+			$this->relationships[$relation] = [];
138
+
139
+			// If the relationship's content is the null value
140
+			// and the Entity's exist in DB, we'll interpret this
141
+			// as the need to detach all related Entities,
142
+			// therefore a sync operation is needed.
143
+			$this->needSync[] = $relation;
144
+			return false;
145
+		}
146
+
147
+		return $value;
148
+	}
149
+
150
+	/**
151
+	 * Parse a 'single' relationship
152
+	 *
153
+	 * @param  string $relation
154
+	 * @return void|boolean
155
+	 */
156
+	protected function parseSingleRelationship($relation)
157
+	{
158
+		if (! $value = $this->parseForCommonValues($relation)) {
159
+			return true;
160
+		}
161 161
         
162
-        if ($value instanceof Collection || is_array($value) || $value instanceof CollectionProxy) {
163
-            throw new MappingException("Entity's attribute $relation should not be array, or collection");
164
-        }
165
-
166
-        if ($value instanceof EntityProxy && ! $value->isLoaded()) {
167
-            $this->relationships[$relation] = [];
168
-            return true;
169
-        }
170
-
171
-        // If the attribute is a loaded proxy, swap it for its
172
-        // loaded entity.
173
-        if ($value instanceof EntityProxy && $value->isLoaded()) {
174
-            $value = $value->getUnderlyingObject();
175
-        }
176
-
177
-        if ($this->isParentOrRoot($value)) {
178
-            $this->relationships[$relation] = [];
179
-            return true;
180
-        }
181
-
182
-        // At this point, we can assume the attribute is an Entity instance
183
-        // so we'll treat it as such.
184
-        $subAggregate = $this->createSubAggregate($value, $relation);
162
+		if ($value instanceof Collection || is_array($value) || $value instanceof CollectionProxy) {
163
+			throw new MappingException("Entity's attribute $relation should not be array, or collection");
164
+		}
165
+
166
+		if ($value instanceof EntityProxy && ! $value->isLoaded()) {
167
+			$this->relationships[$relation] = [];
168
+			return true;
169
+		}
170
+
171
+		// If the attribute is a loaded proxy, swap it for its
172
+		// loaded entity.
173
+		if ($value instanceof EntityProxy && $value->isLoaded()) {
174
+			$value = $value->getUnderlyingObject();
175
+		}
176
+
177
+		if ($this->isParentOrRoot($value)) {
178
+			$this->relationships[$relation] = [];
179
+			return true;
180
+		}
181
+
182
+		// At this point, we can assume the attribute is an Entity instance
183
+		// so we'll treat it as such.
184
+		$subAggregate = $this->createSubAggregate($value, $relation);
185 185
        
186
-        // Even if it's a single entity, we'll store it as an array
187
-        // just for consistency with other relationships
188
-        $this->relationships[$relation] = [$subAggregate];
186
+		// Even if it's a single entity, we'll store it as an array
187
+		// just for consistency with other relationships
188
+		$this->relationships[$relation] = [$subAggregate];
189 189
  
190
-        return true;
191
-    }
192
-
193
-    /**
194
-     * Check if value isn't parent or root in the aggregate
195
-     *
196
-     * @param  mixed
197
-     * @return boolean
198
-     */
199
-    protected function isParentOrRoot($value)
200
-    {
201
-        if (! is_null($this->root)) {
202
-            $rootClass = get_class($this->root->getEntityObject());
203
-            if ($rootClass == get_class($value)) {
204
-                return true;
205
-            }
206
-        }
207
-
208
-
209
-        if (! is_null($this->parent)) {
210
-            $parentClass = get_class($this->parent->getEntityObject());
211
-            if ($parentClass == get_class($value)) {
212
-                return true;
213
-            }
214
-        }
215
-    }
216
-
217
-    /**
218
-     * Parse a 'many' relationship
219
-     *
220
-     * @param  string $relation
221
-     * @return boolean
222
-     */
223
-    protected function parseManyRelationship($relation)
224
-    {
225
-        if (! $value = $this->parseForCommonValues($relation)) {
226
-            return true;
227
-        }
228
-
229
-        if (is_array($value) || $value instanceof Collection) {
230
-            $this->needSync[] = $relation;
231
-        }
232
-        // If the relation is a proxy, we test is the relation
233
-        // has been lazy loaded, otherwise we'll just treat
234
-        // the subset of newly added items.
235
-        if ($value instanceof CollectionProxy && $value->isLoaded()) {
236
-            $this->needSync[] = $relation;
237
-            $value = $value->getUnderlyingCollection();
238
-        }
239
-
240
-        if ($value instanceof CollectionProxy && ! $value->isLoaded()) {
241
-            $value = $value->getAddedItems();
242
-        }
243
-
244
-        // At this point $value should be either an array or an instance
245
-        // of a collection class.
246
-        if (! is_array($value) && ! $value instanceof Collection) {
247
-            throw new MappingException("'$relation' attribute should be array() or Collection");
248
-        }
249
-
250
-        $this->relationships[$relation] = $this->createSubAggregates($value, $relation);
190
+		return true;
191
+	}
192
+
193
+	/**
194
+	 * Check if value isn't parent or root in the aggregate
195
+	 *
196
+	 * @param  mixed
197
+	 * @return boolean
198
+	 */
199
+	protected function isParentOrRoot($value)
200
+	{
201
+		if (! is_null($this->root)) {
202
+			$rootClass = get_class($this->root->getEntityObject());
203
+			if ($rootClass == get_class($value)) {
204
+				return true;
205
+			}
206
+		}
207
+
208
+
209
+		if (! is_null($this->parent)) {
210
+			$parentClass = get_class($this->parent->getEntityObject());
211
+			if ($parentClass == get_class($value)) {
212
+				return true;
213
+			}
214
+		}
215
+	}
216
+
217
+	/**
218
+	 * Parse a 'many' relationship
219
+	 *
220
+	 * @param  string $relation
221
+	 * @return boolean
222
+	 */
223
+	protected function parseManyRelationship($relation)
224
+	{
225
+		if (! $value = $this->parseForCommonValues($relation)) {
226
+			return true;
227
+		}
228
+
229
+		if (is_array($value) || $value instanceof Collection) {
230
+			$this->needSync[] = $relation;
231
+		}
232
+		// If the relation is a proxy, we test is the relation
233
+		// has been lazy loaded, otherwise we'll just treat
234
+		// the subset of newly added items.
235
+		if ($value instanceof CollectionProxy && $value->isLoaded()) {
236
+			$this->needSync[] = $relation;
237
+			$value = $value->getUnderlyingCollection();
238
+		}
239
+
240
+		if ($value instanceof CollectionProxy && ! $value->isLoaded()) {
241
+			$value = $value->getAddedItems();
242
+		}
243
+
244
+		// At this point $value should be either an array or an instance
245
+		// of a collection class.
246
+		if (! is_array($value) && ! $value instanceof Collection) {
247
+			throw new MappingException("'$relation' attribute should be array() or Collection");
248
+		}
249
+
250
+		$this->relationships[$relation] = $this->createSubAggregates($value, $relation);
251 251
         
252
-        return true;
253
-    }
254
-
255
-    /**
256
-     * Return Entity's relationship attribute
257
-     *
258
-     * @param  string $relation
259
-     * @return mixed
260
-     * @throws \Analogue\ORM\Exceptions\MappingException
261
-     */
262
-    protected function getRelationshipValue($relation)
263
-    {
264
-        $value = $this->getEntityAttribute($relation);
265
-
266
-        if (is_bool($value) || is_float($value) || is_int($value) || is_string($value)) {
267
-            throw new MappingException("Entity's attribute $relation should be array, object, collection or null");
268
-        }
269
-
270
-        return $value;
271
-    }
272
-
273
-    /**
274
-     * Create a child, aggregated entity
275
-     *
276
-     * @param  mixed $entities
277
-     * @return
278
-     */
279
-    protected function createSubAggregates($entities, $relation)
280
-    {
281
-        $aggregates = [];
282
-
283
-        foreach ($entities as $entity) {
284
-            $aggregates[] = $this->createSubAggregate($entity, $relation);
285
-        }
286
-
287
-        return $aggregates;
288
-    }
289
-
290
-    /**
291
-     * Create a related subAggregate
292
-     *
293
-     * @param  mixed $entity
294
-     * @return \Analogue\ORM\System\RootAggregate;
295
-     */
296
-    protected function createSubAggregate($entity, $relation)
297
-    {
298
-        // If root isn't defined, then this is the Aggregate Root
299
-        if (is_null($this->root)) {
300
-            $root = $this;
301
-        } else {
302
-            $root = $this->root;
303
-        }
304
-
305
-        $aggregate = new Aggregate($entity, $this, $relation, $root);
306
-
307
-        return $aggregate;
308
-    }
309
-
310
-    /**
311
-     * Get the Entity's primary key attribute
312
-     *
313
-     * @return string|integer
314
-     */
315
-    public function getEntityId()
316
-    {
317
-        return $this->wrappedEntity->getEntityAttribute($this->entityMap->getKeyName());
318
-    }
319
-
320
-    /**
321
-     * Get the name of the primary key
322
-     *
323
-     * @return string
324
-     */
325
-    public function getEntityKey()
326
-    {
327
-        return $this->entityMap->getKeyName();
328
-    }
329
-
330
-    /**
331
-     * Return the entity map for the current entity
332
-     *
333
-     * @return \Analogue\ORM\EntityMap
334
-     */
335
-    public function getEntityMap()
336
-    {
337
-        return $this->entityMap;
338
-    }
339
-
340
-    /**
341
-     * Return the Entity's hash $class.$id
342
-     *
343
-     * @return string
344
-     */
345
-    public function getEntityHash()
346
-    {
347
-        return $this->getEntityClass().'.'.$this->getEntityId();
348
-    }
349
-
350
-    /**
351
-     * Get wrapped entity class
352
-     *
353
-     * @return
354
-     */
355
-    public function getEntityClass()
356
-    {
357
-        return $this->entityMap->getClass();
358
-    }
359
-
360
-    /**
361
-     * Return the Mapper's entity cache
362
-     *
363
-     * @return \Analogue\ORM\System\EntityCache
364
-     */
365
-    protected function getEntityCache()
366
-    {
367
-        return $this->mapper->getEntityCache();
368
-    }
369
-
370
-    /**
371
-     * Get a relationship as an aggregated entities' array
372
-     *
373
-     * @param  string $name
374
-     * @return array
375
-     */
376
-    public function getRelationship($name)
377
-    {
378
-        if (array_key_exists($name, $this->relationships)) {
379
-            return $this->relationships[$name];
380
-        } else {
381
-            return [];
382
-        }
383
-    }
384
-
385
-    /**
386
-     * [TO IMPLEMENT]
387
-     *
388
-     * @return array
389
-     */
390
-    public function getPivotAttributes()
391
-    {
392
-        return [];
393
-    }
394
-
395
-    /**
396
-     * Get Non existing related entities from several relationships
397
-     *
398
-     * @param  array  $relationships
399
-     * @return array
400
-     */
401
-    public function getNonExistingRelated(array $relationships)
402
-    {
403
-        $nonExisting = [];
404
-
405
-        foreach ($relationships as $relation) {
406
-            if ($this->hasAttribute($relation) && array_key_exists($relation, $this->relationships)) {
407
-                $nonExisting = array_merge($nonExisting, $this->getNonExistingFromRelation($relation));
408
-            }
409
-        }
410
-
411
-        return $nonExisting;
412
-    }
413
-
414
-    /**
415
-     * Get non-existing related entities from a single relation
416
-     *
417
-     * @param  string $relation
418
-     * @return array
419
-     */
420
-    protected function getNonExistingFromRelation($relation)
421
-    {
422
-        $nonExisting = [];
423
-
424
-        foreach ($this->relationships[$relation] as $aggregate) {
425
-            if (! $aggregate->exists()) {
426
-                $nonExisting[] = $aggregate;
427
-            }
428
-        }
429
-
430
-        return $nonExisting;
431
-    }
432
-
433
-    /**
434
-     * Synchronize relationships if needed
435
-     *
436
-     * @return
437
-     */
438
-    public function syncRelationships()
439
-    {
440
-        if ($this->exists()) {
441
-            foreach ($this->needSync as $relation) {
442
-                $this->synchronize($relation);
443
-            }
444
-        }
445
-    }
446
-
447
-    /**
448
-     * Synchronize a relationship attribute
449
-     *
450
-     * @return void
451
-     */
452
-    protected function synchronize($relation)
453
-    {
454
-        $actualContent = $this->relationships[$relation];
455
-
456
-        $this->entityMap->$relation($this->getEntityObject())->sync($actualContent);
457
-    }
458
-
459
-    /**
460
-     * Returns an array of Missing related Entities for the
461
-     * given $relation
462
-     *
463
-     * @param  string $relation
464
-     * @return array
465
-     */
466
-    public function getMissingEntities($relation)
467
-    {
468
-        $cachedRelations = $this->getCachedAttribute($relation);
469
-
470
-        if (! is_null($cachedRelations)) {
471
-            $missing = [];
472
-
473
-            foreach ($cachedRelations as $hash) {
474
-                if (! $this->getRelatedAggregateFromHash($hash, $relation)) {
475
-                    $missing[] = $hash;
476
-                }
477
-            }
478
-
479
-            return $missing;
480
-        } else {
481
-            return [];
482
-        }
483
-    }
252
+		return true;
253
+	}
254
+
255
+	/**
256
+	 * Return Entity's relationship attribute
257
+	 *
258
+	 * @param  string $relation
259
+	 * @return mixed
260
+	 * @throws \Analogue\ORM\Exceptions\MappingException
261
+	 */
262
+	protected function getRelationshipValue($relation)
263
+	{
264
+		$value = $this->getEntityAttribute($relation);
265
+
266
+		if (is_bool($value) || is_float($value) || is_int($value) || is_string($value)) {
267
+			throw new MappingException("Entity's attribute $relation should be array, object, collection or null");
268
+		}
269
+
270
+		return $value;
271
+	}
272
+
273
+	/**
274
+	 * Create a child, aggregated entity
275
+	 *
276
+	 * @param  mixed $entities
277
+	 * @return
278
+	 */
279
+	protected function createSubAggregates($entities, $relation)
280
+	{
281
+		$aggregates = [];
282
+
283
+		foreach ($entities as $entity) {
284
+			$aggregates[] = $this->createSubAggregate($entity, $relation);
285
+		}
286
+
287
+		return $aggregates;
288
+	}
289
+
290
+	/**
291
+	 * Create a related subAggregate
292
+	 *
293
+	 * @param  mixed $entity
294
+	 * @return \Analogue\ORM\System\RootAggregate;
295
+	 */
296
+	protected function createSubAggregate($entity, $relation)
297
+	{
298
+		// If root isn't defined, then this is the Aggregate Root
299
+		if (is_null($this->root)) {
300
+			$root = $this;
301
+		} else {
302
+			$root = $this->root;
303
+		}
304
+
305
+		$aggregate = new Aggregate($entity, $this, $relation, $root);
306
+
307
+		return $aggregate;
308
+	}
309
+
310
+	/**
311
+	 * Get the Entity's primary key attribute
312
+	 *
313
+	 * @return string|integer
314
+	 */
315
+	public function getEntityId()
316
+	{
317
+		return $this->wrappedEntity->getEntityAttribute($this->entityMap->getKeyName());
318
+	}
319
+
320
+	/**
321
+	 * Get the name of the primary key
322
+	 *
323
+	 * @return string
324
+	 */
325
+	public function getEntityKey()
326
+	{
327
+		return $this->entityMap->getKeyName();
328
+	}
329
+
330
+	/**
331
+	 * Return the entity map for the current entity
332
+	 *
333
+	 * @return \Analogue\ORM\EntityMap
334
+	 */
335
+	public function getEntityMap()
336
+	{
337
+		return $this->entityMap;
338
+	}
339
+
340
+	/**
341
+	 * Return the Entity's hash $class.$id
342
+	 *
343
+	 * @return string
344
+	 */
345
+	public function getEntityHash()
346
+	{
347
+		return $this->getEntityClass().'.'.$this->getEntityId();
348
+	}
349
+
350
+	/**
351
+	 * Get wrapped entity class
352
+	 *
353
+	 * @return
354
+	 */
355
+	public function getEntityClass()
356
+	{
357
+		return $this->entityMap->getClass();
358
+	}
359
+
360
+	/**
361
+	 * Return the Mapper's entity cache
362
+	 *
363
+	 * @return \Analogue\ORM\System\EntityCache
364
+	 */
365
+	protected function getEntityCache()
366
+	{
367
+		return $this->mapper->getEntityCache();
368
+	}
369
+
370
+	/**
371
+	 * Get a relationship as an aggregated entities' array
372
+	 *
373
+	 * @param  string $name
374
+	 * @return array
375
+	 */
376
+	public function getRelationship($name)
377
+	{
378
+		if (array_key_exists($name, $this->relationships)) {
379
+			return $this->relationships[$name];
380
+		} else {
381
+			return [];
382
+		}
383
+	}
384
+
385
+	/**
386
+	 * [TO IMPLEMENT]
387
+	 *
388
+	 * @return array
389
+	 */
390
+	public function getPivotAttributes()
391
+	{
392
+		return [];
393
+	}
394
+
395
+	/**
396
+	 * Get Non existing related entities from several relationships
397
+	 *
398
+	 * @param  array  $relationships
399
+	 * @return array
400
+	 */
401
+	public function getNonExistingRelated(array $relationships)
402
+	{
403
+		$nonExisting = [];
404
+
405
+		foreach ($relationships as $relation) {
406
+			if ($this->hasAttribute($relation) && array_key_exists($relation, $this->relationships)) {
407
+				$nonExisting = array_merge($nonExisting, $this->getNonExistingFromRelation($relation));
408
+			}
409
+		}
410
+
411
+		return $nonExisting;
412
+	}
413
+
414
+	/**
415
+	 * Get non-existing related entities from a single relation
416
+	 *
417
+	 * @param  string $relation
418
+	 * @return array
419
+	 */
420
+	protected function getNonExistingFromRelation($relation)
421
+	{
422
+		$nonExisting = [];
423
+
424
+		foreach ($this->relationships[$relation] as $aggregate) {
425
+			if (! $aggregate->exists()) {
426
+				$nonExisting[] = $aggregate;
427
+			}
428
+		}
429
+
430
+		return $nonExisting;
431
+	}
432
+
433
+	/**
434
+	 * Synchronize relationships if needed
435
+	 *
436
+	 * @return
437
+	 */
438
+	public function syncRelationships()
439
+	{
440
+		if ($this->exists()) {
441
+			foreach ($this->needSync as $relation) {
442
+				$this->synchronize($relation);
443
+			}
444
+		}
445
+	}
446
+
447
+	/**
448
+	 * Synchronize a relationship attribute
449
+	 *
450
+	 * @return void
451
+	 */
452
+	protected function synchronize($relation)
453
+	{
454
+		$actualContent = $this->relationships[$relation];
455
+
456
+		$this->entityMap->$relation($this->getEntityObject())->sync($actualContent);
457
+	}
458
+
459
+	/**
460
+	 * Returns an array of Missing related Entities for the
461
+	 * given $relation
462
+	 *
463
+	 * @param  string $relation
464
+	 * @return array
465
+	 */
466
+	public function getMissingEntities($relation)
467
+	{
468
+		$cachedRelations = $this->getCachedAttribute($relation);
469
+
470
+		if (! is_null($cachedRelations)) {
471
+			$missing = [];
472
+
473
+			foreach ($cachedRelations as $hash) {
474
+				if (! $this->getRelatedAggregateFromHash($hash, $relation)) {
475
+					$missing[] = $hash;
476
+				}
477
+			}
478
+
479
+			return $missing;
480
+		} else {
481
+			return [];
482
+		}
483
+	}
484 484
        
485
-    /**
486
-     * Get Relationships who have dirty attributes / dirty relationships
487
-     *
488
-     * @return array
489
-     */
490
-    public function getDirtyRelationships()
491
-    {
492
-        $dirtyAggregates = [];
493
-
494
-        foreach ($this->relationships as $relation) {
495
-            foreach ($relation as $aggregate) {
496
-                if (! $aggregate->exists() || $aggregate->isDirty() || count($aggregate->getDirtyRelationships() > 0)) {
497
-                    $dirtyAggregates[] = $aggregate;
498
-                }
499
-            }
500
-        }
501
-
502
-        return $dirtyAggregates;
503
-    }
485
+	/**
486
+	 * Get Relationships who have dirty attributes / dirty relationships
487
+	 *
488
+	 * @return array
489
+	 */
490
+	public function getDirtyRelationships()
491
+	{
492
+		$dirtyAggregates = [];
493
+
494
+		foreach ($this->relationships as $relation) {
495
+			foreach ($relation as $aggregate) {
496
+				if (! $aggregate->exists() || $aggregate->isDirty() || count($aggregate->getDirtyRelationships() > 0)) {
497
+					$dirtyAggregates[] = $aggregate;
498
+				}
499
+			}
500
+		}
501
+
502
+		return $dirtyAggregates;
503
+	}
504 504
     
505
-    /**
506
-     * Compare the object's raw attributes with the record in cache
507
-     *
508
-     * @return boolean
509
-     */
510
-    public function isDirty()
511
-    {
512
-        if (count($this->getDirtyRawAttributes()) > 0) {
513
-            return true;
514
-        } else {
515
-            return false;
516
-        }
517
-    }
518
-
519
-    /**
520
-     * Get Raw Entity's attributes, as they are represented
521
-     * in the database, including value objects & foreign keys
522
-     *
523
-     * @return array
524
-     */
525
-    public function getRawAttributes()
526
-    {
527
-        $attributes = $this->wrappedEntity->getEntityAttributes();
528
-
529
-        foreach ($this->entityMap->getRelationships() as $relation) {
530
-            unset($attributes[$relation]);
531
-        }
532
-
533
-        $attributes = $this->flattenEmbeddables($attributes);
534
-
535
-        $foreignKeys = $this->getForeignKeyAttributes();
536
-
537
-        return $attributes + $foreignKeys;
538
-    }
539
-
540
-    /**
541
-     * Convert Value Objects to raw db attributes
542
-     *
543
-     * @param  array $attributes
544
-     * @return array
545
-     */
546
-    protected function flattenEmbeddables($attributes)
547
-    {
548
-        $embeddables = $this->entityMap->getEmbeddables();
505
+	/**
506
+	 * Compare the object's raw attributes with the record in cache
507
+	 *
508
+	 * @return boolean
509
+	 */
510
+	public function isDirty()
511
+	{
512
+		if (count($this->getDirtyRawAttributes()) > 0) {
513
+			return true;
514
+		} else {
515
+			return false;
516
+		}
517
+	}
518
+
519
+	/**
520
+	 * Get Raw Entity's attributes, as they are represented
521
+	 * in the database, including value objects & foreign keys
522
+	 *
523
+	 * @return array
524
+	 */
525
+	public function getRawAttributes()
526
+	{
527
+		$attributes = $this->wrappedEntity->getEntityAttributes();
528
+
529
+		foreach ($this->entityMap->getRelationships() as $relation) {
530
+			unset($attributes[$relation]);
531
+		}
532
+
533
+		$attributes = $this->flattenEmbeddables($attributes);
534
+
535
+		$foreignKeys = $this->getForeignKeyAttributes();
536
+
537
+		return $attributes + $foreignKeys;
538
+	}
539
+
540
+	/**
541
+	 * Convert Value Objects to raw db attributes
542
+	 *
543
+	 * @param  array $attributes
544
+	 * @return array
545
+	 */
546
+	protected function flattenEmbeddables($attributes)
547
+	{
548
+		$embeddables = $this->entityMap->getEmbeddables();
549 549
         
550
-        foreach ($embeddables as $localKey => $embed) {
551
-            // Retrieve the value object from the entity's attributes
552
-            $valueObject = $attributes[$localKey];
550
+		foreach ($embeddables as $localKey => $embed) {
551
+			// Retrieve the value object from the entity's attributes
552
+			$valueObject = $attributes[$localKey];
553 553
 
554
-            // Unset the corresponding key
555
-            unset($attributes[$localKey]);
554
+			// Unset the corresponding key
555
+			unset($attributes[$localKey]);
556 556
 
557
-            // TODO Make wrapper object compatible with value objects
558
-            $valueObjectAttributes = $valueObject->getEntityAttributes();
557
+			// TODO Make wrapper object compatible with value objects
558
+			$valueObjectAttributes = $valueObject->getEntityAttributes();
559 559
 
560
-            // Now (if setup in the entity map) we prefix the value object's
561
-            // attributes with the snake_case name of the embedded class.
562
-            $prefix = snake_case(class_basename($embed));
560
+			// Now (if setup in the entity map) we prefix the value object's
561
+			// attributes with the snake_case name of the embedded class.
562
+			$prefix = snake_case(class_basename($embed));
563 563
 
564
-            foreach ($valueObjectAttributes as $key=>$value) {
565
-                $valueObjectAttributes[$prefix.'_'.$key] = $value;
566
-                unset($valueObjectAttributes[$key]);
567
-            }
564
+			foreach ($valueObjectAttributes as $key=>$value) {
565
+				$valueObjectAttributes[$prefix.'_'.$key] = $value;
566
+				unset($valueObjectAttributes[$key]);
567
+			}
568 568
 
569
-            $attributes = array_merge($attributes, $valueObjectAttributes);
570
-        }
569
+			$attributes = array_merge($attributes, $valueObjectAttributes);
570
+		}
571 571
         
572
-        return $attributes;
573
-    }
574
-
575
-    /**
576
-     * Return's entity raw attributes in the state they were at last
577
-     * query.
578
-     *
579
-     * @return array
580
-     */
581
-    protected function getCachedRawAttributes(array $columns = null)
582
-    {
583
-        $cachedAttributes = $this->getCache()->get($this->getEntityId());
584
-
585
-        if (is_null($columns)) {
586
-            return $cachedAttributes;
587
-        } else {
588
-            return array_only($cachedAttributes, $columns);
589
-        }
590
-    }
591
-
592
-    /**
593
-     * Return a single attribute from the cache
594
-     * @param  string $key
595
-     * @return mixed
596
-     */
597
-    protected function getCachedAttribute($key)
598
-    {
599
-        $cachedAttributes = $this->getCache()->get($this->getEntityId());
600
-
601
-        if (! array_key_exists($key, $cachedAttributes)) {
602
-            return null;
603
-        } else {
604
-            return $cachedAttributes[$key];
605
-        }
606
-    }
607
-
608
-    /**
609
-     * Convert related Entity's attributes to foreign keys
610
-     *
611
-     * @return array
612
-     */
613
-    protected function getForeignKeyAttributes()
614
-    {
615
-        $foreignKeys = [];
616
-
617
-        foreach ($this->entityMap->getLocalRelationships() as $relation) {
618
-            // check if relationship has been parsed, meaning it has an actual object
619
-            // in the entity's attributes
620
-            if ($this->isActualRelationships($relation)) {
621
-                $foreignKeys = $foreignKeys + $this->getForeignKeyAttributesFromRelation($relation);
622
-            }
623
-        }
624
-
625
-        if (! is_null($this->parent)) {
626
-            $foreignKeys = $foreignKeys + $this->getForeignKeyAttributesFromParent();
627
-        }
628
-
629
-        return $foreignKeys;
630
-    }
631
-
632
-    /**
633
-     * Return an associative array containing the key-value pair(s) from
634
-     * the related entity.
635
-     *
636
-     * @param  string $relation
637
-     * @return array
638
-     */
639
-    protected function getForeignKeyAttributesFromRelation($relation)
640
-    {
641
-        $localRelations = $this->entityMap->getLocalRelationships();
642
-
643
-        if (in_array($relation, $localRelations)) {
644
-            // Call Relationship's method
645
-            $relationship = $this->entityMap->$relation($this->getEntityObject());
646
-
647
-            $relatedAggregate = $this->relationships[$relation][0];
648
-
649
-            return $relationship->getForeignKeyValuePair($relatedAggregate->getEntityObject());
650
-        } else {
651
-            return [];
652
-        }
653
-    }
654
-
655
-    /**
656
-     * Get foreign key attribute(s) from a parent entity in this
657
-     * aggregate context
658
-     *
659
-     * @param  string $relation
660
-     * @return array
661
-     */
662
-    protected function getForeignKeyAttributesFromParent()
663
-    {
664
-        $parentMap = $this->parent->getEntityMap();
665
-
666
-        $parentForeignRelations = $parentMap->getForeignRelationships();
667
-        $parentPivotRelations = $parentMap->getPivotRelationships();
668
-
669
-        $parentRelation = $this->parentRelationship;
670
-
671
-        if (in_array($parentRelation, $parentForeignRelations)
672
-            && ! in_array($parentRelation, $parentPivotRelations)) {
673
-            $parentObject = $this->parent->getEntityObject();
674
-
675
-            // Call Relationship's method on parent map
676
-            $relationship = $parentMap->$parentRelation($parentObject);
677
-
678
-            return $relationship->getForeignKeyValuePair();
679
-        } else {
680
-            return [];
681
-        }
682
-    }
683
-
684
-    /**
685
-     * Update Pivot records on loaded relationships, by comparing the
686
-     * values from the Entity Cache to the actual relationship inside
687
-     * the aggregated entity.
688
-     *
689
-     * @return void
690
-     */
691
-    public function updatePivotRecords()
692
-    {
693
-        $pivots = $this->entityMap->getPivotRelationships();
694
-
695
-        foreach ($pivots as $pivot) {
696
-            if (array_key_exists($pivot, $this->relationships)) {
697
-                $this->updatePivotRelation($pivot);
698
-            }
699
-        }
700
-    }
701
-
702
-    /**
703
-     * Update Single pivot relationship
704
-     *
705
-     * @param  string $relation
706
-     * @return void
707
-     */
708
-    protected function updatePivotRelation($relation)
709
-    {
710
-        $hashes = $this->getEntityHashesFromRelation($relation);
711
-
712
-        $cachedAttributes = $this->getCachedRawAttributes();
713
-
714
-        if (array_key_exists($relation, $cachedAttributes)) {
715
-            // Compare the two array of hashes to find out existing
716
-            // pivot records, and the ones to be created.
717
-            $new = array_diff($hashes, array_keys($cachedAttributes[$relation]));
718
-            $existing = array_intersect($hashes, array_keys($cachedAttributes[$relation]));
719
-        } else {
720
-            $existing = [];
721
-            $new = $hashes;
722
-        }
723
-
724
-        if (count($new) > 0) {
725
-            $relatedCollection = $this->getEntityAttribute($relation);
726
-
727
-            $pivots = $this->getRelatedAggregatesFromHashes($new, $relation);
728
-
729
-            $this->entityMap->$relation($this->getEntityObject())->createPivots($pivots);
730
-        }
731
-
732
-        if (count($existing) > 0) {
733
-            foreach ($existing as $pivotHash) {
734
-                $this->updatePivotIfDirty($pivotHash, $relation);
735
-            }
736
-        }
737
-    }
738
-
739
-    /**
740
-     * Compare existing pivot record in cache and update it
741
-     * if the pivot attributes are dirty
742
-     *
743
-     * @param  string $pivotHash
744
-     * @param  string $relation
745
-     * @return void
746
-     */
747
-    protected function updatePivotIfDirty($pivotHash, $relation)
748
-    {
749
-        $aggregate = $this->getRelatedAggregateFromHash($pivotHash, $relation);
750
-
751
-        if ($aggregate->hasAttribute('pivot')) {
752
-            $pivot = $aggregate->getEntityAttribute('pivot')->getEntityAttributes();
753
-
754
-            $cachedPivotAttributes = $this->getPivotAttributesFromCache($pivotHash, $relation);
755
-
756
-            $actualPivotAttributes = array_only($pivot, array_keys($cachedPivotAttributes));
757
-
758
-            $dirty = $this->getDirtyAttributes($actualPivotAttributes, $cachedPivotAttributes);
572
+		return $attributes;
573
+	}
574
+
575
+	/**
576
+	 * Return's entity raw attributes in the state they were at last
577
+	 * query.
578
+	 *
579
+	 * @return array
580
+	 */
581
+	protected function getCachedRawAttributes(array $columns = null)
582
+	{
583
+		$cachedAttributes = $this->getCache()->get($this->getEntityId());
584
+
585
+		if (is_null($columns)) {
586
+			return $cachedAttributes;
587
+		} else {
588
+			return array_only($cachedAttributes, $columns);
589
+		}
590
+	}
591
+
592
+	/**
593
+	 * Return a single attribute from the cache
594
+	 * @param  string $key
595
+	 * @return mixed
596
+	 */
597
+	protected function getCachedAttribute($key)
598
+	{
599
+		$cachedAttributes = $this->getCache()->get($this->getEntityId());
600
+
601
+		if (! array_key_exists($key, $cachedAttributes)) {
602
+			return null;
603
+		} else {
604
+			return $cachedAttributes[$key];
605
+		}
606
+	}
607
+
608
+	/**
609
+	 * Convert related Entity's attributes to foreign keys
610
+	 *
611
+	 * @return array
612
+	 */
613
+	protected function getForeignKeyAttributes()
614
+	{
615
+		$foreignKeys = [];
616
+
617
+		foreach ($this->entityMap->getLocalRelationships() as $relation) {
618
+			// check if relationship has been parsed, meaning it has an actual object
619
+			// in the entity's attributes
620
+			if ($this->isActualRelationships($relation)) {
621
+				$foreignKeys = $foreignKeys + $this->getForeignKeyAttributesFromRelation($relation);
622
+			}
623
+		}
624
+
625
+		if (! is_null($this->parent)) {
626
+			$foreignKeys = $foreignKeys + $this->getForeignKeyAttributesFromParent();
627
+		}
628
+
629
+		return $foreignKeys;
630
+	}
631
+
632
+	/**
633
+	 * Return an associative array containing the key-value pair(s) from
634
+	 * the related entity.
635
+	 *
636
+	 * @param  string $relation
637
+	 * @return array
638
+	 */
639
+	protected function getForeignKeyAttributesFromRelation($relation)
640
+	{
641
+		$localRelations = $this->entityMap->getLocalRelationships();
642
+
643
+		if (in_array($relation, $localRelations)) {
644
+			// Call Relationship's method
645
+			$relationship = $this->entityMap->$relation($this->getEntityObject());
646
+
647
+			$relatedAggregate = $this->relationships[$relation][0];
648
+
649
+			return $relationship->getForeignKeyValuePair($relatedAggregate->getEntityObject());
650
+		} else {
651
+			return [];
652
+		}
653
+	}
654
+
655
+	/**
656
+	 * Get foreign key attribute(s) from a parent entity in this
657
+	 * aggregate context
658
+	 *
659
+	 * @param  string $relation
660
+	 * @return array
661
+	 */
662
+	protected function getForeignKeyAttributesFromParent()
663
+	{
664
+		$parentMap = $this->parent->getEntityMap();
665
+
666
+		$parentForeignRelations = $parentMap->getForeignRelationships();
667
+		$parentPivotRelations = $parentMap->getPivotRelationships();
668
+
669
+		$parentRelation = $this->parentRelationship;
670
+
671
+		if (in_array($parentRelation, $parentForeignRelations)
672
+			&& ! in_array($parentRelation, $parentPivotRelations)) {
673
+			$parentObject = $this->parent->getEntityObject();
674
+
675
+			// Call Relationship's method on parent map
676
+			$relationship = $parentMap->$parentRelation($parentObject);
677
+
678
+			return $relationship->getForeignKeyValuePair();
679
+		} else {
680
+			return [];
681
+		}
682
+	}
683
+
684
+	/**
685
+	 * Update Pivot records on loaded relationships, by comparing the
686
+	 * values from the Entity Cache to the actual relationship inside
687
+	 * the aggregated entity.
688
+	 *
689
+	 * @return void
690
+	 */
691
+	public function updatePivotRecords()
692
+	{
693
+		$pivots = $this->entityMap->getPivotRelationships();
694
+
695
+		foreach ($pivots as $pivot) {
696
+			if (array_key_exists($pivot, $this->relationships)) {
697
+				$this->updatePivotRelation($pivot);
698
+			}
699
+		}
700
+	}
701
+
702
+	/**
703
+	 * Update Single pivot relationship
704
+	 *
705
+	 * @param  string $relation
706
+	 * @return void
707
+	 */
708
+	protected function updatePivotRelation($relation)
709
+	{
710
+		$hashes = $this->getEntityHashesFromRelation($relation);
711
+
712
+		$cachedAttributes = $this->getCachedRawAttributes();
713
+
714
+		if (array_key_exists($relation, $cachedAttributes)) {
715
+			// Compare the two array of hashes to find out existing
716
+			// pivot records, and the ones to be created.
717
+			$new = array_diff($hashes, array_keys($cachedAttributes[$relation]));
718
+			$existing = array_intersect($hashes, array_keys($cachedAttributes[$relation]));
719
+		} else {
720
+			$existing = [];
721
+			$new = $hashes;
722
+		}
723
+
724
+		if (count($new) > 0) {
725
+			$relatedCollection = $this->getEntityAttribute($relation);
726
+
727
+			$pivots = $this->getRelatedAggregatesFromHashes($new, $relation);
728
+
729
+			$this->entityMap->$relation($this->getEntityObject())->createPivots($pivots);
730
+		}
731
+
732
+		if (count($existing) > 0) {
733
+			foreach ($existing as $pivotHash) {
734
+				$this->updatePivotIfDirty($pivotHash, $relation);
735
+			}
736
+		}
737
+	}
738
+
739
+	/**
740
+	 * Compare existing pivot record in cache and update it
741
+	 * if the pivot attributes are dirty
742
+	 *
743
+	 * @param  string $pivotHash
744
+	 * @param  string $relation
745
+	 * @return void
746
+	 */
747
+	protected function updatePivotIfDirty($pivotHash, $relation)
748
+	{
749
+		$aggregate = $this->getRelatedAggregateFromHash($pivotHash, $relation);
750
+
751
+		if ($aggregate->hasAttribute('pivot')) {
752
+			$pivot = $aggregate->getEntityAttribute('pivot')->getEntityAttributes();
753
+
754
+			$cachedPivotAttributes = $this->getPivotAttributesFromCache($pivotHash, $relation);
755
+
756
+			$actualPivotAttributes = array_only($pivot, array_keys($cachedPivotAttributes));
757
+
758
+			$dirty = $this->getDirtyAttributes($actualPivotAttributes, $cachedPivotAttributes);
759 759
             
760
-            if (count($dirty) > 0) {
761
-                $id = $aggregate->getEntityId();
762
-
763
-                $this->entityMap->$relation($this->getEntityObject())->updateExistingPivot($id, $dirty);
764
-            }
765
-        }
766
-    }
767
-
768
-    /**
769
-     * Compare two attributes array and return dirty attributes
770
-     *
771
-     * @param  array  $actual
772
-     * @param  array  $cached
773
-     * @return array
774
-     */
775
-    protected function getDirtyAttributes(array $actual, array $cached)
776
-    {
777
-        $dirty = [];
778
-
779
-        foreach ($actual as $key => $value) {
780
-            if (! $this->originalIsNumericallyEquivalent($value, $cached[$key])) {
781
-                $dirty[$key] = $actual[$key];
782
-            }
783
-        }
784
-
785
-        return $dirty;
786
-    }
787
-
788
-    /**
789
-     *
790
-     * @param  string $pivotHash
791
-     * @param  string $relation
792
-     * @return array
793
-     */
794
-    protected function getPivotAttributesFromCache($pivotHash, $relation)
795
-    {
796
-        $cachedAttributes = $this->getCachedRawAttributes();
797
-
798
-        $cachedRelations = $cachedAttributes[$relation];
799
-
800
-        foreach ($cachedRelations as $cachedRelation) {
801
-            if ($cachedRelation == $pivotHash) {
802
-                return $cachedRelation->getPivotAttributes();
803
-            }
804
-        }
805
-    }
806
-
807
-    /**
808
-     * Returns an array of related Aggregates from its entity hashes
809
-     *
810
-     * @param  array  $hashes
811
-     * @param  string $relation
812
-     * @return array
813
-     */
814
-    protected function getRelatedAggregatesFromHashes(array $hashes, $relation)
815
-    {
816
-        $related = [];
817
-
818
-        foreach ($hashes as $hash) {
819
-            $aggregate = $this->getRelatedAggregateFromHash($hash, $relation);
820
-
821
-            if (! is_null($aggregate)) {
822
-                $related[] = $aggregate;
823
-            }
824
-        }
825
-
826
-        return $related;
827
-    }
828
-
829
-    /**
830
-     * Get related aggregate from its hash
831
-     *
832
-     * @param  string $hash
833
-     * @param  string $relation
834
-     * @return \Analogue\ORM\System\Aggregate | null
835
-     */
836
-    protected function getRelatedAggregateFromHash($hash, $relation)
837
-    {
838
-        foreach ($this->relationships[$relation] as $aggregate) {
839
-            if ($aggregate->getEntityHash() == $hash) {
840
-                return $aggregate;
841
-            }
842
-        }
843
-        return null;
844
-    }
845
-
846
-    /**
847
-     * Return an array of Entity Hashes from a specific relation
848
-     *
849
-     * @param  string $relation
850
-     * @return array
851
-     */
852
-    protected function getEntityHashesFromRelation($relation)
853
-    {
854
-        return array_map(function ($aggregate) {
855
-            return $aggregate->getEntityHash();
856
-        }, $this->relationships[$relation]);
857
-    }
858
-
859
-    /**
860
-     * Check the existence of an actual relationship
861
-     *
862
-     * @param  string $relation
863
-     * @return boolean
864
-     */
865
-    protected function isActualRelationships($relation)
866
-    {
867
-        return array_key_exists($relation, $this->relationships)
868
-            && count($this->relationships[$relation]) > 0;
869
-    }
870
-
871
-    /**
872
-     * Return cache instance for the current entity type
873
-     *
874
-     * @return \Analogue\ORM\System\EntityCache
875
-     */
876
-    protected function getCache()
877
-    {
878
-        return $this->mapper->getEntityCache();
879
-    }
880
-
881
-    /**
882
-     * Get Only Raw Entiy's attributes which have been modified
883
-     * since last query
884
-     *
885
-     * @return array
886
-     */
887
-    public function getDirtyRawAttributes()
888
-    {
889
-        $attributes = $this->getRawAttributes();
890
-        $cachedAttributes = $this->getCachedRawAttributes(array_keys($attributes));
891
-
892
-        $dirty = [];
893
-
894
-        foreach ($attributes as $key => $value) {
895
-            if ($this->isRelation($key) || $key == 'pivot') {
896
-                continue;
897
-            }
898
-
899
-            if (! array_key_exists($key, $cachedAttributes) && ! $value instanceof Pivot) {
900
-                $dirty[$key] = $value;
901
-            } elseif ($value !== $cachedAttributes[$key] &&
902
-                ! $this->originalIsNumericallyEquivalent($value, $cachedAttributes[$key])) {
903
-                $dirty[$key] = $value;
904
-            }
905
-        }
906
-
907
-        return $dirty;
908
-    }
909
-
910
-    protected function isRelation($key)
911
-    {
912
-        return in_array($key, $this->entityMap->getRelationships());
913
-    }
914
-
915
-    /**
916
-     * Determine if the new and old values for a given key are numerically equivalent.
917
-     *
918
-     * @return boolean
919
-     */
920
-    protected function originalIsNumericallyEquivalent($current, $original)
921
-    {
922
-        return is_numeric($current) && is_numeric($original) && strcmp((string) $current, (string) $original) === 0;
923
-    }
924
-
925
-    /**
926
-     * Get the underlying entity object
927
-     *
928
-     * @return mixed
929
-     */
930
-    public function getEntityObject()
931
-    {
932
-        return $this->wrappedEntity->getObject();
933
-    }
934
-
935
-    /**
936
-     * Return the Mapper instance for the current Entity Type
937
-     *
938
-     * @return \Analogue\ORM\System\Mapper
939
-     */
940
-    public function getMapper()
941
-    {
942
-        return $this->mapper;
943
-    }
944
-
945
-    /**
946
-     * Check that the entity already exists in the database, by checking
947
-     * if it has an EntityCache record
948
-     *
949
-     * @return boolean
950
-     */
951
-    public function exists()
952
-    {
953
-        return $this->getCache()->has($this->getEntityId());
954
-    }
955
-
956
-    /**
957
-     * Set the object attribute raw values (hydration)
958
-     *
959
-     * @param array $attributes
960
-     */
961
-    public function setEntityAttributes(array $attributes)
962
-    {
963
-        $this->wrappedEntity->setEntityAttributes($attributes);
964
-    }
965
-
966
-    /**
967
-     * Get the raw object's values.
968
-     *
969
-     * @return array
970
-     */
971
-    public function getEntityAttributes()
972
-    {
973
-        return $this->wrappedEntity->getEntityAttributes();
974
-    }
975
-
976
-    /**
977
-     * Set the raw entity attributes
978
-     * @param string $key
979
-     * @param string $value
980
-     */
981
-    public function setEntityAttribute($key, $value)
982
-    {
983
-        $this->wrappedEntity->setEntityAttribute($key, $value);
984
-    }
985
-
986
-    /**
987
-     * Return the entity's attribute
988
-     * @param  string $key
989
-     * @return mixed
990
-     */
991
-    public function getEntityAttribute($key)
992
-    {
993
-        return $this->wrappedEntity->getEntityAttribute($key);
994
-    }
995
-
996
-    /**
997
-     * Does the attribute exists on the entity
998
-     *
999
-     * @param  string  $key
1000
-     * @return boolean
1001
-     */
1002
-    public function hasAttribute($key)
1003
-    {
1004
-        return $this->wrappedEntity->hasAttribute($key);
1005
-    }
1006
-
1007
-    /**
1008
-     * Set the lazyloading proxies on the wrapped entity
1009
-     *
1010
-     * @return void
1011
-     */
1012
-    public function setProxies()
1013
-    {
1014
-        return $this->wrappedEntity->setProxies();
1015
-    }
760
+			if (count($dirty) > 0) {
761
+				$id = $aggregate->getEntityId();
762
+
763
+				$this->entityMap->$relation($this->getEntityObject())->updateExistingPivot($id, $dirty);
764
+			}
765
+		}
766
+	}
767
+
768
+	/**
769
+	 * Compare two attributes array and return dirty attributes
770
+	 *
771
+	 * @param  array  $actual
772
+	 * @param  array  $cached
773
+	 * @return array
774
+	 */
775
+	protected function getDirtyAttributes(array $actual, array $cached)
776
+	{
777
+		$dirty = [];
778
+
779
+		foreach ($actual as $key => $value) {
780
+			if (! $this->originalIsNumericallyEquivalent($value, $cached[$key])) {
781
+				$dirty[$key] = $actual[$key];
782
+			}
783
+		}
784
+
785
+		return $dirty;
786
+	}
787
+
788
+	/**
789
+	 *
790
+	 * @param  string $pivotHash
791
+	 * @param  string $relation
792
+	 * @return array
793
+	 */
794
+	protected function getPivotAttributesFromCache($pivotHash, $relation)
795
+	{
796
+		$cachedAttributes = $this->getCachedRawAttributes();
797
+
798
+		$cachedRelations = $cachedAttributes[$relation];
799
+
800
+		foreach ($cachedRelations as $cachedRelation) {
801
+			if ($cachedRelation == $pivotHash) {
802
+				return $cachedRelation->getPivotAttributes();
803
+			}
804
+		}
805
+	}
806
+
807
+	/**
808
+	 * Returns an array of related Aggregates from its entity hashes
809
+	 *
810
+	 * @param  array  $hashes
811
+	 * @param  string $relation
812
+	 * @return array
813
+	 */
814
+	protected function getRelatedAggregatesFromHashes(array $hashes, $relation)
815
+	{
816
+		$related = [];
817
+
818
+		foreach ($hashes as $hash) {
819
+			$aggregate = $this->getRelatedAggregateFromHash($hash, $relation);
820
+
821
+			if (! is_null($aggregate)) {
822
+				$related[] = $aggregate;
823
+			}
824
+		}
825
+
826
+		return $related;
827
+	}
828
+
829
+	/**
830
+	 * Get related aggregate from its hash
831
+	 *
832
+	 * @param  string $hash
833
+	 * @param  string $relation
834
+	 * @return \Analogue\ORM\System\Aggregate | null
835
+	 */
836
+	protected function getRelatedAggregateFromHash($hash, $relation)
837
+	{
838
+		foreach ($this->relationships[$relation] as $aggregate) {
839
+			if ($aggregate->getEntityHash() == $hash) {
840
+				return $aggregate;
841
+			}
842
+		}
843
+		return null;
844
+	}
845
+
846
+	/**
847
+	 * Return an array of Entity Hashes from a specific relation
848
+	 *
849
+	 * @param  string $relation
850
+	 * @return array
851
+	 */
852
+	protected function getEntityHashesFromRelation($relation)
853
+	{
854
+		return array_map(function ($aggregate) {
855
+			return $aggregate->getEntityHash();
856
+		}, $this->relationships[$relation]);
857
+	}
858
+
859
+	/**
860
+	 * Check the existence of an actual relationship
861
+	 *
862
+	 * @param  string $relation
863
+	 * @return boolean
864
+	 */
865
+	protected function isActualRelationships($relation)
866
+	{
867
+		return array_key_exists($relation, $this->relationships)
868
+			&& count($this->relationships[$relation]) > 0;
869
+	}
870
+
871
+	/**
872
+	 * Return cache instance for the current entity type
873
+	 *
874
+	 * @return \Analogue\ORM\System\EntityCache
875
+	 */
876
+	protected function getCache()
877
+	{
878
+		return $this->mapper->getEntityCache();
879
+	}
880
+
881
+	/**
882
+	 * Get Only Raw Entiy's attributes which have been modified
883
+	 * since last query
884
+	 *
885
+	 * @return array
886
+	 */
887
+	public function getDirtyRawAttributes()
888
+	{
889
+		$attributes = $this->getRawAttributes();
890
+		$cachedAttributes = $this->getCachedRawAttributes(array_keys($attributes));
891
+
892
+		$dirty = [];
893
+
894
+		foreach ($attributes as $key => $value) {
895
+			if ($this->isRelation($key) || $key == 'pivot') {
896
+				continue;
897
+			}
898
+
899
+			if (! array_key_exists($key, $cachedAttributes) && ! $value instanceof Pivot) {
900
+				$dirty[$key] = $value;
901
+			} elseif ($value !== $cachedAttributes[$key] &&
902
+				! $this->originalIsNumericallyEquivalent($value, $cachedAttributes[$key])) {
903
+				$dirty[$key] = $value;
904
+			}
905
+		}
906
+
907
+		return $dirty;
908
+	}
909
+
910
+	protected function isRelation($key)
911
+	{
912
+		return in_array($key, $this->entityMap->getRelationships());
913
+	}
914
+
915
+	/**
916
+	 * Determine if the new and old values for a given key are numerically equivalent.
917
+	 *
918
+	 * @return boolean
919
+	 */
920
+	protected function originalIsNumericallyEquivalent($current, $original)
921
+	{
922
+		return is_numeric($current) && is_numeric($original) && strcmp((string) $current, (string) $original) === 0;
923
+	}
924
+
925
+	/**
926
+	 * Get the underlying entity object
927
+	 *
928
+	 * @return mixed
929
+	 */
930
+	public function getEntityObject()
931
+	{
932
+		return $this->wrappedEntity->getObject();
933
+	}
934
+
935
+	/**
936
+	 * Return the Mapper instance for the current Entity Type
937
+	 *
938
+	 * @return \Analogue\ORM\System\Mapper
939
+	 */
940
+	public function getMapper()
941
+	{
942
+		return $this->mapper;
943
+	}
944
+
945
+	/**
946
+	 * Check that the entity already exists in the database, by checking
947
+	 * if it has an EntityCache record
948
+	 *
949
+	 * @return boolean
950
+	 */
951
+	public function exists()
952
+	{
953
+		return $this->getCache()->has($this->getEntityId());
954
+	}
955
+
956
+	/**
957
+	 * Set the object attribute raw values (hydration)
958
+	 *
959
+	 * @param array $attributes
960
+	 */
961
+	public function setEntityAttributes(array $attributes)
962
+	{
963
+		$this->wrappedEntity->setEntityAttributes($attributes);
964
+	}
965
+
966
+	/**
967
+	 * Get the raw object's values.
968
+	 *
969
+	 * @return array
970
+	 */
971
+	public function getEntityAttributes()
972
+	{
973
+		return $this->wrappedEntity->getEntityAttributes();
974
+	}
975
+
976
+	/**
977
+	 * Set the raw entity attributes
978
+	 * @param string $key
979
+	 * @param string $value
980
+	 */
981
+	public function setEntityAttribute($key, $value)
982
+	{
983
+		$this->wrappedEntity->setEntityAttribute($key, $value);
984
+	}
985
+
986
+	/**
987
+	 * Return the entity's attribute
988
+	 * @param  string $key
989
+	 * @return mixed
990
+	 */
991
+	public function getEntityAttribute($key)
992
+	{
993
+		return $this->wrappedEntity->getEntityAttribute($key);
994
+	}
995
+
996
+	/**
997
+	 * Does the attribute exists on the entity
998
+	 *
999
+	 * @param  string  $key
1000
+	 * @return boolean
1001
+	 */
1002
+	public function hasAttribute($key)
1003
+	{
1004
+		return $this->wrappedEntity->hasAttribute($key);
1005
+	}
1006
+
1007
+	/**
1008
+	 * Set the lazyloading proxies on the wrapped entity
1009
+	 *
1010
+	 * @return void
1011
+	 */
1012
+	public function setProxies()
1013
+	{
1014
+		return $this->wrappedEntity->setProxies();
1015
+	}
1016 1016
 }
Please login to merge, or discard this patch.
Spacing   +20 added lines, -20 removed lines patch added patch discarded remove patch
@@ -122,7 +122,7 @@  discard block
 block discarded – undo
122 122
      */
123 123
     protected function parseForCommonValues($relation)
124 124
     {
125
-        if (! $this->hasAttribute($relation)) {
125
+        if (!$this->hasAttribute($relation)) {
126 126
             // If no attribute exists for this relationships
127 127
             // we'll make it a simple empty array. This will
128 128
             // save us from constantly checking for the attributes
@@ -155,7 +155,7 @@  discard block
 block discarded – undo
155 155
      */
156 156
     protected function parseSingleRelationship($relation)
157 157
     {
158
-        if (! $value = $this->parseForCommonValues($relation)) {
158
+        if (!$value = $this->parseForCommonValues($relation)) {
159 159
             return true;
160 160
         }
161 161
         
@@ -163,7 +163,7 @@  discard block
 block discarded – undo
163 163
             throw new MappingException("Entity's attribute $relation should not be array, or collection");
164 164
         }
165 165
 
166
-        if ($value instanceof EntityProxy && ! $value->isLoaded()) {
166
+        if ($value instanceof EntityProxy && !$value->isLoaded()) {
167 167
             $this->relationships[$relation] = [];
168 168
             return true;
169 169
         }
@@ -198,7 +198,7 @@  discard block
 block discarded – undo
198 198
      */
199 199
     protected function isParentOrRoot($value)
200 200
     {
201
-        if (! is_null($this->root)) {
201
+        if (!is_null($this->root)) {
202 202
             $rootClass = get_class($this->root->getEntityObject());
203 203
             if ($rootClass == get_class($value)) {
204 204
                 return true;
@@ -206,7 +206,7 @@  discard block
 block discarded – undo
206 206
         }
207 207
 
208 208
 
209
-        if (! is_null($this->parent)) {
209
+        if (!is_null($this->parent)) {
210 210
             $parentClass = get_class($this->parent->getEntityObject());
211 211
             if ($parentClass == get_class($value)) {
212 212
                 return true;
@@ -222,7 +222,7 @@  discard block
 block discarded – undo
222 222
      */
223 223
     protected function parseManyRelationship($relation)
224 224
     {
225
-        if (! $value = $this->parseForCommonValues($relation)) {
225
+        if (!$value = $this->parseForCommonValues($relation)) {
226 226
             return true;
227 227
         }
228 228
 
@@ -237,13 +237,13 @@  discard block
 block discarded – undo
237 237
             $value = $value->getUnderlyingCollection();
238 238
         }
239 239
 
240
-        if ($value instanceof CollectionProxy && ! $value->isLoaded()) {
240
+        if ($value instanceof CollectionProxy && !$value->isLoaded()) {
241 241
             $value = $value->getAddedItems();
242 242
         }
243 243
 
244 244
         // At this point $value should be either an array or an instance
245 245
         // of a collection class.
246
-        if (! is_array($value) && ! $value instanceof Collection) {
246
+        if (!is_array($value) && !$value instanceof Collection) {
247 247
             throw new MappingException("'$relation' attribute should be array() or Collection");
248 248
         }
249 249
 
@@ -422,7 +422,7 @@  discard block
 block discarded – undo
422 422
         $nonExisting = [];
423 423
 
424 424
         foreach ($this->relationships[$relation] as $aggregate) {
425
-            if (! $aggregate->exists()) {
425
+            if (!$aggregate->exists()) {
426 426
                 $nonExisting[] = $aggregate;
427 427
             }
428 428
         }
@@ -467,11 +467,11 @@  discard block
 block discarded – undo
467 467
     {
468 468
         $cachedRelations = $this->getCachedAttribute($relation);
469 469
 
470
-        if (! is_null($cachedRelations)) {
470
+        if (!is_null($cachedRelations)) {
471 471
             $missing = [];
472 472
 
473 473
             foreach ($cachedRelations as $hash) {
474
-                if (! $this->getRelatedAggregateFromHash($hash, $relation)) {
474
+                if (!$this->getRelatedAggregateFromHash($hash, $relation)) {
475 475
                     $missing[] = $hash;
476 476
                 }
477 477
             }
@@ -493,7 +493,7 @@  discard block
 block discarded – undo
493 493
 
494 494
         foreach ($this->relationships as $relation) {
495 495
             foreach ($relation as $aggregate) {
496
-                if (! $aggregate->exists() || $aggregate->isDirty() || count($aggregate->getDirtyRelationships() > 0)) {
496
+                if (!$aggregate->exists() || $aggregate->isDirty() || count($aggregate->getDirtyRelationships() > 0)) {
497 497
                     $dirtyAggregates[] = $aggregate;
498 498
                 }
499 499
             }
@@ -598,7 +598,7 @@  discard block
 block discarded – undo
598 598
     {
599 599
         $cachedAttributes = $this->getCache()->get($this->getEntityId());
600 600
 
601
-        if (! array_key_exists($key, $cachedAttributes)) {
601
+        if (!array_key_exists($key, $cachedAttributes)) {
602 602
             return null;
603 603
         } else {
604 604
             return $cachedAttributes[$key];
@@ -622,7 +622,7 @@  discard block
 block discarded – undo
622 622
             }
623 623
         }
624 624
 
625
-        if (! is_null($this->parent)) {
625
+        if (!is_null($this->parent)) {
626 626
             $foreignKeys = $foreignKeys + $this->getForeignKeyAttributesFromParent();
627 627
         }
628 628
 
@@ -669,7 +669,7 @@  discard block
 block discarded – undo
669 669
         $parentRelation = $this->parentRelationship;
670 670
 
671 671
         if (in_array($parentRelation, $parentForeignRelations)
672
-            && ! in_array($parentRelation, $parentPivotRelations)) {
672
+            && !in_array($parentRelation, $parentPivotRelations)) {
673 673
             $parentObject = $this->parent->getEntityObject();
674 674
 
675 675
             // Call Relationship's method on parent map
@@ -777,7 +777,7 @@  discard block
 block discarded – undo
777 777
         $dirty = [];
778 778
 
779 779
         foreach ($actual as $key => $value) {
780
-            if (! $this->originalIsNumericallyEquivalent($value, $cached[$key])) {
780
+            if (!$this->originalIsNumericallyEquivalent($value, $cached[$key])) {
781 781
                 $dirty[$key] = $actual[$key];
782 782
             }
783 783
         }
@@ -818,7 +818,7 @@  discard block
 block discarded – undo
818 818
         foreach ($hashes as $hash) {
819 819
             $aggregate = $this->getRelatedAggregateFromHash($hash, $relation);
820 820
 
821
-            if (! is_null($aggregate)) {
821
+            if (!is_null($aggregate)) {
822 822
                 $related[] = $aggregate;
823 823
             }
824 824
         }
@@ -851,7 +851,7 @@  discard block
 block discarded – undo
851 851
      */
852 852
     protected function getEntityHashesFromRelation($relation)
853 853
     {
854
-        return array_map(function ($aggregate) {
854
+        return array_map(function($aggregate) {
855 855
             return $aggregate->getEntityHash();
856 856
         }, $this->relationships[$relation]);
857 857
     }
@@ -896,10 +896,10 @@  discard block
 block discarded – undo
896 896
                 continue;
897 897
             }
898 898
 
899
-            if (! array_key_exists($key, $cachedAttributes) && ! $value instanceof Pivot) {
899
+            if (!array_key_exists($key, $cachedAttributes) && !$value instanceof Pivot) {
900 900
                 $dirty[$key] = $value;
901 901
             } elseif ($value !== $cachedAttributes[$key] &&
902
-                ! $this->originalIsNumericallyEquivalent($value, $cachedAttributes[$key])) {
902
+                !$this->originalIsNumericallyEquivalent($value, $cachedAttributes[$key])) {
903 903
                 $dirty[$key] = $value;
904 904
             }
905 905
         }
Please login to merge, or discard this patch.
Unused Use Statements   -1 removed lines patch added patch discarded remove patch
@@ -9,7 +9,6 @@
 block discarded – undo
9 9
 use Analogue\ORM\System\InternallyMappable;
10 10
 use Analogue\ORM\Relationships\Relationship;
11 11
 use Analogue\ORM\Exceptions\MappingException;
12
-use Analogue\ORM\System\Proxies\ProxyInterface;
13 12
 
14 13
 /**
15 14
  * The EntityCache class is responsible for tracking entity's attribute states
Please login to merge, or discard this patch.
src/System/EntityBuilder.php 3 patches
Doc Comments   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -108,7 +108,7 @@  discard block
 block discarded – undo
108 108
     /**
109 109
      * Get the correct wrapper prototype corresponding to the object type
110 110
      *
111
-     * @return mixed
111
+     * @return InternallyMappable
112 112
      */
113 113
     protected function getWrapperInstance()
114 114
     {
@@ -174,7 +174,7 @@  discard block
 block discarded – undo
174 174
     /**
175 175
      * Build lazy loading proxies for the current entity
176 176
      *
177
-     * @param \Analogue\ORM\Mappable $entity
177
+     * @param InternallyMappable $entity
178 178
      *
179 179
      * @return array
180 180
      */
Please login to merge, or discard this patch.
Indentation   +175 added lines, -175 removed lines patch added patch discarded remove patch
@@ -11,189 +11,189 @@
 block discarded – undo
11 11
 class EntityBuilder
12 12
 {
13 13
 
14
-    /**
15
-     * The mapper for the entity to build
16
-     * @var \Analogue\ORM\System\Mapper
17
-     */
18
-    protected $mapper;
19
-
20
-    /**
21
-     * The Entity Map for the entity to build.
22
-     *
23
-     * @var \Analogue\ORM\EntityMap
24
-     */
25
-    protected $entityMap;
26
-
27
-    /**
28
-     * Relations that will be eager loaded on this query
29
-     *
30
-     * @var array
31
-     */
32
-    protected $eagerLoads;
33
-
34
-    /**
35
-     * Relations that will be lazy loaded on this query
36
-     *
37
-     * @var array
38
-     */
39
-    protected $lazyLoads;
40
-
41
-    /**
42
-     * Entity Wrapper Factory
43
-     * @var \Analogue\ORM\System\Wrappers\Factory
44
-     */
45
-    protected $factory;
46
-
47
-    public function __construct(Mapper $mapper, array $eagerLoads)
48
-    {
49
-        $this->mapper = $mapper;
50
-
51
-        $this->entityMap = $mapper->getEntityMap();
52
-
53
-        $this->eagerLoads = $eagerLoads;
54
-
55
-        $this->lazyLoads = $this->prepareLazyLoading();
56
-
57
-        $this->entityMap = $mapper->getEntityMap();
58
-
59
-        $this->factory = new Factory;
60
-    }
61
-
62
-    /**
63
-     * Convert a result set into an array of entities
64
-     *
65
-     * @param  array  $results
66
-     *
67
-     * @return array
68
-     */
69
-    public function build(array $results)
70
-    {
71
-        $entities = array();
72
-
73
-        //$prototype = $this->getWrapperPrototype();
74
-        //$prototype = $this->mapper->newInstance();
75
-
76
-        $keyName = $this->entityMap->getKeyName();
77
-
78
-        $tmpCache = [];
79
-
80
-        foreach ($results as $result) {
81
-            //$instance = clone $prototype;
82
-            $instance = $this->getWrapperInstance();
83
-
84
-            $resultArray = (array) $result;
85
-
86
-            $tmpCache[$resultArray[$keyName] ] = $resultArray;
87
-
88
-            // Hydrate any embedded Value Object
89
-            $this->hydrateValueObjects($resultArray);
90
-
91
-            $instance->setEntityAttributes($resultArray);
92
-
93
-            // Hydrate relation attributes with lazyloading proxies
94
-            if (count($this->lazyLoads) > 0) {
95
-                $proxies = $this->getLazyLoadingProxies($instance);
96
-                $instance->setEntityAttributes($resultArray + $proxies);
97
-            }
98
-
99
-            // Directly Unwrap the entity now that it has been hydrated
100
-            $entities[] = $instance->getObject();
101
-        }
102
-
103
-        $this->mapper->getEntityCache()->add($tmpCache);
104
-
105
-        return $entities;
106
-    }
107
-
108
-    /**
109
-     * Get the correct wrapper prototype corresponding to the object type
110
-     *
111
-     * @return mixed
112
-     */
113
-    protected function getWrapperInstance()
114
-    {
115
-        return $this->factory->make($this->mapper->newInstance());
116
-    }
117
-
118
-    /**
119
-     * Hydrate value object embedded in this entity
120
-     *
121
-     * @param  array $attributes
122
-     * @return void
123
-     */
124
-    protected function hydrateValueObjects(& $attributes)
125
-    {
126
-        foreach ($this->entityMap->getEmbeddables() as $localKey => $valueClass) {
127
-            $this->hydrateValueObject($attributes, $localKey, $valueClass);
128
-        }
129
-    }
130
-
131
-    /**
132
-     * Hydrate a single value object
133
-     *
134
-     * @param  array $attributes
135
-     * @param  string $localKey
136
-     * @param  string $valueClass
137
-     * @return void
138
-     */
139
-    protected function hydrateValueObject(& $attributes, $localKey, $valueClass)
140
-    {
141
-        $map = $this->mapper->getManager()->getValueMap($valueClass);
14
+	/**
15
+	 * The mapper for the entity to build
16
+	 * @var \Analogue\ORM\System\Mapper
17
+	 */
18
+	protected $mapper;
19
+
20
+	/**
21
+	 * The Entity Map for the entity to build.
22
+	 *
23
+	 * @var \Analogue\ORM\EntityMap
24
+	 */
25
+	protected $entityMap;
26
+
27
+	/**
28
+	 * Relations that will be eager loaded on this query
29
+	 *
30
+	 * @var array
31
+	 */
32
+	protected $eagerLoads;
33
+
34
+	/**
35
+	 * Relations that will be lazy loaded on this query
36
+	 *
37
+	 * @var array
38
+	 */
39
+	protected $lazyLoads;
40
+
41
+	/**
42
+	 * Entity Wrapper Factory
43
+	 * @var \Analogue\ORM\System\Wrappers\Factory
44
+	 */
45
+	protected $factory;
46
+
47
+	public function __construct(Mapper $mapper, array $eagerLoads)
48
+	{
49
+		$this->mapper = $mapper;
50
+
51
+		$this->entityMap = $mapper->getEntityMap();
52
+
53
+		$this->eagerLoads = $eagerLoads;
54
+
55
+		$this->lazyLoads = $this->prepareLazyLoading();
56
+
57
+		$this->entityMap = $mapper->getEntityMap();
58
+
59
+		$this->factory = new Factory;
60
+	}
61
+
62
+	/**
63
+	 * Convert a result set into an array of entities
64
+	 *
65
+	 * @param  array  $results
66
+	 *
67
+	 * @return array
68
+	 */
69
+	public function build(array $results)
70
+	{
71
+		$entities = array();
72
+
73
+		//$prototype = $this->getWrapperPrototype();
74
+		//$prototype = $this->mapper->newInstance();
75
+
76
+		$keyName = $this->entityMap->getKeyName();
77
+
78
+		$tmpCache = [];
79
+
80
+		foreach ($results as $result) {
81
+			//$instance = clone $prototype;
82
+			$instance = $this->getWrapperInstance();
83
+
84
+			$resultArray = (array) $result;
85
+
86
+			$tmpCache[$resultArray[$keyName] ] = $resultArray;
87
+
88
+			// Hydrate any embedded Value Object
89
+			$this->hydrateValueObjects($resultArray);
90
+
91
+			$instance->setEntityAttributes($resultArray);
92
+
93
+			// Hydrate relation attributes with lazyloading proxies
94
+			if (count($this->lazyLoads) > 0) {
95
+				$proxies = $this->getLazyLoadingProxies($instance);
96
+				$instance->setEntityAttributes($resultArray + $proxies);
97
+			}
98
+
99
+			// Directly Unwrap the entity now that it has been hydrated
100
+			$entities[] = $instance->getObject();
101
+		}
102
+
103
+		$this->mapper->getEntityCache()->add($tmpCache);
104
+
105
+		return $entities;
106
+	}
107
+
108
+	/**
109
+	 * Get the correct wrapper prototype corresponding to the object type
110
+	 *
111
+	 * @return mixed
112
+	 */
113
+	protected function getWrapperInstance()
114
+	{
115
+		return $this->factory->make($this->mapper->newInstance());
116
+	}
117
+
118
+	/**
119
+	 * Hydrate value object embedded in this entity
120
+	 *
121
+	 * @param  array $attributes
122
+	 * @return void
123
+	 */
124
+	protected function hydrateValueObjects(& $attributes)
125
+	{
126
+		foreach ($this->entityMap->getEmbeddables() as $localKey => $valueClass) {
127
+			$this->hydrateValueObject($attributes, $localKey, $valueClass);
128
+		}
129
+	}
130
+
131
+	/**
132
+	 * Hydrate a single value object
133
+	 *
134
+	 * @param  array $attributes
135
+	 * @param  string $localKey
136
+	 * @param  string $valueClass
137
+	 * @return void
138
+	 */
139
+	protected function hydrateValueObject(& $attributes, $localKey, $valueClass)
140
+	{
141
+		$map = $this->mapper->getManager()->getValueMap($valueClass);
142 142
 
143
-        $embeddedAttributes = $map->getAttributes();
143
+		$embeddedAttributes = $map->getAttributes();
144 144
 
145
-        //$nestedValueObjects = $map->getEmbeddables();
145
+		//$nestedValueObjects = $map->getEmbeddables();
146 146
 
147
-        $valueObject = $this->mapper->getManager()->getValueObjectInstance($valueClass);
147
+		$valueObject = $this->mapper->getManager()->getValueObjectInstance($valueClass);
148 148
 
149
-        foreach ($embeddedAttributes as $key) {
150
-            $prefix = snake_case(class_basename($valueClass)).'_';
149
+		foreach ($embeddedAttributes as $key) {
150
+			$prefix = snake_case(class_basename($valueClass)).'_';
151 151
 
152
-            $voWrapper = $this->factory->make($valueObject);
152
+			$voWrapper = $this->factory->make($valueObject);
153 153
 
154
-            $voWrapper->setEntityAttribute($key, $attributes[$prefix.$key]);
154
+			$voWrapper->setEntityAttribute($key, $attributes[$prefix.$key]);
155 155
             
156
-            unset($attributes[$prefix.$key]);
157
-        }
156
+			unset($attributes[$prefix.$key]);
157
+		}
158 158
         
159
-        $attributes[$localKey] = $valueObject;
160
-    }
161
-
162
-    /**
163
-     * Deduce the relationships that will be lazy loaded from the eagerLoads array
164
-     *
165
-     * @return array
166
-     */
167
-    protected function prepareLazyLoading()
168
-    {
169
-        $relations = $this->entityMap->getRelationships();
159
+		$attributes[$localKey] = $valueObject;
160
+	}
161
+
162
+	/**
163
+	 * Deduce the relationships that will be lazy loaded from the eagerLoads array
164
+	 *
165
+	 * @return array
166
+	 */
167
+	protected function prepareLazyLoading()
168
+	{
169
+		$relations = $this->entityMap->getRelationships();
170 170
        
171
-        return array_diff($relations, $this->eagerLoads);
172
-    }
173
-
174
-    /**
175
-     * Build lazy loading proxies for the current entity
176
-     *
177
-     * @param \Analogue\ORM\Mappable $entity
178
-     *
179
-     * @return array
180
-     */
181
-    protected function getLazyLoadingProxies(InternallyMappable $entity)
182
-    {
183
-        $proxies = [];
184
-
185
-        $singleRelations = $this->entityMap->getSingleRelationships();
186
-        $manyRelations = $this->entityMap->getManyRelationships();
187
-
188
-        foreach ($this->lazyLoads as $relation) {
189
-            if (in_array($relation, $singleRelations)) {
190
-                $proxies[$relation] = new EntityProxy($entity->getObject(), $relation);
191
-            }
192
-            if (in_array($relation, $manyRelations)) {
193
-                $proxies[$relation] = new CollectionProxy($entity->getObject(), $relation);
194
-            }
195
-        }
171
+		return array_diff($relations, $this->eagerLoads);
172
+	}
173
+
174
+	/**
175
+	 * Build lazy loading proxies for the current entity
176
+	 *
177
+	 * @param \Analogue\ORM\Mappable $entity
178
+	 *
179
+	 * @return array
180
+	 */
181
+	protected function getLazyLoadingProxies(InternallyMappable $entity)
182
+	{
183
+		$proxies = [];
184
+
185
+		$singleRelations = $this->entityMap->getSingleRelationships();
186
+		$manyRelations = $this->entityMap->getManyRelationships();
187
+
188
+		foreach ($this->lazyLoads as $relation) {
189
+			if (in_array($relation, $singleRelations)) {
190
+				$proxies[$relation] = new EntityProxy($entity->getObject(), $relation);
191
+			}
192
+			if (in_array($relation, $manyRelations)) {
193
+				$proxies[$relation] = new CollectionProxy($entity->getObject(), $relation);
194
+			}
195
+		}
196 196
         
197
-        return $proxies;
198
-    }
197
+		return $proxies;
198
+	}
199 199
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -83,7 +83,7 @@
 block discarded – undo
83 83
 
84 84
             $resultArray = (array) $result;
85 85
 
86
-            $tmpCache[$resultArray[$keyName] ] = $resultArray;
86
+            $tmpCache[$resultArray[$keyName]] = $resultArray;
87 87
 
88 88
             // Hydrate any embedded Value Object
89 89
             $this->hydrateValueObjects($resultArray);
Please login to merge, or discard this patch.
src/System/EntityCache.php 4 patches
Doc Comments   +4 added lines, -5 removed lines patch added patch discarded remove patch
@@ -190,8 +190,8 @@  discard block
 block discarded – undo
190 190
      * Cache a many relationship
191 191
      *
192 192
      * @param  [type]       $parentKey    [description]
193
-     * @param  [type]       $relation     [description]
194
-     * @param  [type]       $results      [description]
193
+     * @param  string       $relation     [description]
194
+     * @param  EntityCollection       $results      [description]
195 195
      * @param  Relationship $relationship [description]
196 196
      * @return [type]                     [description]
197 197
      */
@@ -212,8 +212,8 @@  discard block
 block discarded – undo
212 212
      * Cache a single relationship
213 213
      *
214 214
      * @param  [type]       $parentKey    [description]
215
-     * @param  [type]       $relation     [description]
216
-     * @param  [type]       $results      [description]
215
+     * @param  string       $relation     [description]
216
+     * @param  Mappable       $result      [description]
217 217
      * @param  Relationship $relationship [description]
218 218
      * @return [type]                     [description]
219 219
      */
@@ -253,7 +253,6 @@  discard block
 block discarded – undo
253 253
     /**
254 254
      * Transform an Aggregated Entity into a cache record
255 255
      *
256
-     * @param  Aggregate $entity
257 256
      * @return array
258 257
      */
259 258
     protected function transform(Aggregate $aggregatedEntity)
Please login to merge, or discard this patch.
Unused Use Statements   -1 removed lines patch added patch discarded remove patch
@@ -9,7 +9,6 @@
 block discarded – undo
9 9
 use Analogue\ORM\System\InternallyMappable;
10 10
 use Analogue\ORM\Relationships\Relationship;
11 11
 use Analogue\ORM\Exceptions\MappingException;
12
-use Analogue\ORM\System\Proxies\ProxyInterface;
13 12
 
14 13
 /**
15 14
  * The EntityCache class is responsible for tracking entity's attribute states
Please login to merge, or discard this patch.
Indentation   +289 added lines, -289 removed lines patch added patch discarded remove patch
@@ -18,302 +18,302 @@
 block discarded – undo
18 18
 class EntityCache
19 19
 {
20 20
 
21
-    /**
22
-     * Entity's raw attributes/relationships
23
-     *
24
-     * @var array
25
-     */
26
-    protected $cache = [];
27
-
28
-    /**
29
-     * Entity Map for the current Entity Type
30
-     * @var [type]
31
-     */
32
-    protected $entityMap;
33
-
34
-    /**
35
-     * Wrapper factory
36
-     *
37
-     * @var \Analogue\ORM\System\Wrappers\Factory
38
-     */
39
-    protected $factory;
40
-
41
-    /**
42
-     * Associative array containing list of pivot attributes per relationship
43
-     * so we don't have to call relationship method on refresh.
44
-     *
45
-     * @var array
46
-     */
47
-    protected $pivotAttributes = [];
48
-
49
-    public function __construct(EntityMap $entityMap)
50
-    {
51
-        $this->entityMap = $entityMap;
52
-
53
-        $this->factory = new Factory;
54
-    }
55
-
56
-    /**
57
-     * Add an array of key=>attributes representing
58
-     * the initial state of loaded entities.
59
-     *
60
-     * @param array $entities
61
-     */
62
-    public function add(array $entities)
63
-    {
64
-        if (count($this->cache) == 0) {
65
-            $this->cache = $entities;
66
-        } else {
67
-            $this->mergeCacheResults($entities);
68
-        }
69
-    }
70
-
71
-    /**
72
-     * Retrieve initial attributes for a single entity
73
-     *
74
-     * @param  string $id
75
-     * @return array
76
-     */
77
-    public function get($id)
78
-    {
79
-        if ($this->has($id)) {
80
-            return $this->cache[$id];
81
-        } else {
82
-            return [];
83
-        }
84
-    }
85
-
86
-    /**
87
-     * Check if a record for this id exists.
88
-     *
89
-     * @param  string  $id
90
-     * @return boolean
91
-     */
92
-    public function has($id)
93
-    {
94
-        return array_key_exists($id, $this->cache);
95
-    }
96
-
97
-    /**
98
-     * Combine new result set with existing attributes in
99
-     * cache.
100
-     *
101
-     * @param  array $entities
102
-     * @return void
103
-     */
104
-    protected function mergeCacheResults($entities)
105
-    {
106
-        foreach ($entities as $key => $entity) {
107
-            if (array_key_exists($key, $this->cache)) {
108
-                /*
21
+	/**
22
+	 * Entity's raw attributes/relationships
23
+	 *
24
+	 * @var array
25
+	 */
26
+	protected $cache = [];
27
+
28
+	/**
29
+	 * Entity Map for the current Entity Type
30
+	 * @var [type]
31
+	 */
32
+	protected $entityMap;
33
+
34
+	/**
35
+	 * Wrapper factory
36
+	 *
37
+	 * @var \Analogue\ORM\System\Wrappers\Factory
38
+	 */
39
+	protected $factory;
40
+
41
+	/**
42
+	 * Associative array containing list of pivot attributes per relationship
43
+	 * so we don't have to call relationship method on refresh.
44
+	 *
45
+	 * @var array
46
+	 */
47
+	protected $pivotAttributes = [];
48
+
49
+	public function __construct(EntityMap $entityMap)
50
+	{
51
+		$this->entityMap = $entityMap;
52
+
53
+		$this->factory = new Factory;
54
+	}
55
+
56
+	/**
57
+	 * Add an array of key=>attributes representing
58
+	 * the initial state of loaded entities.
59
+	 *
60
+	 * @param array $entities
61
+	 */
62
+	public function add(array $entities)
63
+	{
64
+		if (count($this->cache) == 0) {
65
+			$this->cache = $entities;
66
+		} else {
67
+			$this->mergeCacheResults($entities);
68
+		}
69
+	}
70
+
71
+	/**
72
+	 * Retrieve initial attributes for a single entity
73
+	 *
74
+	 * @param  string $id
75
+	 * @return array
76
+	 */
77
+	public function get($id)
78
+	{
79
+		if ($this->has($id)) {
80
+			return $this->cache[$id];
81
+		} else {
82
+			return [];
83
+		}
84
+	}
85
+
86
+	/**
87
+	 * Check if a record for this id exists.
88
+	 *
89
+	 * @param  string  $id
90
+	 * @return boolean
91
+	 */
92
+	public function has($id)
93
+	{
94
+		return array_key_exists($id, $this->cache);
95
+	}
96
+
97
+	/**
98
+	 * Combine new result set with existing attributes in
99
+	 * cache.
100
+	 *
101
+	 * @param  array $entities
102
+	 * @return void
103
+	 */
104
+	protected function mergeCacheResults($entities)
105
+	{
106
+		foreach ($entities as $key => $entity) {
107
+			if (array_key_exists($key, $this->cache)) {
108
+				/*
109 109
                 $existingValue = $this->cache[$key];
110 110
 
111 111
                 $this->cache[$key] = $entity + $existingValue;*/
112 112
 
113
-                $this->cache[$key] = $entity;
114
-            } else {
115
-                $this->cache[$key] = $entity;
116
-            }
117
-        }
118
-    }
119
-
120
-    /**
121
-     * Cache Relation's query result for an entity
122
-     *
123
-     * @param  mixed 	$parent
124
-     * @param  string   $relation name of the relation
125
-     * @param  mixed 	$results  results of the relationship's query
126
-     *
127
-     * @return void
128
-     */
129
-    public function cacheLoadedRelationResult($parent, $relation, $results, Relationship $relationship)
130
-    {
131
-        $keyName = $this->entityMap->getKeyName();
132
-
133
-        if (! $parent instanceof InternallyMappable) {
134
-            $parent = $this->factory->make($parent);
135
-        }
136
-
137
-        $key = $parent->getEntityAttribute($keyName);
113
+				$this->cache[$key] = $entity;
114
+			} else {
115
+				$this->cache[$key] = $entity;
116
+			}
117
+		}
118
+	}
119
+
120
+	/**
121
+	 * Cache Relation's query result for an entity
122
+	 *
123
+	 * @param  mixed 	$parent
124
+	 * @param  string   $relation name of the relation
125
+	 * @param  mixed 	$results  results of the relationship's query
126
+	 *
127
+	 * @return void
128
+	 */
129
+	public function cacheLoadedRelationResult($parent, $relation, $results, Relationship $relationship)
130
+	{
131
+		$keyName = $this->entityMap->getKeyName();
132
+
133
+		if (! $parent instanceof InternallyMappable) {
134
+			$parent = $this->factory->make($parent);
135
+		}
136
+
137
+		$key = $parent->getEntityAttribute($keyName);
138 138
         
139
-        if ($results instanceof EntityCollection) {
140
-            $this->cacheManyRelationResults($key, $relation, $results, $relationship);
141
-        }
142
-
143
-        // POPO : Maybe this check isn't needed, or we have to check for stdClass
144
-        // instead
145
-        if ($results instanceof Mappable) {
146
-            $this->cacheSingleRelationResult($key, $relation, $results, $relationship);
147
-        }
148
-    }
149
-
150
-    /**
151
-     * Create a cachedRelationship instance which will hold related entity's hash and pivot attributes, if any.
152
-     *
153
-     * @param  [type]       $parentKey    [description]
154
-     * @param  [type]       $relation     [description]
155
-     * @param  [type]       $result       [description]
156
-     * @param  Relationship $relationship [description]
157
-     * @return [type]                     [description]
158
-     */
159
-    protected function getCachedRelationship($parentKey, $relation, $result, Relationship $relationship)
160
-    {
161
-        $pivotColumns = $relationship->getPivotAttributes();
162
-
163
-        if (! array_key_exists($relation, $this->pivotAttributes)) {
164
-            $this->pivotAttributes[$relation] = $pivotColumns;
165
-        }
166
-
167
-        $wrapper = $this->factory->make($result);
168
-
169
-        $hash = $this->getEntityHash($wrapper);
170
-
171
-        if (count($pivotColumns) > 0) {
172
-            $pivotAttributes = [];
173
-            foreach ($pivotColumns as $column) {
174
-                $pivot = $wrapper->getEntityAttribute('pivot');
175
-
176
-                $pivotWrapper = $this->factory->make($pivot);
177
-
178
-                $pivotAttributes[$column] = $pivotWrapper->getEntityAttribute($column);
179
-            }
180
-
181
-            $cachedRelationship = new CachedRelationship($hash, $pivotAttributes);
182
-        } else {
183
-            $cachedRelationship = new CachedRelationship($hash);
184
-        }
185
-
186
-        return $cachedRelationship;
187
-    }
188
-
189
-    /**
190
-     * Cache a many relationship
191
-     *
192
-     * @param  [type]       $parentKey    [description]
193
-     * @param  [type]       $relation     [description]
194
-     * @param  [type]       $results      [description]
195
-     * @param  Relationship $relationship [description]
196
-     * @return [type]                     [description]
197
-     */
198
-    protected function cacheManyRelationResults($parentKey, $relation, $results, Relationship $relationship)
199
-    {
200
-        $this->cache[$parentKey][$relation] = [];
201
-
202
-        foreach ($results as $result) {
203
-            $cachedRelationship = $this->getCachedRelationship($parentKey, $relation, $result, $relationship);
204
-
205
-            $relatedHash = $cachedRelationship->getHash();
206
-
207
-            $this->cache[$parentKey][$relation][$relatedHash] = $cachedRelationship;
208
-        }
209
-    }
210
-
211
-    /**
212
-     * Cache a single relationship
213
-     *
214
-     * @param  [type]       $parentKey    [description]
215
-     * @param  [type]       $relation     [description]
216
-     * @param  [type]       $results      [description]
217
-     * @param  Relationship $relationship [description]
218
-     * @return [type]                     [description]
219
-     */
220
-    protected function cacheSingleRelationResult($parentKey, $relation, $result, Relationship $relationship)
221
-    {
222
-        $this->cache[$parentKey][$relation] = $this->getCachedRelationship($parentKey, $relation, $result, $relationship);
223
-    }
224
-
225
-    /**
226
-     * Get Entity's Hash
227
-     *
228
-     * @param  $entity
229
-     * @return string
230
-     */
231
-    protected function getEntityHash(InternallyMappable $entity)
232
-    {
233
-        $class = get_class($entity->getObject());
234
-
235
-        $mapper = Manager::getMapper($class);
236
-
237
-        $keyName = $mapper->getEntityMap()->getKeyName();
238
-
239
-        return $class.'.'.$entity->getEntityAttribute($keyName);
240
-    }
241
-
242
-    /**
243
-     * Refresh the cache record for an aggregated entity after a write operation
244
-     *
245
-     * @param  InternallyMappable $entity [description]
246
-     * @return [type]                     [description]
247
-     */
248
-    public function refresh(Aggregate $entity)
249
-    {
250
-        $this->cache[$entity->getEntityId()] = $this->transform($entity);
251
-    }
252
-
253
-    /**
254
-     * Transform an Aggregated Entity into a cache record
255
-     *
256
-     * @param  Aggregate $entity
257
-     * @return array
258
-     */
259
-    protected function transform(Aggregate $aggregatedEntity)
260
-    {
261
-        $baseAttributes = $aggregatedEntity->getRawAttributes();
262
-
263
-        $relationAttributes = [];
264
-
265
-        // First we'll handle each relationships that are a one to one
266
-        // relation, and which will be saved as a CachedRelationship
267
-        // object inside the cache.
139
+		if ($results instanceof EntityCollection) {
140
+			$this->cacheManyRelationResults($key, $relation, $results, $relationship);
141
+		}
142
+
143
+		// POPO : Maybe this check isn't needed, or we have to check for stdClass
144
+		// instead
145
+		if ($results instanceof Mappable) {
146
+			$this->cacheSingleRelationResult($key, $relation, $results, $relationship);
147
+		}
148
+	}
149
+
150
+	/**
151
+	 * Create a cachedRelationship instance which will hold related entity's hash and pivot attributes, if any.
152
+	 *
153
+	 * @param  [type]       $parentKey    [description]
154
+	 * @param  [type]       $relation     [description]
155
+	 * @param  [type]       $result       [description]
156
+	 * @param  Relationship $relationship [description]
157
+	 * @return [type]                     [description]
158
+	 */
159
+	protected function getCachedRelationship($parentKey, $relation, $result, Relationship $relationship)
160
+	{
161
+		$pivotColumns = $relationship->getPivotAttributes();
162
+
163
+		if (! array_key_exists($relation, $this->pivotAttributes)) {
164
+			$this->pivotAttributes[$relation] = $pivotColumns;
165
+		}
166
+
167
+		$wrapper = $this->factory->make($result);
168
+
169
+		$hash = $this->getEntityHash($wrapper);
170
+
171
+		if (count($pivotColumns) > 0) {
172
+			$pivotAttributes = [];
173
+			foreach ($pivotColumns as $column) {
174
+				$pivot = $wrapper->getEntityAttribute('pivot');
175
+
176
+				$pivotWrapper = $this->factory->make($pivot);
177
+
178
+				$pivotAttributes[$column] = $pivotWrapper->getEntityAttribute($column);
179
+			}
180
+
181
+			$cachedRelationship = new CachedRelationship($hash, $pivotAttributes);
182
+		} else {
183
+			$cachedRelationship = new CachedRelationship($hash);
184
+		}
185
+
186
+		return $cachedRelationship;
187
+	}
188
+
189
+	/**
190
+	 * Cache a many relationship
191
+	 *
192
+	 * @param  [type]       $parentKey    [description]
193
+	 * @param  [type]       $relation     [description]
194
+	 * @param  [type]       $results      [description]
195
+	 * @param  Relationship $relationship [description]
196
+	 * @return [type]                     [description]
197
+	 */
198
+	protected function cacheManyRelationResults($parentKey, $relation, $results, Relationship $relationship)
199
+	{
200
+		$this->cache[$parentKey][$relation] = [];
201
+
202
+		foreach ($results as $result) {
203
+			$cachedRelationship = $this->getCachedRelationship($parentKey, $relation, $result, $relationship);
204
+
205
+			$relatedHash = $cachedRelationship->getHash();
206
+
207
+			$this->cache[$parentKey][$relation][$relatedHash] = $cachedRelationship;
208
+		}
209
+	}
210
+
211
+	/**
212
+	 * Cache a single relationship
213
+	 *
214
+	 * @param  [type]       $parentKey    [description]
215
+	 * @param  [type]       $relation     [description]
216
+	 * @param  [type]       $results      [description]
217
+	 * @param  Relationship $relationship [description]
218
+	 * @return [type]                     [description]
219
+	 */
220
+	protected function cacheSingleRelationResult($parentKey, $relation, $result, Relationship $relationship)
221
+	{
222
+		$this->cache[$parentKey][$relation] = $this->getCachedRelationship($parentKey, $relation, $result, $relationship);
223
+	}
224
+
225
+	/**
226
+	 * Get Entity's Hash
227
+	 *
228
+	 * @param  $entity
229
+	 * @return string
230
+	 */
231
+	protected function getEntityHash(InternallyMappable $entity)
232
+	{
233
+		$class = get_class($entity->getObject());
234
+
235
+		$mapper = Manager::getMapper($class);
236
+
237
+		$keyName = $mapper->getEntityMap()->getKeyName();
238
+
239
+		return $class.'.'.$entity->getEntityAttribute($keyName);
240
+	}
241
+
242
+	/**
243
+	 * Refresh the cache record for an aggregated entity after a write operation
244
+	 *
245
+	 * @param  InternallyMappable $entity [description]
246
+	 * @return [type]                     [description]
247
+	 */
248
+	public function refresh(Aggregate $entity)
249
+	{
250
+		$this->cache[$entity->getEntityId()] = $this->transform($entity);
251
+	}
252
+
253
+	/**
254
+	 * Transform an Aggregated Entity into a cache record
255
+	 *
256
+	 * @param  Aggregate $entity
257
+	 * @return array
258
+	 */
259
+	protected function transform(Aggregate $aggregatedEntity)
260
+	{
261
+		$baseAttributes = $aggregatedEntity->getRawAttributes();
262
+
263
+		$relationAttributes = [];
264
+
265
+		// First we'll handle each relationships that are a one to one
266
+		// relation, and which will be saved as a CachedRelationship
267
+		// object inside the cache.
268 268
         
269
-        // NOTE : storing localRelationships maybe useless has we store
270
-        // the foreign key in the attributes already.
269
+		// NOTE : storing localRelationships maybe useless has we store
270
+		// the foreign key in the attributes already.
271 271
 
272
-        foreach ($this->entityMap->getSingleRelationships() as $relation) {
273
-            $aggregates = $aggregatedEntity->getRelationship($relation);
272
+		foreach ($this->entityMap->getSingleRelationships() as $relation) {
273
+			$aggregates = $aggregatedEntity->getRelationship($relation);
274 274
             
275
-            if (count($aggregates) == 1) {
276
-                $related = $aggregates[0];
277
-                $relationAttributes[$relation] = new CachedRelationship($related->getEntityHash());
278
-            }
279
-            if (count($aggregates) > 1) {
280
-                throw new MappingException("Single Relationship '$relation' contains several related entities");
281
-            }
282
-        }
283
-
284
-        // Then we'll handle the 'many' relationships and store them as
285
-        // an array of CachedRelationship objects.
286
-
287
-        foreach ($this->entityMap->getManyRelationships() as $relation) {
288
-            $aggregates = $aggregatedEntity->getRelationship($relation);
289
-
290
-            $relationAttributes[$relation] = [];
291
-
292
-            foreach ($aggregates as $aggregate) {
293
-                $relationAttributes[$relation][] = new CachedRelationship(
294
-                    $aggregate->getEntityHash(),
295
-                    $aggregate->getPivotAttributes()
296
-                );
297
-            }
298
-        }
299
-
300
-        return $baseAttributes + $relationAttributes;
301
-    }
275
+			if (count($aggregates) == 1) {
276
+				$related = $aggregates[0];
277
+				$relationAttributes[$relation] = new CachedRelationship($related->getEntityHash());
278
+			}
279
+			if (count($aggregates) > 1) {
280
+				throw new MappingException("Single Relationship '$relation' contains several related entities");
281
+			}
282
+		}
283
+
284
+		// Then we'll handle the 'many' relationships and store them as
285
+		// an array of CachedRelationship objects.
286
+
287
+		foreach ($this->entityMap->getManyRelationships() as $relation) {
288
+			$aggregates = $aggregatedEntity->getRelationship($relation);
289
+
290
+			$relationAttributes[$relation] = [];
291
+
292
+			foreach ($aggregates as $aggregate) {
293
+				$relationAttributes[$relation][] = new CachedRelationship(
294
+					$aggregate->getEntityHash(),
295
+					$aggregate->getPivotAttributes()
296
+				);
297
+			}
298
+		}
299
+
300
+		return $baseAttributes + $relationAttributes;
301
+	}
302 302
         
303
-    protected function getPivotValues($relation, InternallyMappable $entity)
304
-    {
305
-        $values = [];
306
-
307
-        $entityAttributes = $entity->getEntityAttributes();
308
-
309
-        if (array_key_exists($relation, $this->pivotAttributes)) {
310
-            foreach ($this->pivotAttributes[$relation] as $attribute) {
311
-                if (array_key_exists($attribute, $entityAttributes)) {
312
-                    $values[$attribute] = $entity->getEntityAttribute('pivot')->$attribute;
313
-                }
314
-            }
315
-        }
316
-
317
-        return $values;
318
-    }
303
+	protected function getPivotValues($relation, InternallyMappable $entity)
304
+	{
305
+		$values = [];
306
+
307
+		$entityAttributes = $entity->getEntityAttributes();
308
+
309
+		if (array_key_exists($relation, $this->pivotAttributes)) {
310
+			foreach ($this->pivotAttributes[$relation] as $attribute) {
311
+				if (array_key_exists($attribute, $entityAttributes)) {
312
+					$values[$attribute] = $entity->getEntityAttribute('pivot')->$attribute;
313
+				}
314
+			}
315
+		}
316
+
317
+		return $values;
318
+	}
319 319
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -130,7 +130,7 @@  discard block
 block discarded – undo
130 130
     {
131 131
         $keyName = $this->entityMap->getKeyName();
132 132
 
133
-        if (! $parent instanceof InternallyMappable) {
133
+        if (!$parent instanceof InternallyMappable) {
134 134
             $parent = $this->factory->make($parent);
135 135
         }
136 136
 
@@ -160,7 +160,7 @@  discard block
 block discarded – undo
160 160
     {
161 161
         $pivotColumns = $relationship->getPivotAttributes();
162 162
 
163
-        if (! array_key_exists($relation, $this->pivotAttributes)) {
163
+        if (!array_key_exists($relation, $this->pivotAttributes)) {
164 164
             $this->pivotAttributes[$relation] = $pivotColumns;
165 165
         }
166 166
 
Please login to merge, or discard this patch.