Skip to content

Commit

Permalink
Improved Compass\Path support. Bug fixed. Tests updated.
Browse files Browse the repository at this point in the history
  • Loading branch information
vinogradsoft committed Oct 18, 2023
1 parent 8757ae8 commit 3ef83c3
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 77 deletions.
4 changes: 2 additions & 2 deletions src/DefaultPathStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ class DefaultPathStrategy implements PathStrategy
/**
* @inheritDoc
*/
public function updatePath(array $items, ?string $suffix = null): string
public function updatePath(array $items, string $separator, ?string $suffix = null): string
{
if (empty($items)) {
return '';
}
return $suffix ? implode('/', $items) . $suffix : implode('/', $items);
return $suffix ? implode($separator, $items) . $suffix : implode($separator, $items);
}
}
2 changes: 1 addition & 1 deletion src/DefaultUrlStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function updateQuery(array $items): string
/**
* @inheritDoc
*/
public function updatePath(array $items, ?string $suffix = null): string
public function updatePath(array $items, string $separator, ?string $suffix = null): string
{
if (empty($items)) {
return '';
Expand Down
22 changes: 14 additions & 8 deletions src/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ class Path extends AbstractPath

protected PathStrategy $strategy;
protected ?string $suffix = null;
protected string $separator;

/**
* @param string $source
* @param string $separator
* @param PathStrategy|null $strategy
*/
public function __construct(string $source, ?PathStrategy $strategy = null)
public function __construct(string $source, string $separator = DIRECTORY_SEPARATOR, ?PathStrategy $strategy = null)
{
$this->assertNotEmpty($source);
$this->initUrlPath($strategy);
$this->initUrlPath($separator, $strategy);
parent::__construct($source);
}

Expand All @@ -33,20 +35,23 @@ private function assertNotEmpty($source)
}

/**
* @param string $separator
* @param PathStrategy|null $strategy
* @return $this
*/
protected function initUrlPath(?PathStrategy $strategy = null): static
protected function initUrlPath(string $separator, ?PathStrategy $strategy = null): static
{
$this->strategy = $strategy ?? new DefaultPathStrategy();
$this->separator = $separator;
return $this;
}

/**
* @param string $separator
* @param PathStrategy|null $strategy
* @return static
*/
public static function createBlank(?PathStrategy $strategy = null): static
public static function createBlank(string $separator = DIRECTORY_SEPARATOR, ?PathStrategy $strategy = null): static
{
static $prototypePath;
if (!$prototypePath instanceof Path) {
Expand All @@ -56,7 +61,7 @@ public static function createBlank(?PathStrategy $strategy = null): static
$prototypePath->items = [];
$prototypePath->source = '';
}
return (clone $prototypePath)->initUrlPath($strategy);
return (clone $prototypePath)->initUrlPath($separator, $strategy);
}

/**
Expand Down Expand Up @@ -93,7 +98,7 @@ protected function parse(string $source)
*/
public function getSeparator(): string
{
return '/';
return $this->separator;
}

/**
Expand Down Expand Up @@ -135,7 +140,7 @@ public function setSuffix(?string $suffix): void
*/
public function updateSource(): void
{
$this->source = $this->strategy->updatePath($this->items, $this->suffix);
$this->source = $this->strategy->updatePath($this->items, $this->separator, $this->suffix);
}

/**
Expand Down Expand Up @@ -213,7 +218,8 @@ private function replaceIn(int $idx, string $search, string $replace, string $pa
*/
public function setBy(string $currentValue, string $newValue): void
{
if (!$key = $this->getKey($currentValue)) {
$key = $this->getKey($currentValue);
if ($key === null) {
throw new InvalidPathException(sprintf('You are trying to replace "%s" with "%s", but there is no such item in the path.', $currentValue, $newValue));
}
$this->items[$key] = $newValue;
Expand Down
3 changes: 2 additions & 1 deletion src/PathStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ interface PathStrategy

/**
* @param array $items
* @param string $separator
* @param string|null $suffix
* @return string
*/
public function updatePath(array $items, ?string $suffix = null): string;
public function updatePath(array $items, string $separator, ?string $suffix = null): string;
}
40 changes: 14 additions & 26 deletions src/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,6 @@

use Compass\Exception\InvalidUrlException;

/**
*
* |--------------------------------------absolute url-----------------------------------|
* | |
* |----------------base url----------------|------------------relative url--------------|
* | | |
* | authority | path query fragment |
* | /‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾\|/‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾\ /‾‾‾‾‾‾‾‾‾\ /‾‾‾‾‾‾\ |
* http://grigor:[email protected]:8080/path/to/resource.json?query=value#fragment
* \__/ \___/ \_____/ \___________/ \__/ \___/
* scheme user password host port suffix
*/
class Url extends AbstractPath
{
/**
Expand All @@ -32,22 +20,22 @@ class Url extends AbstractPath
const SUFFIX = ':suffix';

/**
* key state
* state keys
*/
const USER_STATE = 1 << 0;
const PASSWORD_STATE = 1 << 1;
const HOST_STATE = 1 << 2;
const PORT_KEY = 1 << 3;
const PORT_STATE = 1 << 3;

const PATH_STATE = 1 << 0;
const QUERY_STATE = 1 << 1;
const FRAGMENT_KEY = 1 << 2;
const FRAGMENT_STATE = 1 << 2;

const AUTHORITY_WHOLE = self::USER_STATE | self::PASSWORD_STATE | self::HOST_STATE | self::PORT_KEY;
const RELATIVE_URL_WHOLE = self::PATH_STATE | self::QUERY_STATE | self::FRAGMENT_KEY;
const AUTHORITY_WHOLE = self::USER_STATE | self::PASSWORD_STATE | self::HOST_STATE | self::PORT_STATE;
const RELATIVE_URL_WHOLE = self::PATH_STATE | self::QUERY_STATE | self::FRAGMENT_STATE;

/**
* current state
* current states
*/
protected int $authoritySate = self::AUTHORITY_WHOLE;
protected int $relativeUrlState = self::RELATIVE_URL_WHOLE;
Expand Down Expand Up @@ -90,7 +78,7 @@ protected function initUrl(
{
$this->isIdnToAscii = $isIdnToAscii;
$this->updateStrategy = $updateStrategy ?? new DefaultUrlStrategy();
$this->path = Path::createBlank($this->updateStrategy);
$this->path = Path::createBlank('/', $this->updateStrategy);
$this->urlQuery = Query::createBlank($this->updateStrategy);
return $this;
}
Expand Down Expand Up @@ -155,7 +143,7 @@ public function setUpdateStrategy(UrlStrategy $updateStrategy): void
$this->authoritySate &= ~self::PASSWORD_STATE;
$this->relativeUrlState &= ~self::QUERY_STATE;
$this->relativeUrlState &= ~self::PATH_STATE;
$this->relativeUrlState &= ~self::FRAGMENT_KEY;
$this->relativeUrlState &= ~self::FRAGMENT_STATE;
}

/**
Expand Down Expand Up @@ -209,7 +197,7 @@ public function clearRelativeUrl(): void
{
$this->relativeUrlState &= ~self::QUERY_STATE;
$this->relativeUrlState &= ~self::PATH_STATE;
$this->relativeUrlState &= ~self::FRAGMENT_KEY;
$this->relativeUrlState &= ~self::FRAGMENT_STATE;
$this->relativeUrl = null;
$this->path->reset();
$this->urlQuery->reset();
Expand Down Expand Up @@ -248,7 +236,7 @@ protected function parse(string $source)

if (isset($data['port'])) {
$this->items[self::PORT] = $data['port'];
$this->authoritySate &= ~self::PORT_KEY;
$this->authoritySate &= ~self::PORT_STATE;
}

if (isset($data['path'])) {
Expand All @@ -263,7 +251,7 @@ protected function parse(string $source)

if (isset($data['fragment'])) {
$this->items[self::FRAGMENT] = rawurldecode($data['fragment']);
$this->relativeUrlState &= ~self::FRAGMENT_KEY;
$this->relativeUrlState &= ~self::FRAGMENT_STATE;
}

if (!isset($data['host']) && !isset($data['scheme'])) {
Expand Down Expand Up @@ -321,7 +309,7 @@ public function getPort(): ?string
public function setPort(string $port): static
{
$this->items[self::PORT] = $port;
$this->authoritySate &= ~self::PORT_KEY;
$this->authoritySate &= ~self::PORT_STATE;
return $this;
}

Expand Down Expand Up @@ -367,7 +355,7 @@ public function setAll(array $items): void
$this->authoritySate &= ~self::PASSWORD_STATE;
$this->relativeUrlState &= ~self::QUERY_STATE;
$this->relativeUrlState &= ~self::PATH_STATE;
$this->relativeUrlState &= ~self::FRAGMENT_KEY;
$this->relativeUrlState &= ~self::FRAGMENT_STATE;

if (empty($items)) {
$this->reset();
Expand Down Expand Up @@ -445,7 +433,7 @@ public function getFragment(): ?string
public function setFragment(string $fragment): static
{
$this->items[self::FRAGMENT] = $fragment;
$this->relativeUrlState &= ~self::FRAGMENT_KEY;
$this->relativeUrlState &= ~self::FRAGMENT_STATE;
return $this;
}

Expand Down
5 changes: 3 additions & 2 deletions tests/Cases/Dummy/DummyUrlStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ public function updateQuery(array $items): string

/**
* @param array $items
* @param string $separator
* @param string|null $suffix
* @return string
*/
public function updatePath(array $items, ?string $suffix = null): string
public function updatePath(array $items, string $separator, ?string $suffix = null): string
{
return implode('/', $items);
return implode($separator, $items);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
use Compass\DefaultPathStrategy;
use PHPUnit\Framework\TestCase;

class DefaultUrlPathStrategyTest extends TestCase
class DefaultPathStrategyTest extends TestCase
{
/**
* @dataProvider getData
*/
public function testUpdatePath(array $items, ?string $suffix, $expected)
{
$strategy = new DefaultPathStrategy();
$path = $strategy->updatePath($items, $suffix);
$path = $strategy->updatePath($items, '/', $suffix);
self::assertEquals($expected, $path);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Compass\Path;
use Compass\Query;

class DefaultUpdateStrategyTest extends TestCase
class DefaultUrlStrategyTest extends TestCase
{
/**
* @dataProvider getItems
Expand Down Expand Up @@ -109,7 +109,7 @@ public function getDataBaseUrl(): array
public function testUpdateRelativeUrl(array $items, string $pathString, string $queryString, string $expected)
{
$updateStrategy = new DefaultUrlStrategy();
$path = Path::createBlank($updateStrategy);
$path = Path::createBlank('/', $updateStrategy);
if (!empty($pathString)) {
$path->setSource($pathString);
}
Expand Down Expand Up @@ -141,7 +141,7 @@ public function testUpdateAbsoluteUrl(string $baseUrl, string $relativeUrl, stri
{
$updateStrategy = new DefaultUrlStrategy();

$path = Path::createBlank($updateStrategy);
$path = Path::createBlank('/', $updateStrategy);
$query = Query::createBlank($updateStrategy);

$result = $updateStrategy->updateAbsoluteUrl(
Expand Down Expand Up @@ -172,7 +172,7 @@ public function getDataAbsoluteUrl(): array
public function testUpdatePath(array $items, ?string $suffix, string $expected)
{
$updateStrategy = new DefaultUrlStrategy();
$result = $updateStrategy->updatePath($items, $suffix);
$result = $updateStrategy->updatePath($items, '/', $suffix);
self::assertEquals($expected, $result);
}

Expand Down
Loading

0 comments on commit 3ef83c3

Please sign in to comment.