Skip to content

Commit

Permalink
Enhancement: Implement Specification::not()
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Nov 28, 2022
1 parent d33ff26 commit 00478b4
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

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

## Added

- Added `Specification::not()` ([#120]), by [@localheinz]

## Changed

- Dropped support for PHP 7.4 ([#119]), by [@localheinz]
Expand Down Expand Up @@ -99,5 +103,6 @@ For a full diff see [`a5ba52c...1.0.0`][a5ba52c...1.0.0].
[#57]: https://github.com/ergebnis/json-pointer/pull/57
[#58]: https://github.com/ergebnis/json-pointer/pull/58
[#119]: https://github.com/ergebnis/json-pointer/pull/119
[#120]: https://github.com/ergebnis/json-pointer/pull/120

[@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 @@ -273,6 +273,21 @@ $specification->isSatisfiedBy(Pointer\JsonPointer::fromJsonString('/foo'));
$specification->isSatisfiedBy(Pointer\JsonPointer::fromJsonString('/foo/bar')); // false
```

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

```php
<?php

declare(strict_types=1);

use Ergebnis\Json\Pointer;

$specification = Pointer\Specification::not(Pointer\Specification::equals(Pointer\JsonPointer::fromJsonString('/foo/bar')));

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

You can compose `Specification`s to find out if a `JsonPointer` satisfies any of them:

```php
Expand Down
7 changes: 7 additions & 0 deletions src/Specification.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,11 @@ public static function never(): self
return false;
});
}

public static function not(self $specification): self
{
return new self(static function (JsonPointer $jsonPointer) use ($specification): bool {
return !$specification->isSatisfiedBy($jsonPointer);
});
}
}
18 changes: 18 additions & 0 deletions test/Unit/SpecificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,22 @@ public function testNeverIsNotSatisfiedByAnyJsonPointer(): void

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

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

$specification = Specification::not(Specification::always());

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

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

$specification = Specification::not(Specification::never());

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

0 comments on commit 00478b4

Please sign in to comment.