public function testRemove() { $bag = new Bag(['foo' => 'bar', 'hello' => 'world']); $bag->remove('foo'); $this->assertEquals(null, $bag->get('foo')); $this->assertEquals('world', $bag->get('hello')); }
public function remove($path) { $parts = explode('/', $path); if (count($parts) <= 1) { return parent::remove($path); } // Loop to collection above item to remove $previousKey = null; $current = $this; while (count($parts) > 1) { $key = array_shift($parts); if (!Utils::isArrayAccessible($current)) { throw new \RuntimeException("Trying to remove path {$path}, " . "but {$previousKey} is not array accessible"); } $previousKey = $key; if (!isset($current[$key])) { return null; } $current =& $this->getSubCollection($current, $key); } // $key currently is key of current collection. // We need the key of the item to remove in current collection. $key = array_shift($parts); if (!Utils::isArrayAccessible($current)) { throw new \RuntimeException("Trying to remove path {$path}, " . "but {$previousKey} is not array accessible"); } if ($current instanceof Bag) { return $current->remove($key); } unset($current[$key]); return null; }