@@ -14,235 +14,235 @@ |
||
14 | 14 | */ |
15 | 15 | class Store extends Command |
16 | 16 | { |
17 | - /** |
|
18 | - * Persist the entity in the database |
|
19 | - * |
|
20 | - * @throws \InvalidArgumentException |
|
21 | - * @return false|mixed |
|
22 | - */ |
|
23 | - public function execute() |
|
24 | - { |
|
25 | - $entity = $this->aggregate->getEntityObject(); |
|
26 | - |
|
27 | - $mapper = $this->aggregate->getMapper(); |
|
28 | - |
|
29 | - if ($mapper->fireEvent('storing', $entity) === false) { |
|
30 | - return false; |
|
31 | - } |
|
32 | - |
|
33 | - $this->preStoreProcess(); |
|
34 | - |
|
35 | - /** |
|
36 | - * We will test the entity for existence |
|
37 | - * and run a creation if it doesn't exists |
|
38 | - */ |
|
39 | - if (!$this->aggregate->exists()) { |
|
40 | - if ($mapper->fireEvent('creating', $entity) === false) { |
|
41 | - return false; |
|
42 | - } |
|
43 | - |
|
44 | - $this->insert(); |
|
45 | - |
|
46 | - $mapper->fireEvent('created', $entity, false); |
|
47 | - } |
|
48 | - else if ($this->aggregate->isDirty()) { |
|
49 | - if ($mapper->fireEvent('updating', $entity) === false) { |
|
50 | - return false; |
|
51 | - } |
|
52 | - $this->update(); |
|
53 | - |
|
54 | - $mapper->fireEvent('updated', $entity, false); |
|
55 | - } |
|
56 | - |
|
57 | - $this->postStoreProcess(); |
|
58 | - |
|
59 | - $mapper->fireEvent('stored', $entity, false); |
|
60 | - |
|
61 | - return $entity; |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * Run all operations that have to occur before actually |
|
66 | - * storing the entity |
|
67 | - * |
|
68 | - * @throws \InvalidArgumentException |
|
69 | - * @return void |
|
70 | - */ |
|
71 | - protected function preStoreProcess() |
|
72 | - { |
|
73 | - // Create any related object that doesn't exist in the database. |
|
74 | - $localRelationships = $this->aggregate->getEntityMap()->getLocalRelationships(); |
|
75 | - |
|
76 | - $this->createRelatedEntities($localRelationships); |
|
77 | - |
|
78 | - // Now we can sync the related collections |
|
79 | - $this->aggregate->syncRelationships($localRelationships); |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Check for existence and create non-existing related entities |
|
84 | - * |
|
85 | - * @param array |
|
86 | - * @throws \InvalidArgumentException |
|
87 | - * @return void |
|
88 | - */ |
|
89 | - protected function createRelatedEntities($relations) |
|
90 | - { |
|
91 | - $entitiesToCreate = $this->aggregate->getNonExistingRelated($relations); |
|
92 | - |
|
93 | - foreach ($entitiesToCreate as $aggregate) { |
|
94 | - $this->createStoreCommand($aggregate)->execute(); |
|
95 | - } |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Create a new store command |
|
100 | - * |
|
101 | - * @param Aggregate $aggregate |
|
102 | - * @return Store |
|
103 | - */ |
|
104 | - protected function createStoreCommand(Aggregate $aggregate) |
|
105 | - { |
|
106 | - // We gotta retrieve the corresponding query adapter to use. |
|
107 | - $mapper = $aggregate->getMapper(); |
|
108 | - |
|
109 | - return new Store($aggregate, $mapper->newQueryBuilder()); |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * Run all operations that have to occur after the entity |
|
114 | - * is stored. |
|
115 | - * |
|
116 | - * @throws \InvalidArgumentException |
|
117 | - * @return void |
|
118 | - */ |
|
119 | - protected function postStoreProcess() |
|
120 | - { |
|
121 | - $aggregate = $this->aggregate; |
|
122 | - |
|
123 | - // Create any related object that doesn't exist in the database. |
|
124 | - $foreignRelationships = $aggregate->getEntityMap()->getForeignRelationships(); |
|
125 | - $this->createRelatedEntities($foreignRelationships); |
|
126 | - |
|
127 | - // Update any pivot tables that has been modified. |
|
128 | - $aggregate->updatePivotRecords(); |
|
129 | - |
|
130 | - // Update any dirty relationship. This include relationships that already exists, have |
|
131 | - // dirty attributes / newly created related entities / dirty related entities. |
|
132 | - $dirtyRelatedAggregates = $aggregate->getDirtyRelationships(); |
|
133 | - |
|
134 | - foreach ($dirtyRelatedAggregates as $related) { |
|
135 | - $this->createStoreCommand($related)->execute(); |
|
136 | - } |
|
137 | - |
|
138 | - // Now we can sync the related collections |
|
139 | - if ($this->aggregate->exists()) { |
|
140 | - $this->aggregate->syncRelationships($foreignRelationships); |
|
141 | - } |
|
17 | + /** |
|
18 | + * Persist the entity in the database |
|
19 | + * |
|
20 | + * @throws \InvalidArgumentException |
|
21 | + * @return false|mixed |
|
22 | + */ |
|
23 | + public function execute() |
|
24 | + { |
|
25 | + $entity = $this->aggregate->getEntityObject(); |
|
26 | + |
|
27 | + $mapper = $this->aggregate->getMapper(); |
|
28 | + |
|
29 | + if ($mapper->fireEvent('storing', $entity) === false) { |
|
30 | + return false; |
|
31 | + } |
|
32 | + |
|
33 | + $this->preStoreProcess(); |
|
34 | + |
|
35 | + /** |
|
36 | + * We will test the entity for existence |
|
37 | + * and run a creation if it doesn't exists |
|
38 | + */ |
|
39 | + if (!$this->aggregate->exists()) { |
|
40 | + if ($mapper->fireEvent('creating', $entity) === false) { |
|
41 | + return false; |
|
42 | + } |
|
43 | + |
|
44 | + $this->insert(); |
|
45 | + |
|
46 | + $mapper->fireEvent('created', $entity, false); |
|
47 | + } |
|
48 | + else if ($this->aggregate->isDirty()) { |
|
49 | + if ($mapper->fireEvent('updating', $entity) === false) { |
|
50 | + return false; |
|
51 | + } |
|
52 | + $this->update(); |
|
53 | + |
|
54 | + $mapper->fireEvent('updated', $entity, false); |
|
55 | + } |
|
56 | + |
|
57 | + $this->postStoreProcess(); |
|
58 | + |
|
59 | + $mapper->fireEvent('stored', $entity, false); |
|
60 | + |
|
61 | + return $entity; |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * Run all operations that have to occur before actually |
|
66 | + * storing the entity |
|
67 | + * |
|
68 | + * @throws \InvalidArgumentException |
|
69 | + * @return void |
|
70 | + */ |
|
71 | + protected function preStoreProcess() |
|
72 | + { |
|
73 | + // Create any related object that doesn't exist in the database. |
|
74 | + $localRelationships = $this->aggregate->getEntityMap()->getLocalRelationships(); |
|
75 | + |
|
76 | + $this->createRelatedEntities($localRelationships); |
|
77 | + |
|
78 | + // Now we can sync the related collections |
|
79 | + $this->aggregate->syncRelationships($localRelationships); |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Check for existence and create non-existing related entities |
|
84 | + * |
|
85 | + * @param array |
|
86 | + * @throws \InvalidArgumentException |
|
87 | + * @return void |
|
88 | + */ |
|
89 | + protected function createRelatedEntities($relations) |
|
90 | + { |
|
91 | + $entitiesToCreate = $this->aggregate->getNonExistingRelated($relations); |
|
92 | + |
|
93 | + foreach ($entitiesToCreate as $aggregate) { |
|
94 | + $this->createStoreCommand($aggregate)->execute(); |
|
95 | + } |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Create a new store command |
|
100 | + * |
|
101 | + * @param Aggregate $aggregate |
|
102 | + * @return Store |
|
103 | + */ |
|
104 | + protected function createStoreCommand(Aggregate $aggregate) |
|
105 | + { |
|
106 | + // We gotta retrieve the corresponding query adapter to use. |
|
107 | + $mapper = $aggregate->getMapper(); |
|
108 | + |
|
109 | + return new Store($aggregate, $mapper->newQueryBuilder()); |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * Run all operations that have to occur after the entity |
|
114 | + * is stored. |
|
115 | + * |
|
116 | + * @throws \InvalidArgumentException |
|
117 | + * @return void |
|
118 | + */ |
|
119 | + protected function postStoreProcess() |
|
120 | + { |
|
121 | + $aggregate = $this->aggregate; |
|
122 | + |
|
123 | + // Create any related object that doesn't exist in the database. |
|
124 | + $foreignRelationships = $aggregate->getEntityMap()->getForeignRelationships(); |
|
125 | + $this->createRelatedEntities($foreignRelationships); |
|
126 | + |
|
127 | + // Update any pivot tables that has been modified. |
|
128 | + $aggregate->updatePivotRecords(); |
|
129 | + |
|
130 | + // Update any dirty relationship. This include relationships that already exists, have |
|
131 | + // dirty attributes / newly created related entities / dirty related entities. |
|
132 | + $dirtyRelatedAggregates = $aggregate->getDirtyRelationships(); |
|
133 | + |
|
134 | + foreach ($dirtyRelatedAggregates as $related) { |
|
135 | + $this->createStoreCommand($related)->execute(); |
|
136 | + } |
|
137 | + |
|
138 | + // Now we can sync the related collections |
|
139 | + if ($this->aggregate->exists()) { |
|
140 | + $this->aggregate->syncRelationships($foreignRelationships); |
|
141 | + } |
|
142 | 142 | |
143 | - // TODO be move it to the wrapper class |
|
144 | - // so it's the same code for the entity builder |
|
145 | - $aggregate->setProxies(); |
|
146 | - |
|
147 | - // Update Entity Cache |
|
148 | - $aggregate->getMapper()->getEntityCache()->refresh($aggregate); |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * Update Related Entities which attributes have |
|
153 | - * been modified. |
|
154 | - * |
|
155 | - * @return void |
|
156 | - */ |
|
157 | - protected function updateDirtyRelated() |
|
158 | - { |
|
159 | - $relations = $this->entityMap->getRelationships(); |
|
160 | - $attributes = $this->getAttributes(); |
|
161 | - |
|
162 | - foreach ($relations as $relation) { |
|
163 | - if (!array_key_exists($relation, $attributes)) { |
|
164 | - continue; |
|
165 | - } |
|
166 | - |
|
167 | - $value = $attributes[$relation]; |
|
168 | - |
|
169 | - if ($value == null) { |
|
170 | - continue; |
|
171 | - } |
|
172 | - |
|
173 | - if ($value instanceof EntityProxy) { |
|
174 | - continue; |
|
175 | - } |
|
176 | - |
|
177 | - if ($value instanceof CollectionProxy && $value->isLoaded()) { |
|
178 | - $value = $value->getUnderlyingCollection(); |
|
179 | - } |
|
180 | - if ($value instanceof CollectionProxy && !$value->isLoaded()) { |
|
181 | - foreach ($value->getAddedItems() as $entity) { |
|
182 | - $this->updateEntityIfDirty($entity); |
|
183 | - } |
|
184 | - continue; |
|
185 | - } |
|
186 | - |
|
187 | - if ($value instanceof EntityCollection) { |
|
188 | - foreach ($value as $entity) { |
|
189 | - if (!$this->createEntityIfNotExists($entity)) { |
|
190 | - $this->updateEntityIfDirty($entity); |
|
191 | - } |
|
192 | - } |
|
193 | - continue; |
|
194 | - } |
|
195 | - if ($value instanceof Mappable) { |
|
196 | - $this->updateEntityIfDirty($value); |
|
197 | - continue; |
|
198 | - } |
|
199 | - } |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * Execute an insert statement on the database |
|
204 | - * |
|
205 | - * @return void |
|
206 | - */ |
|
207 | - protected function insert() |
|
208 | - { |
|
209 | - $aggregate = $this->aggregate; |
|
210 | - |
|
211 | - $attributes = $aggregate->getRawAttributes(); |
|
143 | + // TODO be move it to the wrapper class |
|
144 | + // so it's the same code for the entity builder |
|
145 | + $aggregate->setProxies(); |
|
146 | + |
|
147 | + // Update Entity Cache |
|
148 | + $aggregate->getMapper()->getEntityCache()->refresh($aggregate); |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * Update Related Entities which attributes have |
|
153 | + * been modified. |
|
154 | + * |
|
155 | + * @return void |
|
156 | + */ |
|
157 | + protected function updateDirtyRelated() |
|
158 | + { |
|
159 | + $relations = $this->entityMap->getRelationships(); |
|
160 | + $attributes = $this->getAttributes(); |
|
161 | + |
|
162 | + foreach ($relations as $relation) { |
|
163 | + if (!array_key_exists($relation, $attributes)) { |
|
164 | + continue; |
|
165 | + } |
|
166 | + |
|
167 | + $value = $attributes[$relation]; |
|
168 | + |
|
169 | + if ($value == null) { |
|
170 | + continue; |
|
171 | + } |
|
172 | + |
|
173 | + if ($value instanceof EntityProxy) { |
|
174 | + continue; |
|
175 | + } |
|
176 | + |
|
177 | + if ($value instanceof CollectionProxy && $value->isLoaded()) { |
|
178 | + $value = $value->getUnderlyingCollection(); |
|
179 | + } |
|
180 | + if ($value instanceof CollectionProxy && !$value->isLoaded()) { |
|
181 | + foreach ($value->getAddedItems() as $entity) { |
|
182 | + $this->updateEntityIfDirty($entity); |
|
183 | + } |
|
184 | + continue; |
|
185 | + } |
|
186 | + |
|
187 | + if ($value instanceof EntityCollection) { |
|
188 | + foreach ($value as $entity) { |
|
189 | + if (!$this->createEntityIfNotExists($entity)) { |
|
190 | + $this->updateEntityIfDirty($entity); |
|
191 | + } |
|
192 | + } |
|
193 | + continue; |
|
194 | + } |
|
195 | + if ($value instanceof Mappable) { |
|
196 | + $this->updateEntityIfDirty($value); |
|
197 | + continue; |
|
198 | + } |
|
199 | + } |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * Execute an insert statement on the database |
|
204 | + * |
|
205 | + * @return void |
|
206 | + */ |
|
207 | + protected function insert() |
|
208 | + { |
|
209 | + $aggregate = $this->aggregate; |
|
210 | + |
|
211 | + $attributes = $aggregate->getRawAttributes(); |
|
212 | 212 | |
213 | - $keyName = $aggregate->getEntityMap()->getKeyName(); |
|
213 | + $keyName = $aggregate->getEntityMap()->getKeyName(); |
|
214 | 214 | |
215 | - // Check if the primary key is defined in the attributes |
|
216 | - if (array_key_exists($keyName, $attributes) && $attributes[$keyName] != null) { |
|
217 | - $this->query->insert($attributes); |
|
218 | - } else { |
|
219 | - $sequence = $aggregate->getEntityMap()->getSequence(); |
|
215 | + // Check if the primary key is defined in the attributes |
|
216 | + if (array_key_exists($keyName, $attributes) && $attributes[$keyName] != null) { |
|
217 | + $this->query->insert($attributes); |
|
218 | + } else { |
|
219 | + $sequence = $aggregate->getEntityMap()->getSequence(); |
|
220 | 220 | |
221 | - $id = $this->query->insertGetId($attributes, $sequence); |
|
221 | + $id = $this->query->insertGetId($attributes, $sequence); |
|
222 | 222 | |
223 | - $aggregate->setEntityAttribute($keyName, $id); |
|
224 | - } |
|
225 | - } |
|
223 | + $aggregate->setEntityAttribute($keyName, $id); |
|
224 | + } |
|
225 | + } |
|
226 | 226 | |
227 | - /** |
|
228 | - * Run an update statement on the entity |
|
229 | - * |
|
230 | - * @throws \InvalidArgumentException |
|
231 | - * |
|
232 | - * @return void |
|
233 | - */ |
|
234 | - protected function update() |
|
235 | - { |
|
236 | - $query = $this->query; |
|
227 | + /** |
|
228 | + * Run an update statement on the entity |
|
229 | + * |
|
230 | + * @throws \InvalidArgumentException |
|
231 | + * |
|
232 | + * @return void |
|
233 | + */ |
|
234 | + protected function update() |
|
235 | + { |
|
236 | + $query = $this->query; |
|
237 | 237 | |
238 | - $keyName = $this->aggregate->getEntityKey(); |
|
238 | + $keyName = $this->aggregate->getEntityKey(); |
|
239 | 239 | |
240 | - $query = $query->where($keyName, '=', $this->aggregate->getEntityId()); |
|
240 | + $query = $query->where($keyName, '=', $this->aggregate->getEntityId()); |
|
241 | 241 | |
242 | - $dirtyAttributes = $this->aggregate->getDirtyRawAttributes(); |
|
242 | + $dirtyAttributes = $this->aggregate->getDirtyRawAttributes(); |
|
243 | 243 | |
244 | - if (count($dirtyAttributes) > 0) { |
|
245 | - $query->update($dirtyAttributes); |
|
246 | - } |
|
247 | - } |
|
244 | + if (count($dirtyAttributes) > 0) { |
|
245 | + $query->update($dirtyAttributes); |
|
246 | + } |
|
247 | + } |
|
248 | 248 | } |
@@ -44,8 +44,7 @@ |
||
44 | 44 | $this->insert(); |
45 | 45 | |
46 | 46 | $mapper->fireEvent('created', $entity, false); |
47 | - } |
|
48 | - else if ($this->aggregate->isDirty()) { |
|
47 | + } else if ($this->aggregate->isDirty()) { |
|
49 | 48 | if ($mapper->fireEvent('updating', $entity) === false) { |
50 | 49 | return false; |
51 | 50 | } |
@@ -6,5 +6,5 @@ |
||
6 | 6 | |
7 | 7 | class EntityMapNotFoundException extends RuntimeException |
8 | 8 | { |
9 | - // |
|
9 | + // |
|
10 | 10 | } |
@@ -6,113 +6,113 @@ |
||
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 = []; |
|
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 | 17 | |
18 | - /** |
|
19 | - * Return the entity's attribute |
|
20 | - * @param string $key |
|
21 | - * @return mixed |
|
22 | - */ |
|
23 | - public function __get($key) |
|
24 | - { |
|
25 | - if ($this->hasGetMutator($key)) { |
|
26 | - $method = 'get' . $this->getMutatorMethod($key); |
|
18 | + /** |
|
19 | + * Return the entity's attribute |
|
20 | + * @param string $key |
|
21 | + * @return mixed |
|
22 | + */ |
|
23 | + public function __get($key) |
|
24 | + { |
|
25 | + if ($this->hasGetMutator($key)) { |
|
26 | + $method = 'get' . $this->getMutatorMethod($key); |
|
27 | 27 | |
28 | - $attribute = null; |
|
28 | + $attribute = null; |
|
29 | 29 | |
30 | - if (isset($this->attributes[$key])) { |
|
31 | - $attribute = $this->attributes[$key]; |
|
32 | - } |
|
30 | + if (isset($this->attributes[$key])) { |
|
31 | + $attribute = $this->attributes[$key]; |
|
32 | + } |
|
33 | 33 | |
34 | - return $this->$method($attribute); |
|
35 | - } |
|
36 | - if (!array_key_exists($key, $this->attributes)) { |
|
37 | - return null; |
|
38 | - } |
|
39 | - if ($this->attributes[$key] instanceof EntityProxy) { |
|
40 | - $this->attributes[$key] = $this->attributes[$key]->load(); |
|
41 | - } |
|
42 | - return $this->attributes[$key]; |
|
43 | - } |
|
34 | + return $this->$method($attribute); |
|
35 | + } |
|
36 | + if (!array_key_exists($key, $this->attributes)) { |
|
37 | + return null; |
|
38 | + } |
|
39 | + if ($this->attributes[$key] instanceof EntityProxy) { |
|
40 | + $this->attributes[$key] = $this->attributes[$key]->load(); |
|
41 | + } |
|
42 | + return $this->attributes[$key]; |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * Dynamically set attributes on the entity. |
|
47 | - * |
|
48 | - * @param string $key |
|
49 | - * @param mixed $value |
|
50 | - * @return void |
|
51 | - */ |
|
52 | - public function __set($key, $value) |
|
53 | - { |
|
54 | - if ($this->hasSetMutator($key)) { |
|
55 | - $method = 'set' . $this->getMutatorMethod($key); |
|
45 | + /** |
|
46 | + * Dynamically set attributes on the entity. |
|
47 | + * |
|
48 | + * @param string $key |
|
49 | + * @param mixed $value |
|
50 | + * @return void |
|
51 | + */ |
|
52 | + public function __set($key, $value) |
|
53 | + { |
|
54 | + if ($this->hasSetMutator($key)) { |
|
55 | + $method = 'set' . $this->getMutatorMethod($key); |
|
56 | 56 | |
57 | - $this->$method($value); |
|
58 | - } else { |
|
59 | - $this->attributes[$key] = $value; |
|
60 | - } |
|
61 | - } |
|
57 | + $this->$method($value); |
|
58 | + } else { |
|
59 | + $this->attributes[$key] = $value; |
|
60 | + } |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * Is a getter method defined ? |
|
65 | - * |
|
66 | - * @param string $key |
|
67 | - * @return boolean |
|
68 | - */ |
|
69 | - protected function hasGetMutator($key) |
|
70 | - { |
|
71 | - return method_exists($this, 'get' . $this->getMutatorMethod($key)) ? true : false; |
|
72 | - } |
|
63 | + /** |
|
64 | + * Is a getter method defined ? |
|
65 | + * |
|
66 | + * @param string $key |
|
67 | + * @return boolean |
|
68 | + */ |
|
69 | + protected function hasGetMutator($key) |
|
70 | + { |
|
71 | + return method_exists($this, 'get' . $this->getMutatorMethod($key)) ? true : false; |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * Is a setter method defined ? |
|
76 | - * |
|
77 | - * @param string $key |
|
78 | - * @return boolean |
|
79 | - */ |
|
80 | - protected function hasSetMutator($key) |
|
81 | - { |
|
82 | - return method_exists($this, 'set' . $this->getMutatorMethod($key)) ? true : false; |
|
83 | - } |
|
74 | + /** |
|
75 | + * Is a setter method defined ? |
|
76 | + * |
|
77 | + * @param string $key |
|
78 | + * @return boolean |
|
79 | + */ |
|
80 | + protected function hasSetMutator($key) |
|
81 | + { |
|
82 | + return method_exists($this, 'set' . $this->getMutatorMethod($key)) ? true : false; |
|
83 | + } |
|
84 | 84 | |
85 | - /** |
|
86 | - * @param $key |
|
87 | - * @return string |
|
88 | - */ |
|
89 | - protected function getMutatorMethod($key) |
|
90 | - { |
|
91 | - $key = ucwords(str_replace(['-', '_'], ' ', $key)); |
|
92 | - return str_replace(' ', '', $key) . "Attribute"; |
|
93 | - } |
|
85 | + /** |
|
86 | + * @param $key |
|
87 | + * @return string |
|
88 | + */ |
|
89 | + protected function getMutatorMethod($key) |
|
90 | + { |
|
91 | + $key = ucwords(str_replace(['-', '_'], ' ', $key)); |
|
92 | + return str_replace(' ', '', $key) . "Attribute"; |
|
93 | + } |
|
94 | 94 | |
95 | - /** |
|
96 | - * Convert every attributes to value / arrays |
|
97 | - * |
|
98 | - * @return array |
|
99 | - */ |
|
100 | - public function toArray() |
|
101 | - { |
|
102 | - // First, call the trait method before filtering |
|
103 | - // with Entity specific methods |
|
104 | - $attributes = $this->attributesToArray($this->attributes); |
|
95 | + /** |
|
96 | + * Convert every attributes to value / arrays |
|
97 | + * |
|
98 | + * @return array |
|
99 | + */ |
|
100 | + public function toArray() |
|
101 | + { |
|
102 | + // First, call the trait method before filtering |
|
103 | + // with Entity specific methods |
|
104 | + $attributes = $this->attributesToArray($this->attributes); |
|
105 | 105 | |
106 | - foreach ($this->attributes as $key => $attribute) { |
|
107 | - if (in_array($key, $this->hidden)) { |
|
108 | - unset($attributes[$key]); |
|
109 | - continue; |
|
110 | - } |
|
111 | - if ($this->hasGetMutator($key)) { |
|
112 | - $method = 'get' . $this->getMutatorMethod($key); |
|
113 | - $attributes[$key] = $this->$method($attribute); |
|
114 | - } |
|
115 | - } |
|
116 | - return $attributes; |
|
117 | - } |
|
106 | + foreach ($this->attributes as $key => $attribute) { |
|
107 | + if (in_array($key, $this->hidden)) { |
|
108 | + unset($attributes[$key]); |
|
109 | + continue; |
|
110 | + } |
|
111 | + if ($this->hasGetMutator($key)) { |
|
112 | + $method = 'get' . $this->getMutatorMethod($key); |
|
113 | + $attributes[$key] = $this->$method($attribute); |
|
114 | + } |
|
115 | + } |
|
116 | + return $attributes; |
|
117 | + } |
|
118 | 118 | } |
@@ -23,7 +23,7 @@ discard block |
||
23 | 23 | public function __get($key) |
24 | 24 | { |
25 | 25 | if ($this->hasGetMutator($key)) { |
26 | - $method = 'get' . $this->getMutatorMethod($key); |
|
26 | + $method = 'get'.$this->getMutatorMethod($key); |
|
27 | 27 | |
28 | 28 | $attribute = null; |
29 | 29 | |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | public function __set($key, $value) |
53 | 53 | { |
54 | 54 | if ($this->hasSetMutator($key)) { |
55 | - $method = 'set' . $this->getMutatorMethod($key); |
|
55 | + $method = 'set'.$this->getMutatorMethod($key); |
|
56 | 56 | |
57 | 57 | $this->$method($value); |
58 | 58 | } else { |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | */ |
69 | 69 | protected function hasGetMutator($key) |
70 | 70 | { |
71 | - return method_exists($this, 'get' . $this->getMutatorMethod($key)) ? true : false; |
|
71 | + return method_exists($this, 'get'.$this->getMutatorMethod($key)) ? true : false; |
|
72 | 72 | } |
73 | 73 | |
74 | 74 | /** |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | */ |
80 | 80 | protected function hasSetMutator($key) |
81 | 81 | { |
82 | - return method_exists($this, 'set' . $this->getMutatorMethod($key)) ? true : false; |
|
82 | + return method_exists($this, 'set'.$this->getMutatorMethod($key)) ? true : false; |
|
83 | 83 | } |
84 | 84 | |
85 | 85 | /** |
@@ -89,7 +89,7 @@ discard block |
||
89 | 89 | protected function getMutatorMethod($key) |
90 | 90 | { |
91 | 91 | $key = ucwords(str_replace(['-', '_'], ' ', $key)); |
92 | - return str_replace(' ', '', $key) . "Attribute"; |
|
92 | + return str_replace(' ', '', $key)."Attribute"; |
|
93 | 93 | } |
94 | 94 | |
95 | 95 | /** |
@@ -109,7 +109,7 @@ discard block |
||
109 | 109 | continue; |
110 | 110 | } |
111 | 111 | if ($this->hasGetMutator($key)) { |
112 | - $method = 'get' . $this->getMutatorMethod($key); |
|
112 | + $method = 'get'.$this->getMutatorMethod($key); |
|
113 | 113 | $attributes[$key] = $this->$method($attribute); |
114 | 114 | } |
115 | 115 | } |