<?php
/*
Copyright © 2007 Rémy Sanchez <hide@address.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA; or visit
http://www.gnu.org/
*/
/**
* Path class
* The Path class helps you to manage paths that you have to append the one to
* the other.
*/
class Path {
/**
* The actual path with all components split into an array
*
* @var array
*/
private $path = array();
/**
* Does this path point on a directory or not ?
*
* @var bool
*/
private $isDir = false;
/**
* The actual separator of directories ("\" for windows and "/" for sane
* systems)
*
* @var bool
*/
private $separator = "/";
/**
* Is there some head to the path ?
*
* @var string
*/
private $head = "";
/**
* Constructor of the class
* This constructor take as a parameter the path to be imported into the
* class, and various informations.
*
* @param string $path is the path to be imported. It must be a
* string with a traditional path format.
* @param bool $isDir tells if this path points to a directory or
* to a file.
* @param string $head tells if something goes before the path,
* like \\ for a samba share, http:// for an URL or
* simply / for a path from root
* @param string $separator is the string that separate the
* the directories/files.
* @return Path
*/
function __construct($path, $isDir = null, $head = null, $separator = null) {
$this->isDir = ($isDir === null) ? $this->isDir : $isDir;
$this->head = ($head === null) ? $this->head : $head;
$this->separator = ($separator === null) ? $this->separator : $separator;
$path = (string) $path;
if($this->head !== "") {
if(strpos($path, $this->head) === 0) $path = substr($path, strlen($this->head));
}
$firstpass = explode($this->separator, $path);
foreach($firstpass as $branch) {
if($branch !== "") $this->path[] = $branch;
}
}
/**
* Append the $path to this path. Head and separator of $path will be
* overriden.
*
* @param Path The $path to append to this one
* @return void
*/
function a(Path $path) {
$this->isDir = $path->getIsDir();
$this->path = array_merge($this->path, $path->getPathArray());
}
/**
* Append $path after this path and return the junction of both in a new
* path.
*
* @param Path The $path to be joined
* @return Path
*/
function b(Path $path) {
$oldpath = $this->path;
$oldisdir = $this->isDir;
$object = $this->a($path);
$new = clone $this;
$this->path = $oldpath;
$this->isDir = $oldisdir;
return $new;
}
/**
* Just like the constructor, but it will take this instance's settings
* as defaults
*
* @param string $path is the path to be imported. It must be a
* string with a traditional path format.
* @param bool $isDir tells if this path points to a directory or
* to a file.
* @param string $head tells if something goes before the path,
* like \\ for a samba share or http:// for an URL
* @param string $separator is the string that separate the
* the directories/files.
* @return Path
*/
function makeNew($path, $isDir = null, $head = null, $separator = null) {
$isDir = ($isDir === null) ? $this->isDir : $isDir;
$head = ($head === null) ? $this->head : $head;
$separator = ($separator === null) ? $this->separator : $separator;
return new Path($path, $isDir, $head, $separator);
}
/**
* Returns the path to the path's parent folder
*
* @return Path
*/
function dirname() {
$dirname = $this->path;
if(count($dirname) > 0) unset($dirname[count($dirname)-1]);
$new = clone $this;
$new->setPath($dirname);
$new->setIsDir(true);
return $new;
}
/**
* Returns the name of the pointed file/directory
*
* @param string suffix to be eventually removed
* @return string
*/
function basename($suffix = "") {
$suflen = strlen($suffix);
$pathlast = count($this->path) - 1;
if($suflen > 0) {
if(substr($this->path[$pathlast], 0-$suflen) === $suffix) return substr($this->path[$pathlast], 0, 0-$suflen);
}
return $this->path[$pathlast];
}
/**
* Return the path as a traditional string
*
* @return string
*/
function __toString() {
return (string) ($this->head . implode($this->separator, $this->path) . (($this->isDir) ? $this->separator : ""));
}
/**
* Return a string formated to be a shell argument
*
* @return string
*/
function toShellString() {
return escapeshellarg($this);
}
/**
* Returns the array with the elements of the path
*
* @return array
*/
function getPathArray() {
return $this->path;
}
/**
* Tells if the path points a directory
*
* @return bool
*/
function getIsDir() {
return $this->isDir;
}
/**
* Returns the head of the path
*
* @return string
*/
function getHead() {
return $this->head;
}
/**
* Returns the separator of the path
*
* @return string
*/
function getSeparator() {
return $this->separator;
}
/**
* Set either if the path point a directory or not
*
* @param bool $isDir
*/
function setIsDir($isDir) {
$this->isDir = (bool) $isDir;
}
/**
* Set the head of the path
*
* @param string $head
*/
function setHead($head) {
$this->head = (string) $head;
}
/**
* Set the separator of the path
*
* @param string $separator
*/
function setSeparator($separator) {
$this->separator = (string) $separator;
}
/**
* Set path array manualy
*
* @param array $append
*/
function setPath($path) {
if(is_array($path)) $this->path = $path;
}
}
?>