From ea6bc0a9c3c094f0750aff02095097a1d2a6dac8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Fri, 28 Jan 2022 19:21:18 +0100 Subject: [PATCH] Fix: Rename methods --- CHANGELOG.md | 20 ++++++- README.md | 42 +++++++-------- src/Exception/InvalidJsonPointer.php | 4 +- src/JsonPointer.php | 21 ++++---- src/ReferenceToken.php | 24 +++++---- .../Unit/Exception/InvalidJsonPointerTest.php | 6 +-- test/Unit/JsonPointerTest.php | 54 +++++++++---------- test/Unit/ReferenceTokenTest.php | 50 ++++++++--------- 8 files changed, 121 insertions(+), 100 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 614de4bd..909a0192 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,23 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## Unreleased -For a full diff see [`1.0.0...main`][1.0.0...main]. +For a full diff see [`2.0.0...main`][2.0.0...main]. + +## [`2.0.0`][2.0.0] + +For a full diff see [`1.0.0...2.0.0`][1.0.0...2.0.0]. + +## Changed + +- Renamed named constructors and accessors of `Exception\InvalidJsonPointer`, `JsonPointer`, and `ReferenceToken` ([#4]), by [@localheinz] + + - `Exception\InvalidJsonPointer::fromString()` to `JsonPointer::fromJsonString()` + - `JsonPointer::fromString()` to `JsonPointer::fromJsonString()` + - `JsonPointer::toString()` to `JsonPointer::toJsonString()` + - `ReferenceToken::fromEscapedString()` to `ReferenceToken::fromJsonString()` + - `ReferenceToken::fromUnescapedString()` to `ReferenceToken::fromString()` + - `ReferenceToken::toEscapedString()` to `ReferenceToken::toJsonString()` + - `ReferenceToken::toUnescapedString()` to `ReferenceToken::toString()` ## [`1.0.0`][1.0.0] @@ -19,8 +35,10 @@ For a full diff see [`a5ba52c...1.0.0`][a5ba52c...1.0.0]. [a5ba52c...1.0.0]: https://github.com/ergebnis/json-pointer/compare/a5ba52c...1.0.0 [1.0.0...main]: https://github.com/ergebnis/json-pointer/compare/1.0.0...main +[2.0.0...main]: https://github.com/ergebnis/json-pointer/compare/2.0.0...main [#1]: https://github.com/ergebnis/json-pointer/pull/1 [#2]: https://github.com/ergebnis/json-pointer/pull/2 +[#4]: https://github.com/ergebnis/json-pointer/pull/4 [@localheinz]: https://github.com/localheinz diff --git a/README.md b/README.md index 1c02a4f2..9eb2b720 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ composer require ergebnis/json-pointer ### `ReferenceToken` -You can create a `ReferenceToken` from an unescaped `string` value: +You can create a `ReferenceToken` from a `string` value: ```php toEscapedString(); // 'foo~1bar' -$referenceToken->toUnescapedString(); // 'foo/bar' +$referenceToken->toJsonString(); // 'foo~1bar' +$referenceToken->toString(); // 'foo/bar' ``` -You can create a `ReferenceToken` from an escaped `string` value: +You can create a `ReferenceToken` from a [JSON `string` value](https://datatracker.ietf.org/doc/html/rfc6901#section-5): ```php toEscapedString(); // 'foo~1bar' -$referenceToken->toUnescapedString(); // 'foo/bar' +$referenceToken->toJsonString(); // 'foo~1bar' +$referenceToken->toString(); // 'foo/bar' ``` You can create a `ReferenceToken` from an `int` value: @@ -66,8 +66,8 @@ use Ergebnis\Json\Pointer; $referenceToken = Pointer\ReferenceToken::fromInt(9001); -$referenceToken->toEscapedString(); // '9001' -$referenceToken->toUnescapedString(); // '9001' +$referenceToken->toJsonString(); // '9001' +$referenceToken->toString(); // '9001' ``` You can compare `ReferenceToken`s: @@ -79,8 +79,8 @@ declare(strict_types=1); use Ergebnis\Json\Pointer; -$one = Pointer\ReferenceToken::fromUnescapedString('foo/bar'); -$two = Pointer\ReferenceToken::fromEscapedString('foo~1bar'); +$two = Pointer\ReferenceToken::fromJsonString('foo~1bar'); +$one = Pointer\ReferenceToken::fromString('foo/bar'); $one->equals($two); // true ``` @@ -98,10 +98,10 @@ use Ergebnis\Json\Pointer; $jsonPointer = Pointer\JsonPointer::document(); -$jsonPointer->toString(); // '' +$jsonPointer->toJsonString(); // '' ``` -You can create a `JsonPointer` from a `string` value: +You can create a `JsonPointer` from a [JSON `string` representation](https://datatracker.ietf.org/doc/html/rfc6901#section-5) value: ```php toString(); // '/foo/bar' +$jsonPointer->toJsonString(); // '/foo/bar' ``` You can compare `JsonPointer`s: @@ -124,8 +124,8 @@ declare(strict_types=1); use Ergebnis\Json\Pointer; -$one = Pointer\JsonPointer::fromString('/foo/bar'); -$two = Pointer\JsonPointer::fromString('/foo~1bar'); +$one = Pointer\JsonPointer::fromJsonString('/foo/bar'); +$two = Pointer\JsonPointer::fromJsonString('/foo~1bar'); $one->equals($two); // false ``` @@ -139,13 +139,13 @@ declare(strict_types=1); use Ergebnis\Json\Pointer; -$jsonPointer = Pointer\JsonPointer::fromString('/foo/bar'); +$jsonPointer = Pointer\JsonPointer::fromJsonString('/foo/bar'); -$referenceToken = Pointer\ReferenceToken::fromUnescapedString('baz'); +$referenceToken = Pointer\ReferenceToken::fromString('baz'); $newJsonPointer = $jsonPointer->append($referenceToken); -$newJsonPointer->toString(); // '/foo/bar/baz' +$newJsonPointer->toJsonString(); // '/foo/bar/baz' ``` ## Changelog diff --git a/src/Exception/InvalidJsonPointer.php b/src/Exception/InvalidJsonPointer.php index 7320e8c3..677bb0af 100644 --- a/src/Exception/InvalidJsonPointer.php +++ b/src/Exception/InvalidJsonPointer.php @@ -15,10 +15,10 @@ final class InvalidJsonPointer extends \InvalidArgumentException implements Exception { - public static function fromString(string $value): self + public static function fromJsonString(string $value): self { return new self(\sprintf( - 'Value "%s" does not appear to be a valid JSON Pointer.', + 'Value "%s" does not appear to be a valid JSON string representation of a JSON Pointer.', $value, )); } diff --git a/src/JsonPointer.php b/src/JsonPointer.php index be16882d..23fb88f2 100644 --- a/src/JsonPointer.php +++ b/src/JsonPointer.php @@ -20,22 +20,23 @@ */ final class JsonPointer { - private string $value; + private string $jsonStringValue; - private function __construct(string $value) + private function __construct(string $jsonStringValue) { - $this->value = $value; + $this->jsonStringValue = $jsonStringValue; } /** * @see https://datatracker.ietf.org/doc/html/rfc6901#section-3 + * @see https://datatracker.ietf.org/doc/html/rfc6901#section-5 * * @throws Exception\InvalidJsonPointer */ - public static function fromString(string $value): self + public static function fromJsonString(string $value): self { if (1 !== \preg_match('/^(\/(?P((?P[\x00-\x2E]|[\x30-\x7D]|[\x7F-\x{10FFFF}])|(?P~[01]))*))*$/u', $value, $matches)) { - throw Exception\InvalidJsonPointer::fromString($value); + throw Exception\InvalidJsonPointer::fromJsonString($value); } return new self($value); @@ -50,18 +51,18 @@ public function append(ReferenceToken $referenceToken): self { return new self(\sprintf( '%s/%s', - $this->value, - $referenceToken->toEscapedString(), + $this->jsonStringValue, + $referenceToken->toJsonString(), )); } - public function toString(): string + public function toJsonString(): string { - return $this->value; + return $this->jsonStringValue; } public function equals(self $other): bool { - return $this->value === $other->value; + return $this->jsonStringValue === $other->jsonStringValue; } } diff --git a/src/ReferenceToken.php b/src/ReferenceToken.php index 81c5596f..5f6ebb05 100644 --- a/src/ReferenceToken.php +++ b/src/ReferenceToken.php @@ -20,11 +20,11 @@ */ final class ReferenceToken { - private string $escapedValue; + private string $jsonStringValue; - private function __construct(string $escapedValue) + private function __construct(string $jsonStringValue) { - $this->escapedValue = $escapedValue; + $this->jsonStringValue = $jsonStringValue; } /** @@ -40,9 +40,11 @@ public static function fromInt(int $value): self } /** + * @see https://datatracker.ietf.org/doc/html/rfc6901#section-5 + * * @throws Exception\InvalidReferenceToken */ - public static function fromEscapedString(string $value): self + public static function fromJsonString(string $value): self { if (1 !== \preg_match('/^(?P((?P[\x00-\x2E]|[\x30-\x7D]|[\x7F-\x{10FFFF}])|(?P~[01]))*)$/u', $value)) { throw Exception\InvalidReferenceToken::fromString($value); @@ -51,9 +53,9 @@ public static function fromEscapedString(string $value): self return new self($value); } - public static function fromUnescapedString(string $value): self + public static function fromString(string $value): self { - return self::fromEscapedString(\str_replace( + return self::fromJsonString(\str_replace( [ '~', '/', @@ -66,12 +68,12 @@ public static function fromUnescapedString(string $value): self )); } - public function toEscapedString(): string + public function toJsonString(): string { - return $this->escapedValue; + return $this->jsonStringValue; } - public function toUnescapedString(): string + public function toString(): string { return \str_replace( [ @@ -82,12 +84,12 @@ public function toUnescapedString(): string '/', '~', ], - $this->escapedValue, + $this->jsonStringValue, ); } public function equals(self $other): bool { - return $this->escapedValue === $other->escapedValue; + return $this->jsonStringValue === $other->jsonStringValue; } } diff --git a/test/Unit/Exception/InvalidJsonPointerTest.php b/test/Unit/Exception/InvalidJsonPointerTest.php index 2cbc18eb..18d90ed6 100644 --- a/test/Unit/Exception/InvalidJsonPointerTest.php +++ b/test/Unit/Exception/InvalidJsonPointerTest.php @@ -26,14 +26,14 @@ final class InvalidJsonPointerTest extends Framework\TestCase { use Test\Util\Helper; - public function testFromValueReturnsInvalidJsonPointerException(): void + public function testFromJsonStringReturnsInvalidJsonPointerException(): void { $value = self::faker()->sentence(); - $exception = Exception\InvalidJsonPointer::fromString($value); + $exception = Exception\InvalidJsonPointer::fromJsonString($value); $message = \sprintf( - 'Value "%s" does not appear to be a valid JSON Pointer.', + 'Value "%s" does not appear to be a valid JSON string representation of a JSON Pointer.', $value, ); diff --git a/test/Unit/JsonPointerTest.php b/test/Unit/JsonPointerTest.php index 9da8e2d7..204f5224 100644 --- a/test/Unit/JsonPointerTest.php +++ b/test/Unit/JsonPointerTest.php @@ -32,13 +32,13 @@ final class JsonPointerTest extends Framework\TestCase use Test\Util\Helper; /** - * @dataProvider provideInvalidValue + * @dataProvider provideInvalidJsonStringValue */ - public function testFromStringRejectsInvalidValue(string $value): void + public function testFromJsonStringRejectsInvalidValue(string $value): void { $this->expectException(Exception\InvalidJsonPointer::class); - JsonPointer::fromString($value); + JsonPointer::fromJsonString($value); } /** @@ -46,7 +46,7 @@ public function testFromStringRejectsInvalidValue(string $value): void * * @return \Generator */ - public function provideInvalidValue(): \Generator + public function provideInvalidJsonStringValue(): \Generator { $values = [ 'does-not-start-with-forward-slash' => 'foo', @@ -62,13 +62,13 @@ public function provideInvalidValue(): \Generator } /** - * @dataProvider provideValidValue + * @dataProvider provideValidJsonStringValue */ - public function testFromStringReturnsJsonPointer(string $value): void + public function testFromJsonStringReturnsJsonPointer(string $value): void { - $jsonPointer = JsonPointer::fromString($value); + $jsonPointer = JsonPointer::fromJsonString($value); - self::assertSame($value, $jsonPointer->toString()); + self::assertSame($value, $jsonPointer->toJsonString()); } /** @@ -76,7 +76,7 @@ public function testFromStringReturnsJsonPointer(string $value): void * * @return \Generator */ - public function provideValidValue(): \Generator + public function provideValidJsonStringValue(): \Generator { $values = [ 'document' => '', @@ -108,7 +108,7 @@ public function testDocumentReturnsJsonPointer(): void { $jsonPointer = JsonPointer::document(); - self::assertSame('', $jsonPointer->toString()); + self::assertSame('', $jsonPointer->toJsonString()); } /** @@ -133,28 +133,28 @@ public function provideJsonPointerReferenceTokenAndExpectedJsonPointer(): \Gener $values = [ 'document-and-reference-token-from-unescaped-string' => [ JsonPointer::document(), - ReferenceToken::fromUnescapedString('foo'), - JsonPointer::fromString('/foo'), + ReferenceToken::fromString('foo'), + JsonPointer::fromJsonString('/foo'), ], 'document-from-string-and-reference-token-from-unescaped-string' => [ - JsonPointer::fromString(''), - ReferenceToken::fromUnescapedString('foo'), - JsonPointer::fromString('/foo'), + JsonPointer::fromJsonString(''), + ReferenceToken::fromString('foo'), + JsonPointer::fromJsonString('/foo'), ], 'pointer-and-reference-token-from-int' => [ - JsonPointer::fromString('/foo'), + JsonPointer::fromJsonString('/foo'), ReferenceToken::fromInt(9000), - JsonPointer::fromString('/foo/9000'), + JsonPointer::fromJsonString('/foo/9000'), ], 'pointer-and-reference-token-from-unescaped-string' => [ - JsonPointer::fromString('/foo'), - ReferenceToken::fromUnescapedString('bar/baz'), - JsonPointer::fromString('/foo/bar~1baz'), + JsonPointer::fromJsonString('/foo'), + ReferenceToken::fromString('bar/baz'), + JsonPointer::fromJsonString('/foo/bar~1baz'), ], 'pointer-and-reference-token-from-escaped-string' => [ - JsonPointer::fromString('/foo'), - ReferenceToken::fromEscapedString('bar~1baz'), - JsonPointer::fromString('/foo/bar~1baz'), + JsonPointer::fromJsonString('/foo'), + ReferenceToken::fromJsonString('bar~1baz'), + JsonPointer::fromJsonString('/foo/bar~1baz'), ], ]; @@ -169,8 +169,8 @@ public function provideJsonPointerReferenceTokenAndExpectedJsonPointer(): \Gener public function testEqualsReturnsFalseWhenValueIsDifferent(): void { - $one = JsonPointer::fromString('/foo/bar/0/baz~0'); - $two = JsonPointer::fromString('/foo/bar/1'); + $one = JsonPointer::fromJsonString('/foo/bar/0/baz~0'); + $two = JsonPointer::fromJsonString('/foo/bar/1'); self::assertFalse($one->equals($two)); } @@ -179,8 +179,8 @@ public function testEqualsReturnsFalseWhenValueIsSame(): void { $value = '/foo/bar/0/baz~0'; - $one = JsonPointer::fromString($value); - $two = JsonPointer::fromString($value); + $one = JsonPointer::fromJsonString($value); + $two = JsonPointer::fromJsonString($value); self::assertTrue($one->equals($two)); } diff --git a/test/Unit/ReferenceTokenTest.php b/test/Unit/ReferenceTokenTest.php index 8e6f08eb..fe5b7691 100644 --- a/test/Unit/ReferenceTokenTest.php +++ b/test/Unit/ReferenceTokenTest.php @@ -32,11 +32,11 @@ final class ReferenceTokenTest extends Framework\TestCase /** * @dataProvider provideInvalidEscapedStringValue */ - public function testFromEscapedStringRejectsInvalidValue(string $value): void + public function testFromEscapedJsonStringRejectsInvalidValue(string $value): void { $this->expectException(Exception\InvalidReferenceToken::class); - ReferenceToken::fromEscapedString($value); + ReferenceToken::fromJsonString($value); } /** @@ -59,29 +59,29 @@ public function provideInvalidEscapedStringValue(): \Generator } /** - * @dataProvider provideUnescapedAndEscapedValue + * @dataProvider provideValueAndJsonStringValue */ - public function testFromEscapedStringReturnsReferenceToken( - string $unescaped, - string $escaped + public function testFromJsonStringReturnsReferenceToken( + string $value, + string $jsonStringValue ): void { - $referenceToken = ReferenceToken::fromEscapedString($escaped); + $referenceToken = ReferenceToken::fromJsonString($jsonStringValue); - self::assertSame($escaped, $referenceToken->toEscapedString()); - self::assertSame($unescaped, $referenceToken->toUnescapedString()); + self::assertSame($jsonStringValue, $referenceToken->toJsonString()); + self::assertSame($value, $referenceToken->toString()); } /** - * @dataProvider provideUnescapedAndEscapedValue + * @dataProvider provideValueAndJsonStringValue */ - public function testFromUnescapedStringReturnsReferenceToken( - string $unescaped, - string $escaped + public function testFromStringReturnsReferenceToken( + string $value, + string $jsonStringValue ): void { - $referenceToken = ReferenceToken::fromUnescapedString($unescaped); + $referenceToken = ReferenceToken::fromString($value); - self::assertSame($escaped, $referenceToken->toEscapedString()); - self::assertSame($unescaped, $referenceToken->toUnescapedString()); + self::assertSame($jsonStringValue, $referenceToken->toJsonString()); + self::assertSame($value, $referenceToken->toString()); } /** @@ -89,7 +89,7 @@ public function testFromUnescapedStringReturnsReferenceToken( * * @return \Generator */ - public function provideUnescapedAndEscapedValue(): \Generator + public function provideValueAndJsonStringValue(): \Generator { $values = [ 'integerish-9000' => [ @@ -216,26 +216,26 @@ public function testFromIntReturnsReferenceToken(int $value): void { $referenceToken = ReferenceToken::fromInt($value); - self::assertSame((string) $value, $referenceToken->toEscapedString()); - self::assertSame((string) $value, $referenceToken->toUnescapedString()); + self::assertSame((string) $value, $referenceToken->toJsonString()); + self::assertSame((string) $value, $referenceToken->toString()); } - public function testEqualsReturnsFalseWhenEscapedValueIsDifferent(): void + public function testEqualsReturnsFalseWhenJsonStringValueIsDifferent(): void { $faker = self::faker(); - $one = ReferenceToken::fromUnescapedString($faker->sentence()); - $two = ReferenceToken::fromUnescapedString($faker->sentence()); + $one = ReferenceToken::fromString($faker->sentence()); + $two = ReferenceToken::fromString($faker->sentence()); self::assertFalse($one->equals($two)); } - public function testEqualsReturnsTrueWhenEscapedValueIsSame(): void + public function testEqualsReturnsTrueWhenJsonStringValueIsSame(): void { $value = self::faker()->sentence(); - $one = ReferenceToken::fromUnescapedString($value); - $two = ReferenceToken::fromUnescapedString($value); + $one = ReferenceToken::fromString($value); + $two = ReferenceToken::fromString($value); self::assertTrue($one->equals($two)); }