Skip to content

Commit

Permalink
Enhancement: Use ReferenceToken internally
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Jan 29, 2022
1 parent d36d332 commit b4470e0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 40 deletions.
4 changes: 3 additions & 1 deletion .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@

$license->save();

$config = PhpCsFixer\Config\Factory::fromRuleSet(new PhpCsFixer\Config\RuleSet\Php74($license->header()));
$config = PhpCsFixer\Config\Factory::fromRuleSet(new PhpCsFixer\Config\RuleSet\Php74($license->header()), [
'strict_comparison' => false,
]);

$config->getFinder()
->exclude([
Expand Down
8 changes: 1 addition & 7 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.19.0@a2ad69ae4f5ab1f7d225a8dc4e2ec2d9415ed599">
<file src="src/JsonPointer.php">
<ImpureFunctionCall occurrences="1">
<code>\array_map</code>
</ImpureFunctionCall>
</file>
</files>
<files psalm-version="4.19.0@a2ad69ae4f5ab1f7d225a8dc4e2ec2d9415ed599"/>
65 changes: 33 additions & 32 deletions src/JsonPointer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
*/
final class JsonPointer
{
private string $jsonStringValue;
/**
* @var array<int, ReferenceToken>
*/
private array $referenceTokens;

private function __construct(string $jsonStringValue)
private function __construct(ReferenceToken ...$referenceTokens)
{
$this->jsonStringValue = $jsonStringValue;
$this->referenceTokens = \array_values($referenceTokens);
}

/**
Expand All @@ -39,61 +42,59 @@ public static function fromJsonString(string $value): self
throw Exception\InvalidJsonPointer::fromJsonString($value);
}

return new self($value);
$jsonStringValues = \array_slice(
\explode('/', $value),
1,
);

return new self(...\array_map(static function (string $jsonStringValue): ReferenceToken {
return ReferenceToken::fromJsonString($jsonStringValue);
}, $jsonStringValues));
}

public static function fromReferenceTokens(ReferenceToken ...$referenceTokens): self
{
if ([] === $referenceTokens) {
return new self('');
}

$jsonStringValue = \sprintf(
'/%s',
\implode('/', \array_map(static function (ReferenceToken $referenceToken): string {
return $referenceToken->toJsonString();
}, $referenceTokens)),
);

return new self($jsonStringValue);
return new self(...$referenceTokens);
}

public static function document(): self
{
return new self('');
return new self();
}

public function append(ReferenceToken $referenceToken): self
{
return new self(\sprintf(
'%s/%s',
$this->jsonStringValue,
$referenceToken->toJsonString(),
));
$referenceTokens = $this->referenceTokens;

$referenceTokens[] = $referenceToken;

return new self(...$referenceTokens);
}

public function toJsonString(): string
{
return $this->jsonStringValue;
if ([] === $this->referenceTokens) {
return '';
}

return \sprintf(
'/%s',
\implode('/', \array_map(static function (ReferenceToken $referenceToken): string {
return $referenceToken->toJsonString();
}, $this->referenceTokens)),
);
}

/**
* @return array<int, ReferenceToken>
*/
public function toReferenceTokens(): array
{
$jsonStringValues = \array_slice(
\explode('/', $this->jsonStringValue),
1,
);

return \array_map(static function (string $jsonStringValue): ReferenceToken {
return ReferenceToken::fromJsonString($jsonStringValue);
}, $jsonStringValues);
return $this->referenceTokens;
}

public function equals(self $other): bool
{
return $this->jsonStringValue === $other->jsonStringValue;
return $this->referenceTokens == $other->referenceTokens;
}
}

0 comments on commit b4470e0

Please sign in to comment.