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

Commit 0d2083b

Browse files
committed
recursive array assignment & refactoring
1 parent d61e7d0 commit 0d2083b

File tree

3 files changed

+47
-19
lines changed

3 files changed

+47
-19
lines changed

config/loop-functions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
|
2020
*/
2121

22-
'ignore_attributes' => ['id', 'password'],
22+
'ignore_keys' => ['id', 'password'],
2323

2424
];

src/Traits/WithLoopFunctions.php

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,8 @@ trait WithLoopFunctions
1919
public function attributesToProperties(?Model $model = null, mixed $rescue = null): void
2020
{
2121
if (! is_null($model)) {
22-
$toIgnore = config('loop-functions.ignore_attributes');
23-
24-
$ignores = is_array($toIgnore)
25-
? $toIgnore
26-
: ['id', 'password'];
27-
2822
collect($model->getAttributes())
29-
->except($ignores)
23+
->except($this->ignoreKeys())
3024
->each(function ($value, $property) use ($model, $rescue) {
3125
if (property_exists($this, $property)) {
3226
rescue(
@@ -49,14 +43,48 @@ public function attributesToProperties(?Model $model = null, mixed $rescue = nul
4943
*/
5044
public function arrayToProperties(?array $data, mixed $rescue = null): void
5145
{
52-
collect($data ?? [])->each(function ($value, $key) use ($rescue) {
53-
if (property_exists($this, $key)) {
54-
rescue(
55-
fn () => $this->{$key} = $value,
56-
$rescue,
57-
config('loop-functions.log') ?? false
58-
);
59-
}
60-
});
46+
collect($data ?? [])
47+
->except($this->ignoreKeys())
48+
->each(function ($value, $key) use ($rescue) {
49+
if (is_array($key)) {
50+
$this->arrayToProperties($key, $rescue);
51+
} else {
52+
$this->assignValue($key, $value, $rescue);
53+
}
54+
});
55+
}
56+
57+
/**
58+
* Assign the value to the property or rescue.
59+
*
60+
* @param string $key
61+
* @param mixed $value
62+
* @param mixed|null $rescue
63+
*
64+
* @return void
65+
*/
66+
private function assignValue(string $key, mixed $value, mixed $rescue = null): void
67+
{
68+
if (property_exists($this, $key)) {
69+
rescue(
70+
fn () => $this->{$key} = $value,
71+
$rescue,
72+
config('loop-functions.log') ?? false
73+
);
74+
}
75+
}
76+
77+
/**
78+
* @return array
79+
*/
80+
private function ignoreKeys(): array
81+
{
82+
$defaults = ['id', 'password'];
83+
84+
$ignores = config('loop-functions.ignore_keys', $defaults);
85+
86+
return is_array($ignores)
87+
? $ignores
88+
: $defaults;
6189
}
6290
}

tests/ArrayMappingTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class ArrayMappingTest extends TestCase
1010

1111
public bool $test;
1212
public string $name;
13-
public string $password;
13+
public ?string $password = null;
1414

1515
/** @test */
1616
public function testCanMapAnArrayToProperties()
@@ -25,6 +25,6 @@ public function testCanMapAnArrayToProperties()
2525

2626
$this->assertTrue($this->test);
2727
$this->assertStringContainsString('Michael', $this->name);
28-
$this->assertStringContainsString('$$', $this->password);
28+
$this->assertNull($this->password);
2929
}
3030
}

0 commit comments

Comments
 (0)