Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Implement Specification::never() #57

Merged
merged 1 commit into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}