diff --git a/CHANGELOG.md b/CHANGELOG.md index 552b4c18..60501134 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ For a full diff see [`3.1.0...3.0.0`][2.1.0...3.0.0]. ## Added - Added `Specification::closure()` ([#56]), by [@localheinz] +- Added `Specification::never()` ([#57]), by [@localheinz] ## [`3.0.0`][3.0.0] @@ -87,5 +88,6 @@ For a full diff see [`a5ba52c...1.0.0`][a5ba52c...1.0.0]. [#48]: https://github.com/ergebnis/json-pointer/pull/48 [#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 [@localheinz]: https://github.com/localheinz diff --git a/README.md b/README.md index ce096898..ed81181e 100644 --- a/README.md +++ b/README.md @@ -243,6 +243,20 @@ $specification = Pointer\Specification::equals(Pointer\JsonPointer::fromJsonStri $specification->isSatisfiedBy(Pointer\JsonPointer::fromJsonString('/foo')); // false $specification->isSatisfiedBy(Pointer\JsonPointer::fromJsonString('/foo/bar')); // true ``` +You can create a `Specification` that is never satisfied by a `JsonPointer`: + +```php +isSatisfiedBy(Pointer\JsonPointer::fromJsonString('/foo')); // false +$specification->isSatisfiedBy(Pointer\JsonPointer::fromJsonString('/foo/bar')); // false +``` You can compose `Specification`s to find out if a `JsonPointer` satisfies any of them: @@ -258,6 +272,7 @@ $specification = Pointer\Specification::anyOf( return $jsonPointer->toJsonString() === '/foo/bar'; }), Pointer\Specification::equals(Pointer\JsonPointer::fromJsonString('/foo/baz')), + Pointer\Specification::never(), ); $specification->isSatisfiedBy(Pointer\JsonPointer::fromJsonString('/foo')); // false diff --git a/src/Specification.php b/src/Specification.php index 5e9596c0..6101d848 100644 --- a/src/Specification.php +++ b/src/Specification.php @@ -62,4 +62,11 @@ public static function equals(JsonPointer $other): self return $jsonPointer->equals($other); }); } + + public static function never(): self + { + return new self(static function (): bool { + return false; + }); + } } diff --git a/test/Unit/SpecificationTest.php b/test/Unit/SpecificationTest.php index 6e47e011..9979b30f 100644 --- a/test/Unit/SpecificationTest.php +++ b/test/Unit/SpecificationTest.php @@ -108,4 +108,13 @@ public function testEqualsIsSatisfiedByJsonPointerWhenJsonPointerEqualsOther(): self::assertTrue($specification->isSatisfiedBy($jsonPointer)); } + + public function testNeverIsNotSatisfiedByAnyJsonPointer(): void + { + $jsonPointer = JsonPointer::fromJsonString('/foo/bar'); + + $specification = Specification::never(); + + self::assertFalse($specification->isSatisfiedBy($jsonPointer)); + } }