Skip to content

Commit

Permalink
Merge pull request #57 from ergebnis/feature/never
Browse files Browse the repository at this point in the history
Enhancement: Implement `Specification::never()`
  • Loading branch information
localheinz authored Mar 21, 2022
2 parents 793cba5 + 3811721 commit 4717354
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit 4717354

Please sign in to comment.