/** @test */ public function it_removes_nodes() { $array = ["foo" => ["bar" => 5], "asd" => 2]; $this->assertFalse(TraverseArray::remove($array, "asd.boo")); $this->assertTrue(TraverseArray::remove($array, "asd")); $this->assertEquals(["foo" => ["bar" => 5]], $array); }
public function removeNode(Path $path) { $root = $this->rootNode; $mounted = false; $out = TraverseArray::remove($root, $path, true, function ($mounterClass, $relativePath, $path) use(&$mounted) { $mounted = true; return $this->router->query($mounterClass)->removeNode($relativePath); }); // it has altered the copy of root node if (!$mounted) { throw new NodeReadonlyException(); } return $out; }
public function removeNode(Path $path) { // cannot remove the entire gds data system if ($path->root()) { throw new NodeReadonlyException(); } // load the row we're gonna be working with try { $this->getRow($path[0]); } catch (TableRowNotFoundException $e) { return false; } // removing an entire row if ($path->length() == 1) { // remove from loaded rows if (array_key_exists($path[0], $this->loadedRows)) { unset($this->loadedRows[$path[0]]); } // remove from unsaved rows if (($i = array_search($path[0], $this->unsavedRows)) !== false) { unset($this->unsavedRows[$i]); } // remove from rows to create if (($i_toCreate = array_search($path[0], $this->rowsToCreate)) !== false) { unset($this->rowsToCreate[$i_toCreate]); } // add to rows to remove, but only if it wasn't in rows to create if (!in_array($path[0], $this->rowsToRemove) && $i_toCreate === false) { $this->rowsToRemove[] = $path[0]; } // successfully removed return true; } if (!TraverseArray::remove($this->loadedRows, $path)) { return false; } // unsaved if (!in_array($path[0], $this->unsavedRows)) { $this->unsavedRows[] = $path[0]; } return true; // current node pointer $currentNode =& $this->loadedRows; }