Skip to content

Commit fd6b20e

Browse files
committed
Record::wrap() ignore readonly or virtual #1256
1 parent 2ff95f1 commit fd6b20e

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

src/RecordTrait.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Windwalker\Utilities\Classes\TraitHelper;
1212
use Windwalker\Utilities\TypeCast;
1313

14+
use function Windwalker\get_object_dump_props;
1415
use function Windwalker\get_object_dump_values;
1516

1617
trait RecordTrait
@@ -84,7 +85,23 @@ public function merge(mixed $values, bool $recursive = false, bool $ignoreNulls
8485
{
8586
$values = TypeCast::toArray($values);
8687

88+
$props = get_object_dump_props($this, ignores: $ignores);
89+
8790
foreach ($values as $key => $value) {
91+
$propRef = $props[$key] ?? $ignores[$key] ?? null;
92+
93+
if (
94+
$propRef
95+
&& (
96+
$propRef->isPrivate()
97+
|| $propRef->isProtected()
98+
|| $propRef->isVirtual()
99+
|| $propRef->isReadOnly()
100+
)
101+
) {
102+
continue;
103+
}
104+
88105
if (
89106
$recursive
90107
&& is_object($this->$key ?? null)
@@ -103,6 +120,7 @@ public function merge(mixed $values, bool $recursive = false, bool $ignoreNulls
103120
return $this;
104121
}
105122

123+
#[\NoDiscard]
106124
public function withMerge(mixed $values, bool $recursive = false, bool $ignoreNulls = false): static
107125
{
108126
return (clone $this)->merge($values, $recursive, $ignoreNulls);
@@ -115,13 +133,15 @@ public function defaults(mixed $values, bool $recursive = false): static
115133
return $this->merge($values, $recursive)->merge($new, $recursive, true);
116134
}
117135

136+
#[\NoDiscard]
118137
public function withDefaults(mixed $values, bool $recursive = false): static
119138
{
120139
$new = clone $this;
121140

122141
return (clone $this)->merge($values, $recursive)->merge($new, $recursive, true);
123142
}
124143

144+
#[\NoDiscard]
125145
public function with(...$data): static
126146
{
127147
if (PHP_VERSION_ID >= 80500) {

0 commit comments

Comments
 (0)