Skip to content

Commit

Permalink
Added support for wild cards to Remove.
Browse files Browse the repository at this point in the history
Resolves #41
  • Loading branch information
David Schoenbauer committed Dec 13, 2016
1 parent 61a606d commit 6912a49
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/DotNotation/ArrayDotNotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,9 @@ public function remove($dotNotation) {
*/
protected function recursiveRemove(array &$data, array $keys) {
$key = array_shift($keys);
if (!array_key_exists($key, $data)) {
if (is_array($data) && $key === static::WILD_CARD_CHARACTER) {
$this->wildCardRemove($keys, $data);
} elseif (!array_key_exists($key, $data)) {
throw new PathNotFoundException($key);
} elseif ($key && count($keys) == 0) { //Last Key
unset($data[$key]);
Expand All @@ -276,6 +278,26 @@ protected function recursiveRemove(array &$data, array $keys) {
}
}

/**
* Parses each key when a wild card key is met
* @since 1.3.0
* @param array $keys the remaining keys of focus for the data array
* @param array $data data to be traversed
*/
protected function wildCardRemove(array $keys, &$data) {
$keysNotFound = [];
foreach (array_keys($data) as $key) {
try {
$this->recursiveRemove($data, $this->unshiftKeys($keys, $key));
} catch (PathNotFoundException $exc) {
$keysNotFound[] = implode($this->getNotationType(), $this->unshiftKeys($keys, $key));
}
}
if(count($keysNotFound) === count($data)){
throw new PathNotFoundException(implode(', ', $keysNotFound));
}
}

/**
* consistently parses notation keys
* @since 1.2.0
Expand Down
53 changes: 53 additions & 0 deletions tests/DotNotation/ArrayDotNotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,59 @@ public function testRemove() {
$this->assertEquals($data, $this->_object->remove('levelA.levelB')->getData());
}

public function testRemoveWildCard() {
$data = ["level1" => ["level2" => ["level3" => [
["id" => 1, "email" => null, "name" => "one"],
["id" => 2, "email" => null, "name" => "two"],
["id" => 3, "email" => null, "name" => "three"],
["id" => 4, "email" => null, "name" => "four"],
["id" => 5, "email" => null, "name" => "five"],
]]]
];
$results = ["level1" => ["level2" => ["level3" => [
["id" => 1, "name" => "one"],
["id" => 2, "name" => "two"],
["id" => 3, "name" => "three"],
["id" => 4, "name" => "four"],
["id" => 5, "name" => "five"],
]]]
];
$this->assertEquals($results, $this->_object->setData($data)->remove('level1.level2.level3.*.email')->getData());
}

public function testRemoveWildCardNoException() {
$data = ["level1" => ["level2" => ["level3" => [
["id" => 1, "email" => null, "name" => "one"],
["id" => 2, "email" => null, "name" => "two"],
["id" => 3, "email" => null, "name" => "three"],
["id" => 4, "email" => null, "name" => "four"],
["id" => 5, "name" => "five"],
]]]
];
$results = ["level1" => ["level2" => ["level3" => [
["id" => 1, "name" => "one"],
["id" => 2, "name" => "two"],
["id" => 3, "name" => "three"],
["id" => 4, "name" => "four"],
["id" => 5, "name" => "five"],
]]]
];
$this->assertEquals($results, $this->_object->setData($data)->remove('level1.level2.level3.*.email')->getData());
}

public function testRemoveWildCardException() {
$data = ["level1" => ["level2" => ["level3" => [
["id" => 1, "name" => "one"],
["id" => 2, "name" => "two"],
["id" => 3, "name" => "three"],
["id" => 4, "name" => "four"],
["id" => 5, "name" => "five"],
]]]
];
$this->expectException(PathNotFoundException::class);
$this->_object->setData($data)->remove('level1.level2.level3.*.email')->getData();
}

public function testRemovePathNotFound() {
$this->expectException(PathNotFoundException::class);
$this->_object->remove('levelA.levelC');
Expand Down

0 comments on commit 6912a49

Please sign in to comment.