public function testThatKeysCanBeRemovedInArray() { $items = [["a" => "b", "c" => "d"], ["a" => "c", "c" => "e", "d" => "e"], null]; //Default action (delete) $kept_keys = CollectionUtility::removeKeys($items, ["a", "c"]); $this->assertEquals([[], ["d" => "e"], []], $kept_keys); //Explicit action (delete) $kept_keys = CollectionUtility::removeKeys($items, ["a", "c"], CollectionUtility::REMOVAL_ACTION_DELETE); $this->assertEquals([[], ["d" => "e"], []], $kept_keys); $special_items = [["animal" => "dog", "name" => "John", "weather" => "mild"], ["animal" => "cat", "name" => "William"]]; $kept_keys = CollectionUtility::removeKeys($special_items, ["animal", "weather"], CollectionUtility::REMOVAL_ACTION_DELETE); $this->assertEquals([["name" => "John"], ["name" => "William"]], $kept_keys); //Nullify action $kept_keys = CollectionUtility::removeKeys($items, ["a", "c"], CollectionUtility::REMOVAL_ACTION_NULLIFY); $this->assertEquals([["a" => null, "c" => null], ["a" => null, "c" => null, "d" => "e"], []], $kept_keys); //Invalid actions try { CollectionUtility::removeKeys($items, ["a", "c"], "invalid"); $this->fail("Expected invalid argument exception"); } catch (\InvalidArgumentException $e) { } }
public function testThatKeysCanBeRemovedInMultiDimArray() { $items = [["a" => "b", "c" => "d"], ["a" => "c", "c" => "e", "d" => "e"], null]; $kept_keys = CollectionUtility::removeKeys($items, ["a", "c"]); $this->assertEquals([[], ["d" => "e"], []], $kept_keys); }