Commit 8c3de068 authored by Sebastian De Deyne's avatar Sebastian De Deyne

Added basename and dirname methods

parent 5efc3b14
......@@ -106,6 +106,20 @@ class Url implements UriInterface
return $this->path;
}
public function getBasename(): string
{
return $this->getSegment(-1);
}
public function getDirname(): string
{
$segments = $this->getSegments();
array_pop($segments);
return '/'.implode('/', $segments);
}
public function getQuery(): string
{
return $this->query->__toString();
......@@ -156,7 +170,18 @@ class Url implements UriInterface
public function getSegment(int $index, $default = null)
{
return $this->getSegments()[$index - 1] ?? $default;
$segments = $this->getSegments();
if ($index === 0) {
throw new InvalidArgument("Segment 0 doesn't exist. Segments can be retrieved by using 1-based index or a negative index.");
}
if ($index < 0) {
$segments = array_reverse($segments);
$index = abs($index);
}
return $segments[$index - 1] ?? $default;
}
public function withScheme($scheme)
......@@ -216,6 +241,28 @@ class Url implements UriInterface
return $url;
}
public function withDirname(string $dirname)
{
$dirname = trim($dirname, '/');
if (! $this->getBasename()) {
return $this->withPath($dirname);
}
return $this->withPath($dirname.'/'.$this->getBasename());
}
public function withBasename(string $basename)
{
$basename = trim($basename, '/');
if ($this->getDirname() === '/') {
return $this->withPath('/'.$basename);
}
return $this->withPath($this->getDirname().'/'.$basename);
}
public function withQuery($query)
{
$url = clone $this;
......
......@@ -84,6 +84,27 @@ class UrlBuildTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('/opensource', $url->getPath());
}
/** @test */
public function it_can_build_a_url_with_a_dirname()
{
$url = Url::create()
->withHost('spatie.be')
->withDirname('opensource');
$this->assertEquals('//spatie.be/opensource', $url->__toString());
}
/** @test */
public function it_can_build_a_url_with_a_basename()
{
$url = Url::create()
->withHost('spatie.be')
->withPath('/opensource/php')
->withBasename('laravel');
$this->assertEquals('//spatie.be/opensource/laravel', $url->__toString());
}
/** @test */
public function it_can_build_a_url_with_a_query()
{
......
......@@ -56,6 +56,30 @@ class UrlParseTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('', $url->getPath());
}
/** @test */
public function it_can_parse_a_basename()
{
$url = Url::fromString('https://spatie.be/opensource/laravel');
$this->assertEquals('laravel', $url->getBasename());
}
/** @test */
public function it_can_parse_a_dirname()
{
$url = Url::fromString('https://spatie.be/opensource/laravel');
$this->assertEquals('/opensource', $url->getDirname());
}
/** @test */
public function it_can_parse_a_dirname_if_the_dir_is_the_root()
{
$url = Url::fromString('https://spatie.be/opensource');
$this->assertEquals('/', $url->getDirname());
}
/** @test */
public function it_can_parse_a_relative_url()
{
......
......@@ -2,6 +2,7 @@
namespace Spatie\Url\Test;
use Spatie\Url\Exceptions\InvalidArgument;
use Spatie\Url\Url;
class UrlSegmentTest extends \PHPUnit_Framework_TestCase
......@@ -38,4 +39,20 @@ class UrlSegmentTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('nothing', $url->getSegment(3, 'nothing'));
}
/** @test */
public function it_throws_an_exception_if_segment_0_gets_queried()
{
$this->expectException(InvalidArgument::class);
Url::create()->withPath('opensource/php')->getSegment(0);
}
/** @test */
public function it_can_return_a_path_segment_counting_from_behind()
{
$url = Url::create()->withPath('opensource/php');
$this->assertEquals('php', $url->getSegment(-1));
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment