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.
- Loading branch information
1 parent
46547c2
commit c607de8
Showing
6 changed files
with
393 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?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 InvalidJsonPointer 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.', | ||
$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,67 @@ | ||
<?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 JsonPointer | ||
{ | ||
private string $value; | ||
|
||
private function __construct(string $value) | ||
{ | ||
$this->value = $value; | ||
} | ||
|
||
/** | ||
* @see https://datatracker.ietf.org/doc/html/rfc6901#section-3 | ||
* | ||
* @throws Exception\InvalidJsonPointer | ||
*/ | ||
public static function fromString(string $value): self | ||
{ | ||
if (1 !== \preg_match('/^(\/(?P<referenceToken>((?P<unescaped>[\x00-\x2E]|[\x30-\x7D]|[\x7F-\x{10FFFF}])|(?P<escaped>~[01]))*))*$/u', $value, $matches)) { | ||
throw Exception\InvalidJsonPointer::fromString($value); | ||
} | ||
|
||
return new self($value); | ||
} | ||
|
||
public static function document(): self | ||
{ | ||
return new self(''); | ||
} | ||
|
||
public function append(ReferenceToken $referenceToken): self | ||
{ | ||
return new self(\sprintf( | ||
'%s/%s', | ||
$this->value, | ||
$referenceToken->toEscapedString(), | ||
)); | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return $this->value; | ||
} | ||
|
||
public function equals(self $other): bool | ||
{ | ||
return $this->value === $other->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,42 @@ | ||
<?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\InvalidJsonPointer | ||
*/ | ||
final class InvalidJsonPointerTest extends Framework\TestCase | ||
{ | ||
use Test\Util\Helper; | ||
|
||
public function testFromValueReturnsInvalidJsonPointerException(): void | ||
{ | ||
$value = self::faker()->sentence(); | ||
|
||
$exception = Exception\InvalidJsonPointer::fromString($value); | ||
|
||
$message = \sprintf( | ||
'Value "%s" does not appear to be a valid JSON Pointer.', | ||
$value, | ||
); | ||
|
||
self::assertSame($message, $exception->getMessage()); | ||
} | ||
} |
Oops, something went wrong.