Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/JsonPointer.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private function __construct(ReferenceToken ...$referenceTokens)
*/
public static function fromJsonString(string $value): self
{
if (1 !== \preg_match('/^(\/(?P<referenceToken>((?P<unescaped>[\x00-\x2E]|[\x30-\x7D]|[\x7F-\x{10FFFF}])|(?P<escaped>~[01]))*))*$/u', $value)) {
if (1 !== \preg_match(Pattern::JSON_POINTER_JSON_STRING, $value)) {
throw Exception\InvalidJsonPointer::fromJsonString($value);
}

Expand Down
30 changes: 30 additions & 0 deletions src/Pattern.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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;

/**
* @internal
*/
final class Pattern
{
/**
* @see https://datatracker.ietf.org/doc/html/rfc6901#section-3
*/
public const JSON_POINTER_JSON_STRING = '/^(?P<jsonPointer>(\/(?P<referenceToken>((?P<unescaped>[\x00-\x2E]|[\x30-\x7D]|[\x7F-\x{10FFFF}])|(?P<escaped>~[01]))*))*)$/u';

/**
* @see https://datatracker.ietf.org/doc/html/rfc6901#section-3
*/
public const REFERENCE_TOKEN = '/^(?P<referenceToken>((?P<unescaped>[\x00-\x2E]|[\x30-\x7D]|[\x7F-\x{10FFFF}])|(?P<escaped>~[01]))*)$/u';
}
2 changes: 1 addition & 1 deletion src/ReferenceToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static function fromInt(int $value): self
*/
public static function fromJsonString(string $value): self
{
if (1 !== \preg_match('/^(?P<referenceToken>((?P<unescaped>[\x00-\x2E]|[\x30-\x7D]|[\x7F-\x{10FFFF}])|(?P<escaped>~[01]))*)$/u', $value)) {
if (1 !== \preg_match(Pattern::REFERENCE_TOKEN, $value)) {
throw Exception\InvalidReferenceToken::fromJsonString($value);
}

Expand Down
54 changes: 54 additions & 0 deletions test/Unit/PatternTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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;

use Ergebnis\Json\Pointer\Pattern;
use PHPUnit\Framework;

/**
* @internal
*
* @covers \Ergebnis\Json\Pointer\Pattern
*/
final class PatternTest extends Framework\TestCase
{
public function testJsonPointerJsonStringEqualsPattern(): void
{
$referenceToken = self::referenceToken();

$expected = "/^(?P<jsonPointer>(\\/{$referenceToken})*)$/u";

self::assertSame($expected, Pattern::JSON_POINTER_JSON_STRING);
}

public function testReferenceTokenEqualsPattern(): void
{
$referenceToken = self::referenceToken();

$expected = "/^{$referenceToken}$/u";

self::assertSame($expected, Pattern::REFERENCE_TOKEN);
}

/**
* @see https://datatracker.ietf.org/doc/html/rfc6901#section-3
*/
private static function referenceToken(): string
{
$unescaped = '(?P<unescaped>[\x00-\x2E]|[\x30-\x7D]|[\x7F-\x{10FFFF}])';
$escaped = '(?P<escaped>~[01])';

return "(?P<referenceToken>({$unescaped}|{$escaped})*)";
}
}