-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cleaner.php
263 lines (226 loc) · 6.59 KB
/
Cleaner.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
namespace LaravelLaundromat;
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Model;
class Cleaner
{
/**
* Properties allowed on the clean object.
*
* @var array
*/
protected $allowed = [];
/**
* Methods to run. Returned value will be stored as a snake case property
* on the clean object.
*
* @var array
*/
protected $methods = [];
/**
* Clean the dirty object.
*
* @param object $dirty
*
* @return this
*/
public function clean($dirty)
{
$this->setAllowed($dirty);
$this->setMethods($dirty);
return $this;
}
/**
* Call snake_cased property names with studlyCased methods.
*
* @param string $name [Name of method called]
* @param array $arguments
*
* @return mixed
*/
public function __call($name, $arguments)
{
$name = snake_case($name);
return $this->{$name};
}
/**
* Set allowed properties on cleaner object.
*
* @param object $dirty
*/
protected function setAllowed($dirty)
{
collect($this->allowed)->each(function ($property) use ($dirty) {
if ($this->isNested($property)) {
return $this->setNestedProperty($property, $dirty, 'property');
}
$this->setProperty($dirty, $property, $this, 'property');
});
}
/**
* Set properties from methods on cleaner object.
*
* @param object $dirty
*/
protected function setMethods($dirty)
{
collect($this->methods)->each(function ($method) use ($dirty) {
if ($this->isNested($method)) {
return $this->setNestedProperty($method, $dirty, 'method');
}
$this->setProperty($dirty, $method, $this, 'method');
});
}
/**
* Property is a nested property.
*
* @param string $property
*
* @return bool
*/
protected function isNested($property)
{
return strpos($property, '.') !== false;
}
/**
* Set the nested property on the cleaner object.
*
* @param string $propString [Property string]
* @param Model $dirty [Original dirty object]
* @param string $type [Type of nested property: 'property', 'method']
* @param object $object [Object to set property on]
*/
protected function setNestedProperty($propString, Model $dirty, $type, $object = null)
{
$object = is_null($object) ? $this : $object;
$relation = $this->setRelation($propString, $object);
$property = $this->getProperty($propString);
if ($this->isNested($property)) {
$newDirty = $dirty->{$relation};
$newObject = $this->{$relation};
$this->setNestedProperty($property, $newDirty, $type, $newObject);
} else {
$this->setProperty($dirty, $property, $object, $type, $relation);
}
}
/**
* Create relation on object.
*
* @param string $property
* @param object $object
*
* @return string
*/
protected function setRelation($property, $object)
{
$relation = explode('.', $property)[0];
$this->createRelation($relation, $object);
return $relation;
}
/**
* Get property name from nested property.
*
* @param string $property
*
* @return string
*/
protected function getProperty($property)
{
return collect(explode('.', $property))->forget(0)->implode('.');
}
/**
* If relation doesnt not exist on cleaner object, create it.
*
* @param string $relation
* @param object $object [Object to create relation on]
*/
protected function createRelation($relation, $object)
{
if (!isset($object->{$relation})) {
$class = new EmptyCleaner();
$object->{$relation} = $class;
}
}
/**
* Set property value on clean object.
*
* @param Model $dirty [Original, dirty object]
* @param string $property [Property name]
* @param Cleaner $object [Clean object]
* @param string $type [Type of property: 'property', 'method']
* @param string $relation [Relationship name]
*/
protected function setProperty(
Model $dirty,
$property,
Cleaner $object,
$type,
$relation = null
) {
$key = snake_case($property);
if (is_null($relation)) {
return $this->{$key} = $this->getValue($dirty, $property, $type);
} elseif (is_null($dirty->{$relation})) {
return $object->{$relation} = null;
} elseif (!$dirty->{$relation} instanceof Collection) {
$value = $this->getValue($dirty->{$relation}, $property, $type);
return $object->{$relation}->{$key} = $value;
}
$this->setPropertiesOnRelations(
$dirty,
$property,
$object,
$type,
$relation,
$key
);
}
/**
* Set property value on clean object.
*
* @param Model $dirty [Original, dirty object]
* @param string $property [Property name]
* @param Cleaner $object [Clean object]
* @param string $type [Type of property: 'property', 'method']
* @param string $relation [Relationship name]
* @param string $key
*/
protected function setPropertiesOnRelations(
Model $dirty,
$property,
Cleaner $object,
$type,
$relation,
$key
) {
if ($object->{$relation} instanceof EmptyCleaner) {
$object->{$relation} = [];
}
foreach ($dirty->{$relation} as $index => $dirtyObject) {
if (!isset($object->{$relation}[$index])) {
$object->{$relation}[$index] = new EmptyCleaner();
}
$value = $this->getValue($dirtyObject, $property, $type);
$object->{$relation}[$index]->{$key} = $value;
}
}
/**
* Get the value from the named property/method off object.
*
* @param Model $object [Object which contains property/method]
* @param string $name [Name of property/method]
* @param string $type ['method' or 'property']
*
* @return mixed
*/
protected function getValue(Model $object, $name, $type)
{
if ($type === 'method') {
if (method_exists($object, $name)) {
return $object->{$name}();
}
return;
}
return $object->{$name};
}
}