Skip to content

Commit

Permalink
Merge c939c80 into f12b207
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz authored Mar 21, 2022
2 parents f12b207 + c939c80 commit d9eff21
Show file tree
Hide file tree
Showing 3 changed files with 103 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 [`2.1.0...main`][2.1.0...main].

## Added

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

## Removed

- Removed `JsonPointers` ([#48]), by [@localheinz]
Expand Down Expand Up @@ -67,5 +71,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

[@localheinz]: https://github.com/localheinz
44 changes: 44 additions & 0 deletions src/Specification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2022 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/json-pointer
*/

namespace Ergebnis\Json\Pointer;

final class Specification
{
/**
* @psalm-var \Closure(JsonPointer):bool
*/
private \Closure $closure;

/**
* @param \Closure(JsonPointer):bool $closure
*/
private function __construct(\Closure $closure)
{
$this->closure = $closure;
}

public function isSatisfiedBy(JsonPointer $jsonPointer): bool
{
$closure = $this->closure;

return $closure($jsonPointer);
}

public static function equals(JsonPointer $other): self
{
return new self(static function (JsonPointer $jsonPointer) use ($other) {
return $jsonPointer->equals($other);
});
}
}
54 changes: 54 additions & 0 deletions test/Unit/SpecificationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2022 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/json-pointer
*/

namespace Ergebnis\Json\Pointer\Test\Unit;

use Ergebnis\Json\Pointer\JsonPointer;
use Ergebnis\Json\Pointer\Specification;
use Ergebnis\Json\Pointer\Test;
use PHPUnit\Framework;

/**
* @internal
*
* @covers \Ergebnis\Json\Pointer\Specification
*
* @uses \Ergebnis\Json\Pointer\JsonPointer
* @uses \Ergebnis\Json\Pointer\ReferenceToken
*/
final class SpecificationTest extends Framework\TestCase
{
use Test\Util\Helper;

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

$other = JsonPointer::fromJsonString('/foo/bar');

$specification = Specification::equals($other);

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

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

$other = JsonPointer::fromJsonString('/foo/bar');

$specification = Specification::equals($other);

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

0 comments on commit d9eff21

Please sign in to comment.