Skip to content

Commit

Permalink
Merge pull request #53 from ergebnis/feature/any-of
Browse files Browse the repository at this point in the history
Enhancement: Implement `Specification::anyOf()`
  • Loading branch information
localheinz authored Mar 21, 2022
2 parents 5ed4ffc + 9a66bf6 commit 6a3382c
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 4 deletions.
12 changes: 9 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## Unreleased

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

## [`3.0.0`][3.0.0]

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

## Added

- Added `Specification` ([#50]), by [@localheinz]
- Added `Specification::anyOf()` ([#53]), by [@localheinz]

## Removed

Expand Down Expand Up @@ -61,7 +66,8 @@ For a full diff see [`a5ba52c...1.0.0`][a5ba52c...1.0.0].
[a5ba52c...1.0.0]: https://github.com/ergebnis/json-pointer/compare/a5ba52c...1.0.0
[1.0.0...main]: https://github.com/ergebnis/json-pointer/compare/1.0.0...main
[2.0.0...2.1.0]: https://github.com/ergebnis/json-pointer/compare/2.0.0...2.1.0
[2.1.0...main]: https://github.com/ergebnis/json-pointer/compare/2.1.0...main
[2.1.0...3.0.0]: https://github.com/ergebnis/json-pointer/compare/2.1.0...3.0.0
[3.0.0...main]: https://github.com/ergebnis/json-pointer/compare/3.0.0...main

[#1]: https://github.com/ergebnis/json-pointer/pull/1
[#2]: https://github.com/ergebnis/json-pointer/pull/2
Expand All @@ -71,6 +77,6 @@ For a full diff see [`a5ba52c...1.0.0`][a5ba52c...1.0.0].
[#9]: https://github.com/ergebnis/json-pointer/pull/9
[#17]: https://github.com/ergebnis/json-pointer/pull/17
[#48]: https://github.com/ergebnis/json-pointer/pull/48
[#50]: https://github.com/ergebnis/json-pointer/pull/50
[#53]: https://github.com/ergebnis/json-pointer/pull/53

[@localheinz]: https://github.com/localheinz
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,24 @@ $specification->isSatisfiedBy(Pointer\JsonPointer::fromJsonString('/foo'));
$specification->isSatisfiedBy(Pointer\JsonPointer::fromJsonString('/foo/bar')); // true
```

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

```php
<?php

declare(strict_types=1);

use Ergebnis\Json\Pointer;

$specification = Pointer\Specification::anyOf(
Pointer\JsonPointer::fromJsonString('/foo/bar'),
Pointer\JsonPointer::fromJsonString('/foo/baz'),
);

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

Please have a look at [`CHANGELOG.md`](CHANGELOG.md).
Expand Down
2 changes: 1 addition & 1 deletion infection.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"text": ".build/infection/infection-log.txt"
},
"minCoveredMsi": 100,
"minMsi": 92,
"minMsi": 94,
"phpUnit": {
"configDir": "test\/Unit"
},
Expand Down
13 changes: 13 additions & 0 deletions src/Specification.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ public function isSatisfiedBy(JsonPointer $jsonPointer): bool
return $closure($jsonPointer);
}

public static function anyOf(self ...$specifications): self
{
return new self(static function (JsonPointer $jsonPointer) use ($specifications): bool {
foreach ($specifications as $specification) {
if ($specification->isSatisfiedBy($jsonPointer)) {
return true;
}
}

return false;
});
}

public static function equals(JsonPointer $other): self
{
return new self(static function (JsonPointer $jsonPointer) use ($other) {
Expand Down
35 changes: 35 additions & 0 deletions test/Unit/SpecificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,39 @@ public function testEqualsIsSatisifedByJsonPointerWhenJsonPointerEqualsOther():

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

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

$specification = Specification::anyOf();

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

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

$specification = Specification::anyOf(
Specification::equals(JsonPointer::fromJsonString('/foo/bar')),
Specification::equals(JsonPointer::fromJsonString('/foo/baz')),
Specification::equals(JsonPointer::fromJsonString('/foo/qux')),
);

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

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

$specification = Specification::anyOf(
Specification::equals(JsonPointer::fromJsonString('/foo/bar')),
Specification::equals(JsonPointer::fromJsonString('/foo/baz')),
Specification::equals(JsonPointer::fromJsonString('/foo/qux')),
);

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

0 comments on commit 6a3382c

Please sign in to comment.