1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace MichaelRubel\LoopFunctions\Traits; |
6
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
|
9
|
|
|
trait LoopFunctions |
10
|
|
|
{ |
11
|
|
|
use HelpsLoopFunctions; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Choose proper strategy to loop over the data. |
15
|
|
|
* |
16
|
|
|
* @param Model|array|null $data |
17
|
|
|
* @param mixed|null $rescue |
18
|
|
|
* |
19
|
|
|
* @return void |
20
|
|
|
*/ |
21
|
2 |
|
public function propertiesFrom(Model|array|null $data = null, mixed $rescue = null): void |
22
|
|
|
{ |
23
|
|
|
match (true) { |
24
|
2 |
|
is_array($data) => $this->arrayToProperties($data, $rescue), |
|
|
|
|
25
|
1 |
|
$data instanceof Model => $this->attributesToProperties($data, $rescue), |
|
|
|
|
26
|
|
|
}; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Maps your model attributes to local class properties. |
31
|
|
|
* |
32
|
|
|
* @param Model|null $model |
33
|
|
|
* @param mixed $rescue |
34
|
|
|
* |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
9 |
|
public function attributesToProperties(?Model $model = null, mixed $rescue = null): void |
38
|
|
|
{ |
39
|
9 |
|
collect($model?->getAttributes()) |
|
|
|
|
40
|
9 |
|
->except($this->ignoreKeys()) |
41
|
9 |
|
->each( |
42
|
9 |
|
fn ($value, $property) => $this->assignValue( |
43
|
|
|
$property, |
44
|
9 |
|
$model->{$property}, |
45
|
|
|
$rescue |
46
|
|
|
) |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Map array data to class properties. |
52
|
|
|
* |
53
|
|
|
* @param array|null $data |
54
|
|
|
* @param mixed|null $rescue |
55
|
|
|
* |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
6 |
|
public function arrayToProperties(?array $data, mixed $rescue = null): void |
59
|
|
|
{ |
60
|
6 |
|
collect($data ?? []) |
|
|
|
|
61
|
6 |
|
->except($this->ignoreKeys()) |
62
|
6 |
|
->each(function ($value, $key) use ($rescue) { |
63
|
6 |
|
if ($this->canWalkRecursively($value, $key)) { |
64
|
|
|
$this->arrayToProperties($value, $rescue); |
65
|
|
|
} |
66
|
|
|
|
67
|
6 |
|
$this->assignValue($key, $value, $rescue); |
68
|
|
|
}); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.