Skip to content

Commit

Permalink
Simplify JSON Pointer implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
yakimun committed Apr 5, 2021
1 parent e0e7a2d commit 2b706e5
Show file tree
Hide file tree
Showing 17 changed files with 128 additions and 79 deletions.
17 changes: 6 additions & 11 deletions src/Json/JsonPointer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,19 @@ final class JsonPointer
/**
* @var list<string>
*/
private $tokens;

/**
* @param string ...$tokens
* @no-named-arguments
*/
public function __construct(string ...$tokens)
{
$this->tokens = $tokens;
}
private $tokens = [];

/**
* @param string $token
* @return self
*/
public function addToken(string $token): self
{
return new self(...$this->tokens, ...[$token]);
$pointer = new self();
$pointer->tokens = $this->tokens;
$pointer->tokens[] = $token;

return $pointer;
}

/**
Expand Down
30 changes: 17 additions & 13 deletions tests/Json/JsonArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,27 @@ final class JsonArrayTest extends TestCase

protected function setUp(): void
{
$path = new JsonPointer('a');
$jsonPointer = new JsonPointer();
$path = $jsonPointer->addToken('a');
$items = [new JsonNull($path->addToken('0')), new JsonTrue($path->addToken('1'))];

$this->jsonArray = new JsonArray([new JsonNull($path), new JsonTrue($path)], $path);
$this->jsonArray = new JsonArray($items, $path);
}

public function testGetItems(): void
{
$items = $this->jsonArray->getItems();
$jsonPointer = new JsonPointer();
$path = $jsonPointer->addToken('a');
$items = [new JsonNull($path->addToken('0')), new JsonTrue($path->addToken('1'))];

$this->assertCount(2, $items);
$this->assertInstanceOf(JsonNull::class, $items[0]);
$this->assertInstanceOf(JsonTrue::class, $items[1]);
$this->assertEquals($items, $this->jsonArray->getItems());
}

public function testGetPath(): void
{
$this->assertEquals('/a', $this->jsonArray->getPath());
$jsonPointer = new JsonPointer();

$this->assertEquals($jsonPointer->addToken('a'), $this->jsonArray->getPath());
}

/**
Expand All @@ -57,17 +61,17 @@ public function testEquals(JsonValue $value, bool $expected): void
}

/**
* @return list<array{JsonValue, bool}>
* @return non-empty-list<array{JsonValue, bool}>
*/
public function valueProvider(): array
{
$path = new JsonPointer('b');

$jsonNull = new JsonNull($path);
$jsonTrue = new JsonTrue($path);
$jsonPointer = new JsonPointer();
$path = $jsonPointer->addToken('b');
$jsonNull = new JsonNull($path->addToken('0'));
$jsonTrue = new JsonTrue($path->addToken('1'));

return [
[new JsonArray([$jsonNull, $jsonTrue], $path), true],
[new JsonArray([new JsonNull($path), $jsonTrue], $path), true],
[new JsonArray([], $path), false],
[new JsonArray([$jsonNull], $path), false],
[new JsonArray([$jsonTrue, $jsonNull], $path), false],
Expand Down
12 changes: 9 additions & 3 deletions tests/Json/JsonFalseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,30 @@
*/
final class JsonFalseTest extends TestCase
{
/**
* @var JsonPointer
*/
private $jsonPointer;

/**
* @var JsonFalse
*/
private $jsonFalse;

protected function setUp(): void
{
$this->jsonFalse = new JsonFalse(new JsonPointer('a'));
$this->jsonPointer = new JsonPointer();
$this->jsonFalse = new JsonFalse($this->jsonPointer->addToken('a'));
}

public function testGetPath(): void
{
$this->assertEquals('/a', $this->jsonFalse->getPath());
$this->assertEquals($this->jsonPointer->addToken('a'), $this->jsonFalse->getPath());
}

public function testEquals(): void
{
$path = new JsonPointer('b');
$path = $this->jsonPointer->addToken('b');

$this->assertTrue($this->jsonFalse->equals(new JsonFalse($path)));
$this->assertFalse($this->jsonFalse->equals(new JsonNull($path)));
Expand Down
11 changes: 8 additions & 3 deletions tests/Json/JsonFloatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ final class JsonFloatTest extends TestCase

protected function setUp(): void
{
$this->jsonFloat = new JsonFloat(1.5, new JsonPointer('a'));
$jsonPointer = new JsonPointer();

$this->jsonFloat = new JsonFloat(1.5, $jsonPointer->addToken('a'));
}

public function testGetValue(): void
Expand All @@ -33,12 +35,15 @@ public function testGetValue(): void

public function testGetPath(): void
{
$this->assertEquals('/a', $this->jsonFloat->getPath());
$jsonPointer = new JsonPointer();

$this->assertEquals($jsonPointer->addToken('a'), $this->jsonFloat->getPath());
}

public function testEquals(): void
{
$path = new JsonPointer('b');
$jsonPointer = new JsonPointer();
$path = $jsonPointer->addToken('b');

$this->assertTrue($this->jsonFloat->equals(new JsonFloat(1.5, $path)));
$this->assertFalse($this->jsonFloat->equals(new JsonFloat(2.5, $path)));
Expand Down
11 changes: 8 additions & 3 deletions tests/Json/JsonIntegerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ final class JsonIntegerTest extends TestCase

protected function setUp(): void
{
$this->jsonInteger = new JsonInteger(1, new JsonPointer('a'));
$jsonPointer = new JsonPointer();

$this->jsonInteger = new JsonInteger(1, $jsonPointer->addToken('a'));
}

public function testGetValue(): void
Expand All @@ -33,12 +35,15 @@ public function testGetValue(): void

public function testGetPath(): void
{
$this->assertEquals('/a', $this->jsonInteger->getPath());
$jsonPointer = new JsonPointer();

$this->assertEquals($jsonPointer->addToken('a'), $this->jsonInteger->getPath());
}

public function testEquals(): void
{
$path = new JsonPointer('b');
$jsonPointer = new JsonPointer();
$path = $jsonPointer->addToken('b');

$this->assertTrue($this->jsonInteger->equals(new JsonInteger(1, $path)));
$this->assertFalse($this->jsonInteger->equals(new JsonInteger(2, $path)));
Expand Down
12 changes: 9 additions & 3 deletions tests/Json/JsonNullTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,30 @@
*/
final class JsonNullTest extends TestCase
{
/**
* @var JsonPointer
*/
private $jsonPointer;

/**
* @var JsonNull
*/
private $jsonNull;

protected function setUp(): void
{
$this->jsonNull = new JsonNull(new JsonPointer('a'));
$this->jsonPointer = new JsonPointer();
$this->jsonNull = new JsonNull($this->jsonPointer->addToken('a'));
}

public function testGetPath(): void
{
$this->assertEquals('/a', $this->jsonNull->getPath());
$this->assertEquals($this->jsonPointer->addToken('a'), $this->jsonNull->getPath());
}

public function testEquals(): void
{
$path = new JsonPointer('b');
$path = $this->jsonPointer->addToken('b');

$this->assertTrue($this->jsonNull->equals(new JsonNull($path)));
$this->assertFalse($this->jsonNull->equals(new JsonTrue($path)));
Expand Down
28 changes: 16 additions & 12 deletions tests/Json/JsonObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,27 @@ final class JsonObjectTest extends TestCase

protected function setUp(): void
{
$path = new JsonPointer('a');
$jsonPointer = new JsonPointer();
$path = $jsonPointer->addToken('a');
$properties = ['a' => new JsonNull($path->addToken('a')), 'b' => new JsonTrue($path->addToken('b'))];

$this->jsonObject = new JsonObject(['a' => new JsonNull($path), 'b' => new JsonTrue($path)], $path);
$this->jsonObject = new JsonObject($properties, $path);
}

public function testGetProperties(): void
{
$properties = $this->jsonObject->getProperties();
$jsonPointer = new JsonPointer();
$path = $jsonPointer->addToken('a');
$properties = ['a' => new JsonNull($path->addToken('a')), 'b' => new JsonTrue($path->addToken('b'))];

$this->assertCount(2, $properties);
$this->assertInstanceOf(JsonNull::class, $properties['a']);
$this->assertInstanceOf(JsonTrue::class, $properties['b']);
$this->assertEquals($properties, $this->jsonObject->getProperties());
}

public function testGetPath(): void
{
$this->assertEquals('/a', $this->jsonObject->getPath());
$jsonPointer = new JsonPointer();

$this->assertEquals($jsonPointer->addToken('a'), $this->jsonObject->getPath());
}

/**
Expand All @@ -57,14 +61,14 @@ public function testEquals(JsonValue $value, bool $expected): void
}

/**
* @return list<array{JsonValue, bool}>
* @return non-empty-list<array{JsonValue, bool}>
*/
public function valueProvider(): array
{
$path = new JsonPointer('b');

$jsonNull = new JsonNull($path);
$jsonTrue = new JsonTrue($path);
$jsonPointer = new JsonPointer();
$path = $jsonPointer->addToken('b');
$jsonNull = new JsonNull($path->addToken('a'));
$jsonTrue = new JsonTrue($path->addToken('b'));

return [
[new JsonObject(['a' => $jsonNull, 'b' => $jsonTrue], $path), true],
Expand Down
2 changes: 1 addition & 1 deletion tests/Json/JsonPointerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function testAddToken(array $tokens, string $expected): void
}

/**
* @return list<array{list<string>, string}>
* @return non-empty-list<array{list<string>, string}>
*/
public function tokenProvider(): array
{
Expand Down
11 changes: 8 additions & 3 deletions tests/Json/JsonStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ final class JsonStringTest extends TestCase

protected function setUp(): void
{
$this->jsonString = new JsonString('foo', new JsonPointer('a'));
$jsonPointer = new JsonPointer();

$this->jsonString = new JsonString('foo', $jsonPointer->addToken('a'));
}

public function testGetValue(): void
Expand All @@ -33,12 +35,15 @@ public function testGetValue(): void

public function testGetPath(): void
{
$this->assertEquals('/a', $this->jsonString->getPath());
$jsonPointer = new JsonPointer();

$this->assertEquals($jsonPointer->addToken('a'), $this->jsonString->getPath());
}

public function testEquals(): void
{
$path = new JsonPointer('b');
$jsonPointer = new JsonPointer();
$path = $jsonPointer->addToken('b');

$this->assertTrue($this->jsonString->equals(new JsonString('foo', $path)));
$this->assertFalse($this->jsonString->equals(new JsonString('bar', $path)));
Expand Down
12 changes: 9 additions & 3 deletions tests/Json/JsonTrueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,30 @@
*/
final class JsonTrueTest extends TestCase
{
/**
* @var JsonPointer
*/
private $jsonPointer;

/**
* @var JsonTrue
*/
private $jsonTrue;

protected function setUp(): void
{
$this->jsonTrue = new JsonTrue(new JsonPointer('a'));
$this->jsonPointer = new JsonPointer();
$this->jsonTrue = new JsonTrue($this->jsonPointer->addToken('a'));
}

public function testGetPath(): void
{
$this->assertEquals('/a', $this->jsonTrue->getPath());
$this->assertEquals($this->jsonPointer->addToken('a'), $this->jsonTrue->getPath());
}

public function testEquals(): void
{
$path = new JsonPointer('b');
$path = $this->jsonPointer->addToken('b');

$this->assertTrue($this->jsonTrue->equals(new JsonTrue($path)));
$this->assertFalse($this->jsonTrue->equals(new JsonNull($path)));
Expand Down
Loading

0 comments on commit 2b706e5

Please sign in to comment.