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

Commit f599e45

Browse files
committed
allow default values to be overridden
1 parent c8490ea commit f599e45

File tree

4 files changed

+10
-9
lines changed

4 files changed

+10
-9
lines changed

.github/FUNDING.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
custom: ["paypal.me/observername"]
1+
custom: ["https://paypal.me/observername"]

src/Traits/HelpsLoopFunctions.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ private function assignValue(int|string $key, mixed $value, mixed $rescue = null
1919
{
2020
if ($this->canAssignValue($key)) {
2121
rescue(
22-
fn () => ! empty($this->{$key}) ?: $this->{$key} = $value,
22+
fn () => $this->{$key} = $value,
2323
$rescue,
2424
config('loop-functions.log') ?? false
2525
);
@@ -43,13 +43,14 @@ private function ignoreKeys(): array
4343
/**
4444
* Check if the function can walk recursively over an array.
4545
*
46-
* @param mixed $value
46+
* @param mixed $value
47+
* @param int|string $key
4748
*
4849
* @return bool
4950
*/
50-
private function canWalkRecursively(mixed $value): bool
51+
private function canWalkRecursively(mixed $value, int|string $key): bool
5152
{
52-
return is_array($value);
53+
return is_array($value) && ! empty($this->{$key});
5354
}
5455

5556
/**

src/Traits/LoopFunctions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function arrayToProperties(?array $data, mixed $rescue = null): void
4444
collect($data ?? [])
4545
->except($this->ignoreKeys())
4646
->each(function ($value, $key) use ($rescue) {
47-
if ($this->canWalkRecursively($value)) {
47+
if ($this->canWalkRecursively($value, $key)) {
4848
$this->arrayToProperties($value, $rescue);
4949
}
5050

tests/ArrayMappingTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class ArrayMappingTest extends TestCase
1313
public bool $test;
1414
public string $name;
1515
public ?string $password = null;
16-
public string $next;
16+
public array $additional_data = [];
1717
public array $array = [];
1818
public ?Collection $supportCollection = null;
1919
public ?EloquentCollection $eloquentCollection = null;
@@ -51,11 +51,11 @@ public function testCanMapAnArrayRecursively()
5151
$this->assertTrue($this->test);
5252
$this->assertStringContainsString('Michael', $this->name);
5353
$this->assertNull($this->password);
54-
$this->assertStringContainsString('test', $this->next);
54+
$this->assertArrayHasKey('next', $this->additional_data);
5555
}
5656

5757
/** @test */
58-
public function testAlreadyInitializedPropertiesArentOverridesByNestedArrays()
58+
public function testAlreadyInitializedPropertiesArentOverridenByNestedArrays()
5959
{
6060
$array = [
6161
'name' => 'Michael Rubel',

0 commit comments

Comments
 (0)