Example #1
0
if(!class_exists('Eden_Path')){class Eden_Path extends Eden_Type_String implements ArrayAccess{public static function i(){return self::_getMultiple(__CLASS__);}public function __construct($path){Eden_Path_Error::i()->argument(1,'string');parent::__construct($this->_format($path));}public function __toString(){return $this->_data;}public function absolute($root=NULL){Eden_Path_Error::i()->argument(1,'string','null');if(is_dir($this->_data) || is_file($this->_data)){return $this;}if(is_null($root)){$root=$_SERVER['DOCUMENT_ROOT'];}$absolute=$this->_format($root).$this->_data;if(is_dir($absolute) || is_file($absolute)){$this->_data=$absolute;return $this;}Eden_Path_Error::i()->setMessage(Eden_Path_Error::FULL_PATH_NOT_FOUND)->addVariable($this->_data)->addVariable($absolute)->trigger();}public function append($path){$error=Eden_Path_Error::i()->argument(1,'string');$paths=func_get_args();foreach($paths as $i=>$path){$error->argument($i + 1,$path,'string');$this->_data.=$this->_format($path);}return $this;}public function getArray(){return explode('/',$this->_data);}public function offsetExists($offset){return in_array($offset,$this->getArray());}public function offsetGet($offset){$pathArray=$this->getArray();if($offset=='first'){$offset=0;}if($offset=='last'){$offset=count($pathArray) - 1;}if(is_numeric($offset)){return isset($pathArray[$offset]) ? $pathArray[$offset] : NULL;}return NULL;}public function offsetSet($offset,$value){if (is_null($offset)){$this->append($value);}else if($offset=='prepend'){$this->prepend($value);}else if($offset=='replace'){$this->replace($value);}else{$pathArray=$this->getArray();if($offset > 0 && $offset < count($pathArray)){$pathArray[$offset]=$value;$this->_data=implode('/',$pathArray);}}}public function offsetUnset($offset){}public function prepend($path){$error=Eden_Path_Error::i()->argument(1,'string');$paths=func_get_args();foreach($paths as $i=>$path){$error->argument($i + 1,$path,'string');$this->_data=$this->_format($path).$this->_data;}return $this;}public function pop(){$pathArray=$this->getArray();$path=array_pop($pathArray);$this->_data=implode('/',$pathArray);return $path;}public function replace($path){Eden_Path_Error::i()->argument(1,'string');$pathArray=$this->getArray();array_pop($pathArray);$pathArray[]=$path;$this->_data=implode('/',$pathArray);return $this;}protected function _format($path){$path=str_replace('\\','/',$path);$path=str_replace('//','/',$path);if(substr($path,-1,1)=='/'){$path=substr($path,0,-1);}if(substr($path,0,1) !='/' && !preg_match("/^[A-Za-z]+\:/",$path)){$path='/'.$path;}return $path;}}class Eden_Path_Error extends Eden_Error{const FULL_PATH_NOT_FOUND='The path %s or %s was not found.';public static function i($message=NULL,$code=0){$class=__CLASS__;return new $class($message,$code);}}}
 /**
  * Replaces the last path with this one
  * 
  * @param string
  * @return string
  */
 public function replace($path)
 {
     //argument 1 must be a string
     Eden_Path_Error::i()->argument(1, 'string');
     //get the path array
     $pathArray = $this->getArray();
     //pop out the last
     array_pop($pathArray);
     //push in the new
     $pathArray[] = $path;
     //assign back to path
     $this->_data = implode('/', $pathArray);
     return $this;
 }
Example #3
0
 public function replace($path)
 {
     Eden_Path_Error::i()->argument(1, 'string');
     $pathArray = $this->getArray();
     array_pop($pathArray);
     $pathArray[] = $path;
     $this->_data = implode('/', $pathArray);
     return $this;
 }