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

Commit 17139c7

Browse files
committed
make dynamic properties configurable
1 parent 3563769 commit 17139c7

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

config/loop-functions.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@
1313

1414
'log' => true,
1515

16+
/*
17+
|
18+
| Determine if you want to allow dynamic properties.
19+
|
20+
*/
21+
22+
'dynamic_properties' => false,
23+
1624
/*
1725
|
1826
| Determine if you want to ignore some attributes.

src/Traits/HelpsLoopFunctions.php

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,30 @@ private function assignValue(int|string $key, mixed $value, mixed $rescue = null
2828
}
2929

3030
/**
31-
* @return array
31+
* @param int|string $key
32+
*
33+
* @return bool
34+
* @throws \ReflectionException
3235
*/
33-
private function ignoredPropertyNames(): array
36+
private function canAssignValue(int|string $key): bool
3437
{
35-
return config('loop-functions.ignore_keys', ['id', 'password']);
38+
return is_string($key)
39+
&& $this->checksPropertyExists($key)
40+
&& (empty($this->{$key}) || $this->hasDefaultValue($key));
3641
}
3742

3843
/**
3944
* @param int|string $key
4045
*
4146
* @return bool
42-
* @throws \ReflectionException
4347
*/
44-
private function canAssignValue(int|string $key): bool
48+
private function checksPropertyExists(int|string $key): bool
4549
{
46-
return is_string($key) && (empty($this->{$key}) || $this->hasDefaultValue($key));
50+
if ($this->allowsDynamicProperties()) {
51+
return true;
52+
}
53+
54+
return property_exists($this, $key);
4755
}
4856

4957
/**
@@ -69,4 +77,20 @@ private function canWalkRecursively(mixed $value): bool
6977
{
7078
return is_array($value) || $value instanceof \ArrayAccess;
7179
}
80+
81+
/**
82+
* @return bool
83+
*/
84+
private function allowsDynamicProperties(): bool
85+
{
86+
return config('loop-functions.dynamic_properties', true);
87+
}
88+
89+
/**
90+
* @return array
91+
*/
92+
private function ignoredPropertyNames(): array
93+
{
94+
return config('loop-functions.ignore_keys', ['id', 'password']);
95+
}
7296
}

0 commit comments

Comments
 (0)