Skip to content

Commit 9ec16c1

Browse files
committed
unset
Signed-off-by: Travis Glenn Hansen <travisghansen@yahoo.com>
1 parent 9dfd25f commit 9ec16c1

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# v0.4.1
2+
3+
Released 2023-10-10
4+
5+
- add `unset` method to `Dotty`
6+
17
# v0.4.0
28

39
Released 2023-10-09

src/KubernetesClient/Dotty/DotAccess.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ public static function propSet(&$data, $key, $value) {
5151
}
5252
}
5353

54+
public static function propUnset(&$data, $key) {
55+
if (is_object($data)) {
56+
unset($data->{$key});
57+
}
58+
if (is_array($data)) {
59+
unset($data[$key]);
60+
}
61+
}
62+
5463
public static function isStructuredData(&$data) {
5564
if (is_object($data) || is_array($data)) {
5665
return true;
@@ -141,4 +150,34 @@ public static function set(&$data, $key, $value, $options = []) {
141150
$currentValue = &self::propGet($currentValue, $currentKey);
142151
}
143152
}
153+
154+
public static function unset(&$data, $key) {
155+
if (is_string($key)) {
156+
$keyPath = self::keyToPathArray($key);
157+
} else {
158+
$keyPath = $key;
159+
}
160+
161+
$currentValue = &$data;
162+
163+
$keySize = sizeof($keyPath);
164+
for ($i = 0; $i < $keySize; $i++) {
165+
$currentKey = $keyPath[$i];
166+
167+
if ($i == ($keySize - 1)) {
168+
self::propUnset($currentValue, $currentKey);
169+
return;
170+
}
171+
172+
if (!self::isStructuredData($currentValue)) {
173+
return;
174+
}
175+
176+
if (!self::propExists($currentValue, $currentKey)) {
177+
return;
178+
}
179+
180+
$currentValue = &self::propGet($currentValue, $currentKey);
181+
}
182+
}
144183
}

0 commit comments

Comments
 (0)