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::closure() #56

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion CHANGELOG.md
Expand Up @@ -6,7 +6,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## Unreleased

For a full diff see [`3.0.0...main`][3.0.0...main].
For a full diff see [`3.1.0...main`][3.1.0...main].

## [`3.1.0`][3.1.0]

For a full diff see [`3.1.0...3.0.0`][2.1.0...3.0.0].

## Added

- Added `Specification::closure()` ([#56]), by [@localheinz]

## [`3.0.0`][3.0.0]

Expand Down Expand Up @@ -78,5 +86,6 @@ For a full diff see [`a5ba52c...1.0.0`][a5ba52c...1.0.0].
[#17]: https://github.com/ergebnis/json-pointer/pull/17
[#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

[@localheinz]: https://github.com/localheinz
23 changes: 21 additions & 2 deletions README.md
Expand Up @@ -212,7 +212,24 @@ $newJsonPointer->toUriFragmentIdentifierString(); // '#foo/bar/baz'

### `Specification`

You can create a `Specification` to find out if a `JsonPointer` satisfies it:
You can create a `Specification` that is satisfied when a closure returns `true` for a `JsonPointer`:

```php
<?php

declare(strict_types=1);

use Ergebnis\Json\Pointer;

$specification = Pointer\Specification::closure(static function (Pointer\JsonPointer $jsonPointer) {
return $jsonPointer->toJsonString() === '/foo/bar';
});

$specification->isSatisfiedBy(Pointer\JsonPointer::fromJsonString('/foo')); // false
$specification->isSatisfiedBy(Pointer\JsonPointer::fromJsonString('/foo/bar')); // true
```

You can create a `Specification` that is satisfied when a `JsonPointer` equals another `JsonPointer`:

```php
<?php
Expand All @@ -237,7 +254,9 @@ declare(strict_types=1);
use Ergebnis\Json\Pointer;

$specification = Pointer\Specification::anyOf(
Pointer\Specification::equals(Pointer\JsonPointer::fromJsonString('/foo/bar')),
Pointer\Specification::closure(static function(Pointer\JsonPointer $jsonPointer) {
return $jsonPointer->toJsonString() === '/foo/bar';
}),
Pointer\Specification::equals(Pointer\JsonPointer::fromJsonString('/foo/baz')),
);

Expand Down
8 changes: 8 additions & 0 deletions src/Specification.php
Expand Up @@ -48,6 +48,14 @@ public static function anyOf(self ...$specifications): self
});
}

/**
* @param \Closure(JsonPointer):bool $closure
*/
public static function closure(\Closure $closure): self
{
return new self($closure);
}

public static function equals(JsonPointer $other): self
{
return new self(static function (JsonPointer $jsonPointer) use ($other): bool {
Expand Down
22 changes: 22 additions & 0 deletions test/Unit/SpecificationTest.php
Expand Up @@ -65,6 +65,28 @@ public function testAnyOfIsNotSatisfiedByJsonPointerWhenAnyOfTheSpecificationsIs
self::assertTrue($specification->isSatisfiedBy($jsonPointer));
}

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

$specification = Specification::closure(static function (): bool {
return false;
});

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

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

$specification = Specification::closure(static function (): bool {
return true;
});

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

public function testEqualsIsNotSatisfiedByJsonPointerWhenJsonPointerDoesNotEqualOther(): void
{
$jsonPointer = JsonPointer::fromJsonString('/foo/bar/baz');
Expand Down