Completed
Pull Request — 5.1 (#84)
by Maxim
08:05
created
src/Drivers/DBAdapter.php 1 patch
Indentation   +30 added lines, -30 removed lines patch added patch discarded remove patch
@@ -4,38 +4,38 @@
 block discarded – undo
4 4
 
5 5
 interface DBAdapter
6 6
 {
7
-    /**
8
-     * Return's Driver specific Query Implementation
9
-     *
10
-     * @return \Analogue\ORM\Drivers\QueryAdapter|\Analogue\ORM\Drivers\IlluminateQueryAdapter
11
-     */
12
-    public function getQuery();
7
+	/**
8
+	 * Return's Driver specific Query Implementation
9
+	 *
10
+	 * @return \Analogue\ORM\Drivers\QueryAdapter|\Analogue\ORM\Drivers\IlluminateQueryAdapter
11
+	 */
12
+	public function getQuery();
13 13
 
14
-    /**
15
-     * Return the Date format used on this adapter
16
-     *
17
-     * @return string
18
-     */
19
-    public function getDateFormat();
14
+	/**
15
+	 * Return the Date format used on this adapter
16
+	 *
17
+	 * @return string
18
+	 */
19
+	public function getDateFormat();
20 20
 
21
-    /**
22
-     * Start a DB transaction on driver that supports it.
23
-     *
24
-     * @return void
25
-     */
26
-    public function beginTransaction();
21
+	/**
22
+	 * Start a DB transaction on driver that supports it.
23
+	 *
24
+	 * @return void
25
+	 */
26
+	public function beginTransaction();
27 27
 
28
-    /**
29
-     * Commit a DB transaction on driver that supports it.
30
-     *
31
-     * @return void
32
-     */
33
-    public function commit();
28
+	/**
29
+	 * Commit a DB transaction on driver that supports it.
30
+	 *
31
+	 * @return void
32
+	 */
33
+	public function commit();
34 34
 
35
-    /**
36
-     * Rollback a DB transaction on driver that supports it.
37
-     *
38
-     * @return void
39
-     */
40
-    public function rollback();
35
+	/**
36
+	 * Rollback a DB transaction on driver that supports it.
37
+	 *
38
+	 * @return void
39
+	 */
40
+	public function rollback();
41 41
 }
Please login to merge, or discard this patch.
src/Mappable.php 1 patch
Indentation   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -4,17 +4,17 @@
 block discarded – undo
4 4
 
5 5
 interface Mappable
6 6
 {
7
-    /**
8
-     * Set the object attribute raw values (hydration)
9
-     *
10
-     * @param array $attributes
11
-     */
12
-    public function setEntityAttributes(array $attributes);
7
+	/**
8
+	 * Set the object attribute raw values (hydration)
9
+	 *
10
+	 * @param array $attributes
11
+	 */
12
+	public function setEntityAttributes(array $attributes);
13 13
 
14
-    /**
15
-     * Get the raw object's values.
16
-     *
17
-     * @return array
18
-     */
19
-    public function getEntityAttributes();
14
+	/**
15
+	 * Get the raw object's values.
16
+	 *
17
+	 * @return array
18
+	 */
19
+	public function getEntityAttributes();
20 20
 }
Please login to merge, or discard this patch.
src/Analogue.php 1 patch
Indentation   +109 added lines, -109 removed lines patch added patch discarded remove patch
@@ -17,114 +17,114 @@
 block discarded – undo
17 17
  */
18 18
 class Analogue
19 19
 {
20
-    /**
21
-     * @var self
22
-     */
23
-    protected static $instance;
24
-
25
-    /**
26
-     * @var Manager
27
-     */
28
-    protected static $manager;
29
-
30
-    /**
31
-     * @var Capsule
32
-     */
33
-    protected static $capsule;
34
-
35
-    /**
36
-     * @var bool
37
-     */
38
-    protected static $booted = false;
39
-
40
-    /**
41
-     * Analogue constructor.
42
-     * @param array $connection
43
-     */
44
-    public function __construct(array $connection)
45
-    {
46
-        if (!static::$booted) {
47
-            static::$capsule = new Capsule;
48
-
49
-            $this->addConnection($connection);
50
-
51
-            $this->boot();
52
-        }
53
-    }
54
-
55
-    /**
56
-     * Boot Analogue
57
-     *
58
-     * @return Analogue
59
-     */
60
-    public function boot()
61
-    {
62
-        if (static::$booted) {
63
-            return $this;
64
-        }
65
-
66
-        $dispatcher = new Dispatcher;
67
-
68
-        $connectionProvider = new CapsuleConnectionProvider(static::$capsule);
69
-
70
-        $illuminate = new IlluminateDriver($connectionProvider);
71
-
72
-        $driverManager = new DriverManager;
73
-
74
-        $driverManager->addDriver($illuminate);
75
-
76
-        static::$manager = new Manager($driverManager, $dispatcher);
20
+	/**
21
+	 * @var self
22
+	 */
23
+	protected static $instance;
24
+
25
+	/**
26
+	 * @var Manager
27
+	 */
28
+	protected static $manager;
29
+
30
+	/**
31
+	 * @var Capsule
32
+	 */
33
+	protected static $capsule;
34
+
35
+	/**
36
+	 * @var bool
37
+	 */
38
+	protected static $booted = false;
39
+
40
+	/**
41
+	 * Analogue constructor.
42
+	 * @param array $connection
43
+	 */
44
+	public function __construct(array $connection)
45
+	{
46
+		if (!static::$booted) {
47
+			static::$capsule = new Capsule;
48
+
49
+			$this->addConnection($connection);
50
+
51
+			$this->boot();
52
+		}
53
+	}
54
+
55
+	/**
56
+	 * Boot Analogue
57
+	 *
58
+	 * @return Analogue
59
+	 */
60
+	public function boot()
61
+	{
62
+		if (static::$booted) {
63
+			return $this;
64
+		}
65
+
66
+		$dispatcher = new Dispatcher;
67
+
68
+		$connectionProvider = new CapsuleConnectionProvider(static::$capsule);
69
+
70
+		$illuminate = new IlluminateDriver($connectionProvider);
71
+
72
+		$driverManager = new DriverManager;
73
+
74
+		$driverManager->addDriver($illuminate);
75
+
76
+		static::$manager = new Manager($driverManager, $dispatcher);
77 77
         
78
-        static::$instance = $this;
79
-
80
-        static::$booted = true;
81
-
82
-        return $this;
83
-    }
84
-
85
-    /**
86
-     * Add a connection array to Capsule
87
-     *
88
-     * @param array  $config
89
-     * @param string $name
90
-     */
91
-    public function addConnection($config, $name = 'default')
92
-    {
93
-        static::$capsule->addConnection($config, $name);
94
-    }
95
-
96
-    /**
97
-     * Get a Database connection object
98
-     *
99
-     * @param  $name
100
-     * @return \Illuminate\Database\Connection
101
-     */
102
-    public function connection($name = null)
103
-    {
104
-        return static::$capsule->getConnection($name);
105
-    }
106
-
107
-    /**
108
-     * Dynamically handle static calls to the instance, Facade Style.
109
-     *
110
-     * @param  string $method
111
-     * @param  array  $parameters
112
-     * @return mixed
113
-     */
114
-    public static function __callStatic($method, $parameters)
115
-    {
116
-        return call_user_func_array([static::$instance, $method], $parameters);
117
-    }
118
-
119
-    /**
120
-     * Dynamically handle calls to the Analogue Manager instance.
121
-     *
122
-     * @param  string $method
123
-     * @param  array  $parameters
124
-     * @return mixed
125
-     */
126
-    public function __call($method, $parameters)
127
-    {
128
-        return call_user_func_array([static::$manager, $method], $parameters);
129
-    }
78
+		static::$instance = $this;
79
+
80
+		static::$booted = true;
81
+
82
+		return $this;
83
+	}
84
+
85
+	/**
86
+	 * Add a connection array to Capsule
87
+	 *
88
+	 * @param array  $config
89
+	 * @param string $name
90
+	 */
91
+	public function addConnection($config, $name = 'default')
92
+	{
93
+		static::$capsule->addConnection($config, $name);
94
+	}
95
+
96
+	/**
97
+	 * Get a Database connection object
98
+	 *
99
+	 * @param  $name
100
+	 * @return \Illuminate\Database\Connection
101
+	 */
102
+	public function connection($name = null)
103
+	{
104
+		return static::$capsule->getConnection($name);
105
+	}
106
+
107
+	/**
108
+	 * Dynamically handle static calls to the instance, Facade Style.
109
+	 *
110
+	 * @param  string $method
111
+	 * @param  array  $parameters
112
+	 * @return mixed
113
+	 */
114
+	public static function __callStatic($method, $parameters)
115
+	{
116
+		return call_user_func_array([static::$instance, $method], $parameters);
117
+	}
118
+
119
+	/**
120
+	 * Dynamically handle calls to the Analogue Manager instance.
121
+	 *
122
+	 * @param  string $method
123
+	 * @param  array  $parameters
124
+	 * @return mixed
125
+	 */
126
+	public function __call($method, $parameters)
127
+	{
128
+		return call_user_func_array([static::$manager, $method], $parameters);
129
+	}
130 130
 }
Please login to merge, or discard this patch.
src/EntityCollection.php 2 patches
Indentation   +379 added lines, -379 removed lines patch added patch discarded remove patch
@@ -10,387 +10,387 @@
 block discarded – undo
10 10
 
11 11
 class EntityCollection extends Collection
12 12
 {
13
-    /**
14
-     * Wrapper Factory
15
-     *
16
-     * @var \Analogue\ORM\System\Wrappers\Factory
17
-     */
18
-    protected $factory;
19
-
20
-    /**
21
-     * EntityCollection constructor.
22
-     * @param array|null $entities
23
-     */
24
-    public function __construct(array $entities = null)
25
-    {
26
-        $this->factory = new Factory;
27
-
28
-        parent::__construct($entities);
29
-    }
30
-
31
-    /**
32
-     * Find an entity in the collection by key.
33
-     *
34
-     * @param  mixed $key
35
-     * @param  mixed $default
36
-     * @throws MappingException
37
-     * @return \Analogue\ORM\Entity
38
-     */
39
-    public function find($key, $default = null)
40
-    {
41
-        if ($key instanceof Mappable) {
42
-            $key = $this->getEntityKey($key);
43
-        }
44
-
45
-        return array_first($this->items, function ($itemKey, $entity) use ($key) {
46
-            return $this->getEntityKey($entity) == $key;
47
-        }, $default);
48
-    }
49
-
50
-    /**
51
-     * Add an entity to the collection.
52
-     *
53
-     * @param  Mappable $entity
54
-     * @return $this
55
-     */
56
-    public function add($entity)
57
-    {
58
-        $this->push($entity);
59
-
60
-        return $this;
61
-    }
62
-
63
-    /**
64
-     * Remove an entity from the collection
65
-     *
66
-     * @param $entity
67
-     * @throws MappingException
68
-     * @return mixed
69
-     */
70
-    public function remove($entity)
71
-    {
72
-        $key = $this->getEntityKey($entity);
73
-
74
-        return $this->pull($key);
75
-    }
76
-
77
-    /**
78
-     * Push an item onto the end of the collection.
79
-     *
80
-     * @param  mixed $value
81
-     * @return void
82
-     */
83
-    public function push($value)
84
-    {
85
-        $this->offsetSet(null, $value);
86
-    }
87
-
88
-    /**
89
-     * Put an item in the collection by key.
90
-     *
91
-     * @param  mixed $key
92
-     * @param  mixed $value
93
-     * @return void
94
-     */
95
-    public function put($key, $value)
96
-    {
97
-        $this->offsetSet($key, $value);
98
-    }
99
-
100
-    /**
101
-     * Set the item at a given offset.
102
-     *
103
-     * @param  mixed $key
104
-     * @param  mixed $value
105
-     * @return void
106
-     */
107
-    public function offsetSet($key, $value)
108
-    {
109
-        if (is_null($key)) {
110
-            $this->items[] = $value;
111
-        } else {
112
-            $this->items[$key] = $value;
113
-        }
114
-    }
115
-
116
-    /**
117
-     * Determine if a key exists in the collection.
118
-     *
119
-     * @param  mixed      $key
120
-     * @param  mixed|null $value
121
-     * @return bool
122
-     */
123
-    public function contains($key, $value = null)
124
-    {
125
-        return !is_null($this->find($key));
126
-    }
127
-
128
-    /**
129
-     * Fetch a nested element of the collection.
130
-     *
131
-     * @param  string $key
132
-     * @return self
133
-     */
134
-    public function fetch($key)
135
-    {
136
-        return new static(array_fetch($this->toArray(), $key));
137
-    }
138
-
139
-    /**
140
-     * Generic function for returning class.key value pairs
141
-     *
142
-     * @throws MappingException
143
-     * @return string
144
-     */
145
-    public function getEntityHashes()
146
-    {
147
-        return array_map(function($entity) {
148
-            $class = get_class($entity);
149
-
150
-            $mapper = Manager::getMapper($class);
13
+	/**
14
+	 * Wrapper Factory
15
+	 *
16
+	 * @var \Analogue\ORM\System\Wrappers\Factory
17
+	 */
18
+	protected $factory;
19
+
20
+	/**
21
+	 * EntityCollection constructor.
22
+	 * @param array|null $entities
23
+	 */
24
+	public function __construct(array $entities = null)
25
+	{
26
+		$this->factory = new Factory;
27
+
28
+		parent::__construct($entities);
29
+	}
30
+
31
+	/**
32
+	 * Find an entity in the collection by key.
33
+	 *
34
+	 * @param  mixed $key
35
+	 * @param  mixed $default
36
+	 * @throws MappingException
37
+	 * @return \Analogue\ORM\Entity
38
+	 */
39
+	public function find($key, $default = null)
40
+	{
41
+		if ($key instanceof Mappable) {
42
+			$key = $this->getEntityKey($key);
43
+		}
44
+
45
+		return array_first($this->items, function ($itemKey, $entity) use ($key) {
46
+			return $this->getEntityKey($entity) == $key;
47
+		}, $default);
48
+	}
49
+
50
+	/**
51
+	 * Add an entity to the collection.
52
+	 *
53
+	 * @param  Mappable $entity
54
+	 * @return $this
55
+	 */
56
+	public function add($entity)
57
+	{
58
+		$this->push($entity);
59
+
60
+		return $this;
61
+	}
62
+
63
+	/**
64
+	 * Remove an entity from the collection
65
+	 *
66
+	 * @param $entity
67
+	 * @throws MappingException
68
+	 * @return mixed
69
+	 */
70
+	public function remove($entity)
71
+	{
72
+		$key = $this->getEntityKey($entity);
73
+
74
+		return $this->pull($key);
75
+	}
76
+
77
+	/**
78
+	 * Push an item onto the end of the collection.
79
+	 *
80
+	 * @param  mixed $value
81
+	 * @return void
82
+	 */
83
+	public function push($value)
84
+	{
85
+		$this->offsetSet(null, $value);
86
+	}
87
+
88
+	/**
89
+	 * Put an item in the collection by key.
90
+	 *
91
+	 * @param  mixed $key
92
+	 * @param  mixed $value
93
+	 * @return void
94
+	 */
95
+	public function put($key, $value)
96
+	{
97
+		$this->offsetSet($key, $value);
98
+	}
99
+
100
+	/**
101
+	 * Set the item at a given offset.
102
+	 *
103
+	 * @param  mixed $key
104
+	 * @param  mixed $value
105
+	 * @return void
106
+	 */
107
+	public function offsetSet($key, $value)
108
+	{
109
+		if (is_null($key)) {
110
+			$this->items[] = $value;
111
+		} else {
112
+			$this->items[$key] = $value;
113
+		}
114
+	}
115
+
116
+	/**
117
+	 * Determine if a key exists in the collection.
118
+	 *
119
+	 * @param  mixed      $key
120
+	 * @param  mixed|null $value
121
+	 * @return bool
122
+	 */
123
+	public function contains($key, $value = null)
124
+	{
125
+		return !is_null($this->find($key));
126
+	}
127
+
128
+	/**
129
+	 * Fetch a nested element of the collection.
130
+	 *
131
+	 * @param  string $key
132
+	 * @return self
133
+	 */
134
+	public function fetch($key)
135
+	{
136
+		return new static(array_fetch($this->toArray(), $key));
137
+	}
138
+
139
+	/**
140
+	 * Generic function for returning class.key value pairs
141
+	 *
142
+	 * @throws MappingException
143
+	 * @return string
144
+	 */
145
+	public function getEntityHashes()
146
+	{
147
+		return array_map(function($entity) {
148
+			$class = get_class($entity);
149
+
150
+			$mapper = Manager::getMapper($class);
151 151
             
152
-            $keyName = $mapper->getEntityMap()->getKeyName();
152
+			$keyName = $mapper->getEntityMap()->getKeyName();
153 153
             
154
-            return $class . '.' . $entity->getEntityAttribute($keyName);
155
-        },
156
-        $this->items);
157
-    }
158
-
159
-    /**
160
-     * Get a subset of the collection from entity hashes
161
-     *
162
-     * @param  array $hashes
163
-     * @throws MappingException
164
-     * @return array
165
-     */
166
-    public function getSubsetByHashes(array $hashes)
167
-    {
168
-        $subset = [];
169
-
170
-        foreach ($this->items as $item) {
171
-            $class = get_class($item);
172
-
173
-            $mapper = Manager::getMapper($class);
154
+			return $class . '.' . $entity->getEntityAttribute($keyName);
155
+		},
156
+		$this->items);
157
+	}
158
+
159
+	/**
160
+	 * Get a subset of the collection from entity hashes
161
+	 *
162
+	 * @param  array $hashes
163
+	 * @throws MappingException
164
+	 * @return array
165
+	 */
166
+	public function getSubsetByHashes(array $hashes)
167
+	{
168
+		$subset = [];
169
+
170
+		foreach ($this->items as $item) {
171
+			$class = get_class($item);
172
+
173
+			$mapper = Manager::getMapper($class);
174 174
             
175
-            $keyName = $mapper->getEntityMap()->getKeyName();
176
-
177
-            if (in_array($class . '.' . $item->$keyName, $hashes)) {
178
-                $subset[] = $item;
179
-            }
180
-        }
181
-
182
-        return $subset;
183
-    }
184
-
185
-    /**
186
-     * Merge the collection with the given items.
187
-     *
188
-     * @param  array $items
189
-     * @throws MappingException
190
-     * @return self
191
-     */
192
-    public function merge($items)
193
-    {
194
-        $dictionary = $this->getDictionary();
195
-
196
-        foreach ($items as $item) {
197
-            $dictionary[$this->getEntityKey($item)] = $item;
198
-        }
199
-
200
-        return new static(array_values($dictionary));
201
-    }
202
-
203
-    /**
204
-     * Diff the collection with the given items.
205
-     *
206
-     * @param  \ArrayAccess|array $items
207
-     * @return self
208
-     */
209
-    public function diff($items)
210
-    {
211
-        $diff = new static;
212
-
213
-        $dictionary = $this->getDictionary($items);
214
-
215
-        foreach ($this->items as $item) {
216
-            if (!isset($dictionary[$this->getEntityKey($item)])) {
217
-                $diff->add($item);
218
-            }
219
-        }
220
-
221
-        return $diff;
222
-    }
223
-
224
-    /**
225
-     * Intersect the collection with the given items.
226
-     *
227
-     * @param  \ArrayAccess|array $items
228
-     * @throws MappingException
229
-     * @return self
230
-     */
231
-    public function intersect($items)
232
-    {
233
-        $intersect = new static;
234
-
235
-        $dictionary = $this->getDictionary($items);
236
-
237
-        foreach ($this->items as $item) {
238
-            if (isset($dictionary[$this->getEntityKey($item)])) {
239
-                $intersect->add($item);
240
-            }
241
-        }
242
-
243
-        return $intersect;
244
-    }
245
-
246
-    /**
247
-     * Returns only the models from the collection with the specified keys.
248
-     *
249
-     * @param  mixed $keys
250
-     * @return self
251
-     */
252
-    public function only($keys)
253
-    {
254
-        $dictionary = array_only($this->getDictionary(), $keys);
255
-
256
-        return new static(array_values($dictionary));
257
-    }
258
-
259
-    /**
260
-     * Returns all models in the collection except the models with specified keys.
261
-     *
262
-     * @param  mixed $keys
263
-     * @return self
264
-     */
265
-    public function except($keys)
266
-    {
267
-        $dictionary = array_except($this->getDictionary(), $keys);
268
-
269
-        return new static(array_values($dictionary));
270
-    }
271
-
272
-    /**
273
-     * Get a dictionary keyed by primary keys.
274
-     *
275
-     * @param  \ArrayAccess|array $items
276
-     * @throws MappingException
277
-     * @return array
278
-     */
279
-    public function getDictionary($items = null)
280
-    {
281
-        $items = is_null($items) ? $this->items : $items;
282
-
283
-        $dictionary = [];
284
-
285
-        foreach ($items as $value) {
286
-            $dictionary[$this->getEntityKey($value)] = $value;
287
-        }
288
-
289
-        return $dictionary;
290
-    }
291
-
292
-    /**
293
-     * @throws MappingException
294
-     * @return array
295
-     */
296
-    public function getEntityKeys()
297
-    {
298
-        return array_keys($this->getDictionary());
299
-    }
300
-
301
-    /**
302
-     * @param $entity
303
-     * @throws MappingException
304
-     * @return mixed
305
-     */
306
-    protected function getEntityKey($entity)
307
-    {
308
-        $keyName = Manager::getMapper($entity)->getEntityMap()->getKeyName();
175
+			$keyName = $mapper->getEntityMap()->getKeyName();
176
+
177
+			if (in_array($class . '.' . $item->$keyName, $hashes)) {
178
+				$subset[] = $item;
179
+			}
180
+		}
181
+
182
+		return $subset;
183
+	}
184
+
185
+	/**
186
+	 * Merge the collection with the given items.
187
+	 *
188
+	 * @param  array $items
189
+	 * @throws MappingException
190
+	 * @return self
191
+	 */
192
+	public function merge($items)
193
+	{
194
+		$dictionary = $this->getDictionary();
195
+
196
+		foreach ($items as $item) {
197
+			$dictionary[$this->getEntityKey($item)] = $item;
198
+		}
199
+
200
+		return new static(array_values($dictionary));
201
+	}
202
+
203
+	/**
204
+	 * Diff the collection with the given items.
205
+	 *
206
+	 * @param  \ArrayAccess|array $items
207
+	 * @return self
208
+	 */
209
+	public function diff($items)
210
+	{
211
+		$diff = new static;
212
+
213
+		$dictionary = $this->getDictionary($items);
214
+
215
+		foreach ($this->items as $item) {
216
+			if (!isset($dictionary[$this->getEntityKey($item)])) {
217
+				$diff->add($item);
218
+			}
219
+		}
220
+
221
+		return $diff;
222
+	}
223
+
224
+	/**
225
+	 * Intersect the collection with the given items.
226
+	 *
227
+	 * @param  \ArrayAccess|array $items
228
+	 * @throws MappingException
229
+	 * @return self
230
+	 */
231
+	public function intersect($items)
232
+	{
233
+		$intersect = new static;
234
+
235
+		$dictionary = $this->getDictionary($items);
236
+
237
+		foreach ($this->items as $item) {
238
+			if (isset($dictionary[$this->getEntityKey($item)])) {
239
+				$intersect->add($item);
240
+			}
241
+		}
242
+
243
+		return $intersect;
244
+	}
245
+
246
+	/**
247
+	 * Returns only the models from the collection with the specified keys.
248
+	 *
249
+	 * @param  mixed $keys
250
+	 * @return self
251
+	 */
252
+	public function only($keys)
253
+	{
254
+		$dictionary = array_only($this->getDictionary(), $keys);
255
+
256
+		return new static(array_values($dictionary));
257
+	}
258
+
259
+	/**
260
+	 * Returns all models in the collection except the models with specified keys.
261
+	 *
262
+	 * @param  mixed $keys
263
+	 * @return self
264
+	 */
265
+	public function except($keys)
266
+	{
267
+		$dictionary = array_except($this->getDictionary(), $keys);
268
+
269
+		return new static(array_values($dictionary));
270
+	}
271
+
272
+	/**
273
+	 * Get a dictionary keyed by primary keys.
274
+	 *
275
+	 * @param  \ArrayAccess|array $items
276
+	 * @throws MappingException
277
+	 * @return array
278
+	 */
279
+	public function getDictionary($items = null)
280
+	{
281
+		$items = is_null($items) ? $this->items : $items;
282
+
283
+		$dictionary = [];
284
+
285
+		foreach ($items as $value) {
286
+			$dictionary[$this->getEntityKey($value)] = $value;
287
+		}
288
+
289
+		return $dictionary;
290
+	}
291
+
292
+	/**
293
+	 * @throws MappingException
294
+	 * @return array
295
+	 */
296
+	public function getEntityKeys()
297
+	{
298
+		return array_keys($this->getDictionary());
299
+	}
300
+
301
+	/**
302
+	 * @param $entity
303
+	 * @throws MappingException
304
+	 * @return mixed
305
+	 */
306
+	protected function getEntityKey($entity)
307
+	{
308
+		$keyName = Manager::getMapper($entity)->getEntityMap()->getKeyName();
309 309
         
310
-        $wrapper = $this->factory->make($entity);
311
-
312
-        return $wrapper->getEntityAttribute($keyName);
313
-    }
314
-
315
-    /**
316
-     * Get the max value of a given key.
317
-     *
318
-     * @param  string|null $key
319
-     * @throws MappingException
320
-     * @return mixed
321
-     */
322
-    public function max($key = null)
323
-    {
324
-        return $this->reduce(function($result, $item) use ($key) {
325
-            $wrapper = $this->factory->make($item);
326
-
327
-            return (is_null($result) || $wrapper->getEntityAttribute($key) > $result) ?
328
-                $wrapper->getEntityAttribute($key) : $result;
329
-        });
330
-    }
331
-
332
-    /**
333
-     * Get the min value of a given key.
334
-     *
335
-     * @param  string|null $key
336
-     * @throws MappingException
337
-     * @return mixed
338
-     */
339
-    public function min($key = null)
340
-    {
341
-        return $this->reduce(function($result, $item) use ($key) {
342
-            $wrapper = $this->factory->make($item);
343
-
344
-            return (is_null($result) || $wrapper->getEntityAttribute($key) < $result)
345
-                ? $wrapper->getEntityAttribute($key) : $result;
346
-        });
347
-    }
348
-
349
-    /**
350
-     * Get an array with the values of a given key.
351
-     *
352
-     * @param  string $value
353
-     * @param  string|null $key
354
-     * @return self
355
-     */
356
-    public function pluck($value, $key = null)
357
-    {
358
-        return new Collection(Arr::pluck($this->items, $value, $key));
359
-    }
360
-
361
-    /**
362
-     * Alias for the "pluck" method.
363
-     *
364
-     * @param  string $value
365
-     * @param  string|null $key
366
-     * @return self
367
-     */
368
-    public function lists($value, $key = null)
369
-    {
370
-        return $this->pluck($value, $key);
371
-    }
372
-
373
-    /**
374
-     * Return only unique items from the collection.
375
-     *
376
-     * @param  string|null $key
377
-     * @throws MappingException
378
-     * @return self
379
-     */
380
-    public function unique($key = null)
381
-    {
382
-        $dictionary = $this->getDictionary();
383
-
384
-        return new static(array_values($dictionary));
385
-    }
386
-
387
-    /**
388
-     * Get a base Support collection instance from this collection.
389
-     *
390
-     * @return \Illuminate\Support\Collection
391
-     */
392
-    public function toBase()
393
-    {
394
-        return new Collection($this->items);
395
-    }
310
+		$wrapper = $this->factory->make($entity);
311
+
312
+		return $wrapper->getEntityAttribute($keyName);
313
+	}
314
+
315
+	/**
316
+	 * Get the max value of a given key.
317
+	 *
318
+	 * @param  string|null $key
319
+	 * @throws MappingException
320
+	 * @return mixed
321
+	 */
322
+	public function max($key = null)
323
+	{
324
+		return $this->reduce(function($result, $item) use ($key) {
325
+			$wrapper = $this->factory->make($item);
326
+
327
+			return (is_null($result) || $wrapper->getEntityAttribute($key) > $result) ?
328
+				$wrapper->getEntityAttribute($key) : $result;
329
+		});
330
+	}
331
+
332
+	/**
333
+	 * Get the min value of a given key.
334
+	 *
335
+	 * @param  string|null $key
336
+	 * @throws MappingException
337
+	 * @return mixed
338
+	 */
339
+	public function min($key = null)
340
+	{
341
+		return $this->reduce(function($result, $item) use ($key) {
342
+			$wrapper = $this->factory->make($item);
343
+
344
+			return (is_null($result) || $wrapper->getEntityAttribute($key) < $result)
345
+				? $wrapper->getEntityAttribute($key) : $result;
346
+		});
347
+	}
348
+
349
+	/**
350
+	 * Get an array with the values of a given key.
351
+	 *
352
+	 * @param  string $value
353
+	 * @param  string|null $key
354
+	 * @return self
355
+	 */
356
+	public function pluck($value, $key = null)
357
+	{
358
+		return new Collection(Arr::pluck($this->items, $value, $key));
359
+	}
360
+
361
+	/**
362
+	 * Alias for the "pluck" method.
363
+	 *
364
+	 * @param  string $value
365
+	 * @param  string|null $key
366
+	 * @return self
367
+	 */
368
+	public function lists($value, $key = null)
369
+	{
370
+		return $this->pluck($value, $key);
371
+	}
372
+
373
+	/**
374
+	 * Return only unique items from the collection.
375
+	 *
376
+	 * @param  string|null $key
377
+	 * @throws MappingException
378
+	 * @return self
379
+	 */
380
+	public function unique($key = null)
381
+	{
382
+		$dictionary = $this->getDictionary();
383
+
384
+		return new static(array_values($dictionary));
385
+	}
386
+
387
+	/**
388
+	 * Get a base Support collection instance from this collection.
389
+	 *
390
+	 * @return \Illuminate\Support\Collection
391
+	 */
392
+	public function toBase()
393
+	{
394
+		return new Collection($this->items);
395
+	}
396 396
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -42,7 +42,7 @@  discard block
 block discarded – undo
42 42
             $key = $this->getEntityKey($key);
43 43
         }
44 44
 
45
-        return array_first($this->items, function ($itemKey, $entity) use ($key) {
45
+        return array_first($this->items, function($itemKey, $entity) use ($key) {
46 46
             return $this->getEntityKey($entity) == $key;
47 47
         }, $default);
48 48
     }
@@ -151,7 +151,7 @@  discard block
 block discarded – undo
151 151
             
152 152
             $keyName = $mapper->getEntityMap()->getKeyName();
153 153
             
154
-            return $class . '.' . $entity->getEntityAttribute($keyName);
154
+            return $class.'.'.$entity->getEntityAttribute($keyName);
155 155
         },
156 156
         $this->items);
157 157
     }
@@ -174,7 +174,7 @@  discard block
 block discarded – undo
174 174
             
175 175
             $keyName = $mapper->getEntityMap()->getKeyName();
176 176
 
177
-            if (in_array($class . '.' . $item->$keyName, $hashes)) {
177
+            if (in_array($class.'.'.$item->$keyName, $hashes)) {
178 178
                 $subset[] = $item;
179 179
             }
180 180
         }
Please login to merge, or discard this patch.
src/Relationships/HasOne.php 1 patch
Indentation   +54 added lines, -54 removed lines patch added patch discarded remove patch
@@ -6,58 +6,58 @@
 block discarded – undo
6 6
 
7 7
 class HasOne extends HasOneOrMany
8 8
 {
9
-    /**
10
-     * Get the results of the relationship.
11
-     *
12
-     * @param $relation
13
-     * @return mixed
14
-     */
15
-    public function getResults($relation)
16
-    {
17
-        $result = $this->query->first();
18
-
19
-        $this->cacheRelation($result, $relation);
20
-
21
-        return $result;
22
-    }
23
-
24
-    /**
25
-     * Get the results of the relationship.
26
-     *
27
-     * @return mixed
28
-     */
29
-    public function fetch()
30
-    {
31
-        return $this->query->first();
32
-    }
33
-
34
-
35
-    /**
36
-     * Initialize the relation on a set of entities.
37
-     *
38
-     * @param  \Analogue\ORM\Entity[] $entities
39
-     * @param  string $relation
40
-     * @return array
41
-     */
42
-    public function initRelation(array $entities, $relation)
43
-    {
44
-        foreach ($entities as $entity) {
45
-            $entity->setEntityAttribute($relation, null);
46
-        }
47
-
48
-        return $entities;
49
-    }
50
-
51
-    /**
52
-     * Match the eagerly loaded results to their parents.
53
-     *
54
-     * @param  \Analogue\ORM\Entity[] $entities
55
-     * @param  EntityCollection       $results
56
-     * @param  string                 $relation
57
-     * @return array
58
-     */
59
-    public function match(array $entities, EntityCollection $results, $relation)
60
-    {
61
-        return $this->matchOne($entities, $results, $relation);
62
-    }
9
+	/**
10
+	 * Get the results of the relationship.
11
+	 *
12
+	 * @param $relation
13
+	 * @return mixed
14
+	 */
15
+	public function getResults($relation)
16
+	{
17
+		$result = $this->query->first();
18
+
19
+		$this->cacheRelation($result, $relation);
20
+
21
+		return $result;
22
+	}
23
+
24
+	/**
25
+	 * Get the results of the relationship.
26
+	 *
27
+	 * @return mixed
28
+	 */
29
+	public function fetch()
30
+	{
31
+		return $this->query->first();
32
+	}
33
+
34
+
35
+	/**
36
+	 * Initialize the relation on a set of entities.
37
+	 *
38
+	 * @param  \Analogue\ORM\Entity[] $entities
39
+	 * @param  string $relation
40
+	 * @return array
41
+	 */
42
+	public function initRelation(array $entities, $relation)
43
+	{
44
+		foreach ($entities as $entity) {
45
+			$entity->setEntityAttribute($relation, null);
46
+		}
47
+
48
+		return $entities;
49
+	}
50
+
51
+	/**
52
+	 * Match the eagerly loaded results to their parents.
53
+	 *
54
+	 * @param  \Analogue\ORM\Entity[] $entities
55
+	 * @param  EntityCollection       $results
56
+	 * @param  string                 $relation
57
+	 * @return array
58
+	 */
59
+	public function match(array $entities, EntityCollection $results, $relation)
60
+	{
61
+		return $this->matchOne($entities, $results, $relation);
62
+	}
63 63
 }
Please login to merge, or discard this patch.
src/Relationships/MorphPivot.php 1 patch
Indentation   +49 added lines, -49 removed lines patch added patch discarded remove patch
@@ -6,60 +6,60 @@
 block discarded – undo
6 6
 
7 7
 class MorphPivot extends Pivot
8 8
 {
9
-    /**
10
-     * The type of the polymorphic relation.
11
-     *
12
-     * Explicitly define this so it's not included in saved attributes.
13
-     *
14
-     * @var string
15
-     */
16
-    protected $morphType;
9
+	/**
10
+	 * The type of the polymorphic relation.
11
+	 *
12
+	 * Explicitly define this so it's not included in saved attributes.
13
+	 *
14
+	 * @var string
15
+	 */
16
+	protected $morphType;
17 17
 
18
-    /**
19
-     * The value of the polymorphic relation.
20
-     *
21
-     * Explicitly define this so it's not included in saved attributes.
22
-     *
23
-     * @var string
24
-     */
25
-    protected $morphClass;
18
+	/**
19
+	 * The value of the polymorphic relation.
20
+	 *
21
+	 * Explicitly define this so it's not included in saved attributes.
22
+	 *
23
+	 * @var string
24
+	 */
25
+	protected $morphClass;
26 26
 
27
-    /**
28
-     * Set the keys for a save update query.
29
-     *
30
-     * @param  Query $query
31
-     * @return Query
32
-     */
33
-    protected function setKeysForSaveQuery(Query $query)
34
-    {
35
-        $query->where($this->morphType, $this->morphClass);
27
+	/**
28
+	 * Set the keys for a save update query.
29
+	 *
30
+	 * @param  Query $query
31
+	 * @return Query
32
+	 */
33
+	protected function setKeysForSaveQuery(Query $query)
34
+	{
35
+		$query->where($this->morphType, $this->morphClass);
36 36
 
37
-        return parent::setKeysForSaveQuery($query);
38
-    }
37
+		return parent::setKeysForSaveQuery($query);
38
+	}
39 39
 
40
-    /**
41
-     * Set the morph type for the pivot.
42
-     *
43
-     * @param  string $morphType
44
-     * @return self
45
-     */
46
-    public function setMorphType($morphType)
47
-    {
48
-        $this->morphType = $morphType;
40
+	/**
41
+	 * Set the morph type for the pivot.
42
+	 *
43
+	 * @param  string $morphType
44
+	 * @return self
45
+	 */
46
+	public function setMorphType($morphType)
47
+	{
48
+		$this->morphType = $morphType;
49 49
 
50
-        return $this;
51
-    }
50
+		return $this;
51
+	}
52 52
 
53
-    /**
54
-     * Set the morph class for the pivot.
55
-     *
56
-     * @param  string $morphClass
57
-     * @return self
58
-     */
59
-    public function setMorphClass($morphClass)
60
-    {
61
-        $this->morphClass = $morphClass;
53
+	/**
54
+	 * Set the morph class for the pivot.
55
+	 *
56
+	 * @param  string $morphClass
57
+	 * @return self
58
+	 */
59
+	public function setMorphClass($morphClass)
60
+	{
61
+		$this->morphClass = $morphClass;
62 62
 
63
-        return $this;
64
-    }
63
+		return $this;
64
+	}
65 65
 }
Please login to merge, or discard this patch.
src/Relationships/HasMany.php 1 patch
Indentation   +46 added lines, -46 removed lines patch added patch discarded remove patch
@@ -6,50 +6,50 @@
 block discarded – undo
6 6
 
7 7
 class HasMany extends HasOneOrMany
8 8
 {
9
-    /**
10
-     * Get the results of the relationship.
11
-     *
12
-     * @param  $relation
13
-     *
14
-     * @return mixed
15
-     */
16
-    public function getResults($relation)
17
-    {
18
-        $results = $this->query->get();
19
-
20
-        $this->cacheRelation($results, $relation);
21
-
22
-        return $results;
23
-    }
24
-
25
-    /**
26
-     * Initialize the relation on a set of entities.
27
-     *
28
-     * @param  array  $entities
29
-     * @param  string $relation
30
-     * @return array
31
-     */
32
-    public function initRelation(array $entities, $relation)
33
-    {
34
-        foreach ($entities as $entity) {
35
-            $entityWrapper = $this->factory->make($entity);
36
-
37
-            $entityWrapper->setEntityAttribute($relation, $this->relatedMap->newCollection());
38
-        }
39
-
40
-        return $entities;
41
-    }
42
-
43
-    /**
44
-     * Match the eagerly loaded results to their parents.
45
-     *
46
-     * @param  array             $entities
47
-     * @param  EntityCollection  $results
48
-     * @param  string            $relation
49
-     * @return array
50
-     */
51
-    public function match(array $entities, EntityCollection $results, $relation)
52
-    {
53
-        return $this->matchMany($entities, $results, $relation);
54
-    }
9
+	/**
10
+	 * Get the results of the relationship.
11
+	 *
12
+	 * @param  $relation
13
+	 *
14
+	 * @return mixed
15
+	 */
16
+	public function getResults($relation)
17
+	{
18
+		$results = $this->query->get();
19
+
20
+		$this->cacheRelation($results, $relation);
21
+
22
+		return $results;
23
+	}
24
+
25
+	/**
26
+	 * Initialize the relation on a set of entities.
27
+	 *
28
+	 * @param  array  $entities
29
+	 * @param  string $relation
30
+	 * @return array
31
+	 */
32
+	public function initRelation(array $entities, $relation)
33
+	{
34
+		foreach ($entities as $entity) {
35
+			$entityWrapper = $this->factory->make($entity);
36
+
37
+			$entityWrapper->setEntityAttribute($relation, $this->relatedMap->newCollection());
38
+		}
39
+
40
+		return $entities;
41
+	}
42
+
43
+	/**
44
+	 * Match the eagerly loaded results to their parents.
45
+	 *
46
+	 * @param  array             $entities
47
+	 * @param  EntityCollection  $results
48
+	 * @param  string            $relation
49
+	 * @return array
50
+	 */
51
+	public function match(array $entities, EntityCollection $results, $relation)
52
+	{
53
+		return $this->matchMany($entities, $results, $relation);
54
+	}
55 55
 }
Please login to merge, or discard this patch.
src/Relationships/MorphTo.php 1 patch
Indentation   +207 added lines, -207 removed lines patch added patch discarded remove patch
@@ -8,211 +8,211 @@
 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
-        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
-     * @throws \Analogue\ORM\Exceptions\MappingException
140
-     * @return EntityCollection
141
-     */
142
-    protected function getResultsByType($type)
143
-    {
144
-        $mapper = $this->relatedMapper->getManager()->mapper($type);
145
-
146
-        $key = $mapper->getEntityMap()->getKeyName();
147
-
148
-        $query = $mapper->getQuery();
149
-
150
-        return $query->whereIn($key, $this->gatherKeysByType($type)->all())->get();
151
-    }
152
-
153
-    /**
154
-     * Gather all of the foreign keys for a given type.
155
-     *
156
-     * @param  string $type
157
-     * @return BaseCollection
158
-     */
159
-    protected function gatherKeysByType($type)
160
-    {
161
-        $foreign = $this->foreignKey;
162
-
163
-        return BaseCollection::make($this->dictionary[$type])->map(function ($entities) use ($foreign) {
164
-            return head($entities)->{$foreign};
165
-
166
-        })->unique();
167
-    }
168
-
169
-    /**
170
-     * Associate the model instance to the given parent.
171
-     *
172
-     * @param  mixed $entity
173
-     * @return void
174
-     */
175
-    public function associate($entity)
176
-    {
177
-        // The Mapper will retrieve this association within the object model, we won't be using
178
-        // the foreign key attribute inside the parent Entity.
179
-        //
180
-        //$this->parent->setEntityAttribute($this->foreignKey, $entity->getEntityAttribute($this->otherKey));
181
-        //
182
-        // Instead, we'll just add the object to the Entity's attribute
183
-
184
-        $this->parent->setEntityAttribute($this->relation, $entity);
185
-    }
186
-
187
-    /**
188
-     * Get the foreign key value pair for a related object
189
-     *
190
-     * @var mixed $related
191
-     *
192
-     * @return array
193
-     */
194
-    public function getForeignKeyValuePair($related)
195
-    {
196
-        $foreignKey = $this->getForeignKey();
197
-
198
-        if ($related) {
199
-            $wrapper = $this->factory->make($related);
200
-
201
-            $relatedKey = $this->relatedMap->getKeyName();
202
-
203
-            return [$foreignKey => $wrapper->getEntityAttribute($relatedKey)];
204
-        } else {
205
-            return [$foreignKey => null];
206
-        }
207
-    }
208
-
209
-    /**
210
-     * Get the dictionary used by the relationship.
211
-     *
212
-     * @return array
213
-     */
214
-    public function getDictionary()
215
-    {
216
-        return $this->dictionary;
217
-    }
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
+		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
+	 * @throws \Analogue\ORM\Exceptions\MappingException
140
+	 * @return EntityCollection
141
+	 */
142
+	protected function getResultsByType($type)
143
+	{
144
+		$mapper = $this->relatedMapper->getManager()->mapper($type);
145
+
146
+		$key = $mapper->getEntityMap()->getKeyName();
147
+
148
+		$query = $mapper->getQuery();
149
+
150
+		return $query->whereIn($key, $this->gatherKeysByType($type)->all())->get();
151
+	}
152
+
153
+	/**
154
+	 * Gather all of the foreign keys for a given type.
155
+	 *
156
+	 * @param  string $type
157
+	 * @return BaseCollection
158
+	 */
159
+	protected function gatherKeysByType($type)
160
+	{
161
+		$foreign = $this->foreignKey;
162
+
163
+		return BaseCollection::make($this->dictionary[$type])->map(function ($entities) use ($foreign) {
164
+			return head($entities)->{$foreign};
165
+
166
+		})->unique();
167
+	}
168
+
169
+	/**
170
+	 * Associate the model instance to the given parent.
171
+	 *
172
+	 * @param  mixed $entity
173
+	 * @return void
174
+	 */
175
+	public function associate($entity)
176
+	{
177
+		// The Mapper will retrieve this association within the object model, we won't be using
178
+		// the foreign key attribute inside the parent Entity.
179
+		//
180
+		//$this->parent->setEntityAttribute($this->foreignKey, $entity->getEntityAttribute($this->otherKey));
181
+		//
182
+		// Instead, we'll just add the object to the Entity's attribute
183
+
184
+		$this->parent->setEntityAttribute($this->relation, $entity);
185
+	}
186
+
187
+	/**
188
+	 * Get the foreign key value pair for a related object
189
+	 *
190
+	 * @var mixed $related
191
+	 *
192
+	 * @return array
193
+	 */
194
+	public function getForeignKeyValuePair($related)
195
+	{
196
+		$foreignKey = $this->getForeignKey();
197
+
198
+		if ($related) {
199
+			$wrapper = $this->factory->make($related);
200
+
201
+			$relatedKey = $this->relatedMap->getKeyName();
202
+
203
+			return [$foreignKey => $wrapper->getEntityAttribute($relatedKey)];
204
+		} else {
205
+			return [$foreignKey => null];
206
+		}
207
+	}
208
+
209
+	/**
210
+	 * Get the dictionary used by the relationship.
211
+	 *
212
+	 * @return array
213
+	 */
214
+	public function getDictionary()
215
+	{
216
+		return $this->dictionary;
217
+	}
218 218
 }
Please login to merge, or discard this patch.
src/Relationships/MorphOne.php 1 patch
Indentation   +45 added lines, -45 removed lines patch added patch discarded remove patch
@@ -6,49 +6,49 @@
 block discarded – undo
6 6
 
7 7
 class MorphOne extends MorphOneOrMany
8 8
 {
9
-    /**
10
-     * Get the results of the relationship.
11
-     *
12
-     * @param  $relation
13
-     * @return mixed
14
-     */
15
-    public function getResults($relation)
16
-    {
17
-        $result = $this->query->first();
18
-
19
-        $this->cacheRelation($result, $relation);
20
-
21
-        return $result;
22
-    }
23
-
24
-    /**
25
-     * Initialize the relation on a set of models.
26
-     *
27
-     * @param  array  $entities
28
-     * @param  string $relation
29
-     * @return array
30
-     */
31
-    public function initRelation(array $entities, $relation)
32
-    {
33
-        foreach ($entities as $entity) {
34
-            $entity = $this->factory->make($entity);
35
-
36
-            $entity->setEntityAttribute($relation, null);
37
-        }
38
-
39
-        return $entities;
40
-    }
41
-
42
-    /**
43
-     * Match the eagerly loaded results to their parents.
44
-     *
45
-     * @param  array            $entities
46
-     * @param  EntityCollection $results
47
-     * @param  string           $relation
48
-     * @return array
49
-     */
50
-    public function match(array $entities, EntityCollection $results, $relation)
51
-    {
52
-        return $this->matchOne($entities, $results, $relation);
53
-    }
9
+	/**
10
+	 * Get the results of the relationship.
11
+	 *
12
+	 * @param  $relation
13
+	 * @return mixed
14
+	 */
15
+	public function getResults($relation)
16
+	{
17
+		$result = $this->query->first();
18
+
19
+		$this->cacheRelation($result, $relation);
20
+
21
+		return $result;
22
+	}
23
+
24
+	/**
25
+	 * Initialize the relation on a set of models.
26
+	 *
27
+	 * @param  array  $entities
28
+	 * @param  string $relation
29
+	 * @return array
30
+	 */
31
+	public function initRelation(array $entities, $relation)
32
+	{
33
+		foreach ($entities as $entity) {
34
+			$entity = $this->factory->make($entity);
35
+
36
+			$entity->setEntityAttribute($relation, null);
37
+		}
38
+
39
+		return $entities;
40
+	}
41
+
42
+	/**
43
+	 * Match the eagerly loaded results to their parents.
44
+	 *
45
+	 * @param  array            $entities
46
+	 * @param  EntityCollection $results
47
+	 * @param  string           $relation
48
+	 * @return array
49
+	 */
50
+	public function match(array $entities, EntityCollection $results, $relation)
51
+	{
52
+		return $this->matchOne($entities, $results, $relation);
53
+	}
54 54
 }
Please login to merge, or discard this patch.