Completed
Push — 5.1 ( 78924a...4f568f )
by Rémi
04:05
created
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/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.
src/System/EntityBuilder.php 2 patches
Indentation   +185 added lines, -185 removed lines patch added patch discarded remove patch
@@ -11,194 +11,194 @@
 block discarded – undo
11 11
  */
12 12
 class EntityBuilder
13 13
 {
14
-    /**
15
-     * The mapper for the entity to build
16
-     * @var \Analogue\ORM\System\Mapper
17
-     */
18
-    protected $mapper;
19
-
20
-    /**
21
-     * The Entity Map for the entity to build.
22
-     *
23
-     * @var \Analogue\ORM\EntityMap
24
-     */
25
-    protected $entityMap;
26
-
27
-    /**
28
-     * Relations that will be eager loaded on this query
29
-     *
30
-     * @var array
31
-     */
32
-    protected $eagerLoads;
33
-
34
-    /**
35
-     * Relations that will be lazy loaded on this query
36
-     *
37
-     * @var array
38
-     */
39
-    protected $lazyLoads;
40
-
41
-    /**
42
-     * Entity Wrapper Factory
43
-     * @var \Analogue\ORM\System\Wrappers\Factory
44
-     */
45
-    protected $factory;
46
-
47
-    /**
48
-     * EntityBuilder constructor.
49
-     * @param Mapper $mapper
50
-     * @param array  $eagerLoads
51
-     */
52
-    public function __construct(Mapper $mapper, array $eagerLoads)
53
-    {
54
-        $this->mapper = $mapper;
55
-
56
-        $this->entityMap = $mapper->getEntityMap();
57
-
58
-        $this->eagerLoads = $eagerLoads;
59
-
60
-        $this->lazyLoads = $this->prepareLazyLoading();
61
-
62
-        $this->entityMap = $mapper->getEntityMap();
63
-
64
-        $this->factory = new Factory;
65
-    }
66
-
67
-    /**
68
-     * Convert a result set into an array of entities
69
-     *
70
-     * @param  array $results
71
-     * @return array
72
-     */
73
-    public function build(array $results)
74
-    {
75
-        $entities = [];
76
-
77
-        //$prototype = $this->getWrapperPrototype();
78
-        //$prototype = $this->mapper->newInstance();
79
-
80
-        $keyName = $this->entityMap->getKeyName();
81
-
82
-        $tmpCache = [];
83
-
84
-        foreach ($results as $result) {
85
-            //$instance = clone $prototype;
86
-            $instance = $this->getWrapperInstance();
87
-
88
-            $resultArray = (array) $result;
89
-
90
-            $tmpCache[$resultArray[$keyName]] = $resultArray;
91
-
92
-            // Hydrate any embedded Value Object
93
-            $this->hydrateValueObjects($resultArray);
94
-
95
-            $instance->setEntityAttributes($resultArray);
96
-
97
-            // Hydrate relation attributes with lazyloading proxies
98
-            if (count($this->lazyLoads) > 0) {
99
-                $proxies = $this->getLazyLoadingProxies($instance);
100
-                $instance->setEntityAttributes($resultArray + $proxies);
101
-            }
102
-
103
-            // Directly Unwrap the entity now that it has been hydrated
104
-            $entities[] = $instance->getObject();
105
-        }
106
-
107
-        $this->mapper->getEntityCache()->add($tmpCache);
108
-
109
-        return $entities;
110
-    }
111
-
112
-    /**
113
-     * Get the correct wrapper prototype corresponding to the object type
114
-     *
115
-     * @throws \Analogue\ORM\Exceptions\MappingException
116
-     * @return InternallyMappable
117
-     */
118
-    protected function getWrapperInstance()
119
-    {
120
-        return $this->factory->make($this->mapper->newInstance());
121
-    }
122
-
123
-    /**
124
-     * Hydrate value object embedded in this entity
125
-     *
126
-     * @param  array $attributes
127
-     * @throws \Analogue\ORM\Exceptions\MappingException
128
-     * @return void
129
-     */
130
-    protected function hydrateValueObjects(& $attributes)
131
-    {
132
-        foreach ($this->entityMap->getEmbeddables() as $localKey => $valueClass) {
133
-            $this->hydrateValueObject($attributes, $localKey, $valueClass);
134
-        }
135
-    }
136
-
137
-    /**
138
-     * Hydrate a single value object
139
-     *
140
-     * @param  array  $attributes
141
-     * @param  string $localKey
142
-     * @param  string $valueClass
143
-     * @throws \Analogue\ORM\Exceptions\MappingException
144
-     * @return void
145
-     */
146
-    protected function hydrateValueObject(& $attributes, $localKey, $valueClass)
147
-    {
148
-        $map = $this->mapper->getManager()->getValueMap($valueClass);
149
-
150
-        $embeddedAttributes = $map->getAttributes();
151
-
152
-        $valueObject = $this->mapper->getManager()->getValueObjectInstance($valueClass);
14
+	/**
15
+	 * The mapper for the entity to build
16
+	 * @var \Analogue\ORM\System\Mapper
17
+	 */
18
+	protected $mapper;
19
+
20
+	/**
21
+	 * The Entity Map for the entity to build.
22
+	 *
23
+	 * @var \Analogue\ORM\EntityMap
24
+	 */
25
+	protected $entityMap;
26
+
27
+	/**
28
+	 * Relations that will be eager loaded on this query
29
+	 *
30
+	 * @var array
31
+	 */
32
+	protected $eagerLoads;
33
+
34
+	/**
35
+	 * Relations that will be lazy loaded on this query
36
+	 *
37
+	 * @var array
38
+	 */
39
+	protected $lazyLoads;
40
+
41
+	/**
42
+	 * Entity Wrapper Factory
43
+	 * @var \Analogue\ORM\System\Wrappers\Factory
44
+	 */
45
+	protected $factory;
46
+
47
+	/**
48
+	 * EntityBuilder constructor.
49
+	 * @param Mapper $mapper
50
+	 * @param array  $eagerLoads
51
+	 */
52
+	public function __construct(Mapper $mapper, array $eagerLoads)
53
+	{
54
+		$this->mapper = $mapper;
55
+
56
+		$this->entityMap = $mapper->getEntityMap();
57
+
58
+		$this->eagerLoads = $eagerLoads;
59
+
60
+		$this->lazyLoads = $this->prepareLazyLoading();
61
+
62
+		$this->entityMap = $mapper->getEntityMap();
63
+
64
+		$this->factory = new Factory;
65
+	}
66
+
67
+	/**
68
+	 * Convert a result set into an array of entities
69
+	 *
70
+	 * @param  array $results
71
+	 * @return array
72
+	 */
73
+	public function build(array $results)
74
+	{
75
+		$entities = [];
76
+
77
+		//$prototype = $this->getWrapperPrototype();
78
+		//$prototype = $this->mapper->newInstance();
79
+
80
+		$keyName = $this->entityMap->getKeyName();
81
+
82
+		$tmpCache = [];
83
+
84
+		foreach ($results as $result) {
85
+			//$instance = clone $prototype;
86
+			$instance = $this->getWrapperInstance();
87
+
88
+			$resultArray = (array) $result;
89
+
90
+			$tmpCache[$resultArray[$keyName]] = $resultArray;
91
+
92
+			// Hydrate any embedded Value Object
93
+			$this->hydrateValueObjects($resultArray);
94
+
95
+			$instance->setEntityAttributes($resultArray);
96
+
97
+			// Hydrate relation attributes with lazyloading proxies
98
+			if (count($this->lazyLoads) > 0) {
99
+				$proxies = $this->getLazyLoadingProxies($instance);
100
+				$instance->setEntityAttributes($resultArray + $proxies);
101
+			}
102
+
103
+			// Directly Unwrap the entity now that it has been hydrated
104
+			$entities[] = $instance->getObject();
105
+		}
106
+
107
+		$this->mapper->getEntityCache()->add($tmpCache);
108
+
109
+		return $entities;
110
+	}
111
+
112
+	/**
113
+	 * Get the correct wrapper prototype corresponding to the object type
114
+	 *
115
+	 * @throws \Analogue\ORM\Exceptions\MappingException
116
+	 * @return InternallyMappable
117
+	 */
118
+	protected function getWrapperInstance()
119
+	{
120
+		return $this->factory->make($this->mapper->newInstance());
121
+	}
122
+
123
+	/**
124
+	 * Hydrate value object embedded in this entity
125
+	 *
126
+	 * @param  array $attributes
127
+	 * @throws \Analogue\ORM\Exceptions\MappingException
128
+	 * @return void
129
+	 */
130
+	protected function hydrateValueObjects(& $attributes)
131
+	{
132
+		foreach ($this->entityMap->getEmbeddables() as $localKey => $valueClass) {
133
+			$this->hydrateValueObject($attributes, $localKey, $valueClass);
134
+		}
135
+	}
136
+
137
+	/**
138
+	 * Hydrate a single value object
139
+	 *
140
+	 * @param  array  $attributes
141
+	 * @param  string $localKey
142
+	 * @param  string $valueClass
143
+	 * @throws \Analogue\ORM\Exceptions\MappingException
144
+	 * @return void
145
+	 */
146
+	protected function hydrateValueObject(& $attributes, $localKey, $valueClass)
147
+	{
148
+		$map = $this->mapper->getManager()->getValueMap($valueClass);
149
+
150
+		$embeddedAttributes = $map->getAttributes();
151
+
152
+		$valueObject = $this->mapper->getManager()->getValueObjectInstance($valueClass);
153 153
 
154
-        foreach ($embeddedAttributes as $key) {
155
-            $prefix = snake_case(class_basename($valueClass)) . '_';
156
-
157
-            $voWrapper = $this->factory->make($valueObject);
158
-
159
-            $voWrapper->setEntityAttribute($key, $attributes[$prefix . $key]);
154
+		foreach ($embeddedAttributes as $key) {
155
+			$prefix = snake_case(class_basename($valueClass)) . '_';
156
+
157
+			$voWrapper = $this->factory->make($valueObject);
158
+
159
+			$voWrapper->setEntityAttribute($key, $attributes[$prefix . $key]);
160 160
             
161
-            unset($attributes[$prefix . $key]);
162
-        }
161
+			unset($attributes[$prefix . $key]);
162
+		}
163 163
         
164
-        $attributes[$localKey] = $valueObject;
165
-    }
166
-
167
-    /**
168
-     * Deduce the relationships that will be lazy loaded from the eagerLoads array
169
-     *
170
-     * @return array
171
-     */
172
-    protected function prepareLazyLoading()
173
-    {
174
-        $relations = $this->entityMap->getRelationships();
164
+		$attributes[$localKey] = $valueObject;
165
+	}
166
+
167
+	/**
168
+	 * Deduce the relationships that will be lazy loaded from the eagerLoads array
169
+	 *
170
+	 * @return array
171
+	 */
172
+	protected function prepareLazyLoading()
173
+	{
174
+		$relations = $this->entityMap->getRelationships();
175 175
        
176
-        return array_diff($relations, $this->eagerLoads);
177
-    }
178
-
179
-    /**
180
-     * Build lazy loading proxies for the current entity
181
-     *
182
-     * @param InternallyMappable $entity
183
-     *
184
-     * @return array
185
-     */
186
-    protected function getLazyLoadingProxies(InternallyMappable $entity)
187
-    {
188
-        $proxies = [];
189
-
190
-        $singleRelations = $this->entityMap->getSingleRelationships();
191
-        $manyRelations = $this->entityMap->getManyRelationships();
192
-
193
-        foreach ($this->lazyLoads as $relation) {
194
-            if (in_array($relation, $singleRelations)) {
195
-                $proxies[$relation] = new EntityProxy($entity->getObject(), $relation);
196
-            }
197
-            if (in_array($relation, $manyRelations)) {
198
-                $proxies[$relation] = new CollectionProxy($entity->getObject(), $relation);
199
-            }
200
-        }
176
+		return array_diff($relations, $this->eagerLoads);
177
+	}
178
+
179
+	/**
180
+	 * Build lazy loading proxies for the current entity
181
+	 *
182
+	 * @param InternallyMappable $entity
183
+	 *
184
+	 * @return array
185
+	 */
186
+	protected function getLazyLoadingProxies(InternallyMappable $entity)
187
+	{
188
+		$proxies = [];
189
+
190
+		$singleRelations = $this->entityMap->getSingleRelationships();
191
+		$manyRelations = $this->entityMap->getManyRelationships();
192
+
193
+		foreach ($this->lazyLoads as $relation) {
194
+			if (in_array($relation, $singleRelations)) {
195
+				$proxies[$relation] = new EntityProxy($entity->getObject(), $relation);
196
+			}
197
+			if (in_array($relation, $manyRelations)) {
198
+				$proxies[$relation] = new CollectionProxy($entity->getObject(), $relation);
199
+			}
200
+		}
201 201
         
202
-        return $proxies;
203
-    }
202
+		return $proxies;
203
+	}
204 204
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -152,13 +152,13 @@
 block discarded – undo
152 152
         $valueObject = $this->mapper->getManager()->getValueObjectInstance($valueClass);
153 153
 
154 154
         foreach ($embeddedAttributes as $key) {
155
-            $prefix = snake_case(class_basename($valueClass)) . '_';
155
+            $prefix = snake_case(class_basename($valueClass)).'_';
156 156
 
157 157
             $voWrapper = $this->factory->make($valueObject);
158 158
 
159
-            $voWrapper->setEntityAttribute($key, $attributes[$prefix . $key]);
159
+            $voWrapper->setEntityAttribute($key, $attributes[$prefix.$key]);
160 160
             
161
-            unset($attributes[$prefix . $key]);
161
+            unset($attributes[$prefix.$key]);
162 162
         }
163 163
         
164 164
         $attributes[$localKey] = $valueObject;
Please login to merge, or discard this patch.
src/System/EntityCache.php 2 patches
Indentation   +298 added lines, -298 removed lines patch added patch discarded remove patch
@@ -15,304 +15,304 @@
 block discarded – undo
15 15
  */
16 16
 class EntityCache
17 17
 {
18
-    /**
19
-     * Entity's raw attributes/relationships
20
-     *
21
-     * @var array
22
-     */
23
-    protected $cache = [];
24
-
25
-    /**
26
-     * Entity Map for the current Entity Type
27
-     * @var [type]
28
-     */
29
-    protected $entityMap;
30
-
31
-    /**
32
-     * Wrapper factory
33
-     *
34
-     * @var \Analogue\ORM\System\Wrappers\Factory
35
-     */
36
-    protected $factory;
37
-
38
-    /**
39
-     * Associative array containing list of pivot attributes per relationship
40
-     * so we don't have to call relationship method on refresh.
41
-     *
42
-     * @var array
43
-     */
44
-    protected $pivotAttributes = [];
45
-
46
-    /**
47
-     * EntityCache constructor.
48
-     * @param EntityMap $entityMap
49
-     */
50
-    public function __construct(EntityMap $entityMap)
51
-    {
52
-        $this->entityMap = $entityMap;
53
-
54
-        $this->factory = new Factory;
55
-    }
56
-
57
-    /**
58
-     * Add an array of key=>attributes representing
59
-     * the initial state of loaded entities.
60
-     *
61
-     * @param array $entities
62
-     */
63
-    public function add(array $entities)
64
-    {
65
-        if (count($this->cache) == 0) {
66
-            $this->cache = $entities;
67
-        } else {
68
-            $this->mergeCacheResults($entities);
69
-        }
70
-    }
71
-
72
-    /**
73
-     * Retrieve initial attributes for a single entity
74
-     *
75
-     * @param  string $id
76
-     * @return array
77
-     */
78
-    public function get($id)
79
-    {
80
-        if ($this->has($id)) {
81
-            return $this->cache[$id];
82
-        } else {
83
-            return [];
84
-        }
85
-    }
86
-
87
-    /**
88
-     * Check if a record for this id exists.
89
-     *
90
-     * @param  string  $id
91
-     * @return boolean
92
-     */
93
-    public function has($id)
94
-    {
95
-        return array_key_exists($id, $this->cache);
96
-    }
97
-
98
-    /**
99
-     * Combine new result set with existing attributes in
100
-     * cache.
101
-     *
102
-     * @param  array $entities
103
-     * @return void
104
-     */
105
-    protected function mergeCacheResults($entities)
106
-    {
107
-        foreach ($entities as $key => $entity) {
108
-            $this->cache[$key] = $entity;
109
-        }
110
-    }
111
-
112
-    /**
113
-     * Cache Relation's query result for an entity
114
-     *
115
-     * @param  mixed        $parent
116
-     * @param  string       $relation name of the relation
117
-     * @param  mixed        $results  results of the relationship's query
118
-     * @param  Relationship $relationship
119
-     * @throws MappingException
120
-     * @return void
121
-     */
122
-    public function cacheLoadedRelationResult($parent, $relation, $results, Relationship $relationship)
123
-    {
124
-        $keyName = $this->entityMap->getKeyName();
125
-
126
-        if (!$parent instanceof InternallyMappable) {
127
-            $parent = $this->factory->make($parent);
128
-        }
129
-
130
-        $key = $parent->getEntityAttribute($keyName);
18
+	/**
19
+	 * Entity's raw attributes/relationships
20
+	 *
21
+	 * @var array
22
+	 */
23
+	protected $cache = [];
24
+
25
+	/**
26
+	 * Entity Map for the current Entity Type
27
+	 * @var [type]
28
+	 */
29
+	protected $entityMap;
30
+
31
+	/**
32
+	 * Wrapper factory
33
+	 *
34
+	 * @var \Analogue\ORM\System\Wrappers\Factory
35
+	 */
36
+	protected $factory;
37
+
38
+	/**
39
+	 * Associative array containing list of pivot attributes per relationship
40
+	 * so we don't have to call relationship method on refresh.
41
+	 *
42
+	 * @var array
43
+	 */
44
+	protected $pivotAttributes = [];
45
+
46
+	/**
47
+	 * EntityCache constructor.
48
+	 * @param EntityMap $entityMap
49
+	 */
50
+	public function __construct(EntityMap $entityMap)
51
+	{
52
+		$this->entityMap = $entityMap;
53
+
54
+		$this->factory = new Factory;
55
+	}
56
+
57
+	/**
58
+	 * Add an array of key=>attributes representing
59
+	 * the initial state of loaded entities.
60
+	 *
61
+	 * @param array $entities
62
+	 */
63
+	public function add(array $entities)
64
+	{
65
+		if (count($this->cache) == 0) {
66
+			$this->cache = $entities;
67
+		} else {
68
+			$this->mergeCacheResults($entities);
69
+		}
70
+	}
71
+
72
+	/**
73
+	 * Retrieve initial attributes for a single entity
74
+	 *
75
+	 * @param  string $id
76
+	 * @return array
77
+	 */
78
+	public function get($id)
79
+	{
80
+		if ($this->has($id)) {
81
+			return $this->cache[$id];
82
+		} else {
83
+			return [];
84
+		}
85
+	}
86
+
87
+	/**
88
+	 * Check if a record for this id exists.
89
+	 *
90
+	 * @param  string  $id
91
+	 * @return boolean
92
+	 */
93
+	public function has($id)
94
+	{
95
+		return array_key_exists($id, $this->cache);
96
+	}
97
+
98
+	/**
99
+	 * Combine new result set with existing attributes in
100
+	 * cache.
101
+	 *
102
+	 * @param  array $entities
103
+	 * @return void
104
+	 */
105
+	protected function mergeCacheResults($entities)
106
+	{
107
+		foreach ($entities as $key => $entity) {
108
+			$this->cache[$key] = $entity;
109
+		}
110
+	}
111
+
112
+	/**
113
+	 * Cache Relation's query result for an entity
114
+	 *
115
+	 * @param  mixed        $parent
116
+	 * @param  string       $relation name of the relation
117
+	 * @param  mixed        $results  results of the relationship's query
118
+	 * @param  Relationship $relationship
119
+	 * @throws MappingException
120
+	 * @return void
121
+	 */
122
+	public function cacheLoadedRelationResult($parent, $relation, $results, Relationship $relationship)
123
+	{
124
+		$keyName = $this->entityMap->getKeyName();
125
+
126
+		if (!$parent instanceof InternallyMappable) {
127
+			$parent = $this->factory->make($parent);
128
+		}
129
+
130
+		$key = $parent->getEntityAttribute($keyName);
131 131
         
132
-        if ($results instanceof EntityCollection) {
133
-            $this->cacheManyRelationResults($key, $relation, $results, $relationship);
134
-        }
135
-
136
-        // POPO : Maybe this check isn't needed, or we have to check for stdClass
137
-        // instead
138
-        if ($results instanceof Mappable) {
139
-            $this->cacheSingleRelationResult($key, $relation, $results, $relationship);
140
-        }
141
-    }
142
-
143
-    /**
144
-     * Create a cachedRelationship instance which will hold related entity's hash and pivot attributes, if any.
145
-     *
146
-     * @param               $parentKey
147
-     * @param  string       $relation
148
-     * @param               $result
149
-     * @param  Relationship $relationship
150
-     * @throws MappingException
151
-     * @return CachedRelationship
152
-     */
153
-    protected function getCachedRelationship($parentKey, $relation, $result, Relationship $relationship)
154
-    {
155
-        $pivotColumns = $relationship->getPivotAttributes();
156
-
157
-        if (!array_key_exists($relation, $this->pivotAttributes)) {
158
-            $this->pivotAttributes[$relation] = $pivotColumns;
159
-        }
160
-
161
-        $wrapper = $this->factory->make($result);
162
-
163
-        $hash = $this->getEntityHash($wrapper);
164
-
165
-        if (count($pivotColumns) > 0) {
166
-            $pivotAttributes = [];
167
-            foreach ($pivotColumns as $column) {
168
-                $pivot = $wrapper->getEntityAttribute('pivot');
169
-
170
-                $pivotWrapper = $this->factory->make($pivot);
171
-
172
-                $pivotAttributes[$column] = $pivotWrapper->getEntityAttribute($column);
173
-            }
174
-
175
-            $cachedRelationship = new CachedRelationship($hash, $pivotAttributes);
176
-        } else {
177
-            $cachedRelationship = new CachedRelationship($hash);
178
-        }
179
-
180
-        return $cachedRelationship;
181
-    }
182
-
183
-    /**
184
-     * Cache a many relationship
185
-     *
186
-     * @param                  $parentKey
187
-     * @param string           $relation
188
-     * @param EntityCollection $results
189
-     * @param Relationship     $relationship
190
-     * @throws MappingException
191
-     */
192
-    protected function cacheManyRelationResults($parentKey, $relation, $results, Relationship $relationship)
193
-    {
194
-        $this->cache[$parentKey][$relation] = [];
195
-
196
-        foreach ($results as $result) {
197
-            $cachedRelationship = $this->getCachedRelationship($parentKey, $relation, $result, $relationship);
198
-
199
-            $relatedHash = $cachedRelationship->getHash();
200
-
201
-            $this->cache[$parentKey][$relation][$relatedHash] = $cachedRelationship;
202
-        }
203
-    }
204
-
205
-    /**
206
-     * Cache a single relationship
207
-     *
208
-     * @param              $parentKey
209
-     * @param string       $relation
210
-     * @param Mappable     $result
211
-     * @param Relationship $relationship
212
-     * @throws MappingException
213
-     */
214
-    protected function cacheSingleRelationResult($parentKey, $relation, $result, Relationship $relationship)
215
-    {
216
-        $this->cache[$parentKey][$relation] = $this->getCachedRelationship($parentKey, $relation, $result, $relationship);
217
-    }
218
-
219
-    /**
220
-     * Get Entity's Hash
221
-     *
222
-     * @param  $entity
223
-     * @throws MappingException
224
-     * @return string
225
-     */
226
-    protected function getEntityHash(InternallyMappable $entity)
227
-    {
228
-        $class = get_class($entity->getObject());
229
-
230
-        $mapper = Manager::getMapper($class);
231
-
232
-        $keyName = $mapper->getEntityMap()->getKeyName();
233
-
234
-        return $class . '.' . $entity->getEntityAttribute($keyName);
235
-    }
236
-
237
-    /**
238
-     * Refresh the cache record for an aggregated entity after a write operation
239
-     * @param Aggregate $entity
240
-     */
241
-    public function refresh(Aggregate $entity)
242
-    {
243
-        $this->cache[$entity->getEntityId()] = $this->transform($entity);
244
-    }
245
-
246
-    /**
247
-     * Transform an Aggregated Entity into a cache record
248
-     *
249
-     * @param  Aggregate $aggregatedEntity
250
-     * @throws MappingException
251
-     * @return array
252
-     */
253
-    protected function transform(Aggregate $aggregatedEntity)
254
-    {
255
-        $baseAttributes = $aggregatedEntity->getRawAttributes();
256
-
257
-        $relationAttributes = [];
258
-
259
-        // First we'll handle each relationships that are a one to one
260
-        // relation, and which will be saved as a CachedRelationship
261
-        // object inside the cache.
262
-
263
-        // NOTE : storing localRelationships maybe useless has we store
264
-        // the foreign key in the attributes already.
265
-
266
-        foreach ($this->entityMap->getSingleRelationships() as $relation) {
267
-            $aggregates = $aggregatedEntity->getRelationship($relation);
132
+		if ($results instanceof EntityCollection) {
133
+			$this->cacheManyRelationResults($key, $relation, $results, $relationship);
134
+		}
135
+
136
+		// POPO : Maybe this check isn't needed, or we have to check for stdClass
137
+		// instead
138
+		if ($results instanceof Mappable) {
139
+			$this->cacheSingleRelationResult($key, $relation, $results, $relationship);
140
+		}
141
+	}
142
+
143
+	/**
144
+	 * Create a cachedRelationship instance which will hold related entity's hash and pivot attributes, if any.
145
+	 *
146
+	 * @param               $parentKey
147
+	 * @param  string       $relation
148
+	 * @param               $result
149
+	 * @param  Relationship $relationship
150
+	 * @throws MappingException
151
+	 * @return CachedRelationship
152
+	 */
153
+	protected function getCachedRelationship($parentKey, $relation, $result, Relationship $relationship)
154
+	{
155
+		$pivotColumns = $relationship->getPivotAttributes();
156
+
157
+		if (!array_key_exists($relation, $this->pivotAttributes)) {
158
+			$this->pivotAttributes[$relation] = $pivotColumns;
159
+		}
160
+
161
+		$wrapper = $this->factory->make($result);
162
+
163
+		$hash = $this->getEntityHash($wrapper);
164
+
165
+		if (count($pivotColumns) > 0) {
166
+			$pivotAttributes = [];
167
+			foreach ($pivotColumns as $column) {
168
+				$pivot = $wrapper->getEntityAttribute('pivot');
169
+
170
+				$pivotWrapper = $this->factory->make($pivot);
171
+
172
+				$pivotAttributes[$column] = $pivotWrapper->getEntityAttribute($column);
173
+			}
174
+
175
+			$cachedRelationship = new CachedRelationship($hash, $pivotAttributes);
176
+		} else {
177
+			$cachedRelationship = new CachedRelationship($hash);
178
+		}
179
+
180
+		return $cachedRelationship;
181
+	}
182
+
183
+	/**
184
+	 * Cache a many relationship
185
+	 *
186
+	 * @param                  $parentKey
187
+	 * @param string           $relation
188
+	 * @param EntityCollection $results
189
+	 * @param Relationship     $relationship
190
+	 * @throws MappingException
191
+	 */
192
+	protected function cacheManyRelationResults($parentKey, $relation, $results, Relationship $relationship)
193
+	{
194
+		$this->cache[$parentKey][$relation] = [];
195
+
196
+		foreach ($results as $result) {
197
+			$cachedRelationship = $this->getCachedRelationship($parentKey, $relation, $result, $relationship);
198
+
199
+			$relatedHash = $cachedRelationship->getHash();
200
+
201
+			$this->cache[$parentKey][$relation][$relatedHash] = $cachedRelationship;
202
+		}
203
+	}
204
+
205
+	/**
206
+	 * Cache a single relationship
207
+	 *
208
+	 * @param              $parentKey
209
+	 * @param string       $relation
210
+	 * @param Mappable     $result
211
+	 * @param Relationship $relationship
212
+	 * @throws MappingException
213
+	 */
214
+	protected function cacheSingleRelationResult($parentKey, $relation, $result, Relationship $relationship)
215
+	{
216
+		$this->cache[$parentKey][$relation] = $this->getCachedRelationship($parentKey, $relation, $result, $relationship);
217
+	}
218
+
219
+	/**
220
+	 * Get Entity's Hash
221
+	 *
222
+	 * @param  $entity
223
+	 * @throws MappingException
224
+	 * @return string
225
+	 */
226
+	protected function getEntityHash(InternallyMappable $entity)
227
+	{
228
+		$class = get_class($entity->getObject());
229
+
230
+		$mapper = Manager::getMapper($class);
231
+
232
+		$keyName = $mapper->getEntityMap()->getKeyName();
233
+
234
+		return $class . '.' . $entity->getEntityAttribute($keyName);
235
+	}
236
+
237
+	/**
238
+	 * Refresh the cache record for an aggregated entity after a write operation
239
+	 * @param Aggregate $entity
240
+	 */
241
+	public function refresh(Aggregate $entity)
242
+	{
243
+		$this->cache[$entity->getEntityId()] = $this->transform($entity);
244
+	}
245
+
246
+	/**
247
+	 * Transform an Aggregated Entity into a cache record
248
+	 *
249
+	 * @param  Aggregate $aggregatedEntity
250
+	 * @throws MappingException
251
+	 * @return array
252
+	 */
253
+	protected function transform(Aggregate $aggregatedEntity)
254
+	{
255
+		$baseAttributes = $aggregatedEntity->getRawAttributes();
256
+
257
+		$relationAttributes = [];
258
+
259
+		// First we'll handle each relationships that are a one to one
260
+		// relation, and which will be saved as a CachedRelationship
261
+		// object inside the cache.
262
+
263
+		// NOTE : storing localRelationships maybe useless has we store
264
+		// the foreign key in the attributes already.
265
+
266
+		foreach ($this->entityMap->getSingleRelationships() as $relation) {
267
+			$aggregates = $aggregatedEntity->getRelationship($relation);
268 268
             
269
-            if (count($aggregates) == 1) {
270
-                $related = $aggregates[0];
271
-                $relationAttributes[$relation] = new CachedRelationship($related->getEntityHash());
272
-            }
273
-            if (count($aggregates) > 1) {
274
-                throw new MappingException("Single Relationship '$relation' contains several related entities");
275
-            }
276
-        }
277
-
278
-        // Then we'll handle the 'many' relationships and store them as
279
-        // an array of CachedRelationship objects.
280
-
281
-        foreach ($this->entityMap->getManyRelationships() as $relation) {
282
-            $aggregates = $aggregatedEntity->getRelationship($relation);
283
-
284
-            $relationAttributes[$relation] = [];
285
-
286
-            foreach ($aggregates as $aggregate) {
287
-                $relationAttributes[$relation][] = new CachedRelationship(
288
-                    $aggregate->getEntityHash(),
289
-                    $aggregate->getPivotAttributes()
290
-                );
291
-            }
292
-        }
293
-
294
-        return $baseAttributes + $relationAttributes;
295
-    }
296
-
297
-    /**
298
-     * @param                     $relation
299
-     * @param  InternallyMappable $entity
300
-     * @return array
301
-     */
302
-    protected function getPivotValues($relation, InternallyMappable $entity)
303
-    {
304
-        $values = [];
305
-
306
-        $entityAttributes = $entity->getEntityAttributes();
307
-
308
-        if (array_key_exists($relation, $this->pivotAttributes)) {
309
-            foreach ($this->pivotAttributes[$relation] as $attribute) {
310
-                if (array_key_exists($attribute, $entityAttributes)) {
311
-                    $values[$attribute] = $entity->getEntityAttribute('pivot')->$attribute;
312
-                }
313
-            }
314
-        }
315
-
316
-        return $values;
317
-    }
269
+			if (count($aggregates) == 1) {
270
+				$related = $aggregates[0];
271
+				$relationAttributes[$relation] = new CachedRelationship($related->getEntityHash());
272
+			}
273
+			if (count($aggregates) > 1) {
274
+				throw new MappingException("Single Relationship '$relation' contains several related entities");
275
+			}
276
+		}
277
+
278
+		// Then we'll handle the 'many' relationships and store them as
279
+		// an array of CachedRelationship objects.
280
+
281
+		foreach ($this->entityMap->getManyRelationships() as $relation) {
282
+			$aggregates = $aggregatedEntity->getRelationship($relation);
283
+
284
+			$relationAttributes[$relation] = [];
285
+
286
+			foreach ($aggregates as $aggregate) {
287
+				$relationAttributes[$relation][] = new CachedRelationship(
288
+					$aggregate->getEntityHash(),
289
+					$aggregate->getPivotAttributes()
290
+				);
291
+			}
292
+		}
293
+
294
+		return $baseAttributes + $relationAttributes;
295
+	}
296
+
297
+	/**
298
+	 * @param                     $relation
299
+	 * @param  InternallyMappable $entity
300
+	 * @return array
301
+	 */
302
+	protected function getPivotValues($relation, InternallyMappable $entity)
303
+	{
304
+		$values = [];
305
+
306
+		$entityAttributes = $entity->getEntityAttributes();
307
+
308
+		if (array_key_exists($relation, $this->pivotAttributes)) {
309
+			foreach ($this->pivotAttributes[$relation] as $attribute) {
310
+				if (array_key_exists($attribute, $entityAttributes)) {
311
+					$values[$attribute] = $entity->getEntityAttribute('pivot')->$attribute;
312
+				}
313
+			}
314
+		}
315
+
316
+		return $values;
317
+	}
318 318
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -231,7 +231,7 @@
 block discarded – undo
231 231
 
232 232
         $keyName = $mapper->getEntityMap()->getKeyName();
233 233
 
234
-        return $class . '.' . $entity->getEntityAttribute($keyName);
234
+        return $class.'.'.$entity->getEntityAttribute($keyName);
235 235
     }
236 236
 
237 237
     /**
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->entity, $method], $parameters);
83
-    }
82
+		return call_user_func_array([$this->entity, $method], $parameters);
83
+	}
84 84
 }
Please login to merge, or discard this patch.
src/EntityCollection.php 2 patches
Indentation   +379 added lines, -379 removed lines patch added patch discarded remove patch
@@ -10,387 +10,387 @@
 block discarded – undo
10 10
 
11 11
 class EntityCollection extends Collection
12 12
 {
13
-    /**
14
-     * Wrapper Factory
15
-     *
16
-     * @var \Analogue\ORM\System\Wrappers\Factory
17
-     */
18
-    protected $factory;
19
-
20
-    /**
21
-     * EntityCollection constructor.
22
-     * @param array|null $entities
23
-     */
24
-    public function __construct(array $entities = null)
25
-    {
26
-        $this->factory = new Factory;
27
-
28
-        parent::__construct($entities);
29
-    }
30
-
31
-    /**
32
-     * Find an entity in the collection by key.
33
-     *
34
-     * @param  mixed $key
35
-     * @param  mixed $default
36
-     * @throws MappingException
37
-     * @return \Analogue\ORM\Entity
38
-     */
39
-    public function find($key, $default = null)
40
-    {
41
-        if ($key instanceof Mappable) {
42
-            $key = $this->getEntityKey($key);
43
-        }
44
-
45
-        return array_first($this->items, function ($itemKey, $entity) use ($key) {
46
-            return $this->getEntityKey($entity) == $key;
47
-        }, $default);
48
-    }
49
-
50
-    /**
51
-     * Add an entity to the collection.
52
-     *
53
-     * @param  Mappable $entity
54
-     * @return $this
55
-     */
56
-    public function add($entity)
57
-    {
58
-        $this->push($entity);
59
-
60
-        return $this;
61
-    }
62
-
63
-    /**
64
-     * Remove an entity from the collection
65
-     *
66
-     * @param $entity
67
-     * @throws MappingException
68
-     * @return mixed
69
-     */
70
-    public function remove($entity)
71
-    {
72
-        $key = $this->getEntityKey($entity);
73
-
74
-        return $this->pull($key);
75
-    }
76
-
77
-    /**
78
-     * Push an item onto the end of the collection.
79
-     *
80
-     * @param  mixed $value
81
-     * @return void
82
-     */
83
-    public function push($value)
84
-    {
85
-        $this->offsetSet(null, $value);
86
-    }
87
-
88
-    /**
89
-     * Put an item in the collection by key.
90
-     *
91
-     * @param  mixed $key
92
-     * @param  mixed $value
93
-     * @return void
94
-     */
95
-    public function put($key, $value)
96
-    {
97
-        $this->offsetSet($key, $value);
98
-    }
99
-
100
-    /**
101
-     * Set the item at a given offset.
102
-     *
103
-     * @param  mixed $key
104
-     * @param  mixed $value
105
-     * @return void
106
-     */
107
-    public function offsetSet($key, $value)
108
-    {
109
-        if (is_null($key)) {
110
-            $this->items[] = $value;
111
-        } else {
112
-            $this->items[$key] = $value;
113
-        }
114
-    }
115
-
116
-    /**
117
-     * Determine if a key exists in the collection.
118
-     *
119
-     * @param  mixed      $key
120
-     * @param  mixed|null $value
121
-     * @return bool
122
-     */
123
-    public function contains($key, $value = null)
124
-    {
125
-        return !is_null($this->find($key));
126
-    }
127
-
128
-    /**
129
-     * Fetch a nested element of the collection.
130
-     *
131
-     * @param  string $key
132
-     * @return self
133
-     */
134
-    public function fetch($key)
135
-    {
136
-        return new static(array_fetch($this->toArray(), $key));
137
-    }
138
-
139
-    /**
140
-     * Generic function for returning class.key value pairs
141
-     *
142
-     * @throws MappingException
143
-     * @return string
144
-     */
145
-    public function getEntityHashes()
146
-    {
147
-        return array_map(function ($entity) {
148
-            $class = get_class($entity);
149
-
150
-            $mapper = Manager::getMapper($class);
13
+	/**
14
+	 * Wrapper Factory
15
+	 *
16
+	 * @var \Analogue\ORM\System\Wrappers\Factory
17
+	 */
18
+	protected $factory;
19
+
20
+	/**
21
+	 * EntityCollection constructor.
22
+	 * @param array|null $entities
23
+	 */
24
+	public function __construct(array $entities = null)
25
+	{
26
+		$this->factory = new Factory;
27
+
28
+		parent::__construct($entities);
29
+	}
30
+
31
+	/**
32
+	 * Find an entity in the collection by key.
33
+	 *
34
+	 * @param  mixed $key
35
+	 * @param  mixed $default
36
+	 * @throws MappingException
37
+	 * @return \Analogue\ORM\Entity
38
+	 */
39
+	public function find($key, $default = null)
40
+	{
41
+		if ($key instanceof Mappable) {
42
+			$key = $this->getEntityKey($key);
43
+		}
44
+
45
+		return array_first($this->items, function ($itemKey, $entity) use ($key) {
46
+			return $this->getEntityKey($entity) == $key;
47
+		}, $default);
48
+	}
49
+
50
+	/**
51
+	 * Add an entity to the collection.
52
+	 *
53
+	 * @param  Mappable $entity
54
+	 * @return $this
55
+	 */
56
+	public function add($entity)
57
+	{
58
+		$this->push($entity);
59
+
60
+		return $this;
61
+	}
62
+
63
+	/**
64
+	 * Remove an entity from the collection
65
+	 *
66
+	 * @param $entity
67
+	 * @throws MappingException
68
+	 * @return mixed
69
+	 */
70
+	public function remove($entity)
71
+	{
72
+		$key = $this->getEntityKey($entity);
73
+
74
+		return $this->pull($key);
75
+	}
76
+
77
+	/**
78
+	 * Push an item onto the end of the collection.
79
+	 *
80
+	 * @param  mixed $value
81
+	 * @return void
82
+	 */
83
+	public function push($value)
84
+	{
85
+		$this->offsetSet(null, $value);
86
+	}
87
+
88
+	/**
89
+	 * Put an item in the collection by key.
90
+	 *
91
+	 * @param  mixed $key
92
+	 * @param  mixed $value
93
+	 * @return void
94
+	 */
95
+	public function put($key, $value)
96
+	{
97
+		$this->offsetSet($key, $value);
98
+	}
99
+
100
+	/**
101
+	 * Set the item at a given offset.
102
+	 *
103
+	 * @param  mixed $key
104
+	 * @param  mixed $value
105
+	 * @return void
106
+	 */
107
+	public function offsetSet($key, $value)
108
+	{
109
+		if (is_null($key)) {
110
+			$this->items[] = $value;
111
+		} else {
112
+			$this->items[$key] = $value;
113
+		}
114
+	}
115
+
116
+	/**
117
+	 * Determine if a key exists in the collection.
118
+	 *
119
+	 * @param  mixed      $key
120
+	 * @param  mixed|null $value
121
+	 * @return bool
122
+	 */
123
+	public function contains($key, $value = null)
124
+	{
125
+		return !is_null($this->find($key));
126
+	}
127
+
128
+	/**
129
+	 * Fetch a nested element of the collection.
130
+	 *
131
+	 * @param  string $key
132
+	 * @return self
133
+	 */
134
+	public function fetch($key)
135
+	{
136
+		return new static(array_fetch($this->toArray(), $key));
137
+	}
138
+
139
+	/**
140
+	 * Generic function for returning class.key value pairs
141
+	 *
142
+	 * @throws MappingException
143
+	 * @return string
144
+	 */
145
+	public function getEntityHashes()
146
+	{
147
+		return array_map(function ($entity) {
148
+			$class = get_class($entity);
149
+
150
+			$mapper = Manager::getMapper($class);
151 151
             
152
-            $keyName = $mapper->getEntityMap()->getKeyName();
152
+			$keyName = $mapper->getEntityMap()->getKeyName();
153 153
             
154
-            return $class . '.' . $entity->getEntityAttribute($keyName);
155
-        },
156
-        $this->items);
157
-    }
158
-
159
-    /**
160
-     * Get a subset of the collection from entity hashes
161
-     *
162
-     * @param  array $hashes
163
-     * @throws MappingException
164
-     * @return array
165
-     */
166
-    public function getSubsetByHashes(array $hashes)
167
-    {
168
-        $subset = [];
169
-
170
-        foreach ($this->items as $item) {
171
-            $class = get_class($item);
172
-
173
-            $mapper = Manager::getMapper($class);
154
+			return $class . '.' . $entity->getEntityAttribute($keyName);
155
+		},
156
+		$this->items);
157
+	}
158
+
159
+	/**
160
+	 * Get a subset of the collection from entity hashes
161
+	 *
162
+	 * @param  array $hashes
163
+	 * @throws MappingException
164
+	 * @return array
165
+	 */
166
+	public function getSubsetByHashes(array $hashes)
167
+	{
168
+		$subset = [];
169
+
170
+		foreach ($this->items as $item) {
171
+			$class = get_class($item);
172
+
173
+			$mapper = Manager::getMapper($class);
174 174
             
175
-            $keyName = $mapper->getEntityMap()->getKeyName();
176
-
177
-            if (in_array($class . '.' . $item->$keyName, $hashes)) {
178
-                $subset[] = $item;
179
-            }
180
-        }
181
-
182
-        return $subset;
183
-    }
184
-
185
-    /**
186
-     * Merge the collection with the given items.
187
-     *
188
-     * @param  array $items
189
-     * @throws MappingException
190
-     * @return self
191
-     */
192
-    public function merge($items)
193
-    {
194
-        $dictionary = $this->getDictionary();
195
-
196
-        foreach ($items as $item) {
197
-            $dictionary[$this->getEntityKey($item)] = $item;
198
-        }
199
-
200
-        return new static(array_values($dictionary));
201
-    }
202
-
203
-    /**
204
-     * Diff the collection with the given items.
205
-     *
206
-     * @param  \ArrayAccess|array $items
207
-     * @return self
208
-     */
209
-    public function diff($items)
210
-    {
211
-        $diff = new static;
212
-
213
-        $dictionary = $this->getDictionary($items);
214
-
215
-        foreach ($this->items as $item) {
216
-            if (!isset($dictionary[$this->getEntityKey($item)])) {
217
-                $diff->add($item);
218
-            }
219
-        }
220
-
221
-        return $diff;
222
-    }
223
-
224
-    /**
225
-     * Intersect the collection with the given items.
226
-     *
227
-     * @param  \ArrayAccess|array $items
228
-     * @throws MappingException
229
-     * @return self
230
-     */
231
-    public function intersect($items)
232
-    {
233
-        $intersect = new static;
234
-
235
-        $dictionary = $this->getDictionary($items);
236
-
237
-        foreach ($this->items as $item) {
238
-            if (isset($dictionary[$this->getEntityKey($item)])) {
239
-                $intersect->add($item);
240
-            }
241
-        }
242
-
243
-        return $intersect;
244
-    }
245
-
246
-    /**
247
-     * Returns only the models from the collection with the specified keys.
248
-     *
249
-     * @param  mixed $keys
250
-     * @return self
251
-     */
252
-    public function only($keys)
253
-    {
254
-        $dictionary = array_only($this->getDictionary(), $keys);
255
-
256
-        return new static(array_values($dictionary));
257
-    }
258
-
259
-    /**
260
-     * Returns all models in the collection except the models with specified keys.
261
-     *
262
-     * @param  mixed $keys
263
-     * @return self
264
-     */
265
-    public function except($keys)
266
-    {
267
-        $dictionary = array_except($this->getDictionary(), $keys);
268
-
269
-        return new static(array_values($dictionary));
270
-    }
271
-
272
-    /**
273
-     * Get a dictionary keyed by primary keys.
274
-     *
275
-     * @param  \ArrayAccess|array $items
276
-     * @throws MappingException
277
-     * @return array
278
-     */
279
-    public function getDictionary($items = null)
280
-    {
281
-        $items = is_null($items) ? $this->items : $items;
282
-
283
-        $dictionary = [];
284
-
285
-        foreach ($items as $value) {
286
-            $dictionary[$this->getEntityKey($value)] = $value;
287
-        }
288
-
289
-        return $dictionary;
290
-    }
291
-
292
-    /**
293
-     * @throws MappingException
294
-     * @return array
295
-     */
296
-    public function getEntityKeys()
297
-    {
298
-        return array_keys($this->getDictionary());
299
-    }
300
-
301
-    /**
302
-     * @param $entity
303
-     * @throws MappingException
304
-     * @return mixed
305
-     */
306
-    protected function getEntityKey($entity)
307
-    {
308
-        $keyName = Manager::getMapper($entity)->getEntityMap()->getKeyName();
175
+			$keyName = $mapper->getEntityMap()->getKeyName();
176
+
177
+			if (in_array($class . '.' . $item->$keyName, $hashes)) {
178
+				$subset[] = $item;
179
+			}
180
+		}
181
+
182
+		return $subset;
183
+	}
184
+
185
+	/**
186
+	 * Merge the collection with the given items.
187
+	 *
188
+	 * @param  array $items
189
+	 * @throws MappingException
190
+	 * @return self
191
+	 */
192
+	public function merge($items)
193
+	{
194
+		$dictionary = $this->getDictionary();
195
+
196
+		foreach ($items as $item) {
197
+			$dictionary[$this->getEntityKey($item)] = $item;
198
+		}
199
+
200
+		return new static(array_values($dictionary));
201
+	}
202
+
203
+	/**
204
+	 * Diff the collection with the given items.
205
+	 *
206
+	 * @param  \ArrayAccess|array $items
207
+	 * @return self
208
+	 */
209
+	public function diff($items)
210
+	{
211
+		$diff = new static;
212
+
213
+		$dictionary = $this->getDictionary($items);
214
+
215
+		foreach ($this->items as $item) {
216
+			if (!isset($dictionary[$this->getEntityKey($item)])) {
217
+				$diff->add($item);
218
+			}
219
+		}
220
+
221
+		return $diff;
222
+	}
223
+
224
+	/**
225
+	 * Intersect the collection with the given items.
226
+	 *
227
+	 * @param  \ArrayAccess|array $items
228
+	 * @throws MappingException
229
+	 * @return self
230
+	 */
231
+	public function intersect($items)
232
+	{
233
+		$intersect = new static;
234
+
235
+		$dictionary = $this->getDictionary($items);
236
+
237
+		foreach ($this->items as $item) {
238
+			if (isset($dictionary[$this->getEntityKey($item)])) {
239
+				$intersect->add($item);
240
+			}
241
+		}
242
+
243
+		return $intersect;
244
+	}
245
+
246
+	/**
247
+	 * Returns only the models from the collection with the specified keys.
248
+	 *
249
+	 * @param  mixed $keys
250
+	 * @return self
251
+	 */
252
+	public function only($keys)
253
+	{
254
+		$dictionary = array_only($this->getDictionary(), $keys);
255
+
256
+		return new static(array_values($dictionary));
257
+	}
258
+
259
+	/**
260
+	 * Returns all models in the collection except the models with specified keys.
261
+	 *
262
+	 * @param  mixed $keys
263
+	 * @return self
264
+	 */
265
+	public function except($keys)
266
+	{
267
+		$dictionary = array_except($this->getDictionary(), $keys);
268
+
269
+		return new static(array_values($dictionary));
270
+	}
271
+
272
+	/**
273
+	 * Get a dictionary keyed by primary keys.
274
+	 *
275
+	 * @param  \ArrayAccess|array $items
276
+	 * @throws MappingException
277
+	 * @return array
278
+	 */
279
+	public function getDictionary($items = null)
280
+	{
281
+		$items = is_null($items) ? $this->items : $items;
282
+
283
+		$dictionary = [];
284
+
285
+		foreach ($items as $value) {
286
+			$dictionary[$this->getEntityKey($value)] = $value;
287
+		}
288
+
289
+		return $dictionary;
290
+	}
291
+
292
+	/**
293
+	 * @throws MappingException
294
+	 * @return array
295
+	 */
296
+	public function getEntityKeys()
297
+	{
298
+		return array_keys($this->getDictionary());
299
+	}
300
+
301
+	/**
302
+	 * @param $entity
303
+	 * @throws MappingException
304
+	 * @return mixed
305
+	 */
306
+	protected function getEntityKey($entity)
307
+	{
308
+		$keyName = Manager::getMapper($entity)->getEntityMap()->getKeyName();
309 309
         
310
-        $wrapper = $this->factory->make($entity);
311
-
312
-        return $wrapper->getEntityAttribute($keyName);
313
-    }
314
-
315
-    /**
316
-     * Get the max value of a given key.
317
-     *
318
-     * @param  string|null $key
319
-     * @throws MappingException
320
-     * @return mixed
321
-     */
322
-    public function max($key = null)
323
-    {
324
-        return $this->reduce(function ($result, $item) use ($key) {
325
-            $wrapper = $this->factory->make($item);
326
-
327
-            return (is_null($result) || $wrapper->getEntityAttribute($key) > $result) ?
328
-                $wrapper->getEntityAttribute($key) : $result;
329
-        });
330
-    }
331
-
332
-    /**
333
-     * Get the min value of a given key.
334
-     *
335
-     * @param  string|null $key
336
-     * @throws MappingException
337
-     * @return mixed
338
-     */
339
-    public function min($key = null)
340
-    {
341
-        return $this->reduce(function ($result, $item) use ($key) {
342
-            $wrapper = $this->factory->make($item);
343
-
344
-            return (is_null($result) || $wrapper->getEntityAttribute($key) < $result)
345
-                ? $wrapper->getEntityAttribute($key) : $result;
346
-        });
347
-    }
348
-
349
-    /**
350
-     * Get an array with the values of a given key.
351
-     *
352
-     * @param  string $value
353
-     * @param  string|null $key
354
-     * @return self
355
-     */
356
-    public function pluck($value, $key = null)
357
-    {
358
-        return new Collection(Arr::pluck($this->items, $value, $key));
359
-    }
360
-
361
-    /**
362
-     * Alias for the "pluck" method.
363
-     *
364
-     * @param  string $value
365
-     * @param  string|null $key
366
-     * @return self
367
-     */
368
-    public function lists($value, $key = null)
369
-    {
370
-        return $this->pluck($value, $key);
371
-    }
372
-
373
-    /**
374
-     * Return only unique items from the collection.
375
-     *
376
-     * @param  string|null $key
377
-     * @throws MappingException
378
-     * @return self
379
-     */
380
-    public function unique($key = null)
381
-    {
382
-        $dictionary = $this->getDictionary();
383
-
384
-        return new static(array_values($dictionary));
385
-    }
386
-
387
-    /**
388
-     * Get a base Support collection instance from this collection.
389
-     *
390
-     * @return \Illuminate\Support\Collection
391
-     */
392
-    public function toBase()
393
-    {
394
-        return new Collection($this->items);
395
-    }
310
+		$wrapper = $this->factory->make($entity);
311
+
312
+		return $wrapper->getEntityAttribute($keyName);
313
+	}
314
+
315
+	/**
316
+	 * Get the max value of a given key.
317
+	 *
318
+	 * @param  string|null $key
319
+	 * @throws MappingException
320
+	 * @return mixed
321
+	 */
322
+	public function max($key = null)
323
+	{
324
+		return $this->reduce(function ($result, $item) use ($key) {
325
+			$wrapper = $this->factory->make($item);
326
+
327
+			return (is_null($result) || $wrapper->getEntityAttribute($key) > $result) ?
328
+				$wrapper->getEntityAttribute($key) : $result;
329
+		});
330
+	}
331
+
332
+	/**
333
+	 * Get the min value of a given key.
334
+	 *
335
+	 * @param  string|null $key
336
+	 * @throws MappingException
337
+	 * @return mixed
338
+	 */
339
+	public function min($key = null)
340
+	{
341
+		return $this->reduce(function ($result, $item) use ($key) {
342
+			$wrapper = $this->factory->make($item);
343
+
344
+			return (is_null($result) || $wrapper->getEntityAttribute($key) < $result)
345
+				? $wrapper->getEntityAttribute($key) : $result;
346
+		});
347
+	}
348
+
349
+	/**
350
+	 * Get an array with the values of a given key.
351
+	 *
352
+	 * @param  string $value
353
+	 * @param  string|null $key
354
+	 * @return self
355
+	 */
356
+	public function pluck($value, $key = null)
357
+	{
358
+		return new Collection(Arr::pluck($this->items, $value, $key));
359
+	}
360
+
361
+	/**
362
+	 * Alias for the "pluck" method.
363
+	 *
364
+	 * @param  string $value
365
+	 * @param  string|null $key
366
+	 * @return self
367
+	 */
368
+	public function lists($value, $key = null)
369
+	{
370
+		return $this->pluck($value, $key);
371
+	}
372
+
373
+	/**
374
+	 * Return only unique items from the collection.
375
+	 *
376
+	 * @param  string|null $key
377
+	 * @throws MappingException
378
+	 * @return self
379
+	 */
380
+	public function unique($key = null)
381
+	{
382
+		$dictionary = $this->getDictionary();
383
+
384
+		return new static(array_values($dictionary));
385
+	}
386
+
387
+	/**
388
+	 * Get a base Support collection instance from this collection.
389
+	 *
390
+	 * @return \Illuminate\Support\Collection
391
+	 */
392
+	public function toBase()
393
+	{
394
+		return new Collection($this->items);
395
+	}
396 396
 }
Please login to merge, or discard this patch.
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -42,7 +42,7 @@  discard block
 block discarded – undo
42 42
             $key = $this->getEntityKey($key);
43 43
         }
44 44
 
45
-        return array_first($this->items, function ($itemKey, $entity) use ($key) {
45
+        return array_first($this->items, function($itemKey, $entity) use ($key) {
46 46
             return $this->getEntityKey($entity) == $key;
47 47
         }, $default);
48 48
     }
@@ -144,14 +144,14 @@  discard block
 block discarded – undo
144 144
      */
145 145
     public function getEntityHashes()
146 146
     {
147
-        return array_map(function ($entity) {
147
+        return array_map(function($entity) {
148 148
             $class = get_class($entity);
149 149
 
150 150
             $mapper = Manager::getMapper($class);
151 151
             
152 152
             $keyName = $mapper->getEntityMap()->getKeyName();
153 153
             
154
-            return $class . '.' . $entity->getEntityAttribute($keyName);
154
+            return $class.'.'.$entity->getEntityAttribute($keyName);
155 155
         },
156 156
         $this->items);
157 157
     }
@@ -174,7 +174,7 @@  discard block
 block discarded – undo
174 174
             
175 175
             $keyName = $mapper->getEntityMap()->getKeyName();
176 176
 
177
-            if (in_array($class . '.' . $item->$keyName, $hashes)) {
177
+            if (in_array($class.'.'.$item->$keyName, $hashes)) {
178 178
                 $subset[] = $item;
179 179
             }
180 180
         }
@@ -321,7 +321,7 @@  discard block
 block discarded – undo
321 321
      */
322 322
     public function max($key = null)
323 323
     {
324
-        return $this->reduce(function ($result, $item) use ($key) {
324
+        return $this->reduce(function($result, $item) use ($key) {
325 325
             $wrapper = $this->factory->make($item);
326 326
 
327 327
             return (is_null($result) || $wrapper->getEntityAttribute($key) > $result) ?
@@ -338,7 +338,7 @@  discard block
 block discarded – undo
338 338
      */
339 339
     public function min($key = null)
340 340
     {
341
-        return $this->reduce(function ($result, $item) use ($key) {
341
+        return $this->reduce(function($result, $item) use ($key) {
342 342
             $wrapper = $this->factory->make($item);
343 343
 
344 344
             return (is_null($result) || $wrapper->getEntityAttribute($key) < $result)
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   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -259,7 +259,7 @@  discard block
 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
 
@@ -310,7 +310,7 @@  discard block
 block discarded – undo
310 310
      */
311 311
     public function getGlobalScope($scope)
312 312
     {
313
-        return array_first($this->globalScopes, function ($key, $value) use ($scope) {
313
+        return array_first($this->globalScopes, function($key, $value) use ($scope) {
314 314
             return $scope instanceof $value;
315 315
         });
316 316
     }
Please login to merge, or discard this patch.