1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace MichaelRubel\LoopFunctions\Traits; |
6
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
|
10
|
|
|
trait LoopFunctions |
11
|
|
|
{ |
12
|
|
|
use HelpsLoopFunctions; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Choose proper strategy to loop over the data. |
16
|
|
|
* |
17
|
|
|
* @param Model|\ArrayAccess|array|null $data |
18
|
|
|
* @param mixed|null $rescue |
19
|
|
|
* |
20
|
|
|
* @return void |
21
|
|
|
*/ |
22
|
9 |
|
public function propertiesFrom(Model|\ArrayAccess|array|null $data = null, mixed $rescue = null): void |
23
|
|
|
{ |
24
|
9 |
|
if ($data) { |
25
|
|
|
match (true) { |
26
|
9 |
|
$data instanceof Model => $this->attributesToProperties($data, $rescue), |
|
|
|
|
27
|
7 |
|
default => $this->arrayToProperties($data, $rescue), |
|
|
|
|
28
|
|
|
}; |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Maps your model attributes to local class properties. |
34
|
|
|
* |
35
|
|
|
* @param Model|null $model |
36
|
|
|
* @param mixed $rescue |
37
|
|
|
* |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
10 |
|
public function attributesToProperties(?Model $model = null, mixed $rescue = null): void |
41
|
|
|
{ |
42
|
10 |
|
collect($model?->getAttributes()) |
|
|
|
|
43
|
10 |
|
->except($this->ignoredPropertyNames()) |
44
|
10 |
|
->each( |
45
|
10 |
|
fn ($value, $property) => $this->assignValue( |
46
|
|
|
$property, |
47
|
10 |
|
$model->{$property}, |
48
|
|
|
$rescue |
49
|
|
|
) |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Map array data to class properties. |
55
|
|
|
* |
56
|
|
|
* @param array|\ArrayAccess|null $data |
57
|
|
|
* @param mixed|null $rescue |
58
|
|
|
* |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
8 |
|
public function arrayToProperties(array|\ArrayAccess|null $data, mixed $rescue = null): void |
62
|
|
|
{ |
63
|
8 |
|
collect($data ?? []) |
|
|
|
|
64
|
8 |
|
->except($this->ignoredPropertyNames()) |
65
|
8 |
|
->each(function ($value, $key) use ($rescue) { |
66
|
8 |
|
$this->assignValue($key, $value, $rescue); |
67
|
|
|
|
68
|
8 |
|
if ($this->canWalkRecursively($value)) { |
69
|
6 |
|
$this->propertiesFrom($value, $rescue); |
70
|
|
|
} |
71
|
|
|
}); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Dump class properties as key-valued array. |
76
|
|
|
* |
77
|
|
|
* @param string|object|null $class |
78
|
|
|
* @param int|null $filter |
79
|
|
|
* @param bool $asCollection |
80
|
|
|
* |
81
|
|
|
* @return array|Collection |
82
|
|
|
* @throws \ReflectionException |
83
|
|
|
*/ |
84
|
3 |
|
public function dumpProperties( |
85
|
|
|
string|object|null $class = null, |
86
|
|
|
bool $asCollection = false, |
87
|
|
|
?int $filter = null, |
88
|
|
|
): array|Collection { |
89
|
3 |
|
$class = match (true) { |
90
|
3 |
|
is_string($class) => app($class), |
|
|
|
|
91
|
1 |
|
is_object($class) => $class, |
92
|
1 |
|
default => $this, |
93
|
|
|
}; |
94
|
|
|
|
95
|
3 |
|
$properties = (new \ReflectionClass($class)) |
96
|
3 |
|
->getProperties($filter); |
97
|
|
|
|
98
|
3 |
|
$collection = collect($properties)->mapWithKeys( |
|
|
|
|
99
|
3 |
|
function (\ReflectionProperty $property) use ($class) { |
100
|
3 |
|
$property->setAccessible(true); |
101
|
|
|
|
102
|
3 |
|
return [$property->getName() => $property->getValue($class)]; |
|
|
|
|
103
|
|
|
} |
104
|
|
|
); |
105
|
|
|
|
106
|
3 |
|
return ! $asCollection |
107
|
2 |
|
? $collection->all() |
108
|
3 |
|
: $collection; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get the instance's property. |
113
|
|
|
* |
114
|
|
|
* @param string $name |
115
|
|
|
* |
116
|
|
|
* @return mixed |
117
|
|
|
*/ |
118
|
|
|
public function __get(string $name): mixed |
119
|
|
|
{ |
120
|
|
|
return $this->{$name}; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Set the instance's property. |
125
|
|
|
* |
126
|
|
|
* @param string $name |
127
|
|
|
* @param mixed $value |
128
|
|
|
*/ |
129
|
8 |
|
public function __set(string $name, mixed $value): void |
130
|
|
|
{ |
131
|
8 |
|
$this->{$name} = $value; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
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.