Skip to content

Commit

Permalink
Update Path
Browse files Browse the repository at this point in the history
  • Loading branch information
jfilla committed Nov 12, 2019
1 parent 154e672 commit d1d6512
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
38 changes: 35 additions & 3 deletions src/Utils/Path.php
Expand Up @@ -2,15 +2,28 @@

namespace Wavevision\Utils;

use Nette\StaticClass;
use Nette\SmartObject;

class Path
{

use StaticClass;

public const DELIMITER = '/';

use SmartObject;

/**
* @var array<string|null>
*/
private $path;

/**
* @return static
*/
public static function create(?string ...$path)
{
return new self(...$path);
}

public static function join(?string ...$parts): string
{
return Strings::replace(
Expand All @@ -31,4 +44,23 @@ public static function trim(?string $path): ?string
}
return rtrim($path, self::DELIMITER);
}

/**
* @return static
*/
public function path(?string ...$path)
{
return self::create(self::join(...array_merge($this->path, $path)));
}

public function __toString(): string
{
return self::join(...$this->path);
}

private function __construct(?string ...$path)
{
$this->path = $path;
}

}
10 changes: 10 additions & 0 deletions tests/UtilsTests/PathTest.php
Expand Up @@ -15,5 +15,15 @@ public function testJoin(): void
{
$this->assertEquals('/path/to/somewhere', Path::join('//path', '/to', null, '\\\\somewhere/'));
$this->assertEquals('http://url.tld/path/to/asset', Path::join('http://url.tld', 'path', 'to', '/asset'));
$this->assertEquals('r/path', Path::join('r', 'path'));
}

public function testInstanceJoin(): void
{
$root = Path::create('/r');
$nested = $root->path('a', 'b');
$this->assertEquals('/r/a/b', $nested);
$this->assertEquals('/r/a/b/f.txt', $nested->path('f.txt'));
}

}

0 comments on commit d1d6512

Please sign in to comment.