@@ -6,141 +6,141 @@ |
||
6 | 6 | |
7 | 7 | class Entity extends ValueObject |
8 | 8 | { |
9 | - /** |
|
10 | - * Entities Hidden Attributes, that will be discarded when converting |
|
11 | - * the entity to Array/Json |
|
12 | - * (can include any embedded object's attribute). |
|
13 | - * |
|
14 | - * @var array |
|
15 | - */ |
|
16 | - protected $hidden = []; |
|
17 | - |
|
18 | - /** |
|
19 | - * Return the entity's attribute. |
|
20 | - * |
|
21 | - * @param string $key |
|
22 | - * |
|
23 | - * @return mixed |
|
24 | - */ |
|
25 | - public function __get($key) |
|
26 | - { |
|
27 | - if ($this->hasGetMutator($key)) { |
|
28 | - $method = 'get'.$this->getMutatorMethod($key); |
|
29 | - |
|
30 | - $attribute = null; |
|
31 | - |
|
32 | - if (isset($this->attributes[$key])) { |
|
33 | - $attribute = $this->attributes[$key]; |
|
34 | - } |
|
35 | - |
|
36 | - return $this->$method($attribute); |
|
37 | - } |
|
38 | - if (!array_key_exists($key, $this->attributes)) { |
|
39 | - return; |
|
40 | - } |
|
41 | - if ($this->attributes[$key] instanceof EntityProxy) { |
|
42 | - $this->attributes[$key] = $this->attributes[$key]->load(); |
|
43 | - } |
|
44 | - |
|
45 | - return $this->attributes[$key]; |
|
46 | - } |
|
47 | - |
|
48 | - /** |
|
49 | - * Dynamically set attributes on the entity. |
|
50 | - * |
|
51 | - * @param string $key |
|
52 | - * @param mixed $value |
|
53 | - * |
|
54 | - * @return void |
|
55 | - */ |
|
56 | - public function __set($key, $value) |
|
57 | - { |
|
58 | - if ($this->hasSetMutator($key)) { |
|
59 | - $method = 'set'.$this->getMutatorMethod($key); |
|
60 | - |
|
61 | - $this->$method($value); |
|
62 | - } else { |
|
63 | - $this->attributes[$key] = $value; |
|
64 | - } |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * Is a getter method defined ? |
|
69 | - * |
|
70 | - * @param string $key |
|
71 | - * |
|
72 | - * @return bool |
|
73 | - */ |
|
74 | - protected function hasGetMutator($key) |
|
75 | - { |
|
76 | - return method_exists($this, 'get'.$this->getMutatorMethod($key)) ? true : false; |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * Is a setter method defined ? |
|
81 | - * |
|
82 | - * @param string $key |
|
83 | - * |
|
84 | - * @return bool |
|
85 | - */ |
|
86 | - protected function hasSetMutator($key) |
|
87 | - { |
|
88 | - return method_exists($this, 'set'.$this->getMutatorMethod($key)) ? true : false; |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * @param $key |
|
93 | - * |
|
94 | - * @return string |
|
95 | - */ |
|
96 | - protected function getMutatorMethod($key) |
|
97 | - { |
|
98 | - $key = ucwords(str_replace(['-', '_'], ' ', $key)); |
|
99 | - |
|
100 | - return str_replace(' ', '', $key).'Attribute'; |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Convert every attributes to value / arrays. |
|
105 | - * |
|
106 | - * @return array |
|
107 | - */ |
|
108 | - public function toArray() |
|
109 | - { |
|
110 | - // First, call the trait method before filtering |
|
111 | - // with Entity specific methods |
|
112 | - $attributes = $this->attributesToArray($this->attributes); |
|
113 | - |
|
114 | - foreach ($this->attributes as $key => $attribute) { |
|
115 | - if (in_array($key, $this->hidden)) { |
|
116 | - unset($attributes[$key]); |
|
117 | - continue; |
|
118 | - } |
|
119 | - if ($this->hasGetMutator($key)) { |
|
120 | - $method = 'get'.$this->getMutatorMethod($key); |
|
121 | - $attributes[$key] = $this->$method($attribute); |
|
122 | - } |
|
123 | - } |
|
124 | - |
|
125 | - return $attributes; |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * Fill an entity with key-value pairs. |
|
130 | - * |
|
131 | - * @param array $attributes |
|
132 | - * |
|
133 | - * @return void |
|
134 | - */ |
|
135 | - public function fill(array $attributes) |
|
136 | - { |
|
137 | - foreach ($attributes as $key => $attribute) { |
|
138 | - if ($this->hasSetMutator($key)) { |
|
139 | - $method = 'set'.$this->getMutatorMethod($key); |
|
140 | - $this->attributes[$key] = $this->$method($attribute); |
|
141 | - } else { |
|
142 | - $this->attributes[$key] = $attribute; |
|
143 | - } |
|
144 | - } |
|
145 | - } |
|
9 | + /** |
|
10 | + * Entities Hidden Attributes, that will be discarded when converting |
|
11 | + * the entity to Array/Json |
|
12 | + * (can include any embedded object's attribute). |
|
13 | + * |
|
14 | + * @var array |
|
15 | + */ |
|
16 | + protected $hidden = []; |
|
17 | + |
|
18 | + /** |
|
19 | + * Return the entity's attribute. |
|
20 | + * |
|
21 | + * @param string $key |
|
22 | + * |
|
23 | + * @return mixed |
|
24 | + */ |
|
25 | + public function __get($key) |
|
26 | + { |
|
27 | + if ($this->hasGetMutator($key)) { |
|
28 | + $method = 'get'.$this->getMutatorMethod($key); |
|
29 | + |
|
30 | + $attribute = null; |
|
31 | + |
|
32 | + if (isset($this->attributes[$key])) { |
|
33 | + $attribute = $this->attributes[$key]; |
|
34 | + } |
|
35 | + |
|
36 | + return $this->$method($attribute); |
|
37 | + } |
|
38 | + if (!array_key_exists($key, $this->attributes)) { |
|
39 | + return; |
|
40 | + } |
|
41 | + if ($this->attributes[$key] instanceof EntityProxy) { |
|
42 | + $this->attributes[$key] = $this->attributes[$key]->load(); |
|
43 | + } |
|
44 | + |
|
45 | + return $this->attributes[$key]; |
|
46 | + } |
|
47 | + |
|
48 | + /** |
|
49 | + * Dynamically set attributes on the entity. |
|
50 | + * |
|
51 | + * @param string $key |
|
52 | + * @param mixed $value |
|
53 | + * |
|
54 | + * @return void |
|
55 | + */ |
|
56 | + public function __set($key, $value) |
|
57 | + { |
|
58 | + if ($this->hasSetMutator($key)) { |
|
59 | + $method = 'set'.$this->getMutatorMethod($key); |
|
60 | + |
|
61 | + $this->$method($value); |
|
62 | + } else { |
|
63 | + $this->attributes[$key] = $value; |
|
64 | + } |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * Is a getter method defined ? |
|
69 | + * |
|
70 | + * @param string $key |
|
71 | + * |
|
72 | + * @return bool |
|
73 | + */ |
|
74 | + protected function hasGetMutator($key) |
|
75 | + { |
|
76 | + return method_exists($this, 'get'.$this->getMutatorMethod($key)) ? true : false; |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * Is a setter method defined ? |
|
81 | + * |
|
82 | + * @param string $key |
|
83 | + * |
|
84 | + * @return bool |
|
85 | + */ |
|
86 | + protected function hasSetMutator($key) |
|
87 | + { |
|
88 | + return method_exists($this, 'set'.$this->getMutatorMethod($key)) ? true : false; |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * @param $key |
|
93 | + * |
|
94 | + * @return string |
|
95 | + */ |
|
96 | + protected function getMutatorMethod($key) |
|
97 | + { |
|
98 | + $key = ucwords(str_replace(['-', '_'], ' ', $key)); |
|
99 | + |
|
100 | + return str_replace(' ', '', $key).'Attribute'; |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Convert every attributes to value / arrays. |
|
105 | + * |
|
106 | + * @return array |
|
107 | + */ |
|
108 | + public function toArray() |
|
109 | + { |
|
110 | + // First, call the trait method before filtering |
|
111 | + // with Entity specific methods |
|
112 | + $attributes = $this->attributesToArray($this->attributes); |
|
113 | + |
|
114 | + foreach ($this->attributes as $key => $attribute) { |
|
115 | + if (in_array($key, $this->hidden)) { |
|
116 | + unset($attributes[$key]); |
|
117 | + continue; |
|
118 | + } |
|
119 | + if ($this->hasGetMutator($key)) { |
|
120 | + $method = 'get'.$this->getMutatorMethod($key); |
|
121 | + $attributes[$key] = $this->$method($attribute); |
|
122 | + } |
|
123 | + } |
|
124 | + |
|
125 | + return $attributes; |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * Fill an entity with key-value pairs. |
|
130 | + * |
|
131 | + * @param array $attributes |
|
132 | + * |
|
133 | + * @return void |
|
134 | + */ |
|
135 | + public function fill(array $attributes) |
|
136 | + { |
|
137 | + foreach ($attributes as $key => $attribute) { |
|
138 | + if ($this->hasSetMutator($key)) { |
|
139 | + $method = 'set'.$this->getMutatorMethod($key); |
|
140 | + $this->attributes[$key] = $this->$method($attribute); |
|
141 | + } else { |
|
142 | + $this->attributes[$key] = $attribute; |
|
143 | + } |
|
144 | + } |
|
145 | + } |
|
146 | 146 | } |
@@ -6,36 +6,36 @@ |
||
6 | 6 | |
7 | 7 | class EntityNotFoundException extends RuntimeException |
8 | 8 | { |
9 | - /** |
|
10 | - * Name of the affected Entity Map. |
|
11 | - * |
|
12 | - * @var string |
|
13 | - */ |
|
14 | - protected $entity; |
|
9 | + /** |
|
10 | + * Name of the affected Entity Map. |
|
11 | + * |
|
12 | + * @var string |
|
13 | + */ |
|
14 | + protected $entity; |
|
15 | 15 | |
16 | - /** |
|
17 | - * Set the affected Entity Map. |
|
18 | - * |
|
19 | - * @param string $entity |
|
20 | - * |
|
21 | - * @return $this |
|
22 | - */ |
|
23 | - public function setEntity($entity) |
|
24 | - { |
|
25 | - $this->entity = $entity; |
|
16 | + /** |
|
17 | + * Set the affected Entity Map. |
|
18 | + * |
|
19 | + * @param string $entity |
|
20 | + * |
|
21 | + * @return $this |
|
22 | + */ |
|
23 | + public function setEntity($entity) |
|
24 | + { |
|
25 | + $this->entity = $entity; |
|
26 | 26 | |
27 | - $this->message = "No query results for entity [{$entity}]."; |
|
27 | + $this->message = "No query results for entity [{$entity}]."; |
|
28 | 28 | |
29 | - return $this; |
|
30 | - } |
|
29 | + return $this; |
|
30 | + } |
|
31 | 31 | |
32 | - /** |
|
33 | - * Get the affected Entity. |
|
34 | - * |
|
35 | - * @return string |
|
36 | - */ |
|
37 | - public function getEntity() |
|
38 | - { |
|
39 | - return $this->entity; |
|
40 | - } |
|
32 | + /** |
|
33 | + * Get the affected Entity. |
|
34 | + * |
|
35 | + * @return string |
|
36 | + */ |
|
37 | + public function getEntity() |
|
38 | + { |
|
39 | + return $this->entity; |
|
40 | + } |
|
41 | 41 | } |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | $key = $this->getEntityKey($key); |
46 | 46 | } |
47 | 47 | |
48 | - return array_first($this->items, function ($entity, $itemKey) use ($key) { |
|
48 | + return array_first($this->items, function($entity, $itemKey) use ($key) { |
|
49 | 49 | return $this->getEntityKey($entity) == $key; |
50 | 50 | }, $default); |
51 | 51 | } |
@@ -164,7 +164,7 @@ discard block |
||
164 | 164 | */ |
165 | 165 | public function getEntityHashes() |
166 | 166 | { |
167 | - return array_map(function ($entity) { |
|
167 | + return array_map(function($entity) { |
|
168 | 168 | $class = get_class($entity); |
169 | 169 | |
170 | 170 | $mapper = Manager::getMapper($class); |
@@ -357,7 +357,7 @@ discard block |
||
357 | 357 | */ |
358 | 358 | public function max($key = null) |
359 | 359 | { |
360 | - return $this->reduce(function ($result, $item) use ($key) { |
|
360 | + return $this->reduce(function($result, $item) use ($key) { |
|
361 | 361 | $wrapper = $this->factory->make($item); |
362 | 362 | |
363 | 363 | return (is_null($result) || $wrapper->getEntityAttribute($key) > $result) ? |
@@ -376,7 +376,7 @@ discard block |
||
376 | 376 | */ |
377 | 377 | public function min($key = null) |
378 | 378 | { |
379 | - return $this->reduce(function ($result, $item) use ($key) { |
|
379 | + return $this->reduce(function($result, $item) use ($key) { |
|
380 | 380 | $wrapper = $this->factory->make($item); |
381 | 381 | |
382 | 382 | return (is_null($result) || $wrapper->getEntityAttribute($key) < $result) |
@@ -10,449 +10,449 @@ |
||
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 | - * |
|
23 | - * @param array|null $entities |
|
24 | - */ |
|
25 | - public function __construct(array $entities = null) |
|
26 | - { |
|
27 | - $this->factory = new Factory(); |
|
28 | - |
|
29 | - parent::__construct($entities); |
|
30 | - } |
|
31 | - |
|
32 | - /** |
|
33 | - * Find an entity in the collection by key. |
|
34 | - * |
|
35 | - * @param mixed $key |
|
36 | - * @param mixed $default |
|
37 | - * |
|
38 | - * @throws MappingException |
|
39 | - * |
|
40 | - * @return \Analogue\ORM\Entity |
|
41 | - */ |
|
42 | - public function find($key, $default = null) |
|
43 | - { |
|
44 | - if ($key instanceof Mappable) { |
|
45 | - $key = $this->getEntityKey($key); |
|
46 | - } |
|
47 | - |
|
48 | - return array_first($this->items, function ($entity, $itemKey) use ($key) { |
|
49 | - return $this->getEntityKey($entity) == $key; |
|
50 | - }, $default); |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * Add an entity to the collection. |
|
55 | - * |
|
56 | - * @param Mappable $entity |
|
57 | - * |
|
58 | - * @return $this |
|
59 | - */ |
|
60 | - public function add($entity) |
|
61 | - { |
|
62 | - $this->push($entity); |
|
63 | - |
|
64 | - return $this; |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * Remove an entity from the collection. |
|
69 | - * |
|
70 | - * @param $entity |
|
71 | - * |
|
72 | - * @throws MappingException |
|
73 | - * |
|
74 | - * @return mixed |
|
75 | - */ |
|
76 | - public function remove($entity) |
|
77 | - { |
|
78 | - $key = $this->getEntityKey($entity); |
|
79 | - |
|
80 | - return $this->pull($key); |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * Push an item onto the end of the collection. |
|
85 | - * |
|
86 | - * @param mixed $value |
|
87 | - * |
|
88 | - * @return void |
|
89 | - */ |
|
90 | - public function push($value) |
|
91 | - { |
|
92 | - $this->offsetSet(null, $value); |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * Put an item in the collection by key. |
|
97 | - * |
|
98 | - * @param mixed $key |
|
99 | - * @param mixed $value |
|
100 | - * |
|
101 | - * @return void |
|
102 | - */ |
|
103 | - public function put($key, $value) |
|
104 | - { |
|
105 | - $this->offsetSet($key, $value); |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * Set the item at a given offset. |
|
110 | - * |
|
111 | - * @param mixed $key |
|
112 | - * @param mixed $value |
|
113 | - * |
|
114 | - * @return void |
|
115 | - */ |
|
116 | - public function offsetSet($key, $value) |
|
117 | - { |
|
118 | - if (is_null($key)) { |
|
119 | - $this->items[] = $value; |
|
120 | - } else { |
|
121 | - $this->items[$key] = $value; |
|
122 | - } |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Determine if a key exists in the collection. |
|
127 | - * |
|
128 | - * @param mixed $key |
|
129 | - * @param mixed|null $value |
|
130 | - * |
|
131 | - * @return bool |
|
132 | - */ |
|
133 | - // public function contains($key, $value = null) |
|
134 | - // { |
|
135 | - // if (func_num_args() == 2) { |
|
136 | - // return !$this->where($key, $value)->isEmpty(); |
|
137 | - // } |
|
138 | - |
|
139 | - // if ($this->useAsCallable($key)) { |
|
140 | - // return !is_null($this->first($key)); |
|
141 | - // } |
|
142 | - |
|
143 | - // return !is_null($this->find($key)); |
|
144 | - // } |
|
145 | - |
|
146 | - /** |
|
147 | - * Fetch a nested element of the collection. |
|
148 | - * |
|
149 | - * @param string $key |
|
150 | - * |
|
151 | - * @return self |
|
152 | - */ |
|
153 | - public function fetch($key) |
|
154 | - { |
|
155 | - return new static(array_fetch($this->toArray(), $key)); |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Generic function for returning class.key value pairs. |
|
160 | - * |
|
161 | - * @throws MappingException |
|
162 | - * |
|
163 | - * @return string |
|
164 | - */ |
|
165 | - public function getEntityHashes() |
|
166 | - { |
|
167 | - return array_map(function ($entity) { |
|
168 | - $class = get_class($entity); |
|
169 | - |
|
170 | - $mapper = Manager::getMapper($class); |
|
171 | - |
|
172 | - $keyName = $mapper->getEntityMap()->getKeyName(); |
|
173 | - |
|
174 | - return $class.'.'.$entity->getEntityAttribute($keyName); |
|
175 | - }, |
|
176 | - $this->items); |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * Get a subset of the collection from entity hashes. |
|
181 | - * |
|
182 | - * @param array $hashes |
|
183 | - * |
|
184 | - * @throws MappingException |
|
185 | - * |
|
186 | - * @return array |
|
187 | - */ |
|
188 | - public function getSubsetByHashes(array $hashes) |
|
189 | - { |
|
190 | - $subset = []; |
|
191 | - |
|
192 | - foreach ($this->items as $item) { |
|
193 | - $class = get_class($item); |
|
194 | - |
|
195 | - $mapper = Manager::getMapper($class); |
|
196 | - |
|
197 | - $keyName = $mapper->getEntityMap()->getKeyName(); |
|
198 | - |
|
199 | - if (in_array($class.'.'.$item->$keyName, $hashes)) { |
|
200 | - $subset[] = $item; |
|
201 | - } |
|
202 | - } |
|
203 | - |
|
204 | - return $subset; |
|
205 | - } |
|
206 | - |
|
207 | - /** |
|
208 | - * Merge the collection with the given items. |
|
209 | - * |
|
210 | - * @param array $items |
|
211 | - * |
|
212 | - * @throws MappingException |
|
213 | - * |
|
214 | - * @return self |
|
215 | - */ |
|
216 | - public function merge($items) |
|
217 | - { |
|
218 | - $dictionary = $this->getDictionary(); |
|
219 | - |
|
220 | - foreach ($items as $item) { |
|
221 | - $dictionary[$this->getEntityKey($item)] = $item; |
|
222 | - } |
|
223 | - |
|
224 | - return new static(array_values($dictionary)); |
|
225 | - } |
|
226 | - |
|
227 | - /** |
|
228 | - * Diff the collection with the given items. |
|
229 | - * |
|
230 | - * @param \ArrayAccess|array $items |
|
231 | - * |
|
232 | - * @return self |
|
233 | - */ |
|
234 | - public function diff($items) |
|
235 | - { |
|
236 | - $diff = new static(); |
|
237 | - |
|
238 | - $dictionary = $this->getDictionary($items); |
|
239 | - |
|
240 | - foreach ($this->items as $item) { |
|
241 | - if (!isset($dictionary[$this->getEntityKey($item)])) { |
|
242 | - $diff->add($item); |
|
243 | - } |
|
244 | - } |
|
245 | - |
|
246 | - return $diff; |
|
247 | - } |
|
248 | - |
|
249 | - /** |
|
250 | - * Intersect the collection with the given items. |
|
251 | - * |
|
252 | - * @param \ArrayAccess|array $items |
|
253 | - * |
|
254 | - * @throws MappingException |
|
255 | - * |
|
256 | - * @return self |
|
257 | - */ |
|
258 | - public function intersect($items) |
|
259 | - { |
|
260 | - $intersect = new static(); |
|
261 | - |
|
262 | - $dictionary = $this->getDictionary($items); |
|
263 | - |
|
264 | - foreach ($this->items as $item) { |
|
265 | - if (isset($dictionary[$this->getEntityKey($item)])) { |
|
266 | - $intersect->add($item); |
|
267 | - } |
|
268 | - } |
|
269 | - |
|
270 | - return $intersect; |
|
271 | - } |
|
272 | - |
|
273 | - /** |
|
274 | - * Returns only the models from the collection with the specified keys. |
|
275 | - * |
|
276 | - * @param mixed $keys |
|
277 | - * |
|
278 | - * @return self |
|
279 | - */ |
|
280 | - public function only($keys) |
|
281 | - { |
|
282 | - $dictionary = array_only($this->getDictionary(), $keys); |
|
283 | - |
|
284 | - return new static(array_values($dictionary)); |
|
285 | - } |
|
286 | - |
|
287 | - /** |
|
288 | - * Returns all models in the collection except the models with specified keys. |
|
289 | - * |
|
290 | - * @param mixed $keys |
|
291 | - * |
|
292 | - * @return self |
|
293 | - */ |
|
294 | - public function except($keys) |
|
295 | - { |
|
296 | - $dictionary = array_except($this->getDictionary(), $keys); |
|
297 | - |
|
298 | - return new static(array_values($dictionary)); |
|
299 | - } |
|
300 | - |
|
301 | - /** |
|
302 | - * Get a dictionary keyed by primary keys. |
|
303 | - * |
|
304 | - * @param \ArrayAccess|array $items |
|
305 | - * |
|
306 | - * @throws MappingException |
|
307 | - * |
|
308 | - * @return array |
|
309 | - */ |
|
310 | - public function getDictionary($items = null) |
|
311 | - { |
|
312 | - $items = is_null($items) ? $this->items : $items; |
|
313 | - |
|
314 | - $dictionary = []; |
|
315 | - |
|
316 | - foreach ($items as $value) { |
|
317 | - $dictionary[$this->getEntityKey($value)] = $value; |
|
318 | - } |
|
319 | - |
|
320 | - return $dictionary; |
|
321 | - } |
|
322 | - |
|
323 | - /** |
|
324 | - * @throws MappingException |
|
325 | - * |
|
326 | - * @return array |
|
327 | - */ |
|
328 | - public function getEntityKeys() |
|
329 | - { |
|
330 | - return array_keys($this->getDictionary()); |
|
331 | - } |
|
332 | - |
|
333 | - /** |
|
334 | - * @param $entity |
|
335 | - * |
|
336 | - * @throws MappingException |
|
337 | - * |
|
338 | - * @return mixed |
|
339 | - */ |
|
340 | - protected function getEntityKey($entity) |
|
341 | - { |
|
342 | - $keyName = Manager::getMapper($entity)->getEntityMap()->getKeyName(); |
|
343 | - |
|
344 | - $wrapper = $this->factory->make($entity); |
|
345 | - |
|
346 | - return $wrapper->getEntityAttribute($keyName); |
|
347 | - } |
|
348 | - |
|
349 | - /** |
|
350 | - * Get the max value of a given key. |
|
351 | - * |
|
352 | - * @param string|null $key |
|
353 | - * |
|
354 | - * @throws MappingException |
|
355 | - * |
|
356 | - * @return mixed |
|
357 | - */ |
|
358 | - public function max($key = null) |
|
359 | - { |
|
360 | - return $this->reduce(function ($result, $item) use ($key) { |
|
361 | - $wrapper = $this->factory->make($item); |
|
362 | - |
|
363 | - return (is_null($result) || $wrapper->getEntityAttribute($key) > $result) ? |
|
364 | - $wrapper->getEntityAttribute($key) : $result; |
|
365 | - }); |
|
366 | - } |
|
367 | - |
|
368 | - /** |
|
369 | - * Get the min value of a given key. |
|
370 | - * |
|
371 | - * @param string|null $key |
|
372 | - * |
|
373 | - * @throws MappingException |
|
374 | - * |
|
375 | - * @return mixed |
|
376 | - */ |
|
377 | - public function min($key = null) |
|
378 | - { |
|
379 | - return $this->reduce(function ($result, $item) use ($key) { |
|
380 | - $wrapper = $this->factory->make($item); |
|
381 | - |
|
382 | - return (is_null($result) || $wrapper->getEntityAttribute($key) < $result) |
|
383 | - ? $wrapper->getEntityAttribute($key) : $result; |
|
384 | - }); |
|
385 | - } |
|
386 | - |
|
387 | - /** |
|
388 | - * Get an array with the values of a given key. |
|
389 | - * |
|
390 | - * @param string $value |
|
391 | - * @param string|null $key |
|
392 | - * |
|
393 | - * @return self |
|
394 | - */ |
|
395 | - public function pluck($value, $key = null) |
|
396 | - { |
|
397 | - return new Collection(Arr::pluck($this->items, $value, $key)); |
|
398 | - } |
|
399 | - |
|
400 | - /** |
|
401 | - * Alias for the "pluck" method. |
|
402 | - * |
|
403 | - * @param string $value |
|
404 | - * @param string|null $key |
|
405 | - * |
|
406 | - * @return self |
|
407 | - */ |
|
408 | - public function lists($value, $key = null) |
|
409 | - { |
|
410 | - return $this->pluck($value, $key); |
|
411 | - } |
|
412 | - |
|
413 | - /** |
|
414 | - * Return only unique items from the collection. |
|
415 | - * |
|
416 | - * @param string|null $key |
|
417 | - * @param bool $strict |
|
418 | - * |
|
419 | - * @throws MappingException |
|
420 | - * |
|
421 | - * @return self |
|
422 | - */ |
|
423 | - public function unique($key = null, $strict = false) |
|
424 | - { |
|
425 | - $dictionary = $this->getDictionary(); |
|
426 | - |
|
427 | - return new static(array_values($dictionary)); |
|
428 | - } |
|
429 | - |
|
430 | - /** |
|
431 | - * Get a base Support collection instance from this collection. |
|
432 | - * |
|
433 | - * @return \Illuminate\Support\Collection |
|
434 | - */ |
|
435 | - public function toBase() |
|
436 | - { |
|
437 | - return new Collection($this->items); |
|
438 | - } |
|
439 | - |
|
440 | - public function toArray() |
|
441 | - { |
|
442 | - return array_values(parent::toArray()); |
|
443 | - } |
|
444 | - |
|
445 | - /** |
|
446 | - * Get the collection of items as JSON. |
|
447 | - * |
|
448 | - * @param int $options |
|
449 | - * |
|
450 | - * @return string |
|
451 | - */ |
|
452 | - public function toJson($options = 0) |
|
453 | - { |
|
454 | - $collection = new Collection(array_values($this->items)); |
|
455 | - |
|
456 | - return $collection->toJson($options); |
|
457 | - } |
|
13 | + /** |
|
14 | + * Wrapper Factory. |
|
15 | + * |
|
16 | + * @var \Analogue\ORM\System\Wrappers\Factory |
|
17 | + */ |
|
18 | + protected $factory; |
|
19 | + |
|
20 | + /** |
|
21 | + * EntityCollection constructor. |
|
22 | + * |
|
23 | + * @param array|null $entities |
|
24 | + */ |
|
25 | + public function __construct(array $entities = null) |
|
26 | + { |
|
27 | + $this->factory = new Factory(); |
|
28 | + |
|
29 | + parent::__construct($entities); |
|
30 | + } |
|
31 | + |
|
32 | + /** |
|
33 | + * Find an entity in the collection by key. |
|
34 | + * |
|
35 | + * @param mixed $key |
|
36 | + * @param mixed $default |
|
37 | + * |
|
38 | + * @throws MappingException |
|
39 | + * |
|
40 | + * @return \Analogue\ORM\Entity |
|
41 | + */ |
|
42 | + public function find($key, $default = null) |
|
43 | + { |
|
44 | + if ($key instanceof Mappable) { |
|
45 | + $key = $this->getEntityKey($key); |
|
46 | + } |
|
47 | + |
|
48 | + return array_first($this->items, function ($entity, $itemKey) use ($key) { |
|
49 | + return $this->getEntityKey($entity) == $key; |
|
50 | + }, $default); |
|
51 | + } |
|
52 | + |
|
53 | + /** |
|
54 | + * Add an entity to the collection. |
|
55 | + * |
|
56 | + * @param Mappable $entity |
|
57 | + * |
|
58 | + * @return $this |
|
59 | + */ |
|
60 | + public function add($entity) |
|
61 | + { |
|
62 | + $this->push($entity); |
|
63 | + |
|
64 | + return $this; |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * Remove an entity from the collection. |
|
69 | + * |
|
70 | + * @param $entity |
|
71 | + * |
|
72 | + * @throws MappingException |
|
73 | + * |
|
74 | + * @return mixed |
|
75 | + */ |
|
76 | + public function remove($entity) |
|
77 | + { |
|
78 | + $key = $this->getEntityKey($entity); |
|
79 | + |
|
80 | + return $this->pull($key); |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * Push an item onto the end of the collection. |
|
85 | + * |
|
86 | + * @param mixed $value |
|
87 | + * |
|
88 | + * @return void |
|
89 | + */ |
|
90 | + public function push($value) |
|
91 | + { |
|
92 | + $this->offsetSet(null, $value); |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * Put an item in the collection by key. |
|
97 | + * |
|
98 | + * @param mixed $key |
|
99 | + * @param mixed $value |
|
100 | + * |
|
101 | + * @return void |
|
102 | + */ |
|
103 | + public function put($key, $value) |
|
104 | + { |
|
105 | + $this->offsetSet($key, $value); |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * Set the item at a given offset. |
|
110 | + * |
|
111 | + * @param mixed $key |
|
112 | + * @param mixed $value |
|
113 | + * |
|
114 | + * @return void |
|
115 | + */ |
|
116 | + public function offsetSet($key, $value) |
|
117 | + { |
|
118 | + if (is_null($key)) { |
|
119 | + $this->items[] = $value; |
|
120 | + } else { |
|
121 | + $this->items[$key] = $value; |
|
122 | + } |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Determine if a key exists in the collection. |
|
127 | + * |
|
128 | + * @param mixed $key |
|
129 | + * @param mixed|null $value |
|
130 | + * |
|
131 | + * @return bool |
|
132 | + */ |
|
133 | + // public function contains($key, $value = null) |
|
134 | + // { |
|
135 | + // if (func_num_args() == 2) { |
|
136 | + // return !$this->where($key, $value)->isEmpty(); |
|
137 | + // } |
|
138 | + |
|
139 | + // if ($this->useAsCallable($key)) { |
|
140 | + // return !is_null($this->first($key)); |
|
141 | + // } |
|
142 | + |
|
143 | + // return !is_null($this->find($key)); |
|
144 | + // } |
|
145 | + |
|
146 | + /** |
|
147 | + * Fetch a nested element of the collection. |
|
148 | + * |
|
149 | + * @param string $key |
|
150 | + * |
|
151 | + * @return self |
|
152 | + */ |
|
153 | + public function fetch($key) |
|
154 | + { |
|
155 | + return new static(array_fetch($this->toArray(), $key)); |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Generic function for returning class.key value pairs. |
|
160 | + * |
|
161 | + * @throws MappingException |
|
162 | + * |
|
163 | + * @return string |
|
164 | + */ |
|
165 | + public function getEntityHashes() |
|
166 | + { |
|
167 | + return array_map(function ($entity) { |
|
168 | + $class = get_class($entity); |
|
169 | + |
|
170 | + $mapper = Manager::getMapper($class); |
|
171 | + |
|
172 | + $keyName = $mapper->getEntityMap()->getKeyName(); |
|
173 | + |
|
174 | + return $class.'.'.$entity->getEntityAttribute($keyName); |
|
175 | + }, |
|
176 | + $this->items); |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * Get a subset of the collection from entity hashes. |
|
181 | + * |
|
182 | + * @param array $hashes |
|
183 | + * |
|
184 | + * @throws MappingException |
|
185 | + * |
|
186 | + * @return array |
|
187 | + */ |
|
188 | + public function getSubsetByHashes(array $hashes) |
|
189 | + { |
|
190 | + $subset = []; |
|
191 | + |
|
192 | + foreach ($this->items as $item) { |
|
193 | + $class = get_class($item); |
|
194 | + |
|
195 | + $mapper = Manager::getMapper($class); |
|
196 | + |
|
197 | + $keyName = $mapper->getEntityMap()->getKeyName(); |
|
198 | + |
|
199 | + if (in_array($class.'.'.$item->$keyName, $hashes)) { |
|
200 | + $subset[] = $item; |
|
201 | + } |
|
202 | + } |
|
203 | + |
|
204 | + return $subset; |
|
205 | + } |
|
206 | + |
|
207 | + /** |
|
208 | + * Merge the collection with the given items. |
|
209 | + * |
|
210 | + * @param array $items |
|
211 | + * |
|
212 | + * @throws MappingException |
|
213 | + * |
|
214 | + * @return self |
|
215 | + */ |
|
216 | + public function merge($items) |
|
217 | + { |
|
218 | + $dictionary = $this->getDictionary(); |
|
219 | + |
|
220 | + foreach ($items as $item) { |
|
221 | + $dictionary[$this->getEntityKey($item)] = $item; |
|
222 | + } |
|
223 | + |
|
224 | + return new static(array_values($dictionary)); |
|
225 | + } |
|
226 | + |
|
227 | + /** |
|
228 | + * Diff the collection with the given items. |
|
229 | + * |
|
230 | + * @param \ArrayAccess|array $items |
|
231 | + * |
|
232 | + * @return self |
|
233 | + */ |
|
234 | + public function diff($items) |
|
235 | + { |
|
236 | + $diff = new static(); |
|
237 | + |
|
238 | + $dictionary = $this->getDictionary($items); |
|
239 | + |
|
240 | + foreach ($this->items as $item) { |
|
241 | + if (!isset($dictionary[$this->getEntityKey($item)])) { |
|
242 | + $diff->add($item); |
|
243 | + } |
|
244 | + } |
|
245 | + |
|
246 | + return $diff; |
|
247 | + } |
|
248 | + |
|
249 | + /** |
|
250 | + * Intersect the collection with the given items. |
|
251 | + * |
|
252 | + * @param \ArrayAccess|array $items |
|
253 | + * |
|
254 | + * @throws MappingException |
|
255 | + * |
|
256 | + * @return self |
|
257 | + */ |
|
258 | + public function intersect($items) |
|
259 | + { |
|
260 | + $intersect = new static(); |
|
261 | + |
|
262 | + $dictionary = $this->getDictionary($items); |
|
263 | + |
|
264 | + foreach ($this->items as $item) { |
|
265 | + if (isset($dictionary[$this->getEntityKey($item)])) { |
|
266 | + $intersect->add($item); |
|
267 | + } |
|
268 | + } |
|
269 | + |
|
270 | + return $intersect; |
|
271 | + } |
|
272 | + |
|
273 | + /** |
|
274 | + * Returns only the models from the collection with the specified keys. |
|
275 | + * |
|
276 | + * @param mixed $keys |
|
277 | + * |
|
278 | + * @return self |
|
279 | + */ |
|
280 | + public function only($keys) |
|
281 | + { |
|
282 | + $dictionary = array_only($this->getDictionary(), $keys); |
|
283 | + |
|
284 | + return new static(array_values($dictionary)); |
|
285 | + } |
|
286 | + |
|
287 | + /** |
|
288 | + * Returns all models in the collection except the models with specified keys. |
|
289 | + * |
|
290 | + * @param mixed $keys |
|
291 | + * |
|
292 | + * @return self |
|
293 | + */ |
|
294 | + public function except($keys) |
|
295 | + { |
|
296 | + $dictionary = array_except($this->getDictionary(), $keys); |
|
297 | + |
|
298 | + return new static(array_values($dictionary)); |
|
299 | + } |
|
300 | + |
|
301 | + /** |
|
302 | + * Get a dictionary keyed by primary keys. |
|
303 | + * |
|
304 | + * @param \ArrayAccess|array $items |
|
305 | + * |
|
306 | + * @throws MappingException |
|
307 | + * |
|
308 | + * @return array |
|
309 | + */ |
|
310 | + public function getDictionary($items = null) |
|
311 | + { |
|
312 | + $items = is_null($items) ? $this->items : $items; |
|
313 | + |
|
314 | + $dictionary = []; |
|
315 | + |
|
316 | + foreach ($items as $value) { |
|
317 | + $dictionary[$this->getEntityKey($value)] = $value; |
|
318 | + } |
|
319 | + |
|
320 | + return $dictionary; |
|
321 | + } |
|
322 | + |
|
323 | + /** |
|
324 | + * @throws MappingException |
|
325 | + * |
|
326 | + * @return array |
|
327 | + */ |
|
328 | + public function getEntityKeys() |
|
329 | + { |
|
330 | + return array_keys($this->getDictionary()); |
|
331 | + } |
|
332 | + |
|
333 | + /** |
|
334 | + * @param $entity |
|
335 | + * |
|
336 | + * @throws MappingException |
|
337 | + * |
|
338 | + * @return mixed |
|
339 | + */ |
|
340 | + protected function getEntityKey($entity) |
|
341 | + { |
|
342 | + $keyName = Manager::getMapper($entity)->getEntityMap()->getKeyName(); |
|
343 | + |
|
344 | + $wrapper = $this->factory->make($entity); |
|
345 | + |
|
346 | + return $wrapper->getEntityAttribute($keyName); |
|
347 | + } |
|
348 | + |
|
349 | + /** |
|
350 | + * Get the max value of a given key. |
|
351 | + * |
|
352 | + * @param string|null $key |
|
353 | + * |
|
354 | + * @throws MappingException |
|
355 | + * |
|
356 | + * @return mixed |
|
357 | + */ |
|
358 | + public function max($key = null) |
|
359 | + { |
|
360 | + return $this->reduce(function ($result, $item) use ($key) { |
|
361 | + $wrapper = $this->factory->make($item); |
|
362 | + |
|
363 | + return (is_null($result) || $wrapper->getEntityAttribute($key) > $result) ? |
|
364 | + $wrapper->getEntityAttribute($key) : $result; |
|
365 | + }); |
|
366 | + } |
|
367 | + |
|
368 | + /** |
|
369 | + * Get the min value of a given key. |
|
370 | + * |
|
371 | + * @param string|null $key |
|
372 | + * |
|
373 | + * @throws MappingException |
|
374 | + * |
|
375 | + * @return mixed |
|
376 | + */ |
|
377 | + public function min($key = null) |
|
378 | + { |
|
379 | + return $this->reduce(function ($result, $item) use ($key) { |
|
380 | + $wrapper = $this->factory->make($item); |
|
381 | + |
|
382 | + return (is_null($result) || $wrapper->getEntityAttribute($key) < $result) |
|
383 | + ? $wrapper->getEntityAttribute($key) : $result; |
|
384 | + }); |
|
385 | + } |
|
386 | + |
|
387 | + /** |
|
388 | + * Get an array with the values of a given key. |
|
389 | + * |
|
390 | + * @param string $value |
|
391 | + * @param string|null $key |
|
392 | + * |
|
393 | + * @return self |
|
394 | + */ |
|
395 | + public function pluck($value, $key = null) |
|
396 | + { |
|
397 | + return new Collection(Arr::pluck($this->items, $value, $key)); |
|
398 | + } |
|
399 | + |
|
400 | + /** |
|
401 | + * Alias for the "pluck" method. |
|
402 | + * |
|
403 | + * @param string $value |
|
404 | + * @param string|null $key |
|
405 | + * |
|
406 | + * @return self |
|
407 | + */ |
|
408 | + public function lists($value, $key = null) |
|
409 | + { |
|
410 | + return $this->pluck($value, $key); |
|
411 | + } |
|
412 | + |
|
413 | + /** |
|
414 | + * Return only unique items from the collection. |
|
415 | + * |
|
416 | + * @param string|null $key |
|
417 | + * @param bool $strict |
|
418 | + * |
|
419 | + * @throws MappingException |
|
420 | + * |
|
421 | + * @return self |
|
422 | + */ |
|
423 | + public function unique($key = null, $strict = false) |
|
424 | + { |
|
425 | + $dictionary = $this->getDictionary(); |
|
426 | + |
|
427 | + return new static(array_values($dictionary)); |
|
428 | + } |
|
429 | + |
|
430 | + /** |
|
431 | + * Get a base Support collection instance from this collection. |
|
432 | + * |
|
433 | + * @return \Illuminate\Support\Collection |
|
434 | + */ |
|
435 | + public function toBase() |
|
436 | + { |
|
437 | + return new Collection($this->items); |
|
438 | + } |
|
439 | + |
|
440 | + public function toArray() |
|
441 | + { |
|
442 | + return array_values(parent::toArray()); |
|
443 | + } |
|
444 | + |
|
445 | + /** |
|
446 | + * Get the collection of items as JSON. |
|
447 | + * |
|
448 | + * @param int $options |
|
449 | + * |
|
450 | + * @return string |
|
451 | + */ |
|
452 | + public function toJson($options = 0) |
|
453 | + { |
|
454 | + $collection = new Collection(array_values($this->items)); |
|
455 | + |
|
456 | + return $collection->toJson($options); |
|
457 | + } |
|
458 | 458 | } |
@@ -17,118 +17,118 @@ |
||
17 | 17 | */ |
18 | 18 | class Analogue |
19 | 19 | { |
20 | - /** |
|
21 | - * @var self |
|
22 | - */ |
|
23 | - protected static $instance; |
|
24 | - |
|
25 | - /** |
|
26 | - * @var Manager |
|
27 | - */ |
|
28 | - protected static $manager; |
|
29 | - |
|
30 | - /** |
|
31 | - * @var Capsule |
|
32 | - */ |
|
33 | - protected static $capsule; |
|
34 | - |
|
35 | - /** |
|
36 | - * @var bool |
|
37 | - */ |
|
38 | - protected static $booted = false; |
|
39 | - |
|
40 | - /** |
|
41 | - * Analogue constructor. |
|
42 | - * |
|
43 | - * @param array $connection |
|
44 | - */ |
|
45 | - public function __construct(array $connection) |
|
46 | - { |
|
47 | - if (!static::$booted) { |
|
48 | - static::$capsule = new Capsule(); |
|
49 | - |
|
50 | - $this->addConnection($connection); |
|
51 | - |
|
52 | - $this->boot(); |
|
53 | - } |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * Boot Analogue. |
|
58 | - * |
|
59 | - * @return Analogue |
|
60 | - */ |
|
61 | - public function boot() |
|
62 | - { |
|
63 | - if (static::$booted) { |
|
64 | - return $this; |
|
65 | - } |
|
66 | - |
|
67 | - $dispatcher = new Dispatcher(); |
|
68 | - |
|
69 | - $connectionProvider = new CapsuleConnectionProvider(static::$capsule); |
|
70 | - |
|
71 | - $illuminate = new IlluminateDriver($connectionProvider); |
|
72 | - |
|
73 | - $driverManager = new DriverManager(); |
|
74 | - |
|
75 | - $driverManager->addDriver($illuminate); |
|
76 | - |
|
77 | - static::$manager = new Manager($driverManager, $dispatcher); |
|
78 | - |
|
79 | - static::$instance = $this; |
|
80 | - |
|
81 | - static::$booted = true; |
|
82 | - |
|
83 | - return $this; |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Add a connection array to Capsule. |
|
88 | - * |
|
89 | - * @param array $config |
|
90 | - * @param string $name |
|
91 | - */ |
|
92 | - public function addConnection($config, $name = 'default') |
|
93 | - { |
|
94 | - static::$capsule->addConnection($config, $name); |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Get a Database connection object. |
|
99 | - * |
|
100 | - * @param $name |
|
101 | - * |
|
102 | - * @return \Illuminate\Database\Connection |
|
103 | - */ |
|
104 | - public function connection($name = null) |
|
105 | - { |
|
106 | - return static::$capsule->getConnection($name); |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * Dynamically handle static calls to the instance, Facade Style. |
|
111 | - * |
|
112 | - * @param string $method |
|
113 | - * @param array $parameters |
|
114 | - * |
|
115 | - * @return mixed |
|
116 | - */ |
|
117 | - public static function __callStatic($method, $parameters) |
|
118 | - { |
|
119 | - return call_user_func_array([static::$instance, $method], $parameters); |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Dynamically handle calls to the Analogue Manager instance. |
|
124 | - * |
|
125 | - * @param string $method |
|
126 | - * @param array $parameters |
|
127 | - * |
|
128 | - * @return mixed |
|
129 | - */ |
|
130 | - public function __call($method, $parameters) |
|
131 | - { |
|
132 | - return call_user_func_array([static::$manager, $method], $parameters); |
|
133 | - } |
|
20 | + /** |
|
21 | + * @var self |
|
22 | + */ |
|
23 | + protected static $instance; |
|
24 | + |
|
25 | + /** |
|
26 | + * @var Manager |
|
27 | + */ |
|
28 | + protected static $manager; |
|
29 | + |
|
30 | + /** |
|
31 | + * @var Capsule |
|
32 | + */ |
|
33 | + protected static $capsule; |
|
34 | + |
|
35 | + /** |
|
36 | + * @var bool |
|
37 | + */ |
|
38 | + protected static $booted = false; |
|
39 | + |
|
40 | + /** |
|
41 | + * Analogue constructor. |
|
42 | + * |
|
43 | + * @param array $connection |
|
44 | + */ |
|
45 | + public function __construct(array $connection) |
|
46 | + { |
|
47 | + if (!static::$booted) { |
|
48 | + static::$capsule = new Capsule(); |
|
49 | + |
|
50 | + $this->addConnection($connection); |
|
51 | + |
|
52 | + $this->boot(); |
|
53 | + } |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * Boot Analogue. |
|
58 | + * |
|
59 | + * @return Analogue |
|
60 | + */ |
|
61 | + public function boot() |
|
62 | + { |
|
63 | + if (static::$booted) { |
|
64 | + return $this; |
|
65 | + } |
|
66 | + |
|
67 | + $dispatcher = new Dispatcher(); |
|
68 | + |
|
69 | + $connectionProvider = new CapsuleConnectionProvider(static::$capsule); |
|
70 | + |
|
71 | + $illuminate = new IlluminateDriver($connectionProvider); |
|
72 | + |
|
73 | + $driverManager = new DriverManager(); |
|
74 | + |
|
75 | + $driverManager->addDriver($illuminate); |
|
76 | + |
|
77 | + static::$manager = new Manager($driverManager, $dispatcher); |
|
78 | + |
|
79 | + static::$instance = $this; |
|
80 | + |
|
81 | + static::$booted = true; |
|
82 | + |
|
83 | + return $this; |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Add a connection array to Capsule. |
|
88 | + * |
|
89 | + * @param array $config |
|
90 | + * @param string $name |
|
91 | + */ |
|
92 | + public function addConnection($config, $name = 'default') |
|
93 | + { |
|
94 | + static::$capsule->addConnection($config, $name); |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Get a Database connection object. |
|
99 | + * |
|
100 | + * @param $name |
|
101 | + * |
|
102 | + * @return \Illuminate\Database\Connection |
|
103 | + */ |
|
104 | + public function connection($name = null) |
|
105 | + { |
|
106 | + return static::$capsule->getConnection($name); |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * Dynamically handle static calls to the instance, Facade Style. |
|
111 | + * |
|
112 | + * @param string $method |
|
113 | + * @param array $parameters |
|
114 | + * |
|
115 | + * @return mixed |
|
116 | + */ |
|
117 | + public static function __callStatic($method, $parameters) |
|
118 | + { |
|
119 | + return call_user_func_array([static::$instance, $method], $parameters); |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Dynamically handle calls to the Analogue Manager instance. |
|
124 | + * |
|
125 | + * @param string $method |
|
126 | + * @param array $parameters |
|
127 | + * |
|
128 | + * @return mixed |
|
129 | + */ |
|
130 | + public function __call($method, $parameters) |
|
131 | + { |
|
132 | + return call_user_func_array([static::$manager, $method], $parameters); |
|
133 | + } |
|
134 | 134 | } |
@@ -15,141 +15,141 @@ |
||
15 | 15 | */ |
16 | 16 | class Repository |
17 | 17 | { |
18 | - /** |
|
19 | - * The mapper object for the corresponding entity. |
|
20 | - * |
|
21 | - * @var \Analogue\ORM\System\Mapper |
|
22 | - */ |
|
23 | - protected $mapper; |
|
18 | + /** |
|
19 | + * The mapper object for the corresponding entity. |
|
20 | + * |
|
21 | + * @var \Analogue\ORM\System\Mapper |
|
22 | + */ |
|
23 | + protected $mapper; |
|
24 | 24 | |
25 | - /** |
|
26 | - * To build a repository, either provide :. |
|
27 | - * |
|
28 | - * - Mappable object's class name as a string |
|
29 | - * - Mappable object instance |
|
30 | - * - Instance of mapper |
|
31 | - * |
|
32 | - * @param Mapper $mapper |
|
33 | - * @param EntityMap|null $entityMap (optional) |
|
34 | - * |
|
35 | - * @throws \InvalidArgumentException |
|
36 | - * @throws MappingException |
|
37 | - */ |
|
38 | - public function __construct($mapper, EntityMap $entityMap = null) |
|
39 | - { |
|
40 | - if ($mapper instanceof Mappable || is_string($mapper)) { |
|
41 | - $this->mapper = Manager::getMapper($mapper, $entityMap); |
|
42 | - } elseif ($mapper instanceof Mapper) { |
|
43 | - $this->mapper = $mapper; |
|
44 | - } else { |
|
45 | - new InvalidArgumentException('Repository class constructor need a valid Mapper or Mappable object.'); |
|
46 | - } |
|
47 | - } |
|
25 | + /** |
|
26 | + * To build a repository, either provide :. |
|
27 | + * |
|
28 | + * - Mappable object's class name as a string |
|
29 | + * - Mappable object instance |
|
30 | + * - Instance of mapper |
|
31 | + * |
|
32 | + * @param Mapper $mapper |
|
33 | + * @param EntityMap|null $entityMap (optional) |
|
34 | + * |
|
35 | + * @throws \InvalidArgumentException |
|
36 | + * @throws MappingException |
|
37 | + */ |
|
38 | + public function __construct($mapper, EntityMap $entityMap = null) |
|
39 | + { |
|
40 | + if ($mapper instanceof Mappable || is_string($mapper)) { |
|
41 | + $this->mapper = Manager::getMapper($mapper, $entityMap); |
|
42 | + } elseif ($mapper instanceof Mapper) { |
|
43 | + $this->mapper = $mapper; |
|
44 | + } else { |
|
45 | + new InvalidArgumentException('Repository class constructor need a valid Mapper or Mappable object.'); |
|
46 | + } |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * Return all Entities from database. |
|
51 | - * |
|
52 | - * @return \Analogue\ORM\EntityCollection |
|
53 | - */ |
|
54 | - public function all() |
|
55 | - { |
|
56 | - return $this->mapper->get(); |
|
57 | - } |
|
49 | + /** |
|
50 | + * Return all Entities from database. |
|
51 | + * |
|
52 | + * @return \Analogue\ORM\EntityCollection |
|
53 | + */ |
|
54 | + public function all() |
|
55 | + { |
|
56 | + return $this->mapper->get(); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Fetch a record from the database. |
|
61 | - * |
|
62 | - * @param int $id |
|
63 | - * |
|
64 | - * @return \Analogue\ORM\Mappable |
|
65 | - */ |
|
66 | - public function find($id) |
|
67 | - { |
|
68 | - return $this->mapper->find($id); |
|
69 | - } |
|
59 | + /** |
|
60 | + * Fetch a record from the database. |
|
61 | + * |
|
62 | + * @param int $id |
|
63 | + * |
|
64 | + * @return \Analogue\ORM\Mappable |
|
65 | + */ |
|
66 | + public function find($id) |
|
67 | + { |
|
68 | + return $this->mapper->find($id); |
|
69 | + } |
|
70 | 70 | |
71 | - /** |
|
72 | - * Get the first entity matching the given attributes. |
|
73 | - * |
|
74 | - * @param array $attributes |
|
75 | - * |
|
76 | - * @return \Analogue\ORM\Mappable|null |
|
77 | - */ |
|
78 | - public function firstMatching(array $attributes) |
|
79 | - { |
|
80 | - return $this->mapper->where($attributes)->first(); |
|
81 | - } |
|
71 | + /** |
|
72 | + * Get the first entity matching the given attributes. |
|
73 | + * |
|
74 | + * @param array $attributes |
|
75 | + * |
|
76 | + * @return \Analogue\ORM\Mappable|null |
|
77 | + */ |
|
78 | + public function firstMatching(array $attributes) |
|
79 | + { |
|
80 | + return $this->mapper->where($attributes)->first(); |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * Return all the entities matching the given attributes. |
|
85 | - * |
|
86 | - * @param array $attributes |
|
87 | - * |
|
88 | - * @return \Analogue\ORM\EntityCollection |
|
89 | - */ |
|
90 | - public function allMatching(array $attributes) |
|
91 | - { |
|
92 | - return $this->mapper->where($attributes)->get(); |
|
93 | - } |
|
83 | + /** |
|
84 | + * Return all the entities matching the given attributes. |
|
85 | + * |
|
86 | + * @param array $attributes |
|
87 | + * |
|
88 | + * @return \Analogue\ORM\EntityCollection |
|
89 | + */ |
|
90 | + public function allMatching(array $attributes) |
|
91 | + { |
|
92 | + return $this->mapper->where($attributes)->get(); |
|
93 | + } |
|
94 | 94 | |
95 | - /** |
|
96 | - * Return a paginator instance on the EntityCollection. |
|
97 | - * |
|
98 | - * @param int|null $perPage number of item per page (fallback on default setup in entity map) |
|
99 | - * |
|
100 | - * @return \Illuminate\Pagination\LengthAwarePaginator |
|
101 | - */ |
|
102 | - public function paginate($perPage = null) |
|
103 | - { |
|
104 | - return $this->mapper->paginate($perPage); |
|
105 | - } |
|
95 | + /** |
|
96 | + * Return a paginator instance on the EntityCollection. |
|
97 | + * |
|
98 | + * @param int|null $perPage number of item per page (fallback on default setup in entity map) |
|
99 | + * |
|
100 | + * @return \Illuminate\Pagination\LengthAwarePaginator |
|
101 | + */ |
|
102 | + public function paginate($perPage = null) |
|
103 | + { |
|
104 | + return $this->mapper->paginate($perPage); |
|
105 | + } |
|
106 | 106 | |
107 | - /** |
|
108 | - * Delete an entity or an entity collection from the database. |
|
109 | - * |
|
110 | - * @param Mappable|EntityCollection $entity |
|
111 | - * |
|
112 | - * @throws MappingException |
|
113 | - * @throws \InvalidArgumentException |
|
114 | - * |
|
115 | - * @return \Illuminate\Support\Collection|null |
|
116 | - */ |
|
117 | - public function delete($entity) |
|
118 | - { |
|
119 | - return $this->mapper->delete($entity); |
|
120 | - } |
|
107 | + /** |
|
108 | + * Delete an entity or an entity collection from the database. |
|
109 | + * |
|
110 | + * @param Mappable|EntityCollection $entity |
|
111 | + * |
|
112 | + * @throws MappingException |
|
113 | + * @throws \InvalidArgumentException |
|
114 | + * |
|
115 | + * @return \Illuminate\Support\Collection|null |
|
116 | + */ |
|
117 | + public function delete($entity) |
|
118 | + { |
|
119 | + return $this->mapper->delete($entity); |
|
120 | + } |
|
121 | 121 | |
122 | - /** |
|
123 | - * Persist an entity or an entity collection in the database. |
|
124 | - * |
|
125 | - * @param Mappable|EntityCollection|array $entity |
|
126 | - * |
|
127 | - * @throws MappingException |
|
128 | - * @throws \InvalidArgumentException |
|
129 | - * |
|
130 | - * @return Mappable|EntityCollection|array |
|
131 | - */ |
|
132 | - public function store($entity) |
|
133 | - { |
|
134 | - return $this->mapper->store($entity); |
|
135 | - } |
|
122 | + /** |
|
123 | + * Persist an entity or an entity collection in the database. |
|
124 | + * |
|
125 | + * @param Mappable|EntityCollection|array $entity |
|
126 | + * |
|
127 | + * @throws MappingException |
|
128 | + * @throws \InvalidArgumentException |
|
129 | + * |
|
130 | + * @return Mappable|EntityCollection|array |
|
131 | + */ |
|
132 | + public function store($entity) |
|
133 | + { |
|
134 | + return $this->mapper->store($entity); |
|
135 | + } |
|
136 | 136 | |
137 | - /** |
|
138 | - * Make custom mapper custom commands available in repository. |
|
139 | - * |
|
140 | - * @param string $method |
|
141 | - * @param array $parameters |
|
142 | - * |
|
143 | - * @throws Exception |
|
144 | - * |
|
145 | - * @return mixed |
|
146 | - */ |
|
147 | - public function __call($method, $parameters) |
|
148 | - { |
|
149 | - if ($this->mapper->hasCustomCommand($method)) { |
|
150 | - call_user_func_array([$this->mapper, $method], $parameters); |
|
151 | - } else { |
|
152 | - throw new Exception("No method $method on ".get_class($this)); |
|
153 | - } |
|
154 | - } |
|
137 | + /** |
|
138 | + * Make custom mapper custom commands available in repository. |
|
139 | + * |
|
140 | + * @param string $method |
|
141 | + * @param array $parameters |
|
142 | + * |
|
143 | + * @throws Exception |
|
144 | + * |
|
145 | + * @return mixed |
|
146 | + */ |
|
147 | + public function __call($method, $parameters) |
|
148 | + { |
|
149 | + if ($this->mapper->hasCustomCommand($method)) { |
|
150 | + call_user_func_array([$this->mapper, $method], $parameters); |
|
151 | + } else { |
|
152 | + throw new Exception("No method $method on ".get_class($this)); |
|
153 | + } |
|
154 | + } |
|
155 | 155 | } |
@@ -5,29 +5,29 @@ |
||
5 | 5 | |
6 | 6 | if (!function_exists('analogue')) { |
7 | 7 | |
8 | - /** |
|
9 | - * Return analogue's manager instance. |
|
10 | - * |
|
11 | - * @return \Analogue\ORM\System\Manager |
|
12 | - */ |
|
13 | - function analogue() |
|
14 | - { |
|
15 | - return Manager::getInstance(); |
|
16 | - } |
|
8 | + /** |
|
9 | + * Return analogue's manager instance. |
|
10 | + * |
|
11 | + * @return \Analogue\ORM\System\Manager |
|
12 | + */ |
|
13 | + function analogue() |
|
14 | + { |
|
15 | + return Manager::getInstance(); |
|
16 | + } |
|
17 | 17 | } |
18 | 18 | |
19 | 19 | if (!function_exists('mapper')) { |
20 | 20 | |
21 | - /** |
|
22 | - * Create a mapper for a given entity (static alias). |
|
23 | - * |
|
24 | - * @param \Analogue\ORM\Mappable|string $entity |
|
25 | - * @param mixed $entityMap |
|
26 | - * |
|
27 | - * @return Mapper |
|
28 | - */ |
|
29 | - function mapper($entity, $entityMap = null) |
|
30 | - { |
|
31 | - return Manager::getMapper($entity, $entityMap); |
|
32 | - } |
|
21 | + /** |
|
22 | + * Create a mapper for a given entity (static alias). |
|
23 | + * |
|
24 | + * @param \Analogue\ORM\Mappable|string $entity |
|
25 | + * @param mixed $entityMap |
|
26 | + * |
|
27 | + * @return Mapper |
|
28 | + */ |
|
29 | + function mapper($entity, $entityMap = null) |
|
30 | + { |
|
31 | + return Manager::getMapper($entity, $entityMap); |
|
32 | + } |
|
33 | 33 | } |
@@ -4,19 +4,19 @@ |
||
4 | 4 | |
5 | 5 | interface DriverInterface |
6 | 6 | { |
7 | - /** |
|
8 | - * Return the name of the driver. |
|
9 | - * |
|
10 | - * @return string |
|
11 | - */ |
|
12 | - public function getName(); |
|
7 | + /** |
|
8 | + * Return the name of the driver. |
|
9 | + * |
|
10 | + * @return string |
|
11 | + */ |
|
12 | + public function getName(); |
|
13 | 13 | |
14 | - /** |
|
15 | - * Get Analogue DB Adapter. |
|
16 | - * |
|
17 | - * @param string $connection connection name for drivers supporting multiple connections |
|
18 | - * |
|
19 | - * @return \Analogue\ORM\Drivers\DBAdapter |
|
20 | - */ |
|
21 | - public function getAdapter($connection = null); |
|
14 | + /** |
|
15 | + * Get Analogue DB Adapter. |
|
16 | + * |
|
17 | + * @param string $connection connection name for drivers supporting multiple connections |
|
18 | + * |
|
19 | + * @return \Analogue\ORM\Drivers\DBAdapter |
|
20 | + */ |
|
21 | + public function getAdapter($connection = null); |
|
22 | 22 | } |
@@ -6,30 +6,30 @@ |
||
6 | 6 | |
7 | 7 | class IlluminateConnectionProvider |
8 | 8 | { |
9 | - /** |
|
10 | - * @var DatabaseManager |
|
11 | - */ |
|
12 | - protected $db; |
|
9 | + /** |
|
10 | + * @var DatabaseManager |
|
11 | + */ |
|
12 | + protected $db; |
|
13 | 13 | |
14 | - /** |
|
15 | - * IlluminateConnectionProvider constructor. |
|
16 | - * |
|
17 | - * @param DatabaseManager $db |
|
18 | - */ |
|
19 | - public function __construct(DatabaseManager $db) |
|
20 | - { |
|
21 | - $this->db = $db; |
|
22 | - } |
|
14 | + /** |
|
15 | + * IlluminateConnectionProvider constructor. |
|
16 | + * |
|
17 | + * @param DatabaseManager $db |
|
18 | + */ |
|
19 | + public function __construct(DatabaseManager $db) |
|
20 | + { |
|
21 | + $this->db = $db; |
|
22 | + } |
|
23 | 23 | |
24 | - /** |
|
25 | - * Get a Database connection object. |
|
26 | - * |
|
27 | - * @param $name |
|
28 | - * |
|
29 | - * @return \Illuminate\Database\Connection |
|
30 | - */ |
|
31 | - public function connection($name = null) |
|
32 | - { |
|
33 | - return $this->db->connection($name); |
|
34 | - } |
|
24 | + /** |
|
25 | + * Get a Database connection object. |
|
26 | + * |
|
27 | + * @param $name |
|
28 | + * |
|
29 | + * @return \Illuminate\Database\Connection |
|
30 | + */ |
|
31 | + public function connection($name = null) |
|
32 | + { |
|
33 | + return $this->db->connection($name); |
|
34 | + } |
|
35 | 35 | } |
@@ -10,72 +10,72 @@ |
||
10 | 10 | */ |
11 | 11 | class IlluminateDBAdapter implements DBAdapter |
12 | 12 | { |
13 | - /** |
|
14 | - * @var Connection |
|
15 | - */ |
|
16 | - protected $connection; |
|
13 | + /** |
|
14 | + * @var Connection |
|
15 | + */ |
|
16 | + protected $connection; |
|
17 | 17 | |
18 | - /** |
|
19 | - * IlluminateDBAdapter constructor. |
|
20 | - * |
|
21 | - * @param Connection $connection |
|
22 | - */ |
|
23 | - public function __construct(Connection $connection) |
|
24 | - { |
|
25 | - $this->connection = $connection; |
|
26 | - } |
|
18 | + /** |
|
19 | + * IlluminateDBAdapter constructor. |
|
20 | + * |
|
21 | + * @param Connection $connection |
|
22 | + */ |
|
23 | + public function __construct(Connection $connection) |
|
24 | + { |
|
25 | + $this->connection = $connection; |
|
26 | + } |
|
27 | 27 | |
28 | - /** |
|
29 | - * Return a new Query instance for this driver. |
|
30 | - * |
|
31 | - * @return QueryAdapter |
|
32 | - */ |
|
33 | - public function getQuery() |
|
34 | - { |
|
35 | - $connection = $this->connection; |
|
28 | + /** |
|
29 | + * Return a new Query instance for this driver. |
|
30 | + * |
|
31 | + * @return QueryAdapter |
|
32 | + */ |
|
33 | + public function getQuery() |
|
34 | + { |
|
35 | + $connection = $this->connection; |
|
36 | 36 | |
37 | - $grammar = $connection->getQueryGrammar(); |
|
37 | + $grammar = $connection->getQueryGrammar(); |
|
38 | 38 | |
39 | - return new IlluminateQueryBuilder($connection, $grammar, $connection->getPostProcessor()); |
|
40 | - } |
|
39 | + return new IlluminateQueryBuilder($connection, $grammar, $connection->getPostProcessor()); |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * Get the date format supported by the current connection. |
|
44 | - * |
|
45 | - * @return string |
|
46 | - */ |
|
47 | - public function getDateFormat() |
|
48 | - { |
|
49 | - return $this->connection->getQueryGrammar()->getDateFormat(); |
|
50 | - } |
|
42 | + /** |
|
43 | + * Get the date format supported by the current connection. |
|
44 | + * |
|
45 | + * @return string |
|
46 | + */ |
|
47 | + public function getDateFormat() |
|
48 | + { |
|
49 | + return $this->connection->getQueryGrammar()->getDateFormat(); |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * Start a DB transaction on driver that supports it. |
|
54 | - * |
|
55 | - * @return void |
|
56 | - */ |
|
57 | - public function beginTransaction() |
|
58 | - { |
|
59 | - $this->connection->beginTransaction(); |
|
60 | - } |
|
52 | + /** |
|
53 | + * Start a DB transaction on driver that supports it. |
|
54 | + * |
|
55 | + * @return void |
|
56 | + */ |
|
57 | + public function beginTransaction() |
|
58 | + { |
|
59 | + $this->connection->beginTransaction(); |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * Commit a DB transaction on driver that supports it. |
|
64 | - * |
|
65 | - * @return void |
|
66 | - */ |
|
67 | - public function commit() |
|
68 | - { |
|
69 | - $this->connection->commit(); |
|
70 | - } |
|
62 | + /** |
|
63 | + * Commit a DB transaction on driver that supports it. |
|
64 | + * |
|
65 | + * @return void |
|
66 | + */ |
|
67 | + public function commit() |
|
68 | + { |
|
69 | + $this->connection->commit(); |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * Rollback a DB transaction. |
|
74 | - * |
|
75 | - * @return void |
|
76 | - */ |
|
77 | - public function rollback() |
|
78 | - { |
|
79 | - $this->connection->rollBack(); |
|
80 | - } |
|
72 | + /** |
|
73 | + * Rollback a DB transaction. |
|
74 | + * |
|
75 | + * @return void |
|
76 | + */ |
|
77 | + public function rollback() |
|
78 | + { |
|
79 | + $this->connection->rollBack(); |
|
80 | + } |
|
81 | 81 | } |
@@ -28,7 +28,7 @@ |
||
28 | 28 | /** |
29 | 29 | * Return a new Query instance for this driver. |
30 | 30 | * |
31 | - * @return QueryAdapter |
|
31 | + * @return IlluminateQueryBuilder |
|
32 | 32 | */ |
33 | 33 | public function getQuery() |
34 | 34 | { |