Skip to content

Commit

Permalink
Enhancement: Implement JsonPointers
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Jan 30, 2022
1 parent 8eb33c3 commit 0a05299
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 2 deletions.
15 changes: 13 additions & 2 deletions 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 [`2.0.0...main`][2.0.0...main].
For a full diff see [`2.1.0...main`][2.1.0...main].

## [`2.1.0`][2.1.0]

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

## Added

- Added `JsonPointers` as a value object ([#17]), by [@localheinz]

## [`2.0.0`][2.0.0]

Expand Down Expand Up @@ -40,16 +48,19 @@ For a full diff see [`a5ba52c...1.0.0`][a5ba52c...1.0.0].

[1.0.0]: https://github.com/ergebnis/json-pointer/releases/tag/1.0.0
[2.0.0]: https://github.com/ergebnis/json-pointer/releases/tag/2.0.0
[2.1.0]: https://github.com/ergebnis/json-pointer/releases/tag/2.1.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...main]: https://github.com/ergebnis/json-pointer/compare/2.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

[#1]: https://github.com/ergebnis/json-pointer/pull/1
[#2]: https://github.com/ergebnis/json-pointer/pull/2
[#4]: https://github.com/ergebnis/json-pointer/pull/4
[#5]: https://github.com/ergebnis/json-pointer/pull/5
[#6]: https://github.com/ergebnis/json-pointer/pull/6
[#9]: https://github.com/ergebnis/json-pointer/pull/9
[#17]: https://github.com/ergebnis/json-pointer/pull/17

[@localheinz]: https://github.com/localheinz
35 changes: 35 additions & 0 deletions README.md
Expand Up @@ -206,6 +206,41 @@ $newJsonPointer->toJsonString(); // '/foo/bar/baz'
$newJsonPointer->toUriFragmentIdentifierString(); // '#foo/bar/baz'
```

### `JsonPointers`

You can create a `JsonPointers` collection from `JsonPointer`s:

```php
<?php

declare(strict_types=1);

use Ergebnis\Json\Pointer;

$jsonPointers = Pointer\JsonPointers::create(
Pointer\JsonPointer::fromJsonString('/foo/bar'),
Pointer\JsonPointer::fromJsonString('/foo/9000'),
);
```

You can test if a `JsonPointers` collection contains a `JsonPointer`:

```php
<?php

declare(strict_types=1);

use Ergebnis\Json\Pointer;

$jsonPointers = Pointer\JsonPointers::create(
Pointer\JsonPointer::fromJsonString('/foo/bar');
Pointer\JsonPointer::fromJsonString('/foo/9000');
);

$jsonPointers->contains(Pointer\JsonPointer::fromJsonString('/foo')); // false
$jsonPointers->contains(Pointer\JsonPointer::fromJsonString('/foo/9000')); // true
```

## Changelog

Please have a look at [`CHANGELOG.md`](CHANGELOG.md).
Expand Down
5 changes: 5 additions & 0 deletions psalm-baseline.xml
Expand Up @@ -5,6 +5,11 @@
<code>$referenceTokens</code>
</MixedPropertyTypeCoercion>
</file>
<file src="src/JsonPointers.php">
<MixedPropertyTypeCoercion occurrences="1">
<code>$jsonPointers</code>
</MixedPropertyTypeCoercion>
</file>
<file src="test/Unit/ReferenceTokenTest.php">
<TooFewArguments occurrences="3">
<code>testFromJsonStringReturnsReferenceToken</code>
Expand Down
44 changes: 44 additions & 0 deletions src/JsonPointers.php
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2020-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;

/**
* @psalm-immutable
*/
final class JsonPointers
{
/**
* @var array<int, JsonPointer>
*/
private array $jsonPointers;

private function __construct(JsonPointer ...$jsonPointers)
{
$this->jsonPointers = $jsonPointers;
}

public static function create(JsonPointer ...$jsonPointers): self
{
return new self(...$jsonPointers);
}

public function contains(JsonPointer $other): bool
{
return \in_array(
$other,
$this->jsonPointers,
false,
);
}
}
56 changes: 56 additions & 0 deletions test/Unit/JsonPointersTest.php
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2020-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\JsonPointers;
use PHPUnit\Framework;

/**
* @internal
*
* @covers \Ergebnis\Json\Pointer\JsonPointers
*
* @uses \Ergebnis\Json\Pointer\JsonPointer
* @uses \Ergebnis\Json\Pointer\ReferenceToken
*/
final class JsonPointersTest extends Framework\TestCase
{
public function testContainsReturnsFalseWhenJsonPointersIsEmpty(): void
{
$jsonPointers = JsonPointers::create();

self::assertFalse($jsonPointers->contains(JsonPointer::fromJsonString('/foo')));
}

public function testContainsReturnsFalseWhenJsonPointersDoesNotContainJsonPointer(): void
{
$jsonPointers = JsonPointers::create(
JsonPointer::fromJsonString('/foo/bar'),
JsonPointer::fromJsonString('/foo/9000'),
);

self::assertFalse($jsonPointers->contains(JsonPointer::fromJsonString('/foo')));
}

public function testContainsReturnsTrueWhenJsonPointersContainsJsonPointer(): void
{
$jsonPointers = JsonPointers::create(
JsonPointer::fromJsonString('/foo/bar'),
JsonPointer::fromJsonString('/foo/9000'),
);

self::assertTrue($jsonPointers->contains(JsonPointer::fromJsonString('/foo/9000')));
}
}

0 comments on commit 0a05299

Please sign in to comment.