Completed
Pull Request — 5.1 (#84)
by Maxim
08:05
created
src/System/Wrappers/Wrapper.php 1 patch
Indentation   +103 added lines, -103 removed lines patch added patch discarded remove patch
@@ -11,107 +11,107 @@
 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
-     * Set the lazyloading proxies on the wrapped entity objet
61
-     *
62
-     * @return void
63
-     */
64
-    public function setProxies()
65
-    {
66
-        $attributes = $this->getEntityAttributes();
67
-        $singleRelations = $this->entityMap->getSingleRelationships();
68
-        $manyRelations = $this->entityMap->getManyRelationships();
69
-
70
-        $proxies = [];
71
-
72
-        foreach ($this->entityMap->getRelationships() as $relation) {
73
-            if (!array_key_exists($relation, $attributes) || is_null($attributes[$relation])) {
74
-                if (in_array($relation, $singleRelations)) {
75
-                    $proxies[$relation] = new EntityProxy($this->getObject(), $relation);
76
-                }
77
-                if (in_array($relation, $manyRelations)) {
78
-                    $proxies[$relation] = new CollectionProxy($this->getObject(), $relation);
79
-                }
80
-            }
81
-        }
82
-
83
-        foreach ($proxies as $key => $value) {
84
-            $this->setEntityAttribute($key, $value);
85
-        }
86
-    }
87
-
88
-    /**
89
-     * @param string $key
90
-     * @param string $value
91
-     * @return mixed
92
-     */
93
-    abstract public function setEntityAttribute($key, $value);
94
-
95
-    /**
96
-     * @param string $key
97
-     * @return mixed
98
-     */
99
-    abstract public function getEntityAttribute($key);
100
-
101
-    /**
102
-     * @param array $attributes
103
-     * @return mixed
104
-     */
105
-    abstract public function setEntityAttributes(array $attributes);
106
-
107
-    /**
108
-     * @return mixed
109
-     */
110
-    abstract public function getEntityAttributes();
111
-
112
-    /**
113
-     * @param string $key
114
-     * @return mixed
115
-     */
116
-    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
+	 * Set the lazyloading proxies on the wrapped entity objet
61
+	 *
62
+	 * @return void
63
+	 */
64
+	public function setProxies()
65
+	{
66
+		$attributes = $this->getEntityAttributes();
67
+		$singleRelations = $this->entityMap->getSingleRelationships();
68
+		$manyRelations = $this->entityMap->getManyRelationships();
69
+
70
+		$proxies = [];
71
+
72
+		foreach ($this->entityMap->getRelationships() as $relation) {
73
+			if (!array_key_exists($relation, $attributes) || is_null($attributes[$relation])) {
74
+				if (in_array($relation, $singleRelations)) {
75
+					$proxies[$relation] = new EntityProxy($this->getObject(), $relation);
76
+				}
77
+				if (in_array($relation, $manyRelations)) {
78
+					$proxies[$relation] = new CollectionProxy($this->getObject(), $relation);
79
+				}
80
+			}
81
+		}
82
+
83
+		foreach ($proxies as $key => $value) {
84
+			$this->setEntityAttribute($key, $value);
85
+		}
86
+	}
87
+
88
+	/**
89
+	 * @param string $key
90
+	 * @param string $value
91
+	 * @return mixed
92
+	 */
93
+	abstract public function setEntityAttribute($key, $value);
94
+
95
+	/**
96
+	 * @param string $key
97
+	 * @return mixed
98
+	 */
99
+	abstract public function getEntityAttribute($key);
100
+
101
+	/**
102
+	 * @param array $attributes
103
+	 * @return mixed
104
+	 */
105
+	abstract public function setEntityAttributes(array $attributes);
106
+
107
+	/**
108
+	 * @return mixed
109
+	 */
110
+	abstract public function getEntityAttributes();
111
+
112
+	/**
113
+	 * @param string $key
114
+	 * @return mixed
115
+	 */
116
+	abstract public function hasAttribute($key);
117 117
 }
Please login to merge, or discard this patch.
src/System/Wrappers/PlainObjectWrapper.php 2 patches
Indentation   +195 added lines, -195 removed lines patch added patch discarded remove patch
@@ -6,201 +6,201 @@
 block discarded – undo
6 6
 
7 7
 class PlainObjectWrapper extends Wrapper
8 8
 {
9
-    /**
10
-     * The list of attributes for the managed entity
11
-     *
12
-     * @var array
13
-     */
14
-    protected $attributeList;
15
-
16
-    /**
17
-     * The reflection class for the managed entity
18
-     *
19
-     * @var ReflectionClass
20
-     */
21
-    protected $reflection;
22
-
23
-    /**
24
-     * PlainObjectWrapper constructor.
25
-     * @param $popoEntity
26
-     * @param $entityMap
27
-     */
28
-    public function __construct($popoEntity, $entityMap)
29
-    {
30
-        $this->reflection = new ReflectionClass($popoEntity);
31
-
32
-        $this->attributeList = $this->getAttributeList();
33
-
34
-        parent::__construct($popoEntity, $entityMap);
35
-    }
36
-
37
-    /**
38
-     * Get Compiled Attributes (key, attributes, embed, relations)
39
-     *
40
-     * @return array
41
-     */
42
-    protected function getAttributeList()
43
-    {
44
-        return  $this->entityMap->getCompiledAttributes();
45
-    }
46
-
47
-    /**
48
-     * Extract Attributes from a Plain Php Object
49
-     *
50
-     * @return array $attributes
51
-     */
52
-    protected function extract()
53
-    {
54
-        $properties = $this->getMappedProperties();
55
-
56
-        $attributes = [];
57
-
58
-        foreach ($properties as $property) {
59
-            $name = $property->getName();
60
-
61
-            if ($property->isPublic()) {
62
-                $attributes[$name] = $this->entity->$name;
63
-            } else {
64
-                $property->setAccessible(true);
9
+	/**
10
+	 * The list of attributes for the managed entity
11
+	 *
12
+	 * @var array
13
+	 */
14
+	protected $attributeList;
15
+
16
+	/**
17
+	 * The reflection class for the managed entity
18
+	 *
19
+	 * @var ReflectionClass
20
+	 */
21
+	protected $reflection;
22
+
23
+	/**
24
+	 * PlainObjectWrapper constructor.
25
+	 * @param $popoEntity
26
+	 * @param $entityMap
27
+	 */
28
+	public function __construct($popoEntity, $entityMap)
29
+	{
30
+		$this->reflection = new ReflectionClass($popoEntity);
31
+
32
+		$this->attributeList = $this->getAttributeList();
33
+
34
+		parent::__construct($popoEntity, $entityMap);
35
+	}
36
+
37
+	/**
38
+	 * Get Compiled Attributes (key, attributes, embed, relations)
39
+	 *
40
+	 * @return array
41
+	 */
42
+	protected function getAttributeList()
43
+	{
44
+		return  $this->entityMap->getCompiledAttributes();
45
+	}
46
+
47
+	/**
48
+	 * Extract Attributes from a Plain Php Object
49
+	 *
50
+	 * @return array $attributes
51
+	 */
52
+	protected function extract()
53
+	{
54
+		$properties = $this->getMappedProperties();
55
+
56
+		$attributes = [];
57
+
58
+		foreach ($properties as $property) {
59
+			$name = $property->getName();
60
+
61
+			if ($property->isPublic()) {
62
+				$attributes[$name] = $this->entity->$name;
63
+			} else {
64
+				$property->setAccessible(true);
65 65
     
66
-                $attributes[$name] = $property->getValue($this->entity);
67
-            }
68
-        }
69
-
70
-        return $attributes;
71
-    }
72
-
73
-    /**
74
-     * @return \ReflectionProperty[]
75
-     */
76
-    protected function getMappedProperties()
77
-    {
78
-        $objectProperties = $this->reflection->getProperties();
79
-
80
-        $attributeList = $this->getAttributeList();
81
-
82
-        // We need to filter out properties that could belong to the object
83
-        // and which are not intended to be handled by the ORM
84
-        return array_filter($objectProperties, function (\ReflectionProperty $item) use ($attributeList) {
85
-            if (in_array($item->getName(), $attributeList)) {
86
-                return true;
87
-            }
88
-        });
89
-    }
90
-
91
-    /**
92
-     * @param  string $name
93
-     * @return \ReflectionProperty
94
-     */
95
-    protected function getMappedProperty($name)
96
-    {
97
-        return $this->reflection->getProperty($name);
98
-    }
99
-
100
-    /**
101
-     * Hydrate Plain PHP Object with wrapped attributes
102
-     *
103
-     * @param  $attributes
104
-     * @return void
105
-     */
106
-    protected function hydrate($attributes)
107
-    {
108
-        $properties = $this->getMappedProperties();
109
-
110
-        foreach ($properties as $property) {
111
-            $name = $property->getName();
112
-
113
-            if ($property->isPublic()) {
114
-                $this->entity->$name = $attributes[$name];
115
-            } else {
116
-                $property->setAccessible(true);
117
-
118
-                $property->setValue($this->entity, $attributes[$name]);
119
-            }
120
-        }
121
-    }
122
-
123
-    /**
124
-     * Method used by the mapper to set the object
125
-     * attribute raw values (hydration)
126
-     *
127
-     * @param array $attributes
128
-     *
129
-     * @return void
130
-     */
131
-    public function setEntityAttributes(array $attributes)
132
-    {
133
-        $this->hydrate($attributes);
134
-    }
135
-
136
-    /**
137
-     * Method used by the mapper to get the
138
-     * raw object's values.
139
-     *
140
-     * @return array
141
-     */
142
-    public function getEntityAttributes()
143
-    {
144
-        return $this->extract();
145
-    }
146
-
147
-    /**
148
-     * Method used by the mapper to set raw
149
-     * key-value pair
150
-     *
151
-     * @param string $key
152
-     * @param string $value
153
-     *
154
-     * @return void
155
-     */
156
-    public function setEntityAttribute($key, $value)
157
-    {
158
-        $property = $this->getMappedProperty($key);
159
-
160
-        if ($property->isPublic()) {
161
-            $this->entity->$key = $value;
162
-        } else {
163
-            $property->setAccessible(true);
66
+				$attributes[$name] = $property->getValue($this->entity);
67
+			}
68
+		}
69
+
70
+		return $attributes;
71
+	}
72
+
73
+	/**
74
+	 * @return \ReflectionProperty[]
75
+	 */
76
+	protected function getMappedProperties()
77
+	{
78
+		$objectProperties = $this->reflection->getProperties();
79
+
80
+		$attributeList = $this->getAttributeList();
81
+
82
+		// We need to filter out properties that could belong to the object
83
+		// and which are not intended to be handled by the ORM
84
+		return array_filter($objectProperties, function (\ReflectionProperty $item) use ($attributeList) {
85
+			if (in_array($item->getName(), $attributeList)) {
86
+				return true;
87
+			}
88
+		});
89
+	}
90
+
91
+	/**
92
+	 * @param  string $name
93
+	 * @return \ReflectionProperty
94
+	 */
95
+	protected function getMappedProperty($name)
96
+	{
97
+		return $this->reflection->getProperty($name);
98
+	}
99
+
100
+	/**
101
+	 * Hydrate Plain PHP Object with wrapped attributes
102
+	 *
103
+	 * @param  $attributes
104
+	 * @return void
105
+	 */
106
+	protected function hydrate($attributes)
107
+	{
108
+		$properties = $this->getMappedProperties();
109
+
110
+		foreach ($properties as $property) {
111
+			$name = $property->getName();
112
+
113
+			if ($property->isPublic()) {
114
+				$this->entity->$name = $attributes[$name];
115
+			} else {
116
+				$property->setAccessible(true);
117
+
118
+				$property->setValue($this->entity, $attributes[$name]);
119
+			}
120
+		}
121
+	}
122
+
123
+	/**
124
+	 * Method used by the mapper to set the object
125
+	 * attribute raw values (hydration)
126
+	 *
127
+	 * @param array $attributes
128
+	 *
129
+	 * @return void
130
+	 */
131
+	public function setEntityAttributes(array $attributes)
132
+	{
133
+		$this->hydrate($attributes);
134
+	}
135
+
136
+	/**
137
+	 * Method used by the mapper to get the
138
+	 * raw object's values.
139
+	 *
140
+	 * @return array
141
+	 */
142
+	public function getEntityAttributes()
143
+	{
144
+		return $this->extract();
145
+	}
146
+
147
+	/**
148
+	 * Method used by the mapper to set raw
149
+	 * key-value pair
150
+	 *
151
+	 * @param string $key
152
+	 * @param string $value
153
+	 *
154
+	 * @return void
155
+	 */
156
+	public function setEntityAttribute($key, $value)
157
+	{
158
+		$property = $this->getMappedProperty($key);
159
+
160
+		if ($property->isPublic()) {
161
+			$this->entity->$key = $value;
162
+		} else {
163
+			$property->setAccessible(true);
164 164
     
165
-            $property->setValue($this->entity, $value);
166
-        }
167
-
168
-        $this->attributes[$key] = $value;
169
-    }
170
-
171
-    /**
172
-     * Method used by the mapper to get single
173
-     * key-value pair
174
-     *
175
-     * @param  string $key
176
-     * @return mixed
177
-     */
178
-    public function getEntityAttribute($key)
179
-    {
180
-        $property = $this->getMappedProperty($key);
181
-
182
-        if ($property->isPublic()) {
183
-            $value = $this->entity->$key;
184
-        } else {
185
-            $property->setAccessible(true);
186
-            $value = $property->getValue($this->entity);
187
-        }
188
-
189
-        return $value;
190
-    }
191
-
192
-        /**
193
-         * Test if a given attribute exists
194
-         *
195
-         * @param  string  $key
196
-         * @return boolean
197
-         */
198
-    public function hasAttribute($key)
199
-    {
200
-        if (array_key_exists($key, $$this->attributeList)) {
201
-            return true;
202
-        } else {
203
-            return false;
204
-        }
205
-    }
165
+			$property->setValue($this->entity, $value);
166
+		}
167
+
168
+		$this->attributes[$key] = $value;
169
+	}
170
+
171
+	/**
172
+	 * Method used by the mapper to get single
173
+	 * key-value pair
174
+	 *
175
+	 * @param  string $key
176
+	 * @return mixed
177
+	 */
178
+	public function getEntityAttribute($key)
179
+	{
180
+		$property = $this->getMappedProperty($key);
181
+
182
+		if ($property->isPublic()) {
183
+			$value = $this->entity->$key;
184
+		} else {
185
+			$property->setAccessible(true);
186
+			$value = $property->getValue($this->entity);
187
+		}
188
+
189
+		return $value;
190
+	}
191
+
192
+		/**
193
+		 * Test if a given attribute exists
194
+		 *
195
+		 * @param  string  $key
196
+		 * @return boolean
197
+		 */
198
+	public function hasAttribute($key)
199
+	{
200
+		if (array_key_exists($key, $$this->attributeList)) {
201
+			return true;
202
+		} else {
203
+			return false;
204
+		}
205
+	}
206 206
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -81,7 +81,7 @@
 block discarded – undo
81 81
 
82 82
         // We need to filter out properties that could belong to the object
83 83
         // and which are not intended to be handled by the ORM
84
-        return array_filter($objectProperties, function (\ReflectionProperty $item) use ($attributeList) {
84
+        return array_filter($objectProperties, function(\ReflectionProperty $item) use ($attributeList) {
85 85
             if (in_array($item->getName(), $attributeList)) {
86 86
                 return true;
87 87
             }
Please login to merge, or discard this patch.
src/System/Proxies/EntityProxy.php 1 patch
Indentation   +68 added lines, -68 removed lines patch added patch discarded remove patch
@@ -4,81 +4,81 @@
 block discarded – undo
4 4
 
5 5
 class EntityProxy extends Proxy
6 6
 {
7
-    /**
8
-     * Underlying entity
9
-     *
10
-     * @var mixed
11
-     */
12
-    protected $entity;
7
+	/**
8
+	 * Underlying entity
9
+	 *
10
+	 * @var mixed
11
+	 */
12
+	protected $entity;
13 13
 
14
-    /**
15
-     * Load the underlying relation
16
-     *
17
-     * @return void
18
-     */
19
-    protected function loadOnce()
20
-    {
21
-        $this->entity = $this->load();
22
-    }
14
+	/**
15
+	 * Load the underlying relation
16
+	 *
17
+	 * @return void
18
+	 */
19
+	protected function loadOnce()
20
+	{
21
+		$this->entity = $this->load();
22
+	}
23 23
 
24
-    /**
25
-     * Return the actual Entity
26
-     *
27
-     * @return mixed
28
-     */
29
-    public function getUnderlyingObject()
30
-    {
31
-        if (!$this->isLoaded()) {
32
-            $this->loadOnce();
33
-        }
24
+	/**
25
+	 * Return the actual Entity
26
+	 *
27
+	 * @return mixed
28
+	 */
29
+	public function getUnderlyingObject()
30
+	{
31
+		if (!$this->isLoaded()) {
32
+			$this->loadOnce();
33
+		}
34 34
 
35
-        return $this->entity;
36
-    }
35
+		return $this->entity;
36
+	}
37 37
 
38
-    /**
39
-     * Transparently passes get operation to underlying entity
40
-     *
41
-     * @param  string $attribute
42
-     * @return mixed
43
-     */
44
-    public function __get($attribute)
45
-    {
46
-        if (!$this->isLoaded()) {
47
-            $this->loadOnce();
48
-        }
38
+	/**
39
+	 * Transparently passes get operation to underlying entity
40
+	 *
41
+	 * @param  string $attribute
42
+	 * @return mixed
43
+	 */
44
+	public function __get($attribute)
45
+	{
46
+		if (!$this->isLoaded()) {
47
+			$this->loadOnce();
48
+		}
49 49
 
50
-        return $this->entity->$attribute;
51
-    }
50
+		return $this->entity->$attribute;
51
+	}
52 52
 
53
-    /**
54
-     * Transparently passes set operation to underlying entity
55
-     *
56
-     * @param  string $attribute [description]
57
-     * @param  mixed
58
-     * @return void
59
-     */
60
-    public function __set($attribute, $value)
61
-    {
62
-        if (!$this->isLoaded()) {
63
-            $this->loadOnce();
64
-        }
53
+	/**
54
+	 * Transparently passes set operation to underlying entity
55
+	 *
56
+	 * @param  string $attribute [description]
57
+	 * @param  mixed
58
+	 * @return void
59
+	 */
60
+	public function __set($attribute, $value)
61
+	{
62
+		if (!$this->isLoaded()) {
63
+			$this->loadOnce();
64
+		}
65 65
 
66
-        $this->entity->$attribute = $value;
67
-    }
66
+		$this->entity->$attribute = $value;
67
+	}
68 68
 
69
-    /**
70
-     * Transparently Redirect non overrided calls to the lazy loaded Entity
71
-     *
72
-     * @param  string $method
73
-     * @param  array  $parameters
74
-     * @return mixed
75
-     */
76
-    public function __call($method, $parameters)
77
-    {
78
-        if (!$this->isLoaded()) {
79
-            $this->loadOnce();
80
-        }
69
+	/**
70
+	 * Transparently Redirect non overrided calls to the lazy loaded Entity
71
+	 *
72
+	 * @param  string $method
73
+	 * @param  array  $parameters
74
+	 * @return mixed
75
+	 */
76
+	public function __call($method, $parameters)
77
+	{
78
+		if (!$this->isLoaded()) {
79
+			$this->loadOnce();
80
+		}
81 81
 
82
-        return call_user_func_array([$this->loadedCollection, $method], $parameters);
83
-    }
82
+		return call_user_func_array([$this->loadedCollection, $method], $parameters);
83
+	}
84 84
 }
Please login to merge, or discard this patch.
src/System/Proxies/CollectionProxy.php 1 patch
Indentation   +210 added lines, -210 removed lines patch added patch discarded remove patch
@@ -17,218 +17,218 @@
 block discarded – undo
17 17
  */
18 18
 class CollectionProxy extends Proxy implements ArrayAccess, Arrayable, Countable, IteratorAggregate, Jsonable, JsonSerializable
19 19
 {
20
-    /**
21
-     * Underlying Lazyloaded collection
22
-     * @var EntityCollection
23
-     */
24
-    protected $loadedCollection;
25
-
26
-    /**
27
-     * Added Items Collection
28
-     * @var EntityCollection
29
-     */
30
-    protected $addedItems;
31
-
32
-    /**
33
-     * @param mixed  $parentEntity
34
-     * @param string $relation relationship method handled by the proxy.
35
-     */
36
-    public function __construct($parentEntity, $relation)
37
-    {
38
-        $this->addedItems = new EntityCollection;
39
-
40
-        parent::__construct($parentEntity, $relation);
41
-    }
42
-
43
-    /**
44
-     * Add an entity to the proxy collection, weither it's loaded or not
45
-     *
46
-     * @param mixed $entity
47
-     * @return self|void
48
-     */
49
-    public function add($entity)
50
-    {
51
-        if ($this->isLoaded()) {
52
-            return $this->loadedCollection->add($entity);
53
-        } else {
54
-            $this->addedItems->add($entity);
55
-        }
56
-    }
57
-
58
-    /**
59
-     * Check if Proxy collection has been lazy-loaded
60
-     *
61
-     * @return boolean
62
-     */
63
-    public function isLoaded()
64
-    {
65
-        return !is_null($this->loadedCollection);
66
-    }
67
-
68
-    /**
69
-     * Return the underlying collection
70
-     *
71
-     * @return EntityCollection
72
-     */
73
-    public function getUnderlyingCollection()
74
-    {
75
-        return $this->loadedCollection;
76
-    }
77
-
78
-    /**
79
-     * Return Items that has been added prior to lazy-loading
80
-     *
81
-     * @return EntityCollection
82
-     */
83
-    public function getAddedItems()
84
-    {
85
-        return $this->addedItems;
86
-    }
87
-
88
-    /**
89
-     * Load the underlying relation
90
-     *
91
-     * @return void
92
-     */
93
-    protected function loadOnce()
94
-    {
95
-        if ($this->isLoaded()) {
96
-            return;
97
-        }
20
+	/**
21
+	 * Underlying Lazyloaded collection
22
+	 * @var EntityCollection
23
+	 */
24
+	protected $loadedCollection;
25
+
26
+	/**
27
+	 * Added Items Collection
28
+	 * @var EntityCollection
29
+	 */
30
+	protected $addedItems;
31
+
32
+	/**
33
+	 * @param mixed  $parentEntity
34
+	 * @param string $relation relationship method handled by the proxy.
35
+	 */
36
+	public function __construct($parentEntity, $relation)
37
+	{
38
+		$this->addedItems = new EntityCollection;
39
+
40
+		parent::__construct($parentEntity, $relation);
41
+	}
42
+
43
+	/**
44
+	 * Add an entity to the proxy collection, weither it's loaded or not
45
+	 *
46
+	 * @param mixed $entity
47
+	 * @return self|void
48
+	 */
49
+	public function add($entity)
50
+	{
51
+		if ($this->isLoaded()) {
52
+			return $this->loadedCollection->add($entity);
53
+		} else {
54
+			$this->addedItems->add($entity);
55
+		}
56
+	}
57
+
58
+	/**
59
+	 * Check if Proxy collection has been lazy-loaded
60
+	 *
61
+	 * @return boolean
62
+	 */
63
+	public function isLoaded()
64
+	{
65
+		return !is_null($this->loadedCollection);
66
+	}
67
+
68
+	/**
69
+	 * Return the underlying collection
70
+	 *
71
+	 * @return EntityCollection
72
+	 */
73
+	public function getUnderlyingCollection()
74
+	{
75
+		return $this->loadedCollection;
76
+	}
77
+
78
+	/**
79
+	 * Return Items that has been added prior to lazy-loading
80
+	 *
81
+	 * @return EntityCollection
82
+	 */
83
+	public function getAddedItems()
84
+	{
85
+		return $this->addedItems;
86
+	}
87
+
88
+	/**
89
+	 * Load the underlying relation
90
+	 *
91
+	 * @return void
92
+	 */
93
+	protected function loadOnce()
94
+	{
95
+		if ($this->isLoaded()) {
96
+			return;
97
+		}
98 98
         
99
-        $this->loadedCollection = $this->load();
99
+		$this->loadedCollection = $this->load();
100 100
 
101
-        foreach ($this->addedItems as $entity) {
102
-            $this->loadedCollection->add($entity);
103
-        }
101
+		foreach ($this->addedItems as $entity) {
102
+			$this->loadedCollection->add($entity);
103
+		}
104 104
 
105
-        $this->addedItems = null;
106
-    }
105
+		$this->addedItems = null;
106
+	}
107 107
     
108
-    /**
109
-     * Count the number of items in the collection.
110
-     *
111
-     * @return int
112
-     */
113
-    public function count()
114
-    {
115
-        $this->loadOnce();
116
-
117
-        return $this->getUnderlyingCollection()->count();
118
-    }
119
-
120
-    /**
121
-     * Determine if an item exists at an offset.
122
-     *
123
-     * @param  mixed $key
124
-     * @return bool
125
-     */
126
-    public function offsetExists($key)
127
-    {
128
-        $this->loadOnce();
129
-
130
-        return $this->getUnderlyingCollection()->offsetExists($key);
131
-    }
132
-
133
-    /**
134
-     * Get an item at a given offset.
135
-     *
136
-     * @param  mixed $key
137
-     * @return mixed
138
-     */
139
-    public function offsetGet($key)
140
-    {
141
-        $this->loadOnce();
142
-
143
-        return $this->getUnderlyingCollection()->offsetGet($key);
144
-    }
145
-
146
-    /**
147
-     * Set the item at a given offset.
148
-     *
149
-     * @param mixed $key
150
-     * @param mixed $value
151
-     */
152
-    public function offsetSet($key, $value)
153
-    {
154
-        $this->loadOnce();
155
-
156
-        $this->getUnderlyingCollection()->offsetSet($key, $value);
157
-    }
158
-
159
-    /**
160
-     * Unset the item at a given offset.
161
-     *
162
-     * @param string $key
163
-     */
164
-    public function offsetUnset($key)
165
-    {
166
-        $this->loadOnce();
167
-
168
-        $this->getUnderlyingCollection()->offsetUnset($key);
169
-    }
170
-
171
-    /**
172
-     * Get the collection of items as a plain array.
173
-     *
174
-     * @return array
175
-     */
176
-    public function toArray()
177
-    {
178
-        $this->loadOnce();
179
-
180
-        return $this->getUnderlyingCollection()->toArray();
181
-    }
182
-
183
-    /**
184
-     * Convert the object into something JSON serializable.
185
-     *
186
-     * @return array
187
-     */
188
-    public function jsonSerialize()
189
-    {
190
-        $this->loadOnce();
191
-
192
-        return $this->getUnderlyingCollection()->jsonSerialize();
193
-    }
194
-
195
-    /**
196
-     * Get the collection of items as JSON.
197
-     *
198
-     * @param  int $options
199
-     * @return string
200
-     */
201
-    public function toJson($options = 0)
202
-    {
203
-        $this->loadOnce();
204
-
205
-        return $this->getUnderlyingCollection()->toJson();
206
-    }
207
-
208
-    /**
209
-     * Get an iterator for the items.
210
-     *
211
-     * @return \ArrayIterator
212
-     */
213
-    public function getIterator()
214
-    {
215
-        $this->loadOnce();
216
-
217
-        return $this->getUnderlyingCollection()->getIterator();
218
-    }
219
-
220
-
221
-    /**
222
-     * @param  $method
223
-     * @param  $parameters
224
-     * @return mixed
225
-     */
226
-    public function __call($method, $parameters)
227
-    {
228
-        if (!$this->isLoaded()) {
229
-            $this->loadOnce();
230
-        }
231
-
232
-        return call_user_func_array([$this->loadedCollection, $method], $parameters);
233
-    }
108
+	/**
109
+	 * Count the number of items in the collection.
110
+	 *
111
+	 * @return int
112
+	 */
113
+	public function count()
114
+	{
115
+		$this->loadOnce();
116
+
117
+		return $this->getUnderlyingCollection()->count();
118
+	}
119
+
120
+	/**
121
+	 * Determine if an item exists at an offset.
122
+	 *
123
+	 * @param  mixed $key
124
+	 * @return bool
125
+	 */
126
+	public function offsetExists($key)
127
+	{
128
+		$this->loadOnce();
129
+
130
+		return $this->getUnderlyingCollection()->offsetExists($key);
131
+	}
132
+
133
+	/**
134
+	 * Get an item at a given offset.
135
+	 *
136
+	 * @param  mixed $key
137
+	 * @return mixed
138
+	 */
139
+	public function offsetGet($key)
140
+	{
141
+		$this->loadOnce();
142
+
143
+		return $this->getUnderlyingCollection()->offsetGet($key);
144
+	}
145
+
146
+	/**
147
+	 * Set the item at a given offset.
148
+	 *
149
+	 * @param mixed $key
150
+	 * @param mixed $value
151
+	 */
152
+	public function offsetSet($key, $value)
153
+	{
154
+		$this->loadOnce();
155
+
156
+		$this->getUnderlyingCollection()->offsetSet($key, $value);
157
+	}
158
+
159
+	/**
160
+	 * Unset the item at a given offset.
161
+	 *
162
+	 * @param string $key
163
+	 */
164
+	public function offsetUnset($key)
165
+	{
166
+		$this->loadOnce();
167
+
168
+		$this->getUnderlyingCollection()->offsetUnset($key);
169
+	}
170
+
171
+	/**
172
+	 * Get the collection of items as a plain array.
173
+	 *
174
+	 * @return array
175
+	 */
176
+	public function toArray()
177
+	{
178
+		$this->loadOnce();
179
+
180
+		return $this->getUnderlyingCollection()->toArray();
181
+	}
182
+
183
+	/**
184
+	 * Convert the object into something JSON serializable.
185
+	 *
186
+	 * @return array
187
+	 */
188
+	public function jsonSerialize()
189
+	{
190
+		$this->loadOnce();
191
+
192
+		return $this->getUnderlyingCollection()->jsonSerialize();
193
+	}
194
+
195
+	/**
196
+	 * Get the collection of items as JSON.
197
+	 *
198
+	 * @param  int $options
199
+	 * @return string
200
+	 */
201
+	public function toJson($options = 0)
202
+	{
203
+		$this->loadOnce();
204
+
205
+		return $this->getUnderlyingCollection()->toJson();
206
+	}
207
+
208
+	/**
209
+	 * Get an iterator for the items.
210
+	 *
211
+	 * @return \ArrayIterator
212
+	 */
213
+	public function getIterator()
214
+	{
215
+		$this->loadOnce();
216
+
217
+		return $this->getUnderlyingCollection()->getIterator();
218
+	}
219
+
220
+
221
+	/**
222
+	 * @param  $method
223
+	 * @param  $parameters
224
+	 * @return mixed
225
+	 */
226
+	public function __call($method, $parameters)
227
+	{
228
+		if (!$this->isLoaded()) {
229
+			$this->loadOnce();
230
+		}
231
+
232
+		return call_user_func_array([$this->loadedCollection, $method], $parameters);
233
+	}
234 234
 }
Please login to merge, or discard this patch.
src/System/Proxies/ProxyInterface.php 1 patch
Indentation   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -5,17 +5,17 @@
 block discarded – undo
5 5
 
6 6
 interface ProxyInterface
7 7
 {
8
-    /**
9
-     * Convert a proxy into the underlying related Object
10
-     *
11
-     * @return Mappable|\Analogue\ORM\EntityCollection
12
-     */
13
-    public function load();
8
+	/**
9
+	 * Convert a proxy into the underlying related Object
10
+	 *
11
+	 * @return Mappable|\Analogue\ORM\EntityCollection
12
+	 */
13
+	public function load();
14 14
 
15
-    /**
16
-     * Return true if the underlying relation has been lazy loaded
17
-     *
18
-     * @return boolean
19
-     */
20
-    public function isLoaded();
15
+	/**
16
+	 * Return true if the underlying relation has been lazy loaded
17
+	 *
18
+	 * @return boolean
19
+	 */
20
+	public function isLoaded();
21 21
 }
Please login to merge, or discard this patch.
src/System/Proxies/Proxy.php 1 patch
Indentation   +72 added lines, -72 removed lines patch added patch discarded remove patch
@@ -7,87 +7,87 @@
 block discarded – undo
7 7
 
8 8
 abstract class Proxy implements ProxyInterface
9 9
 {
10
-    /**
11
-     * The name of the relationship method handled by the proxy.
12
-     *
13
-     * @var string
14
-     */
15
-    protected $relation;
10
+	/**
11
+	 * The name of the relationship method handled by the proxy.
12
+	 *
13
+	 * @var string
14
+	 */
15
+	protected $relation;
16 16
 
17
-    /**
18
-     * Reference to parent entity object
19
-     *
20
-     * @var \Analogue\ORM\System\InternallyMappable
21
-     */
22
-    protected $parentEntity;
17
+	/**
18
+	 * Reference to parent entity object
19
+	 *
20
+	 * @var \Analogue\ORM\System\InternallyMappable
21
+	 */
22
+	protected $parentEntity;
23 23
 
24
-    /**
25
-     * Lazy loaded relation flag
26
-     *
27
-     * @var boolean
28
-     */
29
-    protected $loaded = false;
24
+	/**
25
+	 * Lazy loaded relation flag
26
+	 *
27
+	 * @var boolean
28
+	 */
29
+	protected $loaded = false;
30 30
 
31
-    /**
32
-     * @param mixed  $parentEntity
33
-     * @param string $relation     relationship method handled by the proxy.
34
-     */
35
-    public function __construct($parentEntity, $relation)
36
-    {
37
-        $this->parentEntity = $parentEntity;
31
+	/**
32
+	 * @param mixed  $parentEntity
33
+	 * @param string $relation     relationship method handled by the proxy.
34
+	 */
35
+	public function __construct($parentEntity, $relation)
36
+	{
37
+		$this->parentEntity = $parentEntity;
38 38
 
39
-        $this->relation = $relation;
40
-    }
39
+		$this->relation = $relation;
40
+	}
41 41
 
42
-    /**
43
-     * Call the relationship method on the underlying entity map
44
-     *
45
-     * @throws MappingException
46
-     * @return mixed
47
-     */
48
-    public function load()
49
-    {
50
-        $entities = $this->query($this->parentEntity, $this->relation)->getResults($this->relation);
42
+	/**
43
+	 * Call the relationship method on the underlying entity map
44
+	 *
45
+	 * @throws MappingException
46
+	 * @return mixed
47
+	 */
48
+	public function load()
49
+	{
50
+		$entities = $this->query($this->parentEntity, $this->relation)->getResults($this->relation);
51 51
 
52
-        $this->loaded = true;
52
+		$this->loaded = true;
53 53
 
54
-        return $entities;
55
-    }
54
+		return $entities;
55
+	}
56 56
 
57
-    /**
58
-     * Return true if the underlying relation has been lazy loaded
59
-     *
60
-     * @return boolean
61
-     */
62
-    public function isLoaded()
63
-    {
64
-        return $this->loaded;
65
-    }
57
+	/**
58
+	 * Return true if the underlying relation has been lazy loaded
59
+	 *
60
+	 * @return boolean
61
+	 */
62
+	public function isLoaded()
63
+	{
64
+		return $this->loaded;
65
+	}
66 66
 
67
-    /**
68
-     * Return the Query Builder on the relation
69
-     *
70
-     * @param  \Analogue\ORM\System\InternallyMappable  $entity
71
-     * @param  string $relation
72
-     * @throws MappingException
73
-     * @return \Analogue\ORM\System\Query
74
-     */
75
-    protected function query($entity, $relation)
76
-    {
77
-        $entityMap = $this->getMapper($entity)->getEntityMap();
67
+	/**
68
+	 * Return the Query Builder on the relation
69
+	 *
70
+	 * @param  \Analogue\ORM\System\InternallyMappable  $entity
71
+	 * @param  string $relation
72
+	 * @throws MappingException
73
+	 * @return \Analogue\ORM\System\Query
74
+	 */
75
+	protected function query($entity, $relation)
76
+	{
77
+		$entityMap = $this->getMapper($entity)->getEntityMap();
78 78
 
79
-        return $entityMap->$relation($entity);
80
-    }
79
+		return $entityMap->$relation($entity);
80
+	}
81 81
 
82
-    /**
83
-     * Get the mapper instance for the entity
84
-     *
85
-     * @param  \Analogue\ORM\System\InternallyMappable $entity
86
-     * @throws MappingException
87
-     * @return \Analogue\ORM\System\Mapper
88
-     */
89
-    protected function getMapper($entity)
90
-    {
91
-        return Manager::getMapper($entity);
92
-    }
82
+	/**
83
+	 * Get the mapper instance for the entity
84
+	 *
85
+	 * @param  \Analogue\ORM\System\InternallyMappable $entity
86
+	 * @throws MappingException
87
+	 * @return \Analogue\ORM\System\Mapper
88
+	 */
89
+	protected function getMapper($entity)
90
+	{
91
+		return Manager::getMapper($entity);
92
+	}
93 93
 }
Please login to merge, or discard this patch.
src/System/Query.php 2 patches
Indentation   +837 added lines, -837 removed lines patch added patch discarded remove patch
@@ -20,846 +20,846 @@
 block discarded – undo
20 20
  */
21 21
 class Query
22 22
 {
23
-    /**
24
-     * Mapper Instance
25
-     *
26
-     * @var \Analogue\ORM\System\Mapper
27
-     */
28
-    protected $mapper;
29
-
30
-    /**
31
-     * DB Adatper
32
-     *
33
-     * @var \Analogue\ORM\Drivers\DBAdapter
34
-     */
35
-    protected $adapter;
36
-
37
-    /**
38
-     * Query Builder Instance
39
-     *
40
-     * @var \Analogue\ORM\Drivers\QueryAdapter|\Analogue\ORM\Drivers\IlluminateQueryAdapter
41
-     */
42
-    protected $query;
43
-
44
-    /**
45
-     * Entity Map Instance
46
-     *
47
-     * @var \Analogue\ORM\EntityMap
48
-     */
49
-    protected $entityMap;
23
+	/**
24
+	 * Mapper Instance
25
+	 *
26
+	 * @var \Analogue\ORM\System\Mapper
27
+	 */
28
+	protected $mapper;
29
+
30
+	/**
31
+	 * DB Adatper
32
+	 *
33
+	 * @var \Analogue\ORM\Drivers\DBAdapter
34
+	 */
35
+	protected $adapter;
36
+
37
+	/**
38
+	 * Query Builder Instance
39
+	 *
40
+	 * @var \Analogue\ORM\Drivers\QueryAdapter|\Analogue\ORM\Drivers\IlluminateQueryAdapter
41
+	 */
42
+	protected $query;
43
+
44
+	/**
45
+	 * Entity Map Instance
46
+	 *
47
+	 * @var \Analogue\ORM\EntityMap
48
+	 */
49
+	protected $entityMap;
50 50
     
51
-    /**
52
-     * The relationships that should be eager loaded.
53
-     *
54
-     * @var array
55
-     */
56
-    protected $eagerLoad = [];
57
-
58
-    /**
59
-     * All of the registered builder macros.
60
-     *
61
-     * @var array
62
-     */
63
-    protected $macros = [];
64
-
65
-    /**
66
-     * The methods that should be returned from query builder.
67
-     *
68
-     * @var array
69
-     */
70
-    protected $passthru = [
71
-        'toSql',
72
-        'lists',
73
-        'pluck',
74
-        'count',
75
-        'min',
76
-        'max',
77
-        'avg',
78
-        'sum',
79
-        'exists',
80
-        'getBindings',
81
-    ];
82
-
83
-    /**
84
-     * Query Builder Blacklist
85
-     */
86
-    protected $blacklist = [
87
-        'insert',
88
-        'insertGetId',
89
-        'lock',
90
-        'lockForUpdate',
91
-        'sharedLock',
92
-        'update',
93
-        'increment',
94
-        'decrement',
95
-        'delete',
96
-        'truncate',
97
-        'raw',
98
-    ];
99
-
100
-    /**
101
-     * Create a new Analogue Query Builder instance.
102
-     *
103
-     * @param  Mapper    $mapper
104
-     * @param  DBAdapter $adapter
105
-     */
106
-    public function __construct(Mapper $mapper, DBAdapter $adapter)
107
-    {
108
-        $this->mapper = $mapper;
109
-
110
-        $this->adapter = $adapter;
111
-
112
-        $this->entityMap = $mapper->getEntityMap();
113
-
114
-        // Specify the table to work on
115
-        $this->query = $adapter->getQuery()->from($this->entityMap->getTable());
116
-
117
-        $this->with($this->entityMap->getEagerloadedRelationships());
118
-    }
119
-
120
-    /**
121
-     * Run the query and return the result
122
-     *
123
-     * @param  array $columns
124
-     * @return \Analogue\ORM\EntityCollection
125
-     */
126
-    public function get($columns = ['*'])
127
-    {
128
-        $entities = $this->getEntities($columns);
129
-
130
-        // If we actually found models we will also eager load any relationships that
131
-        // have been specified as needing to be eager loaded, which will solve the
132
-        // n+1 query issue for the developers to avoid running a lot of queries.
133
-
134
-        if (count($entities) > 0) {
135
-            $entities = $this->eagerLoadRelations($entities);
136
-        }
137
-
138
-        return $this->entityMap->newCollection($entities);
139
-    }
140
-
141
-    /**
142
-     * Find an entity by its primary key
143
-     *
144
-     * @param  string|integer $id
145
-     * @param  array          $columns
146
-     * @return \Analogue\ORM\Mappable
147
-     */
148
-    public function find($id, $columns = ['*'])
149
-    {
150
-        if (is_array($id)) {
151
-            return $this->findMany($id, $columns);
152
-        }
153
-
154
-        $this->query->where($this->entityMap->getQualifiedKeyName(), '=', $id);
155
-
156
-        return $this->first($columns);
157
-    }
158
-
159
-    /**
160
-     * Find many entities by their primary keys.
161
-     *
162
-     * @param  array $id
163
-     * @param  array $columns
164
-     * @return EntityCollection
165
-     */
166
-    public function findMany($id, $columns = ['*'])
167
-    {
168
-        if (empty($id)) {
169
-            return new EntityCollection;
170
-        }
171
-
172
-        $this->query->whereIn($this->entityMap->getQualifiedKeyName(), $id);
173
-
174
-        return $this->get($columns);
175
-    }
176
-
177
-    /**
178
-     * Find a model by its primary key or throw an exception.
179
-     *
180
-     * @param  mixed $id
181
-     * @param  array $columns
182
-     * @throws \Analogue\ORM\Exceptions\EntityNotFoundException
183
-     * @return mixed|self
184
-     */
185
-    public function findOrFail($id, $columns = ['*'])
186
-    {
187
-        if (!is_null($entity = $this->find($id, $columns))) {
188
-            return $entity;
189
-        }
190
-
191
-        throw (new EntityNotFoundException)->setEntity(get_class($this->entityMap));
192
-    }
193
-
194
-
195
-    /**
196
-     * Execute the query and get the first result.
197
-     *
198
-     * @param  array $columns
199
-     * @return \Analogue\ORM\Entity
200
-     */
201
-    public function first($columns = ['*'])
202
-    {
203
-        return $this->take(1)->get($columns)->first();
204
-    }
205
-
206
-    /**
207
-     * Execute the query and get the first result or throw an exception.
208
-     *
209
-     * @param  array $columns
210
-     * @throws EntityNotFoundException
211
-     * @return \Analogue\ORM\Entity
212
-     */
213
-    public function firstOrFail($columns = ['*'])
214
-    {
215
-        if (!is_null($entity = $this->first($columns))) {
216
-            return $entity;
217
-        }
218
-
219
-        throw (new EntityNotFoundException)->setEntity(get_class($this->entityMap));
220
-    }
221
-
222
-    /**
223
-     * Pluck a single column from the database.
224
-     *
225
-     * @param  string $column
226
-     * @return mixed
227
-     */
228
-    public function pluck($column)
229
-    {
230
-        $result = $this->first([$column]);
231
-
232
-        if ($result) {
233
-            return $result->{$column};
234
-        }
235
-    }
236
-
237
-    /**
238
-     * Chunk the results of the query.
239
-     *
240
-     * @param  int      $count
241
-     * @param  callable $callback
242
-     * @return void
243
-     */
244
-    public function chunk($count, callable $callback)
245
-    {
246
-        $results = $this->forPage($page = 1, $count)->get();
247
-
248
-        while (count($results) > 0) {
249
-            // On each chunk result set, we will pass them to the callback and then let the
250
-            // developer take care of everything within the callback, which allows us to
251
-            // keep the memory low for spinning through large result sets for working.
252
-            call_user_func($callback, $results);
253
-
254
-            $page++;
255
-
256
-            $results = $this->forPage($page, $count)->get();
257
-        }
258
-    }
51
+	/**
52
+	 * The relationships that should be eager loaded.
53
+	 *
54
+	 * @var array
55
+	 */
56
+	protected $eagerLoad = [];
57
+
58
+	/**
59
+	 * All of the registered builder macros.
60
+	 *
61
+	 * @var array
62
+	 */
63
+	protected $macros = [];
64
+
65
+	/**
66
+	 * The methods that should be returned from query builder.
67
+	 *
68
+	 * @var array
69
+	 */
70
+	protected $passthru = [
71
+		'toSql',
72
+		'lists',
73
+		'pluck',
74
+		'count',
75
+		'min',
76
+		'max',
77
+		'avg',
78
+		'sum',
79
+		'exists',
80
+		'getBindings',
81
+	];
82
+
83
+	/**
84
+	 * Query Builder Blacklist
85
+	 */
86
+	protected $blacklist = [
87
+		'insert',
88
+		'insertGetId',
89
+		'lock',
90
+		'lockForUpdate',
91
+		'sharedLock',
92
+		'update',
93
+		'increment',
94
+		'decrement',
95
+		'delete',
96
+		'truncate',
97
+		'raw',
98
+	];
99
+
100
+	/**
101
+	 * Create a new Analogue Query Builder instance.
102
+	 *
103
+	 * @param  Mapper    $mapper
104
+	 * @param  DBAdapter $adapter
105
+	 */
106
+	public function __construct(Mapper $mapper, DBAdapter $adapter)
107
+	{
108
+		$this->mapper = $mapper;
109
+
110
+		$this->adapter = $adapter;
111
+
112
+		$this->entityMap = $mapper->getEntityMap();
113
+
114
+		// Specify the table to work on
115
+		$this->query = $adapter->getQuery()->from($this->entityMap->getTable());
116
+
117
+		$this->with($this->entityMap->getEagerloadedRelationships());
118
+	}
119
+
120
+	/**
121
+	 * Run the query and return the result
122
+	 *
123
+	 * @param  array $columns
124
+	 * @return \Analogue\ORM\EntityCollection
125
+	 */
126
+	public function get($columns = ['*'])
127
+	{
128
+		$entities = $this->getEntities($columns);
129
+
130
+		// If we actually found models we will also eager load any relationships that
131
+		// have been specified as needing to be eager loaded, which will solve the
132
+		// n+1 query issue for the developers to avoid running a lot of queries.
133
+
134
+		if (count($entities) > 0) {
135
+			$entities = $this->eagerLoadRelations($entities);
136
+		}
137
+
138
+		return $this->entityMap->newCollection($entities);
139
+	}
140
+
141
+	/**
142
+	 * Find an entity by its primary key
143
+	 *
144
+	 * @param  string|integer $id
145
+	 * @param  array          $columns
146
+	 * @return \Analogue\ORM\Mappable
147
+	 */
148
+	public function find($id, $columns = ['*'])
149
+	{
150
+		if (is_array($id)) {
151
+			return $this->findMany($id, $columns);
152
+		}
153
+
154
+		$this->query->where($this->entityMap->getQualifiedKeyName(), '=', $id);
155
+
156
+		return $this->first($columns);
157
+	}
158
+
159
+	/**
160
+	 * Find many entities by their primary keys.
161
+	 *
162
+	 * @param  array $id
163
+	 * @param  array $columns
164
+	 * @return EntityCollection
165
+	 */
166
+	public function findMany($id, $columns = ['*'])
167
+	{
168
+		if (empty($id)) {
169
+			return new EntityCollection;
170
+		}
171
+
172
+		$this->query->whereIn($this->entityMap->getQualifiedKeyName(), $id);
173
+
174
+		return $this->get($columns);
175
+	}
176
+
177
+	/**
178
+	 * Find a model by its primary key or throw an exception.
179
+	 *
180
+	 * @param  mixed $id
181
+	 * @param  array $columns
182
+	 * @throws \Analogue\ORM\Exceptions\EntityNotFoundException
183
+	 * @return mixed|self
184
+	 */
185
+	public function findOrFail($id, $columns = ['*'])
186
+	{
187
+		if (!is_null($entity = $this->find($id, $columns))) {
188
+			return $entity;
189
+		}
190
+
191
+		throw (new EntityNotFoundException)->setEntity(get_class($this->entityMap));
192
+	}
193
+
194
+
195
+	/**
196
+	 * Execute the query and get the first result.
197
+	 *
198
+	 * @param  array $columns
199
+	 * @return \Analogue\ORM\Entity
200
+	 */
201
+	public function first($columns = ['*'])
202
+	{
203
+		return $this->take(1)->get($columns)->first();
204
+	}
205
+
206
+	/**
207
+	 * Execute the query and get the first result or throw an exception.
208
+	 *
209
+	 * @param  array $columns
210
+	 * @throws EntityNotFoundException
211
+	 * @return \Analogue\ORM\Entity
212
+	 */
213
+	public function firstOrFail($columns = ['*'])
214
+	{
215
+		if (!is_null($entity = $this->first($columns))) {
216
+			return $entity;
217
+		}
218
+
219
+		throw (new EntityNotFoundException)->setEntity(get_class($this->entityMap));
220
+	}
221
+
222
+	/**
223
+	 * Pluck a single column from the database.
224
+	 *
225
+	 * @param  string $column
226
+	 * @return mixed
227
+	 */
228
+	public function pluck($column)
229
+	{
230
+		$result = $this->first([$column]);
231
+
232
+		if ($result) {
233
+			return $result->{$column};
234
+		}
235
+	}
236
+
237
+	/**
238
+	 * Chunk the results of the query.
239
+	 *
240
+	 * @param  int      $count
241
+	 * @param  callable $callback
242
+	 * @return void
243
+	 */
244
+	public function chunk($count, callable $callback)
245
+	{
246
+		$results = $this->forPage($page = 1, $count)->get();
247
+
248
+		while (count($results) > 0) {
249
+			// On each chunk result set, we will pass them to the callback and then let the
250
+			// developer take care of everything within the callback, which allows us to
251
+			// keep the memory low for spinning through large result sets for working.
252
+			call_user_func($callback, $results);
253
+
254
+			$page++;
255
+
256
+			$results = $this->forPage($page, $count)->get();
257
+		}
258
+	}
259 259
     
260
-    /**
261
-     * Get an array with the values of a given column.
262
-     *
263
-     * @param  string $column
264
-     * @param  string $key
265
-     * @return array
266
-     */
267
-    public function lists($column, $key = null)
268
-    {
269
-        return $this->query->lists($column, $key);
270
-    }
271
-
272
-    /**
273
-     * Get a paginator for the "select" statement.
274
-     *
275
-     * @param  int   $perPage
276
-     * @param  array $columns
277
-     * @return LengthAwarePaginator
278
-     */
279
-    public function paginate($perPage = null, $columns = ['*'])
280
-    {
281
-        $total = $this->query->getCountForPagination();
282
-
283
-        $this->query->forPage(
284
-            $page = Paginator::resolveCurrentPage(),
285
-            $perPage = $perPage ?: $this->entityMap->getPerPage()
286
-        );
287
-
288
-        return new LengthAwarePaginator($this->get($columns)->all(), $total, $perPage, $page, [
289
-            'path' => Paginator::resolveCurrentPath()
290
-        ]);
291
-    }
292
-
293
-    /**
294
-     * Get a paginator for a grouped statement.
295
-     *
296
-     * @param  \Illuminate\Pagination\Factory $paginator
297
-     * @param  int                            $perPage
298
-     * @param  array                          $columns
299
-     * @return \Illuminate\Pagination\Paginator
300
-     */
301
-    protected function groupedPaginate($paginator, $perPage, $columns)
302
-    {
303
-        $results = $this->get($columns)->all();
304
-
305
-        return $this->query->buildRawPaginator($paginator, $results, $perPage);
306
-    }
307
-
308
-    /**
309
-     * Get a paginator for an ungrouped statement.
310
-     *
311
-     * @param  \Illuminate\Pagination\Factory $paginator
312
-     * @param  int                            $perPage
313
-     * @param  array                          $columns
314
-     * @return \Illuminate\Pagination\Paginator
315
-     */
316
-    protected function ungroupedPaginate($paginator, $perPage, $columns)
317
-    {
318
-        $total = $this->query->getPaginationCount();
319
-
320
-        // Once we have the paginator we need to set the limit and offset values for
321
-        // the query so we can get the properly paginated items. Once we have an
322
-        // array of items we can create the paginator instances for the items.
323
-        $page = $paginator->getCurrentPage($total);
324
-
325
-        $this->query->forPage($page, $perPage);
326
-
327
-        return $paginator->make($this->get($columns)->all(), $total, $perPage);
328
-    }
329
-
330
-    /**
331
-     * Paginate the given query into a simple paginator.
332
-     *
333
-     * @param  int   $perPage
334
-     * @param  array $columns
335
-     * @return \Illuminate\Contracts\Pagination\Paginator
336
-     */
337
-    public function simplePaginate($perPage = null, $columns = ['*'])
338
-    {
339
-        $page = Paginator::resolveCurrentPage();
340
-
341
-        $perPage = $perPage ?: $this->entityMap->getPerPage();
342
-
343
-        $this->skip(($page - 1) * $perPage)->take($perPage + 1);
344
-
345
-        return new Paginator($this->get($columns)->all(), $perPage, $page, ['path' => Paginator::resolveCurrentPath()]);
346
-    }
347
-
348
-    /**
349
-     * Add a basic where clause to the query.
350
-     *
351
-     * @param  string $column
352
-     * @param  string $operator
353
-     * @param  mixed  $value
354
-     * @param  string $boolean
355
-     * @return $this
356
-     */
357
-    public function where($column, $operator = null, $value = null, $boolean = 'and')
358
-    {
359
-        if ($column instanceof Closure) {
360
-            $query = $this->newQueryWithoutScopes();
361
-
362
-            call_user_func($column, $query);
363
-
364
-            $this->query->addNestedWhereQuery($query->getQuery(), $boolean);
365
-        } else {
366
-            call_user_func_array([$this->query, 'where'], func_get_args());
367
-        }
368
-
369
-        return $this;
370
-    }
371
-
372
-    /**
373
-     * Add an "or where" clause to the query.
374
-     *
375
-     * @param  string $column
376
-     * @param  string $operator
377
-     * @param  mixed  $value
378
-     * @return \Analogue\ORM\System\Query
379
-     */
380
-    public function orWhere($column, $operator = null, $value = null)
381
-    {
382
-        return $this->where($column, $operator, $value, 'or');
383
-    }
384
-
385
-    /**
386
-     * Add a relationship count condition to the query.
387
-     *
388
-     * @param  string   $relation
389
-     * @param  string   $operator
390
-     * @param  int      $count
391
-     * @param  string   $boolean
392
-     * @param  \Closure $callback
393
-     * @return \Analogue\ORM\System\Query
394
-     */
395
-    public function has($relation, $operator = '>=', $count = 1, $boolean = 'and', $callback = null)
396
-    {
397
-        $entity = $this->mapper->newInstance();
398
-
399
-        $relation = $this->getHasRelationQuery($relation, $entity);
400
-
401
-        $query = $relation->getRelationCountQuery($relation->getRelatedMapper()->getQuery(), $this);
402
-
403
-        if ($callback) {
404
-            call_user_func($callback, $query);
405
-        }
406
-
407
-        return $this->addHasWhere($query, $relation, $operator, $count, $boolean);
408
-    }
409
-
410
-    /**
411
-     * Add a relationship count condition to the query with where clauses.
412
-     *
413
-     * @param  string   $relation
414
-     * @param  \Closure $callback
415
-     * @param  string   $operator
416
-     * @param  int      $count
417
-     * @return \Analogue\ORM\System\Query
418
-     */
419
-    public function whereHas($relation, Closure $callback, $operator = '>=', $count = 1)
420
-    {
421
-        return $this->has($relation, $operator, $count, 'and', $callback);
422
-    }
423
-
424
-    /**
425
-     * Add a relationship count condition to the query with an "or".
426
-     *
427
-     * @param  string $relation
428
-     * @param  string $operator
429
-     * @param  int    $count
430
-     * @return \Analogue\ORM\System\Query
431
-     */
432
-    public function orHas($relation, $operator = '>=', $count = 1)
433
-    {
434
-        return $this->has($relation, $operator, $count, 'or');
435
-    }
436
-
437
-    /**
438
-     * Add a relationship count condition to the query with where clauses and an "or".
439
-     *
440
-     * @param  string   $relation
441
-     * @param  \Closure $callback
442
-     * @param  string   $operator
443
-     * @param  int      $count
444
-     * @return \Analogue\ORM\System\Query
445
-     */
446
-    public function orWhereHas($relation, Closure $callback, $operator = '>=', $count = 1)
447
-    {
448
-        return $this->has($relation, $operator, $count, 'or', $callback);
449
-    }
450
-
451
-    /**
452
-     * Add the "has" condition where clause to the query.
453
-     *
454
-     * @param  \Analogue\ORM\System\Query               $hasQuery
455
-     * @param  \Analogue\ORM\Relationships\Relationship $relation
456
-     * @param  string                                   $operator
457
-     * @param  int                                      $count
458
-     * @param  string                                   $boolean
459
-     * @return \Analogue\ORM\System\Query
460
-     */
461
-    protected function addHasWhere(Query $hasQuery, Relationship $relation, $operator, $count, $boolean)
462
-    {
463
-        $this->mergeWheresToHas($hasQuery, $relation);
464
-
465
-        if (is_numeric($count)) {
466
-            $count = new Expression($count);
467
-        }
468
-
469
-        return $this->where(new Expression('(' . $hasQuery->toSql() . ')'), $operator, $count, $boolean);
470
-    }
471
-
472
-    /**
473
-     * Merge the "wheres" from a relation query to a has query.
474
-     *
475
-     * @param  \Analogue\ORM\System\Query               $hasQuery
476
-     * @param  \Analogue\ORM\Relationships\Relationship $relation
477
-     * @return void
478
-     */
479
-    protected function mergeWheresToHas(Query $hasQuery, Relationship $relation)
480
-    {
481
-        // Here we have the "has" query and the original relation. We need to copy over any
482
-        // where clauses the developer may have put in the relationship function over to
483
-        // the has query, and then copy the bindings from the "has" query to the main.
484
-        $relationQuery = $relation->getBaseQuery();
485
-
486
-        $hasQuery->mergeWheres(
487
-            $relationQuery->wheres, $relationQuery->getBindings()
488
-        );
489
-
490
-        $this->query->mergeBindings($hasQuery->getQuery());
491
-    }
492
-
493
-    /**
494
-     * Get the "has relation" base query instance.
495
-     *
496
-     * @param  string $relation
497
-     * @param         $entity
498
-     * @return \Analogue\ORM\System\Query
499
-     */
500
-    protected function getHasRelationQuery($relation, $entity)
501
-    {
502
-        return Relationship::noConstraints(function () use ($relation, $entity) {
503
-            return $this->entityMap->$relation($entity);
504
-        });
505
-    }
506
-
507
-    /**
508
-     * Get the table for the current query object
509
-     *
510
-     * @return string
511
-     */
512
-    public function getTable()
513
-    {
514
-        return $this->entityMap->getTable();
515
-    }
516
-
517
-    /**
518
-     * Set the relationships that should be eager loaded.
519
-     *
520
-     * @param  mixed $relations
521
-     * @return $this
522
-     */
523
-    public function with($relations)
524
-    {
525
-        if (is_string($relations)) {
526
-            $relations = func_get_args();
527
-        }
528
-
529
-        $eagers = $this->parseRelations($relations);
530
-
531
-        $this->eagerLoad = array_merge($this->eagerLoad, $eagers);
532
-
533
-        return $this;
534
-    }
535
-
536
-    /**
537
-     * Parse a list of relations into individuals.
538
-     *
539
-     * @param  array $relations
540
-     * @return array
541
-     */
542
-    protected function parseRelations(array $relations)
543
-    {
544
-        $results = [];
545
-
546
-        foreach ($relations as $name => $constraints) {
547
-            // If the "relation" value is actually a numeric key, we can assume that no
548
-            // constraints have been specified for the eager load and we'll just put
549
-            // an empty Closure with the loader so that we can treat all the same.
550
-            if (is_numeric($name)) {
551
-                $f = function () {};
552
-
553
-                list($name, $constraints) = [$constraints, $f];
554
-            }
555
-
556
-            // We need to separate out any nested includes. Which allows the developers
557
-            // to load deep relationships using "dots" without stating each level of
558
-            // the relationship with its own key in the array of eager load names.
559
-            $results = $this->parseNested($name, $results);
560
-
561
-            $results[$name] = $constraints;
562
-        }
563
-
564
-        return $results;
565
-    }
566
-
567
-
568
-    /**
569
-     * Parse the nested relationships in a relation.
570
-     *
571
-     * @param  string $name
572
-     * @param  array  $results
573
-     * @return array
574
-     */
575
-    protected function parseNested($name, $results)
576
-    {
577
-        $progress = [];
578
-
579
-        // If the relation has already been set on the result array, we will not set it
580
-        // again, since that would override any constraints that were already placed
581
-        // on the relationships. We will only set the ones that are not specified.
582
-        foreach (explode('.', $name) as $segment) {
583
-            $progress[] = $segment;
584
-
585
-            if (!isset($results[$last = implode('.', $progress)])) {
586
-                $results[$last] = function () {};
587
-            }
588
-        }
589
-
590
-        return $results;
591
-    }
592
-
593
-    /**
594
-     * Get the relationships being eagerly loaded.
595
-     *
596
-     * @return array
597
-     */
598
-    public function getEagerLoads()
599
-    {
600
-        return $this->eagerLoad;
601
-    }
602
-
603
-    /**
604
-     * Set the relationships being eagerly loaded.
605
-     *
606
-     * @param  array $eagerLoad
607
-     * @return void
608
-     */
609
-    public function setEagerLoads(array $eagerLoad)
610
-    {
611
-        $this->eagerLoad = $eagerLoad;
612
-    }
613
-
614
-    /**
615
-     * Eager load the relationships for the entities.
616
-     *
617
-     * @param  array $entities
618
-     * @return array
619
-     */
620
-    public function eagerLoadRelations($entities)
621
-    {
622
-        foreach ($this->eagerLoad as $name => $constraints) {
623
-            // For nested eager loads we'll skip loading them here and they will be set as an
624
-            // eager load on the query to retrieve the relation so that they will be eager
625
-            // loaded on that query, because that is where they get hydrated as models.
626
-            if (strpos($name, '.') === false) {
627
-                $entities = $this->loadRelation($entities, $name, $constraints);
628
-            }
629
-        }
630
-
631
-        return $entities;
632
-    }
633
-
634
-    /**
635
-     * Eagerly load the relationship on a set of entities.
636
-     *
637
-     * @param  array    $entities
638
-     * @param  string   $name
639
-     * @param  \Closure $constraints
640
-     * @return array
641
-     */
642
-    protected function loadRelation(array $entities, $name, Closure $constraints)
643
-    {
644
-        // First we will "back up" the existing where conditions on the query so we can
645
-        // add our eager constraints. Then we will merge the wheres that were on the
646
-        // query back to it in order that any where conditions might be specified.
647
-        $relation = $this->getRelation($name);
648
-
649
-        $relation->addEagerConstraints($entities);
650
-
651
-        call_user_func($constraints, $relation);
652
-
653
-        $entities = $relation->initRelation($entities, $name);
654
-
655
-        // Once we have the results, we just match those back up to their parent models
656
-        // using the relationship instance. Then we just return the finished arrays
657
-        // of models which have been eagerly hydrated and are readied for return.
658
-
659
-        $results = $relation->getEager();
660
-
661
-        return $relation->match($entities, $results, $name);
662
-    }
663
-
664
-    /**
665
-     * Get the relation instance for the given relation name.
666
-     *
667
-     * @param  string $relation
668
-     * @return \Analogue\ORM\Relationships\Relationship
669
-     */
670
-    public function getRelation($relation)
671
-    {
672
-        // We want to run a relationship query without any constrains so that we will
673
-        // not have to remove these where clauses manually which gets really hacky
674
-        // and is error prone while we remove the developer's own where clauses.
675
-        $query = Relationship::noConstraints(function() use ($relation) {
676
-            return $this->entityMap->$relation($this->getEntityInstance());
677
-        });
678
-
679
-        $nested = $this->nestedRelations($relation);
680
-
681
-        // If there are nested relationships set on the query, we will put those onto
682
-        // the query instances so that they can be handled after this relationship
683
-        // is loaded. In this way they will all trickle down as they are loaded.
684
-        if (count($nested) > 0) {
685
-            $query->getQuery()->with($nested);
686
-        }
687
-
688
-        return $query;
689
-    }
690
-
691
-    /**
692
-     * Get the deeply nested relations for a given top-level relation.
693
-     *
694
-     * @param  string $relation
695
-     * @return array
696
-     */
697
-    protected function nestedRelations($relation)
698
-    {
699
-        $nested = [];
700
-
701
-        // We are basically looking for any relationships that are nested deeper than
702
-        // the given top-level relationship. We will just check for any relations
703
-        // that start with the given top relations and adds them to our arrays.
704
-        foreach ($this->eagerLoad as $name => $constraints) {
705
-            if ($this->isNested($name, $relation)) {
706
-                $nested[substr($name, strlen($relation . '.'))] = $constraints;
707
-            }
708
-        }
709
-
710
-        return $nested;
711
-    }
712
-
713
-    /**
714
-     * Determine if the relationship is nested.
715
-     *
716
-     * @param  string $name
717
-     * @param  string $relation
718
-     * @return bool
719
-     */
720
-    protected function isNested($name, $relation)
721
-    {
722
-        $dots = str_contains($name, '.');
723
-
724
-        return $dots && starts_with($name, $relation . '.');
725
-    }
726
-
727
-    /**
728
-     * Add the Entity primary key if not in requested columns
729
-     *
730
-     * @param  array $columns
731
-     * @return array
732
-     */
733
-    protected function enforceIdColumn($columns)
734
-    {
735
-        if (!in_array($this->entityMap->getKeyName(), $columns)) {
736
-            $columns[] = $this->entityMap->getKeyName();
737
-        }
738
-        return $columns;
739
-    }
740
-
741
-    /**
742
-     * Get the hydrated models without eager loading.
743
-     *
744
-     * @param  array  $columns
745
-     * @return \Analogue\ORM\EntityCollection
746
-     */
747
-    public function getEntities($columns = ['*'])
748
-    {
749
-        // As we need the primary key to feed the
750
-        // entity cache, we need it loaded on each
751
-        // request
752
-        $columns = $this->enforceIdColumn($columns);
753
-
754
-        // Run the query
755
-        $results = $this->query->get($columns);
756
-
757
-        $builder = new EntityBuilder($this->mapper, array_keys($this->getEagerLoads()));
758
-
759
-        return $builder->build($results);
760
-    }
761
-
762
-    /**
763
-     * Get a new instance for the entity
764
-     *
765
-     * @param  array  $attributes
766
-     * @return \Analogue\ORM\Entity
767
-     */
768
-    public function getEntityInstance(array $attributes = [])
769
-    {
770
-        return $this->mapper->newInstance($attributes);
771
-    }
772
-
773
-    /**
774
-     * Extend the builder with a given callback.
775
-     *
776
-     * @param  string   $name
777
-     * @param  \Closure $callback
778
-     * @return void
779
-     */
780
-    public function macro($name, Closure $callback)
781
-    {
782
-        $this->macros[$name] = $callback;
783
-    }
784
-
785
-    /**
786
-     * Get the given macro by name.
787
-     *
788
-     * @param  string $name
789
-     * @return \Closure
790
-     */
791
-    public function getMacro($name)
792
-    {
793
-        return array_get($this->macros, $name);
794
-    }
795
-
796
-    /**
797
-     * Get a new query builder for the model's table.
798
-     *
799
-     * @return \Analogue\ORM\System\Query
800
-     */
801
-    public function newQuery()
802
-    {
803
-        $builder = new Query($this->mapper, $this->adapter);
804
-
805
-        return $this->applyGlobalScopes($builder);
806
-    }
807
-
808
-    /**
809
-     * Get a new query builder without any scope applied.
810
-     *
811
-     * @return \Analogue\ORM\System\Query
812
-     */
813
-    public function newQueryWithoutScopes()
814
-    {
815
-        return new Query($this->mapper, $this->adapter);
816
-    }
817
-
818
-    /**
819
-     * Get the Mapper instance for this Query Builder
820
-     *
821
-     * @return \Analogue\ORM\System\Mapper
822
-     */
823
-    public function getMapper()
824
-    {
825
-        return $this->mapper;
826
-    }
827
-
828
-    /**
829
-     * Get the underlying query adapter
830
-     *
831
-     * (REFACTOR: this method should move out, we need to provide the client classes
832
-     * with the adapter instead.)
833
-     *
834
-     * @return \Analogue\ORM\Drivers\QueryAdapter|\Analogue\ORM\Drivers\IlluminateQueryAdapter
835
-     */
836
-    public function getQuery()
837
-    {
838
-        return $this->query;
839
-    }
840
-
841
-    /**
842
-     * Dynamically handle calls into the query instance.
843
-     *
844
-     * @param  string $method
845
-     * @param  array  $parameters
846
-     * @throws Exception
847
-     * @return mixed
848
-     */
849
-    public function __call($method, $parameters)
850
-    {
851
-        if (isset($this->macros[$method])) {
852
-            array_unshift($parameters, $this);
853
-
854
-            return call_user_func_array($this->macros[$method], $parameters);
855
-        }
260
+	/**
261
+	 * Get an array with the values of a given column.
262
+	 *
263
+	 * @param  string $column
264
+	 * @param  string $key
265
+	 * @return array
266
+	 */
267
+	public function lists($column, $key = null)
268
+	{
269
+		return $this->query->lists($column, $key);
270
+	}
271
+
272
+	/**
273
+	 * Get a paginator for the "select" statement.
274
+	 *
275
+	 * @param  int   $perPage
276
+	 * @param  array $columns
277
+	 * @return LengthAwarePaginator
278
+	 */
279
+	public function paginate($perPage = null, $columns = ['*'])
280
+	{
281
+		$total = $this->query->getCountForPagination();
282
+
283
+		$this->query->forPage(
284
+			$page = Paginator::resolveCurrentPage(),
285
+			$perPage = $perPage ?: $this->entityMap->getPerPage()
286
+		);
287
+
288
+		return new LengthAwarePaginator($this->get($columns)->all(), $total, $perPage, $page, [
289
+			'path' => Paginator::resolveCurrentPath()
290
+		]);
291
+	}
292
+
293
+	/**
294
+	 * Get a paginator for a grouped statement.
295
+	 *
296
+	 * @param  \Illuminate\Pagination\Factory $paginator
297
+	 * @param  int                            $perPage
298
+	 * @param  array                          $columns
299
+	 * @return \Illuminate\Pagination\Paginator
300
+	 */
301
+	protected function groupedPaginate($paginator, $perPage, $columns)
302
+	{
303
+		$results = $this->get($columns)->all();
304
+
305
+		return $this->query->buildRawPaginator($paginator, $results, $perPage);
306
+	}
307
+
308
+	/**
309
+	 * Get a paginator for an ungrouped statement.
310
+	 *
311
+	 * @param  \Illuminate\Pagination\Factory $paginator
312
+	 * @param  int                            $perPage
313
+	 * @param  array                          $columns
314
+	 * @return \Illuminate\Pagination\Paginator
315
+	 */
316
+	protected function ungroupedPaginate($paginator, $perPage, $columns)
317
+	{
318
+		$total = $this->query->getPaginationCount();
319
+
320
+		// Once we have the paginator we need to set the limit and offset values for
321
+		// the query so we can get the properly paginated items. Once we have an
322
+		// array of items we can create the paginator instances for the items.
323
+		$page = $paginator->getCurrentPage($total);
324
+
325
+		$this->query->forPage($page, $perPage);
326
+
327
+		return $paginator->make($this->get($columns)->all(), $total, $perPage);
328
+	}
329
+
330
+	/**
331
+	 * Paginate the given query into a simple paginator.
332
+	 *
333
+	 * @param  int   $perPage
334
+	 * @param  array $columns
335
+	 * @return \Illuminate\Contracts\Pagination\Paginator
336
+	 */
337
+	public function simplePaginate($perPage = null, $columns = ['*'])
338
+	{
339
+		$page = Paginator::resolveCurrentPage();
340
+
341
+		$perPage = $perPage ?: $this->entityMap->getPerPage();
342
+
343
+		$this->skip(($page - 1) * $perPage)->take($perPage + 1);
344
+
345
+		return new Paginator($this->get($columns)->all(), $perPage, $page, ['path' => Paginator::resolveCurrentPath()]);
346
+	}
347
+
348
+	/**
349
+	 * Add a basic where clause to the query.
350
+	 *
351
+	 * @param  string $column
352
+	 * @param  string $operator
353
+	 * @param  mixed  $value
354
+	 * @param  string $boolean
355
+	 * @return $this
356
+	 */
357
+	public function where($column, $operator = null, $value = null, $boolean = 'and')
358
+	{
359
+		if ($column instanceof Closure) {
360
+			$query = $this->newQueryWithoutScopes();
361
+
362
+			call_user_func($column, $query);
363
+
364
+			$this->query->addNestedWhereQuery($query->getQuery(), $boolean);
365
+		} else {
366
+			call_user_func_array([$this->query, 'where'], func_get_args());
367
+		}
368
+
369
+		return $this;
370
+	}
371
+
372
+	/**
373
+	 * Add an "or where" clause to the query.
374
+	 *
375
+	 * @param  string $column
376
+	 * @param  string $operator
377
+	 * @param  mixed  $value
378
+	 * @return \Analogue\ORM\System\Query
379
+	 */
380
+	public function orWhere($column, $operator = null, $value = null)
381
+	{
382
+		return $this->where($column, $operator, $value, 'or');
383
+	}
384
+
385
+	/**
386
+	 * Add a relationship count condition to the query.
387
+	 *
388
+	 * @param  string   $relation
389
+	 * @param  string   $operator
390
+	 * @param  int      $count
391
+	 * @param  string   $boolean
392
+	 * @param  \Closure $callback
393
+	 * @return \Analogue\ORM\System\Query
394
+	 */
395
+	public function has($relation, $operator = '>=', $count = 1, $boolean = 'and', $callback = null)
396
+	{
397
+		$entity = $this->mapper->newInstance();
398
+
399
+		$relation = $this->getHasRelationQuery($relation, $entity);
400
+
401
+		$query = $relation->getRelationCountQuery($relation->getRelatedMapper()->getQuery(), $this);
402
+
403
+		if ($callback) {
404
+			call_user_func($callback, $query);
405
+		}
406
+
407
+		return $this->addHasWhere($query, $relation, $operator, $count, $boolean);
408
+	}
409
+
410
+	/**
411
+	 * Add a relationship count condition to the query with where clauses.
412
+	 *
413
+	 * @param  string   $relation
414
+	 * @param  \Closure $callback
415
+	 * @param  string   $operator
416
+	 * @param  int      $count
417
+	 * @return \Analogue\ORM\System\Query
418
+	 */
419
+	public function whereHas($relation, Closure $callback, $operator = '>=', $count = 1)
420
+	{
421
+		return $this->has($relation, $operator, $count, 'and', $callback);
422
+	}
423
+
424
+	/**
425
+	 * Add a relationship count condition to the query with an "or".
426
+	 *
427
+	 * @param  string $relation
428
+	 * @param  string $operator
429
+	 * @param  int    $count
430
+	 * @return \Analogue\ORM\System\Query
431
+	 */
432
+	public function orHas($relation, $operator = '>=', $count = 1)
433
+	{
434
+		return $this->has($relation, $operator, $count, 'or');
435
+	}
436
+
437
+	/**
438
+	 * Add a relationship count condition to the query with where clauses and an "or".
439
+	 *
440
+	 * @param  string   $relation
441
+	 * @param  \Closure $callback
442
+	 * @param  string   $operator
443
+	 * @param  int      $count
444
+	 * @return \Analogue\ORM\System\Query
445
+	 */
446
+	public function orWhereHas($relation, Closure $callback, $operator = '>=', $count = 1)
447
+	{
448
+		return $this->has($relation, $operator, $count, 'or', $callback);
449
+	}
450
+
451
+	/**
452
+	 * Add the "has" condition where clause to the query.
453
+	 *
454
+	 * @param  \Analogue\ORM\System\Query               $hasQuery
455
+	 * @param  \Analogue\ORM\Relationships\Relationship $relation
456
+	 * @param  string                                   $operator
457
+	 * @param  int                                      $count
458
+	 * @param  string                                   $boolean
459
+	 * @return \Analogue\ORM\System\Query
460
+	 */
461
+	protected function addHasWhere(Query $hasQuery, Relationship $relation, $operator, $count, $boolean)
462
+	{
463
+		$this->mergeWheresToHas($hasQuery, $relation);
464
+
465
+		if (is_numeric($count)) {
466
+			$count = new Expression($count);
467
+		}
468
+
469
+		return $this->where(new Expression('(' . $hasQuery->toSql() . ')'), $operator, $count, $boolean);
470
+	}
471
+
472
+	/**
473
+	 * Merge the "wheres" from a relation query to a has query.
474
+	 *
475
+	 * @param  \Analogue\ORM\System\Query               $hasQuery
476
+	 * @param  \Analogue\ORM\Relationships\Relationship $relation
477
+	 * @return void
478
+	 */
479
+	protected function mergeWheresToHas(Query $hasQuery, Relationship $relation)
480
+	{
481
+		// Here we have the "has" query and the original relation. We need to copy over any
482
+		// where clauses the developer may have put in the relationship function over to
483
+		// the has query, and then copy the bindings from the "has" query to the main.
484
+		$relationQuery = $relation->getBaseQuery();
485
+
486
+		$hasQuery->mergeWheres(
487
+			$relationQuery->wheres, $relationQuery->getBindings()
488
+		);
489
+
490
+		$this->query->mergeBindings($hasQuery->getQuery());
491
+	}
492
+
493
+	/**
494
+	 * Get the "has relation" base query instance.
495
+	 *
496
+	 * @param  string $relation
497
+	 * @param         $entity
498
+	 * @return \Analogue\ORM\System\Query
499
+	 */
500
+	protected function getHasRelationQuery($relation, $entity)
501
+	{
502
+		return Relationship::noConstraints(function () use ($relation, $entity) {
503
+			return $this->entityMap->$relation($entity);
504
+		});
505
+	}
506
+
507
+	/**
508
+	 * Get the table for the current query object
509
+	 *
510
+	 * @return string
511
+	 */
512
+	public function getTable()
513
+	{
514
+		return $this->entityMap->getTable();
515
+	}
516
+
517
+	/**
518
+	 * Set the relationships that should be eager loaded.
519
+	 *
520
+	 * @param  mixed $relations
521
+	 * @return $this
522
+	 */
523
+	public function with($relations)
524
+	{
525
+		if (is_string($relations)) {
526
+			$relations = func_get_args();
527
+		}
528
+
529
+		$eagers = $this->parseRelations($relations);
530
+
531
+		$this->eagerLoad = array_merge($this->eagerLoad, $eagers);
532
+
533
+		return $this;
534
+	}
535
+
536
+	/**
537
+	 * Parse a list of relations into individuals.
538
+	 *
539
+	 * @param  array $relations
540
+	 * @return array
541
+	 */
542
+	protected function parseRelations(array $relations)
543
+	{
544
+		$results = [];
545
+
546
+		foreach ($relations as $name => $constraints) {
547
+			// If the "relation" value is actually a numeric key, we can assume that no
548
+			// constraints have been specified for the eager load and we'll just put
549
+			// an empty Closure with the loader so that we can treat all the same.
550
+			if (is_numeric($name)) {
551
+				$f = function () {};
552
+
553
+				list($name, $constraints) = [$constraints, $f];
554
+			}
555
+
556
+			// We need to separate out any nested includes. Which allows the developers
557
+			// to load deep relationships using "dots" without stating each level of
558
+			// the relationship with its own key in the array of eager load names.
559
+			$results = $this->parseNested($name, $results);
560
+
561
+			$results[$name] = $constraints;
562
+		}
563
+
564
+		return $results;
565
+	}
566
+
567
+
568
+	/**
569
+	 * Parse the nested relationships in a relation.
570
+	 *
571
+	 * @param  string $name
572
+	 * @param  array  $results
573
+	 * @return array
574
+	 */
575
+	protected function parseNested($name, $results)
576
+	{
577
+		$progress = [];
578
+
579
+		// If the relation has already been set on the result array, we will not set it
580
+		// again, since that would override any constraints that were already placed
581
+		// on the relationships. We will only set the ones that are not specified.
582
+		foreach (explode('.', $name) as $segment) {
583
+			$progress[] = $segment;
584
+
585
+			if (!isset($results[$last = implode('.', $progress)])) {
586
+				$results[$last] = function () {};
587
+			}
588
+		}
589
+
590
+		return $results;
591
+	}
592
+
593
+	/**
594
+	 * Get the relationships being eagerly loaded.
595
+	 *
596
+	 * @return array
597
+	 */
598
+	public function getEagerLoads()
599
+	{
600
+		return $this->eagerLoad;
601
+	}
602
+
603
+	/**
604
+	 * Set the relationships being eagerly loaded.
605
+	 *
606
+	 * @param  array $eagerLoad
607
+	 * @return void
608
+	 */
609
+	public function setEagerLoads(array $eagerLoad)
610
+	{
611
+		$this->eagerLoad = $eagerLoad;
612
+	}
613
+
614
+	/**
615
+	 * Eager load the relationships for the entities.
616
+	 *
617
+	 * @param  array $entities
618
+	 * @return array
619
+	 */
620
+	public function eagerLoadRelations($entities)
621
+	{
622
+		foreach ($this->eagerLoad as $name => $constraints) {
623
+			// For nested eager loads we'll skip loading them here and they will be set as an
624
+			// eager load on the query to retrieve the relation so that they will be eager
625
+			// loaded on that query, because that is where they get hydrated as models.
626
+			if (strpos($name, '.') === false) {
627
+				$entities = $this->loadRelation($entities, $name, $constraints);
628
+			}
629
+		}
630
+
631
+		return $entities;
632
+	}
633
+
634
+	/**
635
+	 * Eagerly load the relationship on a set of entities.
636
+	 *
637
+	 * @param  array    $entities
638
+	 * @param  string   $name
639
+	 * @param  \Closure $constraints
640
+	 * @return array
641
+	 */
642
+	protected function loadRelation(array $entities, $name, Closure $constraints)
643
+	{
644
+		// First we will "back up" the existing where conditions on the query so we can
645
+		// add our eager constraints. Then we will merge the wheres that were on the
646
+		// query back to it in order that any where conditions might be specified.
647
+		$relation = $this->getRelation($name);
648
+
649
+		$relation->addEagerConstraints($entities);
650
+
651
+		call_user_func($constraints, $relation);
652
+
653
+		$entities = $relation->initRelation($entities, $name);
654
+
655
+		// Once we have the results, we just match those back up to their parent models
656
+		// using the relationship instance. Then we just return the finished arrays
657
+		// of models which have been eagerly hydrated and are readied for return.
658
+
659
+		$results = $relation->getEager();
660
+
661
+		return $relation->match($entities, $results, $name);
662
+	}
663
+
664
+	/**
665
+	 * Get the relation instance for the given relation name.
666
+	 *
667
+	 * @param  string $relation
668
+	 * @return \Analogue\ORM\Relationships\Relationship
669
+	 */
670
+	public function getRelation($relation)
671
+	{
672
+		// We want to run a relationship query without any constrains so that we will
673
+		// not have to remove these where clauses manually which gets really hacky
674
+		// and is error prone while we remove the developer's own where clauses.
675
+		$query = Relationship::noConstraints(function() use ($relation) {
676
+			return $this->entityMap->$relation($this->getEntityInstance());
677
+		});
678
+
679
+		$nested = $this->nestedRelations($relation);
680
+
681
+		// If there are nested relationships set on the query, we will put those onto
682
+		// the query instances so that they can be handled after this relationship
683
+		// is loaded. In this way they will all trickle down as they are loaded.
684
+		if (count($nested) > 0) {
685
+			$query->getQuery()->with($nested);
686
+		}
687
+
688
+		return $query;
689
+	}
690
+
691
+	/**
692
+	 * Get the deeply nested relations for a given top-level relation.
693
+	 *
694
+	 * @param  string $relation
695
+	 * @return array
696
+	 */
697
+	protected function nestedRelations($relation)
698
+	{
699
+		$nested = [];
700
+
701
+		// We are basically looking for any relationships that are nested deeper than
702
+		// the given top-level relationship. We will just check for any relations
703
+		// that start with the given top relations and adds them to our arrays.
704
+		foreach ($this->eagerLoad as $name => $constraints) {
705
+			if ($this->isNested($name, $relation)) {
706
+				$nested[substr($name, strlen($relation . '.'))] = $constraints;
707
+			}
708
+		}
709
+
710
+		return $nested;
711
+	}
712
+
713
+	/**
714
+	 * Determine if the relationship is nested.
715
+	 *
716
+	 * @param  string $name
717
+	 * @param  string $relation
718
+	 * @return bool
719
+	 */
720
+	protected function isNested($name, $relation)
721
+	{
722
+		$dots = str_contains($name, '.');
723
+
724
+		return $dots && starts_with($name, $relation . '.');
725
+	}
726
+
727
+	/**
728
+	 * Add the Entity primary key if not in requested columns
729
+	 *
730
+	 * @param  array $columns
731
+	 * @return array
732
+	 */
733
+	protected function enforceIdColumn($columns)
734
+	{
735
+		if (!in_array($this->entityMap->getKeyName(), $columns)) {
736
+			$columns[] = $this->entityMap->getKeyName();
737
+		}
738
+		return $columns;
739
+	}
740
+
741
+	/**
742
+	 * Get the hydrated models without eager loading.
743
+	 *
744
+	 * @param  array  $columns
745
+	 * @return \Analogue\ORM\EntityCollection
746
+	 */
747
+	public function getEntities($columns = ['*'])
748
+	{
749
+		// As we need the primary key to feed the
750
+		// entity cache, we need it loaded on each
751
+		// request
752
+		$columns = $this->enforceIdColumn($columns);
753
+
754
+		// Run the query
755
+		$results = $this->query->get($columns);
756
+
757
+		$builder = new EntityBuilder($this->mapper, array_keys($this->getEagerLoads()));
758
+
759
+		return $builder->build($results);
760
+	}
761
+
762
+	/**
763
+	 * Get a new instance for the entity
764
+	 *
765
+	 * @param  array  $attributes
766
+	 * @return \Analogue\ORM\Entity
767
+	 */
768
+	public function getEntityInstance(array $attributes = [])
769
+	{
770
+		return $this->mapper->newInstance($attributes);
771
+	}
772
+
773
+	/**
774
+	 * Extend the builder with a given callback.
775
+	 *
776
+	 * @param  string   $name
777
+	 * @param  \Closure $callback
778
+	 * @return void
779
+	 */
780
+	public function macro($name, Closure $callback)
781
+	{
782
+		$this->macros[$name] = $callback;
783
+	}
784
+
785
+	/**
786
+	 * Get the given macro by name.
787
+	 *
788
+	 * @param  string $name
789
+	 * @return \Closure
790
+	 */
791
+	public function getMacro($name)
792
+	{
793
+		return array_get($this->macros, $name);
794
+	}
795
+
796
+	/**
797
+	 * Get a new query builder for the model's table.
798
+	 *
799
+	 * @return \Analogue\ORM\System\Query
800
+	 */
801
+	public function newQuery()
802
+	{
803
+		$builder = new Query($this->mapper, $this->adapter);
804
+
805
+		return $this->applyGlobalScopes($builder);
806
+	}
807
+
808
+	/**
809
+	 * Get a new query builder without any scope applied.
810
+	 *
811
+	 * @return \Analogue\ORM\System\Query
812
+	 */
813
+	public function newQueryWithoutScopes()
814
+	{
815
+		return new Query($this->mapper, $this->adapter);
816
+	}
817
+
818
+	/**
819
+	 * Get the Mapper instance for this Query Builder
820
+	 *
821
+	 * @return \Analogue\ORM\System\Mapper
822
+	 */
823
+	public function getMapper()
824
+	{
825
+		return $this->mapper;
826
+	}
827
+
828
+	/**
829
+	 * Get the underlying query adapter
830
+	 *
831
+	 * (REFACTOR: this method should move out, we need to provide the client classes
832
+	 * with the adapter instead.)
833
+	 *
834
+	 * @return \Analogue\ORM\Drivers\QueryAdapter|\Analogue\ORM\Drivers\IlluminateQueryAdapter
835
+	 */
836
+	public function getQuery()
837
+	{
838
+		return $this->query;
839
+	}
840
+
841
+	/**
842
+	 * Dynamically handle calls into the query instance.
843
+	 *
844
+	 * @param  string $method
845
+	 * @param  array  $parameters
846
+	 * @throws Exception
847
+	 * @return mixed
848
+	 */
849
+	public function __call($method, $parameters)
850
+	{
851
+		if (isset($this->macros[$method])) {
852
+			array_unshift($parameters, $this);
853
+
854
+			return call_user_func_array($this->macros[$method], $parameters);
855
+		}
856 856
         
857
-        if (in_array($method, $this->blacklist)) {
858
-            throw new Exception("Method $method doesn't exist");
859
-        }
857
+		if (in_array($method, $this->blacklist)) {
858
+			throw new Exception("Method $method doesn't exist");
859
+		}
860 860
 
861
-        $result = call_user_func_array([$this->query, $method], $parameters);
861
+		$result = call_user_func_array([$this->query, $method], $parameters);
862 862
 
863
-        return in_array($method, $this->passthru) ? $result : $this;
864
-    }
863
+		return in_array($method, $this->passthru) ? $result : $this;
864
+	}
865 865
 }
Please login to merge, or discard this patch.
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -466,7 +466,7 @@  discard block
 block discarded – undo
466 466
             $count = new Expression($count);
467 467
         }
468 468
 
469
-        return $this->where(new Expression('(' . $hasQuery->toSql() . ')'), $operator, $count, $boolean);
469
+        return $this->where(new Expression('('.$hasQuery->toSql().')'), $operator, $count, $boolean);
470 470
     }
471 471
 
472 472
     /**
@@ -499,7 +499,7 @@  discard block
 block discarded – undo
499 499
      */
500 500
     protected function getHasRelationQuery($relation, $entity)
501 501
     {
502
-        return Relationship::noConstraints(function () use ($relation, $entity) {
502
+        return Relationship::noConstraints(function() use ($relation, $entity) {
503 503
             return $this->entityMap->$relation($entity);
504 504
         });
505 505
     }
@@ -548,7 +548,7 @@  discard block
 block discarded – undo
548 548
             // constraints have been specified for the eager load and we'll just put
549 549
             // an empty Closure with the loader so that we can treat all the same.
550 550
             if (is_numeric($name)) {
551
-                $f = function () {};
551
+                $f = function() {};
552 552
 
553 553
                 list($name, $constraints) = [$constraints, $f];
554 554
             }
@@ -583,7 +583,7 @@  discard block
 block discarded – undo
583 583
             $progress[] = $segment;
584 584
 
585 585
             if (!isset($results[$last = implode('.', $progress)])) {
586
-                $results[$last] = function () {};
586
+                $results[$last] = function() {};
587 587
             }
588 588
         }
589 589
 
@@ -703,7 +703,7 @@  discard block
 block discarded – undo
703 703
         // that start with the given top relations and adds them to our arrays.
704 704
         foreach ($this->eagerLoad as $name => $constraints) {
705 705
             if ($this->isNested($name, $relation)) {
706
-                $nested[substr($name, strlen($relation . '.'))] = $constraints;
706
+                $nested[substr($name, strlen($relation.'.'))] = $constraints;
707 707
             }
708 708
         }
709 709
 
@@ -721,7 +721,7 @@  discard block
 block discarded – undo
721 721
     {
722 722
         $dots = str_contains($name, '.');
723 723
 
724
-        return $dots && starts_with($name, $relation . '.');
724
+        return $dots && starts_with($name, $relation.'.');
725 725
     }
726 726
 
727 727
     /**
Please login to merge, or discard this patch.
src/System/Mapper.php 2 patches
Indentation   +556 added lines, -556 removed lines patch added patch discarded remove patch
@@ -22,563 +22,563 @@
 block discarded – undo
22 22
  */
23 23
 class Mapper
24 24
 {
25
-    /**
26
-     * The Manager instance
27
-     *
28
-     * @var \Analogue\ORM\System\Manager
29
-     */
30
-    protected $manager;
31
-
32
-    /**
33
-     * Instance of EntityMapper Object
34
-     *
35
-     * @var \Analogue\ORM\EntityMap
36
-     */
37
-    protected $entityMap;
38
-
39
-    /**
40
-     * The instance of db adapter
41
-     *
42
-     * @var \Analogue\ORM\Drivers\DBAdapter
43
-     */
44
-    protected $adapter;
45
-
46
-
47
-    /**
48
-     * Event dispatcher instance
49
-     *
50
-     * @var \Illuminate\Contracts\Events\Dispatcher
51
-     */
52
-    protected $dispatcher;
53
-
54
-    /**
55
-     * Entity Cache
56
-     *
57
-     * @var  \Analogue\ORM\System\EntityCache
58
-     */
59
-    protected $cache;
60
-
61
-    /**
62
-     * Global scopes
63
-     *
64
-     * @var array
65
-     */
66
-    protected $globalScopes = [];
67
-
68
-    /**
69
-     * Custom Commands
70
-     *
71
-     * @var array
72
-     */
73
-    protected $customCommands = [];
74
-
75
-    /**
76
-     * @param EntityMap  $entityMap
77
-     * @param DBAdapter  $adapter
78
-     * @param Dispatcher $dispatcher
79
-     * @param Manager    $manager
80
-     */
81
-    public function __construct(EntityMap $entityMap, DBAdapter $adapter, Dispatcher $dispatcher, Manager $manager)
82
-    {
83
-        $this->entityMap = $entityMap;
84
-
85
-        $this->adapter = $adapter;
86
-
87
-        $this->dispatcher = $dispatcher;
88
-
89
-        $this->manager = $manager;
90
-
91
-        $this->cache = new EntityCache($entityMap);
92
-    }
93
-
94
-    /**
95
-     * Persist an entity or an entity collection into the database
96
-     *
97
-     * @param  Mappable|Collection $entity
98
-     * @throws \InvalidArgumentException
99
-     * @throws MappingException
100
-     * @return Mappable|Collection
101
-     */
102
-    public function store($entity)
103
-    {
104
-        if ($this->isArrayOrCollection($entity)) {
105
-            return $this->storeCollection($entity);
106
-        } else {
107
-            return $this->storeEntity($entity);
108
-        }
109
-    }
110
-
111
-    /**
112
-     * Return true if an object is an array or collection
113
-     *
114
-     * @param  mixed $argument
115
-     * @return boolean
116
-     */
117
-    protected function isArrayOrCollection($argument)
118
-    {
119
-        return $argument instanceof Collection || is_array($argument);
120
-    }
121
-
122
-    /**
123
-     * Store a single entity into the database
124
-     *
125
-     * @param  Mappable $entity
126
-     * @throws \InvalidArgumentException
127
-     * @throws MappingException
128
-     * @return \Analogue\ORM\Entity
129
-     */
130
-    protected function storeEntity($entity)
131
-    {
132
-        $this->checkEntityType($entity);
133
-
134
-        $store = new Store($this->aggregate($entity), $this->newQueryBuilder());
135
-
136
-        return $store->execute();
137
-    }
138
-
139
-    /**
140
-     * Convert an entity into an aggregate root
141
-     *
142
-     * @param  mixed $entity
143
-     * @throws MappingException
144
-     * @return \Analogue\ORM\System\Aggregate
145
-     */
146
-    protected function aggregate($entity)
147
-    {
148
-        return new Aggregate($entity);
149
-    }
150
-
151
-    /**
152
-     * Store an entity collection inside a single DB Transaction
153
-     *
154
-     * @param  Collection|array $entities
155
-     * @throws \InvalidArgumentException
156
-     * @throws MappingException
157
-     * @return Collection
158
-     */
159
-    protected function storeCollection($entities)
160
-    {
161
-        $this->adapter->beginTransaction();
162
-
163
-        foreach ($entities as $entity) {
164
-            $this->storeEntity($entity);
165
-        }
166
-
167
-        $this->adapter->commit();
168
-
169
-        return $entities;
170
-    }
171
-
172
-    /**
173
-     * Delete an entity or an entity collection from the database
174
-     *
175
-     * @param  mixed|Collection
176
-     * @throws MappingException
177
-     * @throws \InvalidArgumentException
178
-     * @return Collection|void
179
-     */
180
-    public function delete($entity)
181
-    {
182
-        if ($this->isArrayOrCollection($entity)) {
183
-            return $this->deleteCollection($entity);
184
-        } else {
185
-            $this->deleteEntity($entity);
186
-        }
187
-    }
188
-
189
-    /**
190
-     * Delete a single entity from the database.
191
-     *
192
-     * @param  Mappable $entity
193
-     * @throws \InvalidArgumentException
194
-     * @throws MappingException
195
-     * @return void
196
-     */
197
-    protected function deleteEntity($entity)
198
-    {
199
-        $this->checkEntityType($entity);
200
-
201
-        $delete = new Delete($this->aggregate($entity), $this->newQueryBuilder());
202
-
203
-        $delete->execute();
204
-    }
205
-
206
-    /**
207
-     * Delete an Entity Collection inside a single db transaction
208
-     *
209
-     * @param  Collection|array $entities
210
-     * @throws \InvalidArgumentException
211
-     * @throws MappingException
212
-     * @return Collection
213
-     */
214
-    protected function deleteCollection($entities)
215
-    {
216
-        $this->adapter->beginTransaction();
217
-
218
-        foreach ($entities as $entity) {
219
-            $this->deleteEntity($entity);
220
-        }
221
-
222
-        $this->adapter->commit();
25
+	/**
26
+	 * The Manager instance
27
+	 *
28
+	 * @var \Analogue\ORM\System\Manager
29
+	 */
30
+	protected $manager;
31
+
32
+	/**
33
+	 * Instance of EntityMapper Object
34
+	 *
35
+	 * @var \Analogue\ORM\EntityMap
36
+	 */
37
+	protected $entityMap;
38
+
39
+	/**
40
+	 * The instance of db adapter
41
+	 *
42
+	 * @var \Analogue\ORM\Drivers\DBAdapter
43
+	 */
44
+	protected $adapter;
45
+
46
+
47
+	/**
48
+	 * Event dispatcher instance
49
+	 *
50
+	 * @var \Illuminate\Contracts\Events\Dispatcher
51
+	 */
52
+	protected $dispatcher;
53
+
54
+	/**
55
+	 * Entity Cache
56
+	 *
57
+	 * @var  \Analogue\ORM\System\EntityCache
58
+	 */
59
+	protected $cache;
60
+
61
+	/**
62
+	 * Global scopes
63
+	 *
64
+	 * @var array
65
+	 */
66
+	protected $globalScopes = [];
67
+
68
+	/**
69
+	 * Custom Commands
70
+	 *
71
+	 * @var array
72
+	 */
73
+	protected $customCommands = [];
74
+
75
+	/**
76
+	 * @param EntityMap  $entityMap
77
+	 * @param DBAdapter  $adapter
78
+	 * @param Dispatcher $dispatcher
79
+	 * @param Manager    $manager
80
+	 */
81
+	public function __construct(EntityMap $entityMap, DBAdapter $adapter, Dispatcher $dispatcher, Manager $manager)
82
+	{
83
+		$this->entityMap = $entityMap;
84
+
85
+		$this->adapter = $adapter;
86
+
87
+		$this->dispatcher = $dispatcher;
88
+
89
+		$this->manager = $manager;
90
+
91
+		$this->cache = new EntityCache($entityMap);
92
+	}
93
+
94
+	/**
95
+	 * Persist an entity or an entity collection into the database
96
+	 *
97
+	 * @param  Mappable|Collection $entity
98
+	 * @throws \InvalidArgumentException
99
+	 * @throws MappingException
100
+	 * @return Mappable|Collection
101
+	 */
102
+	public function store($entity)
103
+	{
104
+		if ($this->isArrayOrCollection($entity)) {
105
+			return $this->storeCollection($entity);
106
+		} else {
107
+			return $this->storeEntity($entity);
108
+		}
109
+	}
110
+
111
+	/**
112
+	 * Return true if an object is an array or collection
113
+	 *
114
+	 * @param  mixed $argument
115
+	 * @return boolean
116
+	 */
117
+	protected function isArrayOrCollection($argument)
118
+	{
119
+		return $argument instanceof Collection || is_array($argument);
120
+	}
121
+
122
+	/**
123
+	 * Store a single entity into the database
124
+	 *
125
+	 * @param  Mappable $entity
126
+	 * @throws \InvalidArgumentException
127
+	 * @throws MappingException
128
+	 * @return \Analogue\ORM\Entity
129
+	 */
130
+	protected function storeEntity($entity)
131
+	{
132
+		$this->checkEntityType($entity);
133
+
134
+		$store = new Store($this->aggregate($entity), $this->newQueryBuilder());
135
+
136
+		return $store->execute();
137
+	}
138
+
139
+	/**
140
+	 * Convert an entity into an aggregate root
141
+	 *
142
+	 * @param  mixed $entity
143
+	 * @throws MappingException
144
+	 * @return \Analogue\ORM\System\Aggregate
145
+	 */
146
+	protected function aggregate($entity)
147
+	{
148
+		return new Aggregate($entity);
149
+	}
150
+
151
+	/**
152
+	 * Store an entity collection inside a single DB Transaction
153
+	 *
154
+	 * @param  Collection|array $entities
155
+	 * @throws \InvalidArgumentException
156
+	 * @throws MappingException
157
+	 * @return Collection
158
+	 */
159
+	protected function storeCollection($entities)
160
+	{
161
+		$this->adapter->beginTransaction();
162
+
163
+		foreach ($entities as $entity) {
164
+			$this->storeEntity($entity);
165
+		}
166
+
167
+		$this->adapter->commit();
168
+
169
+		return $entities;
170
+	}
171
+
172
+	/**
173
+	 * Delete an entity or an entity collection from the database
174
+	 *
175
+	 * @param  mixed|Collection
176
+	 * @throws MappingException
177
+	 * @throws \InvalidArgumentException
178
+	 * @return Collection|void
179
+	 */
180
+	public function delete($entity)
181
+	{
182
+		if ($this->isArrayOrCollection($entity)) {
183
+			return $this->deleteCollection($entity);
184
+		} else {
185
+			$this->deleteEntity($entity);
186
+		}
187
+	}
188
+
189
+	/**
190
+	 * Delete a single entity from the database.
191
+	 *
192
+	 * @param  Mappable $entity
193
+	 * @throws \InvalidArgumentException
194
+	 * @throws MappingException
195
+	 * @return void
196
+	 */
197
+	protected function deleteEntity($entity)
198
+	{
199
+		$this->checkEntityType($entity);
200
+
201
+		$delete = new Delete($this->aggregate($entity), $this->newQueryBuilder());
202
+
203
+		$delete->execute();
204
+	}
205
+
206
+	/**
207
+	 * Delete an Entity Collection inside a single db transaction
208
+	 *
209
+	 * @param  Collection|array $entities
210
+	 * @throws \InvalidArgumentException
211
+	 * @throws MappingException
212
+	 * @return Collection
213
+	 */
214
+	protected function deleteCollection($entities)
215
+	{
216
+		$this->adapter->beginTransaction();
217
+
218
+		foreach ($entities as $entity) {
219
+			$this->deleteEntity($entity);
220
+		}
221
+
222
+		$this->adapter->commit();
223 223
         
224
-        return $entities;
225
-    }
226
-
227
-    /**
228
-     * Return the entity map for this mapper
229
-     *
230
-     * @return EntityMap
231
-     */
232
-    public function getEntityMap()
233
-    {
234
-        return $this->entityMap;
235
-    }
236
-
237
-    /**
238
-     * Get the entity cache for the current mapper
239
-     *
240
-     * @return EntityCache  $entityCache
241
-     */
242
-    public function getEntityCache()
243
-    {
244
-        return $this->cache;
245
-    }
246
-
247
-    /**
248
-     * Fire the given event for the entity
249
-     *
250
-     * @param  string               $event
251
-     * @param  \Analogue\ORM\Entity $entity
252
-     * @param  bool                 $halt
253
-     * @throws InvalidArgumentException
254
-     * @return mixed
255
-     */
256
-    public function fireEvent($event, $entity, $halt = true)
257
-    {
258
-        if ($entity instanceof Wrapper) {
259
-            throw new InvalidArgumentException('Fired Event with invalid Entity Object');
260
-        }
261
-
262
-        $event = "analogue.{$event}." . $this->entityMap->getClass();
263
-
264
-        $method = $halt ? 'until' : 'fire';
265
-
266
-        return $this->dispatcher->$method($event, $entity);
267
-    }
268
-
269
-    /**
270
-     * Register an entity event with the dispatcher.
271
-     *
272
-     * @param  string   $event
273
-     * @param  \Closure $callback
274
-     * @return void
275
-     */
276
-    public function registerEvent($event, $callback)
277
-    {
278
-        $name = $this->entityMap->getClass();
279
-
280
-        $this->dispatcher->listen("analogue.{$event}.{$name}", $callback);
281
-    }
282
-
283
-    /**
284
-     * Add a global scope to this mapper query builder
285
-     *
286
-     * @param  ScopeInterface $scope
287
-     * @return void
288
-     */
289
-    public function addGlobalScope(ScopeInterface $scope)
290
-    {
291
-        $this->globalScopes[get_class($scope)] = $scope;
292
-    }
293
-
294
-    /**
295
-     * Determine if the mapper has a global scope.
296
-     *
297
-     * @param  \Analogue\ORM\System\ScopeInterface $scope
298
-     * @return bool
299
-     */
300
-    public function hasGlobalScope($scope)
301
-    {
302
-        return !is_null($this->getGlobalScope($scope));
303
-    }
304
-
305
-    /**
306
-     * Get a global scope registered with the modal.
307
-     *
308
-     * @param  \Analogue\ORM\System\ScopeInterface $scope
309
-     * @return \Analogue\ORM\System\ScopeInterface|null
310
-     */
311
-    public function getGlobalScope($scope)
312
-    {
313
-        return array_first($this->globalScopes, function($key, $value) use ($scope) {
314
-            return $scope instanceof $value;
315
-        });
316
-    }
317
-
318
-    /**
319
-     * Get the global scopes for this class instance.
320
-     *
321
-     * @return \Analogue\ORM\System\ScopeInterface
322
-     */
323
-    public function getGlobalScopes()
324
-    {
325
-        return $this->globalScopes;
326
-    }
327
-
328
-    /**
329
-     * Apply all of the global scopes to an Analogue Query builder.
330
-     *
331
-     * @param Query $query
332
-     * @return \Analogue\ORM\System\Query
333
-     */
334
-    public function applyGlobalScopes($query)
335
-    {
336
-        foreach ($this->getGlobalScopes() as $scope) {
337
-            $scope->apply($query, $this);
338
-        }
339
-
340
-        return $query;
341
-    }
342
-
343
-    /**
344
-     * Remove all of the global scopes from an Analogue Query builder.
345
-     *
346
-     * @param Query $query
347
-     * @return \Analogue\ORM\System\Query
348
-     */
349
-    public function removeGlobalScopes($query)
350
-    {
351
-        foreach ($this->getGlobalScopes() as $scope) {
352
-            $scope->remove($query, $this);
353
-        }
354
-
355
-        return $query;
356
-    }
357
-
358
-    /**
359
-     * Get a new query instance without a given scope.
360
-     *
361
-     * @param  \Analogue\ORM\System\ScopeInterface $scope
362
-     * @return \Analogue\ORM\System\Query
363
-     */
364
-    public function newQueryWithoutScope($scope)
365
-    {
366
-        $this->getGlobalScope($scope)->remove($query = $this->getQuery(), $this);
367
-
368
-        return $query;
369
-    }
370
-
371
-    /**
372
-     * Get a new query builder that doesn't have any global scopes.
373
-     *
374
-     * @return Query
375
-     */
376
-    public function newQueryWithoutScopes()
377
-    {
378
-        return $this->removeGlobalScopes($this->getQuery());
379
-    }
380
-
381
-    /**
382
-     * Add a dynamic method that extends the mapper/repository
383
-     *
384
-     * @param string $command
385
-     */
386
-    public function addCustomCommand($command)
387
-    {
388
-        $name = lcfirst(class_basename($command));
389
-
390
-        $this->customCommands[$name] = $command;
391
-    }
392
-
393
-    /**
394
-     * Execute a custom command on an Entity
395
-     *
396
-     * @param  string                 $command
397
-     * @param  mixed|Collection|array $entity
398
-     * @throws \InvalidArgumentException
399
-     * @throws MappingException
400
-     * @return mixed
401
-     */
402
-    public function executeCustomCommand($command, $entity)
403
-    {
404
-        $commandClass = $this->customCommands[$command];
405
-
406
-        if ($this->isArrayOrCollection($entity)) {
407
-            foreach ($entity as $instance) {
408
-                $this->executeSingleCustomCommand($commandClass, $instance);
409
-            }
410
-        } else {
411
-            return $this->executeSingleCustomCommand($commandClass, $entity);
412
-        }
413
-    }
414
-
415
-    /**
416
-     * Execute a single command instance
417
-     *
418
-     * @param  string $commandClass
419
-     * @param  mixed  $entity
420
-     * @throws \InvalidArgumentException
421
-     * @throws MappingException
422
-     * @return mixed
423
-     */
424
-    protected function executeSingleCustomCommand($commandClass, $entity)
425
-    {
426
-        $this->checkEntityType($entity);
427
-
428
-        $instance = new $commandClass($this->aggregate($entity), $this->newQueryBuilder());
429
-
430
-        return $instance->execute();
431
-    }
432
-
433
-    /**
434
-     * Check that the entity correspond to the current mapper.
435
-     *
436
-     * @param  mixed $entity
437
-     * @throws InvalidArgumentException
438
-     * @return void
439
-     */
440
-    protected function checkEntityType($entity)
441
-    {
442
-        if (get_class($entity) != $this->entityMap->getClass()) {
443
-            $expected = $this->entityMap->getClass();
444
-            $actual = get_class($entity);
445
-            throw new InvalidArgumentException("Expected : $expected, got $actual.");
446
-        }
447
-    }
448
-
449
-    /**
450
-     * Get all the custom commands registered on this mapper
451
-     *
452
-     * @return array
453
-     */
454
-    public function getCustomCommands()
455
-    {
456
-        return array_keys($this->customCommands);
457
-    }
458
-
459
-    /**
460
-     * Check if this mapper supports this command
461
-     * @param  string $command
462
-     * @return boolean
463
-     */
464
-    public function hasCustomCommand($command)
465
-    {
466
-        return in_array($command, $this->getCustomCommands());
467
-    }
468
-
469
-    /**
470
-     * Create a new instance of the mapped entity class
471
-     *
472
-     * @param  array $attributes
473
-     * @return mixed
474
-     */
475
-    public function newInstance($attributes = [])
476
-    {
477
-        $class = $this->entityMap->getClass();
478
-
479
-        if ($this->entityMap->activator() != null) {
480
-            $entity = $this->entityMap->activator();
481
-        } else {
482
-            $entity = $this->customClassInstance($class);
483
-        }
484
-
485
-        // prevent hydrating with an empty array
486
-        if (count($attributes) > 0) {
487
-            $entity->setEntityAttributes($attributes);
488
-        }
489
-
490
-        return $entity;
491
-    }
492
-
493
-    /**
494
-     * Use a trick to generate a class prototype that we
495
-     * can instantiate without calling the constructor.
496
-     *
497
-     * @param string|null $className
498
-     * @throws MappingException
499
-     * @return mixed
500
-     */
501
-    protected function customClassInstance($className)
502
-    {
503
-        if (!class_exists($className)) {
504
-            throw new MappingException("Tried to instantiate a non-existing Entity class : $className");
505
-        }
506
-
507
-        $prototype = unserialize(sprintf('O:%d:"%s":0:{}', strlen($className), $className));
508
-        return $prototype;
509
-    }
224
+		return $entities;
225
+	}
226
+
227
+	/**
228
+	 * Return the entity map for this mapper
229
+	 *
230
+	 * @return EntityMap
231
+	 */
232
+	public function getEntityMap()
233
+	{
234
+		return $this->entityMap;
235
+	}
236
+
237
+	/**
238
+	 * Get the entity cache for the current mapper
239
+	 *
240
+	 * @return EntityCache  $entityCache
241
+	 */
242
+	public function getEntityCache()
243
+	{
244
+		return $this->cache;
245
+	}
246
+
247
+	/**
248
+	 * Fire the given event for the entity
249
+	 *
250
+	 * @param  string               $event
251
+	 * @param  \Analogue\ORM\Entity $entity
252
+	 * @param  bool                 $halt
253
+	 * @throws InvalidArgumentException
254
+	 * @return mixed
255
+	 */
256
+	public function fireEvent($event, $entity, $halt = true)
257
+	{
258
+		if ($entity instanceof Wrapper) {
259
+			throw new InvalidArgumentException('Fired Event with invalid Entity Object');
260
+		}
261
+
262
+		$event = "analogue.{$event}." . $this->entityMap->getClass();
263
+
264
+		$method = $halt ? 'until' : 'fire';
265
+
266
+		return $this->dispatcher->$method($event, $entity);
267
+	}
268
+
269
+	/**
270
+	 * Register an entity event with the dispatcher.
271
+	 *
272
+	 * @param  string   $event
273
+	 * @param  \Closure $callback
274
+	 * @return void
275
+	 */
276
+	public function registerEvent($event, $callback)
277
+	{
278
+		$name = $this->entityMap->getClass();
279
+
280
+		$this->dispatcher->listen("analogue.{$event}.{$name}", $callback);
281
+	}
282
+
283
+	/**
284
+	 * Add a global scope to this mapper query builder
285
+	 *
286
+	 * @param  ScopeInterface $scope
287
+	 * @return void
288
+	 */
289
+	public function addGlobalScope(ScopeInterface $scope)
290
+	{
291
+		$this->globalScopes[get_class($scope)] = $scope;
292
+	}
293
+
294
+	/**
295
+	 * Determine if the mapper has a global scope.
296
+	 *
297
+	 * @param  \Analogue\ORM\System\ScopeInterface $scope
298
+	 * @return bool
299
+	 */
300
+	public function hasGlobalScope($scope)
301
+	{
302
+		return !is_null($this->getGlobalScope($scope));
303
+	}
304
+
305
+	/**
306
+	 * Get a global scope registered with the modal.
307
+	 *
308
+	 * @param  \Analogue\ORM\System\ScopeInterface $scope
309
+	 * @return \Analogue\ORM\System\ScopeInterface|null
310
+	 */
311
+	public function getGlobalScope($scope)
312
+	{
313
+		return array_first($this->globalScopes, function($key, $value) use ($scope) {
314
+			return $scope instanceof $value;
315
+		});
316
+	}
317
+
318
+	/**
319
+	 * Get the global scopes for this class instance.
320
+	 *
321
+	 * @return \Analogue\ORM\System\ScopeInterface
322
+	 */
323
+	public function getGlobalScopes()
324
+	{
325
+		return $this->globalScopes;
326
+	}
327
+
328
+	/**
329
+	 * Apply all of the global scopes to an Analogue Query builder.
330
+	 *
331
+	 * @param Query $query
332
+	 * @return \Analogue\ORM\System\Query
333
+	 */
334
+	public function applyGlobalScopes($query)
335
+	{
336
+		foreach ($this->getGlobalScopes() as $scope) {
337
+			$scope->apply($query, $this);
338
+		}
339
+
340
+		return $query;
341
+	}
342
+
343
+	/**
344
+	 * Remove all of the global scopes from an Analogue Query builder.
345
+	 *
346
+	 * @param Query $query
347
+	 * @return \Analogue\ORM\System\Query
348
+	 */
349
+	public function removeGlobalScopes($query)
350
+	{
351
+		foreach ($this->getGlobalScopes() as $scope) {
352
+			$scope->remove($query, $this);
353
+		}
354
+
355
+		return $query;
356
+	}
357
+
358
+	/**
359
+	 * Get a new query instance without a given scope.
360
+	 *
361
+	 * @param  \Analogue\ORM\System\ScopeInterface $scope
362
+	 * @return \Analogue\ORM\System\Query
363
+	 */
364
+	public function newQueryWithoutScope($scope)
365
+	{
366
+		$this->getGlobalScope($scope)->remove($query = $this->getQuery(), $this);
367
+
368
+		return $query;
369
+	}
370
+
371
+	/**
372
+	 * Get a new query builder that doesn't have any global scopes.
373
+	 *
374
+	 * @return Query
375
+	 */
376
+	public function newQueryWithoutScopes()
377
+	{
378
+		return $this->removeGlobalScopes($this->getQuery());
379
+	}
380
+
381
+	/**
382
+	 * Add a dynamic method that extends the mapper/repository
383
+	 *
384
+	 * @param string $command
385
+	 */
386
+	public function addCustomCommand($command)
387
+	{
388
+		$name = lcfirst(class_basename($command));
389
+
390
+		$this->customCommands[$name] = $command;
391
+	}
392
+
393
+	/**
394
+	 * Execute a custom command on an Entity
395
+	 *
396
+	 * @param  string                 $command
397
+	 * @param  mixed|Collection|array $entity
398
+	 * @throws \InvalidArgumentException
399
+	 * @throws MappingException
400
+	 * @return mixed
401
+	 */
402
+	public function executeCustomCommand($command, $entity)
403
+	{
404
+		$commandClass = $this->customCommands[$command];
405
+
406
+		if ($this->isArrayOrCollection($entity)) {
407
+			foreach ($entity as $instance) {
408
+				$this->executeSingleCustomCommand($commandClass, $instance);
409
+			}
410
+		} else {
411
+			return $this->executeSingleCustomCommand($commandClass, $entity);
412
+		}
413
+	}
414
+
415
+	/**
416
+	 * Execute a single command instance
417
+	 *
418
+	 * @param  string $commandClass
419
+	 * @param  mixed  $entity
420
+	 * @throws \InvalidArgumentException
421
+	 * @throws MappingException
422
+	 * @return mixed
423
+	 */
424
+	protected function executeSingleCustomCommand($commandClass, $entity)
425
+	{
426
+		$this->checkEntityType($entity);
427
+
428
+		$instance = new $commandClass($this->aggregate($entity), $this->newQueryBuilder());
429
+
430
+		return $instance->execute();
431
+	}
432
+
433
+	/**
434
+	 * Check that the entity correspond to the current mapper.
435
+	 *
436
+	 * @param  mixed $entity
437
+	 * @throws InvalidArgumentException
438
+	 * @return void
439
+	 */
440
+	protected function checkEntityType($entity)
441
+	{
442
+		if (get_class($entity) != $this->entityMap->getClass()) {
443
+			$expected = $this->entityMap->getClass();
444
+			$actual = get_class($entity);
445
+			throw new InvalidArgumentException("Expected : $expected, got $actual.");
446
+		}
447
+	}
448
+
449
+	/**
450
+	 * Get all the custom commands registered on this mapper
451
+	 *
452
+	 * @return array
453
+	 */
454
+	public function getCustomCommands()
455
+	{
456
+		return array_keys($this->customCommands);
457
+	}
458
+
459
+	/**
460
+	 * Check if this mapper supports this command
461
+	 * @param  string $command
462
+	 * @return boolean
463
+	 */
464
+	public function hasCustomCommand($command)
465
+	{
466
+		return in_array($command, $this->getCustomCommands());
467
+	}
468
+
469
+	/**
470
+	 * Create a new instance of the mapped entity class
471
+	 *
472
+	 * @param  array $attributes
473
+	 * @return mixed
474
+	 */
475
+	public function newInstance($attributes = [])
476
+	{
477
+		$class = $this->entityMap->getClass();
478
+
479
+		if ($this->entityMap->activator() != null) {
480
+			$entity = $this->entityMap->activator();
481
+		} else {
482
+			$entity = $this->customClassInstance($class);
483
+		}
484
+
485
+		// prevent hydrating with an empty array
486
+		if (count($attributes) > 0) {
487
+			$entity->setEntityAttributes($attributes);
488
+		}
489
+
490
+		return $entity;
491
+	}
492
+
493
+	/**
494
+	 * Use a trick to generate a class prototype that we
495
+	 * can instantiate without calling the constructor.
496
+	 *
497
+	 * @param string|null $className
498
+	 * @throws MappingException
499
+	 * @return mixed
500
+	 */
501
+	protected function customClassInstance($className)
502
+	{
503
+		if (!class_exists($className)) {
504
+			throw new MappingException("Tried to instantiate a non-existing Entity class : $className");
505
+		}
506
+
507
+		$prototype = unserialize(sprintf('O:%d:"%s":0:{}', strlen($className), $className));
508
+		return $prototype;
509
+	}
510 510
     
511
-    /**
512
-     * Get the Analogue Query Builder for this instance
513
-     *
514
-     * @return \Analogue\ORM\System\Query
515
-     */
516
-    public function getQuery()
517
-    {
518
-        $query = new Query($this, $this->adapter);
519
-
520
-        return $this->applyGlobalScopes($query);
521
-    }
511
+	/**
512
+	 * Get the Analogue Query Builder for this instance
513
+	 *
514
+	 * @return \Analogue\ORM\System\Query
515
+	 */
516
+	public function getQuery()
517
+	{
518
+		$query = new Query($this, $this->adapter);
519
+
520
+		return $this->applyGlobalScopes($query);
521
+	}
522 522
     
523
-    /**
524
-     * Get the Analogue Query Builder for this instance
525
-     *
526
-     * @return \Analogue\ORM\System\Query
527
-     */
528
-    public function query()
529
-    {
530
-        return $this->getQuery();
531
-    }
532
-
533
-    /**
534
-     * Get an unscoped Analogue Query Builder for this instance
535
-     *
536
-     * @return \Analogue\ORM\System\Query
537
-     */
538
-    public function globalQuery()
539
-    {
540
-        return $this->newQueryWithoutScopes();
541
-    }
542
-
543
-    /**
544
-     * Get a the Underlying QueryAdapter.
545
-     *
546
-     * @return \Analogue\ORM\Drivers\QueryAdapter
547
-     */
548
-    public function newQueryBuilder()
549
-    {
550
-        return $this->adapter->getQuery();
551
-    }
552
-
553
-    /**
554
-     * Return the manager instance
555
-     *
556
-     * @return \Analogue\ORM\System\Manager
557
-     */
558
-    public function getManager()
559
-    {
560
-        return $this->manager;
561
-    }
562
-
563
-    /**
564
-     * Dynamically handle calls to custom commands, or Redirects to query()
565
-     *
566
-     * @param  string $method
567
-     * @param  array  $parameters
568
-     * @throws \Exception
569
-     * @return mixed
570
-     */
571
-    public function __call($method, $parameters)
572
-    {
573
-        // Check if method is a custom command on the mapper
574
-        if ($this->hasCustomCommand($method)) {
575
-            if (count($parameters) == 0) {
576
-                throw new \Exception("$method must at least have 1 argument");
577
-            }
578
-            return $this->executeCustomCommand($method, $parameters[0]);
579
-        }
580
-
581
-        // Redirect call on a new query instance
582
-        return call_user_func_array([$this->query(), $method], $parameters);
583
-    }
523
+	/**
524
+	 * Get the Analogue Query Builder for this instance
525
+	 *
526
+	 * @return \Analogue\ORM\System\Query
527
+	 */
528
+	public function query()
529
+	{
530
+		return $this->getQuery();
531
+	}
532
+
533
+	/**
534
+	 * Get an unscoped Analogue Query Builder for this instance
535
+	 *
536
+	 * @return \Analogue\ORM\System\Query
537
+	 */
538
+	public function globalQuery()
539
+	{
540
+		return $this->newQueryWithoutScopes();
541
+	}
542
+
543
+	/**
544
+	 * Get a the Underlying QueryAdapter.
545
+	 *
546
+	 * @return \Analogue\ORM\Drivers\QueryAdapter
547
+	 */
548
+	public function newQueryBuilder()
549
+	{
550
+		return $this->adapter->getQuery();
551
+	}
552
+
553
+	/**
554
+	 * Return the manager instance
555
+	 *
556
+	 * @return \Analogue\ORM\System\Manager
557
+	 */
558
+	public function getManager()
559
+	{
560
+		return $this->manager;
561
+	}
562
+
563
+	/**
564
+	 * Dynamically handle calls to custom commands, or Redirects to query()
565
+	 *
566
+	 * @param  string $method
567
+	 * @param  array  $parameters
568
+	 * @throws \Exception
569
+	 * @return mixed
570
+	 */
571
+	public function __call($method, $parameters)
572
+	{
573
+		// Check if method is a custom command on the mapper
574
+		if ($this->hasCustomCommand($method)) {
575
+			if (count($parameters) == 0) {
576
+				throw new \Exception("$method must at least have 1 argument");
577
+			}
578
+			return $this->executeCustomCommand($method, $parameters[0]);
579
+		}
580
+
581
+		// Redirect call on a new query instance
582
+		return call_user_func_array([$this->query(), $method], $parameters);
583
+	}
584 584
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -259,7 +259,7 @@
 block discarded – undo
259 259
             throw new InvalidArgumentException('Fired Event with invalid Entity Object');
260 260
         }
261 261
 
262
-        $event = "analogue.{$event}." . $this->entityMap->getClass();
262
+        $event = "analogue.{$event}.".$this->entityMap->getClass();
263 263
 
264 264
         $method = $halt ? 'until' : 'fire';
265 265
 
Please login to merge, or discard this patch.
src/System/MapperFactory.php 1 patch
Indentation   +52 added lines, -52 removed lines patch added patch discarded remove patch
@@ -13,70 +13,70 @@
 block discarded – undo
13 13
  */
14 14
 class MapperFactory
15 15
 {
16
-    /**
17
-     * Manager instance
18
-     *
19
-     * @var \Analogue\ORM\System\Manager
20
-     */
21
-    protected $manager;
16
+	/**
17
+	 * Manager instance
18
+	 *
19
+	 * @var \Analogue\ORM\System\Manager
20
+	 */
21
+	protected $manager;
22 22
 
23
-    /**
24
-     * DriverManager instance
25
-     *
26
-     * @var \Analogue\ORM\Drivers\Manager
27
-     */
28
-    protected $drivers;
23
+	/**
24
+	 * DriverManager instance
25
+	 *
26
+	 * @var \Analogue\ORM\Drivers\Manager
27
+	 */
28
+	protected $drivers;
29 29
 
30
-    /**
31
-     * Event dispatcher instance
32
-     *
33
-     * @var \Illuminate\Contracts\Events\Dispatcher
34
-     */
35
-    protected $dispatcher;
30
+	/**
31
+	 * Event dispatcher instance
32
+	 *
33
+	 * @var \Illuminate\Contracts\Events\Dispatcher
34
+	 */
35
+	protected $dispatcher;
36 36
 
37
-    /**
38
-     * MapperFactory constructor.
39
-     * @param DriverManager $drivers
40
-     * @param Dispatcher    $dispatcher
41
-     * @param Manager       $manager
42
-     */
43
-    public function __construct(DriverManager $drivers, Dispatcher $dispatcher, Manager $manager)
44
-    {
45
-        $this->drivers = $drivers;
37
+	/**
38
+	 * MapperFactory constructor.
39
+	 * @param DriverManager $drivers
40
+	 * @param Dispatcher    $dispatcher
41
+	 * @param Manager       $manager
42
+	 */
43
+	public function __construct(DriverManager $drivers, Dispatcher $dispatcher, Manager $manager)
44
+	{
45
+		$this->drivers = $drivers;
46 46
 
47
-        $this->dispatcher = $dispatcher;
47
+		$this->dispatcher = $dispatcher;
48 48
 
49
-        $this->manager = $manager;
50
-    }
49
+		$this->manager = $manager;
50
+	}
51 51
 
52
-    /**
53
-     * Return a new Mapper instance
54
-     *
55
-     * @param  string    $entityClass
56
-     * @param  EntityMap $entityMap
57
-     * @return Mapper
58
-     */
59
-    public function make($entityClass, EntityMap $entityMap)
60
-    {
61
-        $driver = $entityMap->getDriver();
52
+	/**
53
+	 * Return a new Mapper instance
54
+	 *
55
+	 * @param  string    $entityClass
56
+	 * @param  EntityMap $entityMap
57
+	 * @return Mapper
58
+	 */
59
+	public function make($entityClass, EntityMap $entityMap)
60
+	{
61
+		$driver = $entityMap->getDriver();
62 62
         
63
-        $connection = $entityMap->getConnection();
63
+		$connection = $entityMap->getConnection();
64 64
 
65
-        $adapter = $this->drivers->getAdapter($driver, $connection);
65
+		$adapter = $this->drivers->getAdapter($driver, $connection);
66 66
         
67
-        $entityMap->setDateFormat($adapter->getDateFormat());
67
+		$entityMap->setDateFormat($adapter->getDateFormat());
68 68
 
69
-        $mapper = new Mapper($entityMap, $adapter, $this->dispatcher, $this->manager);
69
+		$mapper = new Mapper($entityMap, $adapter, $this->dispatcher, $this->manager);
70 70
 
71
-        // Fire Initializing Event
72
-        $mapper->fireEvent('initializing', $mapper);
71
+		// Fire Initializing Event
72
+		$mapper->fireEvent('initializing', $mapper);
73 73
         
74
-        // Proceed necessary parsing on the EntityMap object
75
-        $entityMap->initialize();
74
+		// Proceed necessary parsing on the EntityMap object
75
+		$entityMap->initialize();
76 76
 
77
-        // Fire Initialized Event
78
-        $mapper->fireEvent('initialized', $mapper);
77
+		// Fire Initialized Event
78
+		$mapper->fireEvent('initialized', $mapper);
79 79
 
80
-        return $mapper;
81
-    }
80
+		return $mapper;
81
+	}
82 82
 }
Please login to merge, or discard this patch.