Skip to content
This repository was archived by the owner on Mar 12, 2024. It is now read-only.

Commit c4f2e17

Browse files
committed
allow default values to be overridden
1 parent db79395 commit c4f2e17

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

src/Traits/HelpsLoopFunctions.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ trait HelpsLoopFunctions
1414
* @param mixed|null $rescue
1515
*
1616
* @return void
17+
* @throws \ReflectionException
1718
*/
1819
private function assignValue(int|string $key, mixed $value, mixed $rescue = null): void
1920
{
@@ -38,9 +39,36 @@ private function ignoredPropertyNames(): array
3839
* @param int|string $key
3940
*
4041
* @return bool
42+
* @throws \ReflectionException
4143
*/
4244
private function canAssignValue(int|string $key): bool
4345
{
44-
return is_string($key) && property_exists($this, $key);
46+
return is_string($key)
47+
&& property_exists($this, $key)
48+
&& (empty($this->{$key}) || $this->hasDefaultValue($key));
49+
}
50+
51+
/**
52+
* @param int|string $key
53+
*
54+
* @return bool
55+
* @throws \ReflectionException
56+
*/
57+
private function hasDefaultValue(int|string $key): bool
58+
{
59+
$default = (new \ReflectionProperty($this, $key))
60+
->getDefaultValue();
61+
62+
return ! empty($default);
63+
}
64+
65+
/**
66+
* @param mixed $value
67+
*
68+
* @return bool
69+
*/
70+
private function canWalkRecursively(mixed $value): bool
71+
{
72+
return is_array($value) || $value instanceof \ArrayAccess;
4573
}
4674
}

src/Traits/LoopFunctions.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ trait LoopFunctions
1313
/**
1414
* Choose proper strategy to loop over the data.
1515
*
16-
* @param Model|array|null $data
17-
* @param mixed|null $rescue
16+
* @param Model|\ArrayAccess|array|null $data
17+
* @param mixed|null $rescue
1818
*
1919
* @return void
2020
*/
21-
public function propertiesFrom(Model|array|null $data = null, mixed $rescue = null): void
21+
public function propertiesFrom(Model|\ArrayAccess|array|null $data = null, mixed $rescue = null): void
2222
{
2323
if ($data) {
2424
match (true) {
25-
is_array($data) => $this->arrayToProperties($data, $rescue),
2625
$data instanceof Model => $this->attributesToProperties($data, $rescue),
26+
default => $this->arrayToProperties($data, $rescue),
2727
};
2828
}
2929
}
@@ -52,15 +52,21 @@ public function attributesToProperties(?Model $model = null, mixed $rescue = nul
5252
/**
5353
* Map array data to class properties.
5454
*
55-
* @param array|null $data
56-
* @param mixed|null $rescue
55+
* @param array|\ArrayAccess|null $data
56+
* @param mixed|null $rescue
5757
*
5858
* @return void
5959
*/
60-
public function arrayToProperties(?array $data, mixed $rescue = null): void
60+
public function arrayToProperties(array|\ArrayAccess|null $data, mixed $rescue = null): void
6161
{
6262
collect($data ?? [])
6363
->except($this->ignoredPropertyNames())
64-
->each(fn ($value, $key) => $this->assignValue($key, $value, $rescue));
64+
->each(function ($value, $key) use ($rescue) {
65+
if ($this->canWalkRecursively($value)) {
66+
$this->propertiesFrom($value, $rescue);
67+
}
68+
69+
$this->assignValue($key, $value, $rescue);
70+
});
6571
}
6672
}

0 commit comments

Comments
 (0)