Skip to content

Commit

Permalink
Enhancement: Implement Specification::never()
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Mar 21, 2022
1 parent 793cba5 commit 66300f5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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]

Expand Down Expand Up @@ -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
15 changes: 15 additions & 0 deletions README.md
Expand Up @@ -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
<?php

declare(strict_types=1);

use Ergebnis\Json\Pointer;

$specification = Pointer\Specification::never();

$specification->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:

Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/Specification.php
Expand Up @@ -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;
});
}
}
9 changes: 9 additions & 0 deletions test/Unit/SpecificationTest.php
Expand Up @@ -108,4 +108,13 @@ public function testEqualsIsSatisfiedByJsonPointerWhenJsonPointerEqualsOther():

self::assertTrue($specification->isSatisfiedBy($jsonPointer));
}

public function testNeverIsNotSSatisfiedByAnyJsonPointer(): void
{
$jsonPointer = JsonPointer::fromJsonString('/foo/bar');

$specification = Specification::never();

self::assertFalse($specification->isSatisfiedBy($jsonPointer));
}
}

0 comments on commit 66300f5

Please sign in to comment.