Skip to content

Commit

Permalink
Merge pull request #13 from ergebnis/feature/pattern
Browse files Browse the repository at this point in the history
Enhancement: Extract `Pattern`
  • Loading branch information
localheinz committed Jan 29, 2022
2 parents 0d0b4c2 + 8f3ad62 commit 537a5b0
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/JsonPointer.php
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
@@ -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
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
@@ -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})*)";
}
}

0 comments on commit 537a5b0

Please sign in to comment.