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

Commit 6b50d84

Browse files
committed
refactoring
1 parent 177e331 commit 6b50d84

File tree

3 files changed

+87
-42
lines changed

3 files changed

+87
-42
lines changed

src/Traits/HelpsLoopFunctions.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MichaelRubel\LoopFunctions\Traits;
6+
7+
trait HelpsLoopFunctions
8+
{
9+
/**
10+
* Assign the value to the property or rescue.
11+
*
12+
* @param int|string $key
13+
* @param mixed $value
14+
* @param mixed|null $rescue
15+
*
16+
* @return void
17+
*/
18+
private function assignValue(int|string $key, mixed $value, mixed $rescue = null): void
19+
{
20+
if ($this->canAssignValue($key)) {
21+
rescue(
22+
fn () => ! empty($this->{$key}) ?: $this->{$key} = $value,
23+
$rescue,
24+
config('loop-functions.log') ?? false
25+
);
26+
}
27+
}
28+
29+
/**
30+
* @return array
31+
*/
32+
private function ignoreKeys(): array
33+
{
34+
$defaults = ['id', 'password'];
35+
36+
$ignores = config('loop-functions.ignore_keys', $defaults);
37+
38+
return is_array($ignores)
39+
? $ignores
40+
: $defaults;
41+
}
42+
43+
/**
44+
* Check if the function can walk recursively over an array.
45+
*
46+
* @param mixed $value
47+
*
48+
* @return bool
49+
*/
50+
private function canWalkRecursively(mixed $value): bool
51+
{
52+
return is_array($value);
53+
}
54+
55+
/**
56+
* @param int|string $key
57+
*
58+
* @return bool
59+
*/
60+
private function canAssignValue(int|string $key): bool
61+
{
62+
return is_string($key) && property_exists($this, $key);
63+
}
64+
}

src/Traits/LoopFunctions.php

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
trait LoopFunctions
1010
{
11+
use HelpsLoopFunctions;
12+
1113
/**
1214
* Maps your model attributes to local class properties.
1315
*
@@ -46,52 +48,11 @@ public function arrayToProperties(?array $data, mixed $rescue = null): void
4648
collect($data ?? [])
4749
->except($this->ignoreKeys())
4850
->each(function ($value, $key) use ($rescue) {
49-
$propertyType = rescue(
50-
callback: fn () => (
51-
new \ReflectionProperty($this, $key)
52-
)->getType()->getName(),
53-
report: false
54-
);
55-
56-
if (is_array($value) && $propertyType !== 'array') {
51+
if ($this->canWalkRecursively($value)) {
5752
$this->arrayToProperties($value, $rescue);
5853
}
5954

6055
$this->assignValue($key, $value, $rescue);
6156
});
6257
}
63-
64-
/**
65-
* Assign the value to the property or rescue.
66-
*
67-
* @param int|string $key
68-
* @param mixed $value
69-
* @param mixed|null $rescue
70-
*
71-
* @return void
72-
*/
73-
private function assignValue(int|string $key, mixed $value, mixed $rescue = null): void
74-
{
75-
if (is_string($key) && property_exists($this, $key)) {
76-
rescue(
77-
fn () => ! empty($this->{$key}) ?: $this->{$key} = $value,
78-
$rescue,
79-
config('loop-functions.log') ?? false
80-
);
81-
}
82-
}
83-
84-
/**
85-
* @return array
86-
*/
87-
private function ignoreKeys(): array
88-
{
89-
$defaults = ['id', 'password'];
90-
91-
$ignores = config('loop-functions.ignore_keys', $defaults);
92-
93-
return is_array($ignores)
94-
? $ignores
95-
: $defaults;
96-
}
9758
}

tests/ArrayMappingTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace MichaelRubel\LoopFunctions\Tests;
44

5+
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
6+
use Illuminate\Support\Collection;
57
use MichaelRubel\LoopFunctions\Traits\LoopFunctions;
68

79
class ArrayMappingTest extends TestCase
@@ -13,6 +15,8 @@ class ArrayMappingTest extends TestCase
1315
public ?string $password = null;
1416
public string $next;
1517
public array $array = [];
18+
public ?Collection $supportCollection = null;
19+
public ?EloquentCollection $eloquentCollection = null;
1620

1721
/** @test */
1822
public function testCanMapAnArrayToProperties()
@@ -81,4 +85,20 @@ public function testArrayIsAssignedAsIs()
8185
$this->assertArrayHasKey('test', $this->array);
8286
$this->assertTrue($this->array['test']);
8387
}
88+
89+
/** @test */
90+
public function testCollectionAssignmentIsOk()
91+
{
92+
$array = [
93+
'supportCollection' => collect([
94+
'test' => true,
95+
]),
96+
'eloquentCollection' => new EloquentCollection(),
97+
];
98+
99+
$this->arrayToProperties($array);
100+
101+
$this->assertArrayHasKey('test', $this->supportCollection->toArray());
102+
$this->assertTrue($this->supportCollection->toArray()['test']);
103+
}
84104
}

0 commit comments

Comments
 (0)