generated from ergebnis/php-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from ergebnis/feature/reference-token
Enhancement: Implement `ReferenceToken`
- Loading branch information
Showing
10 changed files
with
513 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,8 @@ | |
"type": "library", | ||
"keywords": [ | ||
"json", | ||
"pointer" | ||
"pointer", | ||
"rfc6901" | ||
], | ||
"authors": [ | ||
{ | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2020-2022 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE.md file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/json-pointer | ||
*/ | ||
|
||
namespace Ergebnis\Json\Pointer\Exception; | ||
|
||
interface Exception extends \Throwable | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2020-2022 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE.md file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/json-pointer | ||
*/ | ||
|
||
namespace Ergebnis\Json\Pointer\Exception; | ||
|
||
final class InvalidReferenceToken extends \InvalidArgumentException implements Exception | ||
{ | ||
public static function fromString(string $value): self | ||
{ | ||
return new self(\sprintf( | ||
'Value "%s" does not appear to be a valid JSON Pointer reference token.', | ||
$value, | ||
)); | ||
} | ||
|
||
public static function fromInt(int $value): self | ||
{ | ||
return new self(\sprintf( | ||
'Value "%d" does not appear to be a valid JSON Pointer array index.', | ||
$value, | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2020-2022 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE.md file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/json-pointer | ||
*/ | ||
|
||
namespace Ergebnis\Json\Pointer; | ||
|
||
/** | ||
* @psalm-immutable | ||
* | ||
* @see https://datatracker.ietf.org/doc/html/rfc6901 | ||
*/ | ||
final class ReferenceToken | ||
{ | ||
private string $escapedValue; | ||
|
||
private function __construct(string $escapedValue) | ||
{ | ||
$this->escapedValue = $escapedValue; | ||
} | ||
|
||
/** | ||
* @throws Exception\InvalidReferenceToken | ||
*/ | ||
public static function fromInt(int $value): self | ||
{ | ||
if (0 > $value) { | ||
throw Exception\InvalidReferenceToken::fromInt($value); | ||
} | ||
|
||
return new self((string) $value); | ||
} | ||
|
||
/** | ||
* @throws Exception\InvalidReferenceToken | ||
*/ | ||
public static function fromEscapedString(string $value): self | ||
{ | ||
if (1 !== \preg_match('/^(?P<referenceToken>((?P<unescaped>[\x00-\x2E]|[\x30-\x7D]|[\x7F-\x{10FFFF}])|(?P<escaped>~[01]))*)$/u', $value)) { | ||
throw Exception\InvalidReferenceToken::fromString($value); | ||
} | ||
|
||
return new self($value); | ||
} | ||
|
||
public static function fromUnescapedString(string $value): self | ||
{ | ||
return self::fromEscapedString(\str_replace( | ||
[ | ||
'~', | ||
'/', | ||
], | ||
[ | ||
'~0', | ||
'~1', | ||
], | ||
$value, | ||
)); | ||
} | ||
|
||
public function toEscapedString(): string | ||
{ | ||
return $this->escapedValue; | ||
} | ||
|
||
public function toUnescapedString(): string | ||
{ | ||
return \str_replace( | ||
[ | ||
'~1', | ||
'~0', | ||
], | ||
[ | ||
'/', | ||
'~', | ||
], | ||
$this->escapedValue, | ||
); | ||
} | ||
|
||
public function equals(self $other): bool | ||
{ | ||
return $this->escapedValue === $other->escapedValue; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2020-2022 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE.md file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/json-pointer | ||
*/ | ||
|
||
namespace Ergebnis\Json\Pointer\Test\Unit\Exception; | ||
|
||
use Ergebnis\Json\Pointer\Exception; | ||
use Ergebnis\Json\Pointer\Test; | ||
use PHPUnit\Framework; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @covers \Ergebnis\Json\Pointer\Exception\InvalidReferenceToken | ||
*/ | ||
final class InvalidReferenceTokenTest extends Framework\TestCase | ||
{ | ||
use Test\Util\Helper; | ||
|
||
public function testFromStringReturnsInvalidReferenceToken(): void | ||
{ | ||
$value = self::faker()->word(); | ||
|
||
$exception = Exception\InvalidReferenceToken::fromString($value); | ||
|
||
$message = \sprintf( | ||
'Value "%s" does not appear to be a valid JSON Pointer reference token.', | ||
$value, | ||
); | ||
|
||
self::assertSame($message, $exception->getMessage()); | ||
} | ||
|
||
public function testFromIntReturnsInvalidReferenceToken(): void | ||
{ | ||
$value = self::faker()->numberBetween(); | ||
|
||
$exception = Exception\InvalidReferenceToken::fromInt($value); | ||
|
||
$message = \sprintf( | ||
'Value "%d" does not appear to be a valid JSON Pointer array index.', | ||
$value, | ||
); | ||
|
||
self::assertSame($message, $exception->getMessage()); | ||
} | ||
} |
Oops, something went wrong.