Skip to content

Commit

Permalink
Merge pull request #58 from ergebnis/feature/always
Browse files Browse the repository at this point in the history
Enhancement: Implement `Specification::always()`
  • Loading branch information
localheinz committed Mar 21, 2022
2 parents 4717354 + 6652d9b commit 9321094
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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]

Expand Down Expand Up @@ -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
14 changes: 14 additions & 0 deletions README.md
Expand Up @@ -212,6 +212,20 @@ $newJsonPointer->toUriFragmentIdentifierString(); // '#foo/bar/baz'

### `Specification`

You can create a `Specification` that is always satisfied by a `JsonPointer`:

```php
<?php

declare(strict_types=1);

use Ergebnis\Json\Pointer;

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

$specification->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
Expand Down
7 changes: 7 additions & 0 deletions src/Specification.php
Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions test/Unit/SpecificationTest.php
Expand Up @@ -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');
Expand Down

0 comments on commit 9321094

Please sign in to comment.