Completed
Pull Request — 5.1 (#103)
by
unknown
04:37
created
src/System/Wrappers/Wrapper.php 1 patch
Indentation   +113 added lines, -113 removed lines patch added patch discarded remove patch
@@ -11,117 +11,117 @@
 block discarded – undo
11 11
  */
12 12
 abstract class Wrapper implements InternallyMappable
13 13
 {
14
-    /**
15
-     * Original Entity Object
16
-     *
17
-     * @var mixed
18
-     */
19
-    protected $entity;
20
-
21
-    /**
22
-     * Corresponding EntityMap
23
-     *
24
-     * @var \Analogue\ORM\EntityMap
25
-     */
26
-    protected $entityMap;
27
-
28
-    /**
29
-     * Wrapper constructor.
30
-     * @param $entity
31
-     * @param $entityMap
32
-     */
33
-    public function __construct($entity, $entityMap)
34
-    {
35
-        $this->entity = $entity;
36
-        $this->entityMap = $entityMap;
37
-    }
38
-
39
-    /**
40
-     * Return the wrapped entity class
41
-     *
42
-     * @return mixed
43
-     */
44
-    public function getEntityClass()
45
-    {
46
-        return get_class($this->entity);
47
-    }
48
-
49
-    /**
50
-     * Returns the wrapped entity
51
-     *
52
-     * @return mixed
53
-     */
54
-    public function getObject()
55
-    {
56
-        return $this->entity;
57
-    }
58
-
59
-    /**
60
-     * Returns the wrapped entity's map
61
-     *
62
-     * @return mixed
63
-     */
64
-    public function getMap()
65
-    {
66
-        return $this->entityMap;
67
-    }
68
-
69
-    /**
70
-     * Set the lazyloading proxies on the wrapped entity objet
71
-     *
72
-     * @return void
73
-     */
74
-    public function setProxies()
75
-    {
76
-        $attributes = $this->getEntityAttributes();
77
-        $singleRelations = $this->entityMap->getSingleRelationships();
78
-        $manyRelations = $this->entityMap->getManyRelationships();
79
-
80
-        $proxies = [];
81
-
82
-        foreach ($this->entityMap->getRelationships() as $relation) {
83
-            if (!array_key_exists($relation, $attributes) || is_null($attributes[$relation])) {
84
-                if (in_array($relation, $singleRelations)) {
85
-                    $proxies[$relation] = new EntityProxy($this->getObject(), $relation);
86
-                }
87
-                if (in_array($relation, $manyRelations)) {
88
-                    $proxies[$relation] = new CollectionProxy($this->getObject(), $relation);
89
-                }
90
-            }
91
-        }
92
-
93
-        foreach ($proxies as $key => $value) {
94
-            $this->setEntityAttribute($key, $value);
95
-        }
96
-    }
97
-
98
-    /**
99
-     * @param string $key
100
-     * @param string $value
101
-     * @return mixed
102
-     */
103
-    abstract public function setEntityAttribute($key, $value);
104
-
105
-    /**
106
-     * @param string $key
107
-     * @return mixed
108
-     */
109
-    abstract public function getEntityAttribute($key);
110
-
111
-    /**
112
-     * @param array $attributes
113
-     * @return mixed
114
-     */
115
-    abstract public function setEntityAttributes(array $attributes);
116
-
117
-    /**
118
-     * @return mixed
119
-     */
120
-    abstract public function getEntityAttributes();
121
-
122
-    /**
123
-     * @param string $key
124
-     * @return mixed
125
-     */
126
-    abstract public function hasAttribute($key);
14
+	/**
15
+	 * Original Entity Object
16
+	 *
17
+	 * @var mixed
18
+	 */
19
+	protected $entity;
20
+
21
+	/**
22
+	 * Corresponding EntityMap
23
+	 *
24
+	 * @var \Analogue\ORM\EntityMap
25
+	 */
26
+	protected $entityMap;
27
+
28
+	/**
29
+	 * Wrapper constructor.
30
+	 * @param $entity
31
+	 * @param $entityMap
32
+	 */
33
+	public function __construct($entity, $entityMap)
34
+	{
35
+		$this->entity = $entity;
36
+		$this->entityMap = $entityMap;
37
+	}
38
+
39
+	/**
40
+	 * Return the wrapped entity class
41
+	 *
42
+	 * @return mixed
43
+	 */
44
+	public function getEntityClass()
45
+	{
46
+		return get_class($this->entity);
47
+	}
48
+
49
+	/**
50
+	 * Returns the wrapped entity
51
+	 *
52
+	 * @return mixed
53
+	 */
54
+	public function getObject()
55
+	{
56
+		return $this->entity;
57
+	}
58
+
59
+	/**
60
+	 * Returns the wrapped entity's map
61
+	 *
62
+	 * @return mixed
63
+	 */
64
+	public function getMap()
65
+	{
66
+		return $this->entityMap;
67
+	}
68
+
69
+	/**
70
+	 * Set the lazyloading proxies on the wrapped entity objet
71
+	 *
72
+	 * @return void
73
+	 */
74
+	public function setProxies()
75
+	{
76
+		$attributes = $this->getEntityAttributes();
77
+		$singleRelations = $this->entityMap->getSingleRelationships();
78
+		$manyRelations = $this->entityMap->getManyRelationships();
79
+
80
+		$proxies = [];
81
+
82
+		foreach ($this->entityMap->getRelationships() as $relation) {
83
+			if (!array_key_exists($relation, $attributes) || is_null($attributes[$relation])) {
84
+				if (in_array($relation, $singleRelations)) {
85
+					$proxies[$relation] = new EntityProxy($this->getObject(), $relation);
86
+				}
87
+				if (in_array($relation, $manyRelations)) {
88
+					$proxies[$relation] = new CollectionProxy($this->getObject(), $relation);
89
+				}
90
+			}
91
+		}
92
+
93
+		foreach ($proxies as $key => $value) {
94
+			$this->setEntityAttribute($key, $value);
95
+		}
96
+	}
97
+
98
+	/**
99
+	 * @param string $key
100
+	 * @param string $value
101
+	 * @return mixed
102
+	 */
103
+	abstract public function setEntityAttribute($key, $value);
104
+
105
+	/**
106
+	 * @param string $key
107
+	 * @return mixed
108
+	 */
109
+	abstract public function getEntityAttribute($key);
110
+
111
+	/**
112
+	 * @param array $attributes
113
+	 * @return mixed
114
+	 */
115
+	abstract public function setEntityAttributes(array $attributes);
116
+
117
+	/**
118
+	 * @return mixed
119
+	 */
120
+	abstract public function getEntityAttributes();
121
+
122
+	/**
123
+	 * @param string $key
124
+	 * @return mixed
125
+	 */
126
+	abstract public function hasAttribute($key);
127 127
 }
Please login to merge, or discard this patch.
src/Relationships/MorphTo.php 1 patch
Indentation   +215 added lines, -215 removed lines patch added patch discarded remove patch
@@ -8,219 +8,219 @@
 block discarded – undo
8 8
 
9 9
 class MorphTo extends BelongsTo
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 EntityCollection
22
-     */
23
-    protected $entities;
24
-
25
-    /**
26
-     * All of the models keyed by ID.
27
-     *
28
-     * @var array
29
-     */
30
-    protected $dictionary = [];
31
-
32
-    /**
33
-     * Indicates if soft-deleted model instances should be fetched.
34
-     *
35
-     * @var bool
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 Mapper                 $mapper
50
-     * @param \Analogue\ORM\Mappable $parent
51
-     * @param string                 $foreignKey
52
-     * @param string                 $otherKey
53
-     * @param string                 $type
54
-     * @param string                 $relation
55
-     */
56
-    public function __construct(Mapper $mapper, $parent, $foreignKey, $otherKey, $type, $relation)
57
-    {
58
-        $this->morphType = $type;
59
-
60
-        parent::__construct($mapper, $parent, $foreignKey, $otherKey, $relation);
61
-    }
62
-
63
-    /**
64
-     * Set the constraints for an eager load of the relation.
65
-     *
66
-     * @param  array $entities
67
-     * @return void
68
-     */
69
-    public function addEagerConstraints(array $entities)
70
-    {
71
-        $this->buildDictionary($this->entities = EntityCollection::make($entities));
72
-    }
73
-
74
-    /**
75
-     * Build a dictionary with the entities
76
-     *
77
-     * @param  EntityCollection $entities
78
-     * @return void
79
-     */
80
-    protected function buildDictionary(EntityCollection $entities)
81
-    {
82
-        foreach ($entities as $entity) {
83
-            if ($entity->getEntityAttribute($this->morphType)) {
84
-                $this->dictionary[$entity->getEntityAttribute($this->morphType)][$entity->getEntityAttribute($this->foreignKey)][] = $entity;
85
-            }
86
-        }
87
-    }
88
-
89
-    /**
90
-     * Match the eagerly loaded results to their parents.
91
-     *
92
-     * @param  array            $entities
93
-     * @param  EntityCollection $results
94
-     * @param  string           $relation
95
-     * @return array
96
-     */
97
-    public function match(array $entities, EntityCollection $results, $relation)
98
-    {
99
-        return $entities;
100
-    }
101
-
102
-    /**
103
-     * Get the results of the relationship.
104
-     *
105
-     * @throws \Analogue\ORM\Exceptions\MappingException
106
-     * @return EntityCollection
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  EntityCollection $results
122
-     * @return void
123
-     */
124
-    protected function matchToMorphParents($type, EntityCollection $results)
125
-    {
126
-        $mapper = $this->relatedMapper->getManager()->mapper($type);
127
-        $keyName = $mapper->getEntityMap()->getKeyName();
128
-
129
-        foreach ($results as $result) {
130
-            $key = $result->{$keyName};
131
-
132
-            if (isset($this->dictionary[$type][$key])) {
133
-                foreach ($this->dictionary[$type][$key] as $entity) {
134
-                    $entity->setEntityAttribute($this->relation, $result);
135
-                }
136
-            }
137
-        }
138
-    }
139
-
140
-    /**
141
-     * Get all of the relation results for a type.
142
-     *
143
-     * @param  string $type
144
-     * @throws \Analogue\ORM\Exceptions\MappingException
145
-     * @return EntityCollection
146
-     */
147
-    protected function getResultsByType($type)
148
-    {
149
-        $mapper = $this->relatedMapper->getManager()->mapper($type);
150
-
151
-        $key = $mapper->getEntityMap()->getKeyName();
152
-
153
-        $query = $mapper->getQuery();
154
-
155
-        return $query->whereIn($key, $this->gatherKeysByType($type)->all())->get();
156
-    }
157
-
158
-    /**
159
-     * Gather all of the foreign keys for a given type.
160
-     *
161
-     * @param  string $type
162
-     * @return BaseCollection
163
-     */
164
-    protected function gatherKeysByType($type)
165
-    {
166
-        $foreign = $this->foreignKey;
167
-
168
-        return BaseCollection::make($this->dictionary[$type])->map(function ($entities) use ($foreign) {
169
-            return head($entities)->{$foreign};
170
-
171
-        })->unique();
172
-    }
173
-
174
-    /**
175
-     * Associate the model instance to the given parent.
176
-     *
177
-     * @param  mixed $entity
178
-     * @return void
179
-     */
180
-    public function associate($entity)
181
-    {
182
-        // The Mapper will retrieve this association within the object model, we won't be using
183
-        // the foreign key attribute inside the parent Entity.
184
-        //
185
-        //$this->parent->setEntityAttribute($this->foreignKey, $entity->getEntityAttribute($this->otherKey));
186
-        //
187
-        // Instead, we'll just add the object to the Entity's attribute
188
-
189
-        $this->parent->setEntityAttribute($this->relation, $entity);
190
-    }
191
-
192
-    /**
193
-     * Get the foreign key value pair for a related object
194
-     *
195
-     * @var mixed $related
196
-     *
197
-     * @return array
198
-     */
199
-    public function getForeignKeyValuePair($related)
200
-    {
201
-        $foreignKey = $this->getForeignKey();
202
-
203
-        if ($related) {
204
-            $wrapper = $this->factory->make($related);
205
-
206
-            $relatedKey = $this->relatedMap->getKeyName();
207
-
208
-            return [
209
-                $foreignKey => $wrapper->getEntityAttribute($relatedKey),
210
-                $this->morphType => $wrapper->getMap()->getMorphClass(),
211
-            ];
212
-        } else {
213
-            return [$foreignKey => null];
214
-        }
215
-    }
216
-
217
-    /**
218
-     * Get the dictionary used by the relationship.
219
-     *
220
-     * @return array
221
-     */
222
-    public function getDictionary()
223
-    {
224
-        return $this->dictionary;
225
-    }
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 EntityCollection
22
+	 */
23
+	protected $entities;
24
+
25
+	/**
26
+	 * All of the models keyed by ID.
27
+	 *
28
+	 * @var array
29
+	 */
30
+	protected $dictionary = [];
31
+
32
+	/**
33
+	 * Indicates if soft-deleted model instances should be fetched.
34
+	 *
35
+	 * @var bool
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 Mapper                 $mapper
50
+	 * @param \Analogue\ORM\Mappable $parent
51
+	 * @param string                 $foreignKey
52
+	 * @param string                 $otherKey
53
+	 * @param string                 $type
54
+	 * @param string                 $relation
55
+	 */
56
+	public function __construct(Mapper $mapper, $parent, $foreignKey, $otherKey, $type, $relation)
57
+	{
58
+		$this->morphType = $type;
59
+
60
+		parent::__construct($mapper, $parent, $foreignKey, $otherKey, $relation);
61
+	}
62
+
63
+	/**
64
+	 * Set the constraints for an eager load of the relation.
65
+	 *
66
+	 * @param  array $entities
67
+	 * @return void
68
+	 */
69
+	public function addEagerConstraints(array $entities)
70
+	{
71
+		$this->buildDictionary($this->entities = EntityCollection::make($entities));
72
+	}
73
+
74
+	/**
75
+	 * Build a dictionary with the entities
76
+	 *
77
+	 * @param  EntityCollection $entities
78
+	 * @return void
79
+	 */
80
+	protected function buildDictionary(EntityCollection $entities)
81
+	{
82
+		foreach ($entities as $entity) {
83
+			if ($entity->getEntityAttribute($this->morphType)) {
84
+				$this->dictionary[$entity->getEntityAttribute($this->morphType)][$entity->getEntityAttribute($this->foreignKey)][] = $entity;
85
+			}
86
+		}
87
+	}
88
+
89
+	/**
90
+	 * Match the eagerly loaded results to their parents.
91
+	 *
92
+	 * @param  array            $entities
93
+	 * @param  EntityCollection $results
94
+	 * @param  string           $relation
95
+	 * @return array
96
+	 */
97
+	public function match(array $entities, EntityCollection $results, $relation)
98
+	{
99
+		return $entities;
100
+	}
101
+
102
+	/**
103
+	 * Get the results of the relationship.
104
+	 *
105
+	 * @throws \Analogue\ORM\Exceptions\MappingException
106
+	 * @return EntityCollection
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  EntityCollection $results
122
+	 * @return void
123
+	 */
124
+	protected function matchToMorphParents($type, EntityCollection $results)
125
+	{
126
+		$mapper = $this->relatedMapper->getManager()->mapper($type);
127
+		$keyName = $mapper->getEntityMap()->getKeyName();
128
+
129
+		foreach ($results as $result) {
130
+			$key = $result->{$keyName};
131
+
132
+			if (isset($this->dictionary[$type][$key])) {
133
+				foreach ($this->dictionary[$type][$key] as $entity) {
134
+					$entity->setEntityAttribute($this->relation, $result);
135
+				}
136
+			}
137
+		}
138
+	}
139
+
140
+	/**
141
+	 * Get all of the relation results for a type.
142
+	 *
143
+	 * @param  string $type
144
+	 * @throws \Analogue\ORM\Exceptions\MappingException
145
+	 * @return EntityCollection
146
+	 */
147
+	protected function getResultsByType($type)
148
+	{
149
+		$mapper = $this->relatedMapper->getManager()->mapper($type);
150
+
151
+		$key = $mapper->getEntityMap()->getKeyName();
152
+
153
+		$query = $mapper->getQuery();
154
+
155
+		return $query->whereIn($key, $this->gatherKeysByType($type)->all())->get();
156
+	}
157
+
158
+	/**
159
+	 * Gather all of the foreign keys for a given type.
160
+	 *
161
+	 * @param  string $type
162
+	 * @return BaseCollection
163
+	 */
164
+	protected function gatherKeysByType($type)
165
+	{
166
+		$foreign = $this->foreignKey;
167
+
168
+		return BaseCollection::make($this->dictionary[$type])->map(function ($entities) use ($foreign) {
169
+			return head($entities)->{$foreign};
170
+
171
+		})->unique();
172
+	}
173
+
174
+	/**
175
+	 * Associate the model instance to the given parent.
176
+	 *
177
+	 * @param  mixed $entity
178
+	 * @return void
179
+	 */
180
+	public function associate($entity)
181
+	{
182
+		// The Mapper will retrieve this association within the object model, we won't be using
183
+		// the foreign key attribute inside the parent Entity.
184
+		//
185
+		//$this->parent->setEntityAttribute($this->foreignKey, $entity->getEntityAttribute($this->otherKey));
186
+		//
187
+		// Instead, we'll just add the object to the Entity's attribute
188
+
189
+		$this->parent->setEntityAttribute($this->relation, $entity);
190
+	}
191
+
192
+	/**
193
+	 * Get the foreign key value pair for a related object
194
+	 *
195
+	 * @var mixed $related
196
+	 *
197
+	 * @return array
198
+	 */
199
+	public function getForeignKeyValuePair($related)
200
+	{
201
+		$foreignKey = $this->getForeignKey();
202
+
203
+		if ($related) {
204
+			$wrapper = $this->factory->make($related);
205
+
206
+			$relatedKey = $this->relatedMap->getKeyName();
207
+
208
+			return [
209
+				$foreignKey => $wrapper->getEntityAttribute($relatedKey),
210
+				$this->morphType => $wrapper->getMap()->getMorphClass(),
211
+			];
212
+		} else {
213
+			return [$foreignKey => null];
214
+		}
215
+	}
216
+
217
+	/**
218
+	 * Get the dictionary used by the relationship.
219
+	 *
220
+	 * @return array
221
+	 */
222
+	public function getDictionary()
223
+	{
224
+		return $this->dictionary;
225
+	}
226 226
 }
Please login to merge, or discard this patch.