From 6652d9bb45339d7d0d9b3c285bb13e33aeb8141d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Mon, 21 Mar 2022 14:20:21 +0100 Subject: [PATCH] Enhancement: Implement Specification::always() --- CHANGELOG.md | 2 ++ README.md | 14 ++++++++++++++ src/Specification.php | 7 +++++++ test/Unit/SpecificationTest.php | 9 +++++++++ 4 files changed, 32 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 60501134..c527d9c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ For a full diff see [`3.1.0...3.0.0`][2.1.0...3.0.0]. - Added `Specification::closure()` ([#56]), by [@localheinz] - Added `Specification::never()` ([#57]), by [@localheinz] +- Added `Specification::always()` ([#58]), by [@localheinz] ## [`3.0.0`][3.0.0] @@ -89,5 +90,6 @@ For a full diff see [`a5ba52c...1.0.0`][a5ba52c...1.0.0]. [#53]: https://github.com/ergebnis/json-pointer/pull/53 [#56]: https://github.com/ergebnis/json-pointer/pull/56 [#57]: https://github.com/ergebnis/json-pointer/pull/57 +[#58]: https://github.com/ergebnis/json-pointer/pull/58 [@localheinz]: https://github.com/localheinz diff --git a/README.md b/README.md index ed81181e..aa567f3b 100644 --- a/README.md +++ b/README.md @@ -212,6 +212,20 @@ $newJsonPointer->toUriFragmentIdentifierString(); // '#foo/bar/baz' ### `Specification` +You can create a `Specification` that is always satisfied by a `JsonPointer`: + +```php +isSatisfiedBy(Pointer\JsonPointer::fromJsonString('/foo')); // true +$specification->isSatisfiedBy(Pointer\JsonPointer::fromJsonString('/foo/bar')); // true +``` You can create a `Specification` that is satisfied when a closure returns `true` for a `JsonPointer`: ```php diff --git a/src/Specification.php b/src/Specification.php index 6101d848..22947b9f 100644 --- a/src/Specification.php +++ b/src/Specification.php @@ -35,6 +35,13 @@ public function isSatisfiedBy(JsonPointer $jsonPointer): bool return $closure($jsonPointer); } + public static function always(): self + { + return new self(static function (): bool { + return true; + }); + } + public static function anyOf(self ...$specifications): self { return new self(static function (JsonPointer $jsonPointer) use ($specifications): bool { diff --git a/test/Unit/SpecificationTest.php b/test/Unit/SpecificationTest.php index 9979b30f..18b1e436 100644 --- a/test/Unit/SpecificationTest.php +++ b/test/Unit/SpecificationTest.php @@ -30,6 +30,15 @@ final class SpecificationTest extends Framework\TestCase { use Test\Util\Helper; + public function testAlwaysIsAlwaysSatisfiedByAnyJsonPointer(): void + { + $jsonPointer = JsonPointer::fromJsonString('/foo/bar'); + + $specification = Specification::always(); + + self::assertTrue($specification->isSatisfiedBy($jsonPointer)); + } + public function testAnyOfIsNotSatisfiedByJsonPointerWhenEmpty(): void { $jsonPointer = JsonPointer::fromJsonString('/foo/bar/baz');