Skip to content

Instantly share code, notes, and snippets.

@TheRealJAG
Last active September 23, 2019 02:06
Show Gist options
  • Save TheRealJAG/0acc8b81be8012f14a6b58808a0de26d to your computer and use it in GitHub Desktop.
Save TheRealJAG/0acc8b81be8012f14a6b58808a0de26d to your computer and use it in GitHub Desktop.
Write a function that provides change directory (cd) function for an abstract file system.
<?php
class Path {
public $currentPath;
function __construct($path) {
$this->currentPath = $path;
}
public function cd($newPath) {
$innerCounter = 0;
$strOut= '';
$newPath = explode('/',$newPath);
$oldPath = explode('/', $this->currentPath);
foreach($newPath as $str) {
echo $str.'<P>';
if($str == '..') $innerCounter++;
}
$oldLength = count($oldPath);
for($i=0;$i<($oldLength - $innerCounter);$i++)
$strOut .= $oldPath[$i]."/";
$newLength = count($newPath);
for($i=0;$i<$newLength;$i++){
if($newPath[$i] !='..'){
$strOut = $strOut.$newPath[$i].'';
}
}
$this->currentPath = $strOut;
return $this;
}
}
$path = new Path('/a/b/c/d');
echo $path->cd('../x')->currentPath;
@mvestil
Copy link

mvestil commented Jun 22, 2018

Much shorter and simpler version without using for/foreach loop, and it Passed! :D

class Path
{
    public $currentPath;

    function __construct($path)
    {
        $this->currentPath = $path;
    }

    public function cd($newPath)
    {
	if ($this->isAbsolutePath($newPath)) {
	    $this->currentPath = $newPath;
	    return;
	}
		
        $currentPathLevelCount = substr_count($this->currentPath, '/');
        $parentLevelCount = substr_count($newPath, '..');
        
        $currentDirs = array_filter(explode('/', $this->currentPath));
	$newDirs = array_filter(explode('/', $newPath), function($value) {
		return !empty($value) && $value != '..';
	});
        
	$newPaths= array_slice($currentDirs, 0,  $parentLevelCount > $currentPathLevelCount ? 0 : $currentPathLevelCount - $parentLevelCount );
        array_push($newPaths, ...$newDirs);
        
	$this->currentPath = '/' . implode('/', $newPaths);
		
    }
    
    // For Unix
    public function isAbsolutePath($path)
    {
        return substr($path, 0, 1) == '/';
    }
}

$path = new Path('/a/b/c/d');
$path->cd('../x');
echo $path->currentPath;

@zachary0303
Copy link

`
class Path
{
public $currentPath;

function __construct($path)
{
    $this->currentPath = $path;
}

public function cd($newPath)
{
    $dirs = explode('/', $this->currentPath);
    $new_dirs = explode('/', $newPath);
    
    $new_path = [];
    
    foreach ($new_dirs as $new_dir) {
        if ($new_dir === '..') {
            array_pop($dirs);
        } else {
            $new_path[] = $new_dir;
        }
    }
            
    $new_path = array_merge($dirs, $new_path);
    
    $this->currentPath = implode('/', $new_path);
    
}

}

$path = new Path('/a/b/c/d');
$path->cd('../x');
echo $path->currentPath;
`

@SuperStar518
Copy link

I think this might be an approved answer.

https://github.com/alidavid0418/php-path-testdome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment