Esempio n. 1
0
 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;
 }
Esempio n. 2
0
 /**
  * Append some segments at the end of this path
  */
 public function append($path)
 {
     if (!is_array($path) && !is_string($path) && !$path instanceof Path) {
         $path = [$path];
     }
     $path = new Path($path);
     $this->path = array_merge($this->toArray(), $path->toArray());
     return $this;
 }
Esempio n. 3
0
 /**
  * Remove a node
  * @return bool Returns true if node node has been removed, false if it doesn't exist already
  */
 public static function remove(&$array, $path, $handleMounting = false, $onMountHit = null)
 {
     $path = new Path($path);
     if ($path->root()) {
         throw new MyceliumException("Cannot remove entire array.");
     }
     $node =& $array;
     for ($i = 0; $i < $path->length(); $i++) {
         // is the current node a mounted node?
         if ($handleMounting && array_key_exists("@mount", $node)) {
             // callback(mounterClass, relativePath, pathToTheMountNode)
             $onMountHit($node["@mount"], $path->cut($i), $path->clamp($i));
         }
         // does the next node exist?
         if (!array_key_exists($path[$i], $node)) {
             return false;
         }
         // last path segment?
         if ($i == $path->length() - 1) {
             // remove the node
             unset($node[$path[$i]]);
             return true;
         }
         // is the next node an array?
         if (!is_array($node[$path[$i]])) {
             return false;
         }
         // continue deeper
         $node =& $node[$path[$i]];
     }
 }