diff --git a/CHANGELOG.md b/CHANGELOG.md index 39bf4e89..168fd11e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## Unreleased -For a full diff see [`2.0.0...main`][2.0.0...main]. +For a full diff see [`2.1.0...main`][2.1.0...main]. + +## [`2.1.0`][2.1.0] + +For a full diff see [`2.0.0...2.1.0`][2.0.0...2.1.0]. + +## Added + +- Added `JsonPointers` as a value object ([#17]), by [@localheinz] ## [`2.0.0`][2.0.0] @@ -40,10 +48,12 @@ For a full diff see [`a5ba52c...1.0.0`][a5ba52c...1.0.0]. [1.0.0]: https://github.com/ergebnis/json-pointer/releases/tag/1.0.0 [2.0.0]: https://github.com/ergebnis/json-pointer/releases/tag/2.0.0 +[2.1.0]: https://github.com/ergebnis/json-pointer/releases/tag/2.1.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 +[2.0.0...2.1.0]: https://github.com/ergebnis/json-pointer/compare/2.0.0...2.1.0 +[2.1.0...main]: https://github.com/ergebnis/json-pointer/compare/2.1.0...main [#1]: https://github.com/ergebnis/json-pointer/pull/1 [#2]: https://github.com/ergebnis/json-pointer/pull/2 @@ -51,5 +61,6 @@ For a full diff see [`a5ba52c...1.0.0`][a5ba52c...1.0.0]. [#5]: https://github.com/ergebnis/json-pointer/pull/5 [#6]: https://github.com/ergebnis/json-pointer/pull/6 [#9]: https://github.com/ergebnis/json-pointer/pull/9 +[#17]: https://github.com/ergebnis/json-pointer/pull/17 [@localheinz]: https://github.com/localheinz diff --git a/README.md b/README.md index ebcad91c..0c554d58 100644 --- a/README.md +++ b/README.md @@ -206,6 +206,41 @@ $newJsonPointer->toJsonString(); // '/foo/bar/baz' $newJsonPointer->toUriFragmentIdentifierString(); // '#foo/bar/baz' ``` +### `JsonPointers` + +You can create a `JsonPointers` collection from `JsonPointer`s: + +```php +contains(Pointer\JsonPointer::fromJsonString('/foo')); // false +$jsonPointers->contains(Pointer\JsonPointer::fromJsonString('/foo/9000')); // true +``` + ## Changelog Please have a look at [`CHANGELOG.md`](CHANGELOG.md). diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 1e9e61c9..92fb7d45 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -5,6 +5,11 @@ $referenceTokens + + + $jsonPointers + + testFromJsonStringReturnsReferenceToken diff --git a/src/JsonPointers.php b/src/JsonPointers.php new file mode 100644 index 00000000..67345cd3 --- /dev/null +++ b/src/JsonPointers.php @@ -0,0 +1,44 @@ + + */ + private array $jsonPointers; + + private function __construct(JsonPointer ...$jsonPointers) + { + $this->jsonPointers = $jsonPointers; + } + + public static function create(JsonPointer ...$jsonPointers): self + { + return new self(...$jsonPointers); + } + + public function contains(JsonPointer $other): bool + { + return \in_array( + $other, + $this->jsonPointers, + false, + ); + } +} diff --git a/test/Unit/JsonPointersTest.php b/test/Unit/JsonPointersTest.php new file mode 100644 index 00000000..2b22eca2 --- /dev/null +++ b/test/Unit/JsonPointersTest.php @@ -0,0 +1,56 @@ +contains(JsonPointer::fromJsonString('/foo'))); + } + + public function testContainsReturnsFalseWhenJsonPointersDoesNotContainJsonPointer(): void + { + $jsonPointers = JsonPointers::create( + JsonPointer::fromJsonString('/foo/bar'), + JsonPointer::fromJsonString('/foo/9000'), + ); + + self::assertFalse($jsonPointers->contains(JsonPointer::fromJsonString('/foo'))); + } + + public function testContainsReturnsTrueWhenJsonPointersContainsJsonPointer(): void + { + $jsonPointers = JsonPointers::create( + JsonPointer::fromJsonString('/foo/bar'), + JsonPointer::fromJsonString('/foo/9000'), + ); + + self::assertTrue($jsonPointers->contains(JsonPointer::fromJsonString('/foo/9000'))); + } +}