Source for file PathNavigator.php
Documentation is available at PathNavigator.php
* Facilitates navigation of path strings
* @author Denny Shimkoski <bytebrite@gmail.com>
* @copyright 2006-2009 Denny Shimkoski
* @license http://www.opensource.org/licenses/mit-license.html MIT
* @link http://pear.php.net/package/Text_PathNavigator
* @author Denny Shimkoski <bytebrite@gmail.com>
* @license http://www.opensource.org/licenses/mit-license.html MIT
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_PathNavigator
* Directory separator, i.e, forward or backward slash
* Normalized path after explode() on $slash
* Current path segment index (Iterator implementation)
* Constructs a new Text_PathNavigator object
* @param array|string$path Path string or array of path segments
* @param string $slash character used to separate path segments
public function __construct($path, $slash = DIRECTORY_SEPARATOR )
if ($this->path !== null ) {
* Removes all leading and trailing slashes from given path string/segment array.
* @param array|string$path Path string or array of path segments
while ($path{$offset} == $this->slash) {
while ($path{$len - 1 } == $this->slash) {
return substr($path, $offset, $len - $offset);
* Slices current path and returns it as a new Text_PathNavigator object
* @param int $offset If offset is non-negative, the new path will start
at that offset in the path segment array.
If offset is negative, the new path will start
that far from the end of the path segment array.
* @param int $length If length is given and is positive, the new path
will have that many segments in it. If length is given
and is negative, the new path will stop that many
segments from the end of the path segment array.
If it is omitted, the new path will have everything
from offset up until the end of the path segment array.
* @return PEAR_Text_PathNavigator
public function slice($offset = 0 , $length = null )
// PHP gets all confused if you pass a null into the $length param
// of array_slice(). hence...
* Returns path substring following the given regular expression
* @param string $pattern regular expression to use for matching
* @return Text_PathNavigator
public function after($pattern)
if (preg_match(" !$pattern!" , $this->path, $matches, PREG_OFFSET_CAPTURE )) {
* Returns path substring preceding the given regular expression
* @param string $pattern regular expression to use for matching
* @return Text_PathNavigator
public function before($pattern)
if (preg_match(" !$pattern!" , $this->path, $matches, PREG_OFFSET_CAPTURE )) {
* Returns path substring between the given regular expressions $start and $end
* @param string $start regular expression
* @param string $end regular expression
* @return Text_PathNavigator
public function between($start, $end)
return $this->after($start)->before ($end);
* Returns this path relative to another one. Assumes both paths are absolute.
* If located in a path $p2, we could navigate to this one
* using $p2->cd($this->relativeTo($p2))
* @param mixed $path new path will be expressed in relation to this
* @return Text_PathNavigator
// given path can also be a string or an array of segments,
// although Text_PathNavigator is preferred
// array to hold new path segments
// make a copy of path segments for shift operation
// for each segment in the given path....
foreach ($path as $i => $segment) {
// this effectively truncates any "absolute" portion of the two paths,
// i.e., beginning segments shared in common
// escape from the remaining elements of the given path
// shifting no longer allowed since a differing segment was encountered
// now just tack on any segments left over from the shift operation...
foreach ($segments as $segment) {
// ...and return a new Text_PathNavigator object
* Navigates from this path to another using the specified relative path.
* @param mixed $path relative path
* @return Text_PathNavigator
public function cd($path)
// given path can also be a string or an array of segments
// (probably a string, i.e., '../../some/dir')
// make a copy of path segments for pop operation
// for each segment in the given path....
foreach ($path as $segment) {
// navigate up and down the hierarchy
// ...and return a new Text_PathNavigator object
* Maps path segments to variable names given in $template.
* Suitable for use with PHP's extract() function.
* @param string $template e.g., '/controller/method/'
public function map($template)
foreach ($map as $i => $key) {
* Check if a particular segment exists (ArrayAccess implementation)
* @param int $i path segment index
* Get a particular segment (ArrayAccess implementation)
* @param int $i path segment index
* Throws Exception for the purpose of immutability (ArrayAccess implementation)
* @param int $i path segment index
* @throws Exception for the purpose of immutability
throw new PEAR_Exception ('Text_PathNavigator is immutable');
* Throws Exception for the purpose of immutability (ArrayAccess implementation)
* @param int $i path segment index
* @param string $val path segment
* @throws Exception for the purpose of immutability
throw new PEAR_Exception ('Text_PathNavigator is immutable');
* Returns the number of path segments (Countable implementation)
* Returns current path segment (Iterator implementation)
* Returns index of current path segment (Iterator implementation)
* Advances to next path segment (Iterator implementation)
* Rewinds to first path segment (Iterator implementation)
* Checks if current path segment index is valid (Iterator implementation)
Documentation generated on Mon, 14 May 2012 15:00:02 +0000 by phpDocumentor 1.4.3. PEAR Logo Copyright © PHP Group 2004.
|