/** * Returns the node with the given path, relative to this node. If the path * does not exist, missing nodes will be created automatically. * @param string $path The path to change to * @return \arc\tree\NamedNode The target node corresponding with the given path. */ public function cd($path) { if (\arc\path::isAbsolute($path)) { $node = $this->getRootNode(); } else { $node = $this; } $result = \arc\path::reduce($path, function ($node, $name) { switch ($name) { case '..': return isset($node->parentNode) ? $node->parentNode : $node; break; case '.': case '': return $node; break; default: if (!isset($node->childNodes[$name])) { return $node->appendChild($name); } else { return $node->childNodes[$name]; } break; } }, $node); return $result; }
/** * Returns the path without its root entry. * * Usage: * $remainder = \arc\path::tail( '/root/of/a/path/' ); // => '/of/a/path/' * * @param string $path The path to get the tail of. * @return string The path without its root entry. */ public static function tail($path) { if (!\arc\path::isAbsolute($path)) { $path = '/' . $path; } return substr($path, strpos($path, '/', 1)); }