- array_push: documentation ( source)
- array_slice: documentation ( source)
- implode: documentation ( source)
- explode: documentation ( source)
- array_pop: documentation ( source)
<?php
class Path {
public $currentPath;
private $array;
function __construct( $path ) {
$this->currentPath = $path;
$this->array = array_slice( explode( "/", $path ), 1 );
}
public function cd( $newPath ) {
if ( $newPath == "" || $newPath == "." ) {
return $this;
}
$change = explode( "/", $newPath );
foreach ( $change as $value ) {
if ( $value == ".." ) {
if ( count( $this->array ) > 0 ) {
array_pop($this->array);
}
} elseif ( $value !== "" ) {
array_push( $this->array, $value );
}
}
if ( count( $this->array ) > 0 ) {
$this->currentPath = implode( "/", $this->array );
} else {
$this->currentPath = "/";
}
return $this;
}
}
$path = new Path('/a/b/c/d');
echo $path->cd('../x')->currentPath;