Skip to content

Commit a64033f

Browse files
committed
Reflecty 0.5.2
1 parent f654941 commit a64033f

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Includes the following helpers:
1414
- [Levex](https://github.com/nabeghe/levex-php) <small>v1.0.1</small>
1515
- [Matcher](https://github.com/nabeghe/matcher-php)<small> v1.0.0</small>
1616
- [Mem](https://github.com/nabeghe/mem-php) <small>v1.2.0</small>
17-
- [Reflecty](https://github.com/nabeghe/reflecty-php) <small>v0.5.1</small>
17+
- [Reflecty](https://github.com/nabeghe/reflecty-php) <small>v0.5.2</small>
1818
- [Servery](https://github.com/nabeghe/servery-php) <small>v0.2.2</small>
1919
- [Shortnum](https://github.com/nabeghe/shortnum-php) <small>v1.0.0</small>
2020
- [SimpleCipher](https://github.com/nabeghe/simple-cipher-php) <small>v1.0.0</small>

src/Reflecty/Meta.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public function set(string $path, $value)
127127
public function merge(array $_, bool $deep = false, bool $overwrite = true)
128128
{
129129
if ($deep) {
130-
$this->_ = static::deepMege($this->_, $_, $overwrite);
130+
$this->_ = static::deepMerge($this->_, $_, $overwrite);
131131
} else {
132132
if ($overwrite) {
133133
$this->_ = array_merge($this->_, $_);
@@ -149,18 +149,22 @@ public function merge(array $_, bool $deep = false, bool $overwrite = true)
149149
* @param bool $overwrite Optional. Whether to overwrite existing keys in $a with values from $b. Default true.
150150
* @return array The merged array.
151151
*/
152-
protected static function deepMege(array &$a, array &$b, bool $overwrite = true): array
152+
protected static function deepMerge(array $a, array $b, bool $overwrite = true): array
153153
{
154-
$merged = $a;
154+
foreach ($b as $key => $value) {
155+
if (is_array($value) && isset($a[$key]) && is_array($a[$key])) {
156+
if (array_keys($a[$key]) !== range(0, count($a[$key]) - 1) ||
157+
array_keys($value) !== range(0, count($value) - 1)) {
158+
$a[$key] = static::deepMerge($a[$key], $value, $overwrite);
159+
continue;
160+
}
161+
}
155162

156-
foreach ($b as $key => &$value) {
157-
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
158-
$merged[$key] = static::deepMege($merged[$key], $value);
159-
} elseif ($overwrite || !isset($merged[$key])) {
160-
$merged[$key] = $value;
163+
if ($overwrite || !isset($a[$key])) {
164+
$a[$key] = $value;
161165
}
162166
}
163167

164-
return $merged;
168+
return $a;
165169
}
166170
}

0 commit comments

Comments
 (0)