Skip to content

Commit

Permalink
Merge pull request #21 from sburba/cleanup/rename-jsoncontext
Browse files Browse the repository at this point in the history
Rename JsonContext to JsonPath
  • Loading branch information
sburba authored Apr 17, 2019
2 parents 93d87bf + 6ef2fc9 commit 6903103
Show file tree
Hide file tree
Showing 17 changed files with 117 additions and 113 deletions.
8 changes: 4 additions & 4 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,13 @@ For example, you can create a custom class adapter like this:
```php
<?php declare(strict_types=1);
use Burba\StrictJson\Adapter;
use Burba\StrictJson\JsonContext;
use Burba\StrictJson\JsonPath;
use Burba\StrictJson\StrictJson;
use Burba\StrictJson\Type;

class DateAdapter implements Adapter
{
public function fromJson($decoded_json, StrictJson $delegate, JsonContext $context): DateTime
public function fromJson($decoded_json, StrictJson $delegate, JsonPath $context): DateTime
{
return DateTime::createFromFormat(DateTime::ISO8601, $decoded_json);
}
Expand Down Expand Up @@ -209,15 +209,15 @@ If you only want to map a single parameter of a class, you can use a parameter a
<?php declare(strict_types=1);
use Burba\StrictJson\Adapter;
use Burba\StrictJson\Fixtures\Docs\Event;
use Burba\StrictJson\JsonContext;
use Burba\StrictJson\JsonPath;
use Burba\StrictJson\StrictJson;
use Burba\StrictJson\Type;
use Burba\StrictJson\Fixtures\Docs\DateAdapter;

// Create your adapter as normal
class LenientBooleanAdapter implements Adapter
{
public function fromJson($decoded_value, StrictJson $delegate, JsonContext $context): bool
public function fromJson($decoded_value, StrictJson $delegate, JsonPath $path): bool
{
return (bool)$decoded_value;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface Adapter
*
* @param StrictJson $delegate You can use this instance of StrictJson to delegate mapping decoded json to
* StrictJson.
* @param JsonContext $context This gives you the JSON path of the position of the decoded json passed to this
* @param JsonPath $path This gives you the JSON path of the position of the decoded json passed to this
* method. It's meant to be used to provide more detailed error messages either by passing to JsonFormatException or
* by passing it to StrictJson when delegating
*
Expand All @@ -26,7 +26,7 @@ interface Adapter
*
* @see ArrayAdapter For an example implementation that uses all three parameters
*/
public function fromJson($decoded_json, StrictJson $delegate, JsonContext $context);
public function fromJson($decoded_json, StrictJson $delegate, JsonPath $path);

/**
* The list of types this adapter supports in the decoded_json parameter of the fromJson method. StrictJson will
Expand Down
6 changes: 3 additions & 3 deletions src/ArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ public function __construct(Type $type)
/**
* @param array $items
* @param StrictJson $delegate
* @param JsonContext $context
* @param JsonPath $path
* @return array
*
* @throws JsonFormatException
*/
public function fromJson($items, StrictJson $delegate, JsonContext $context)
public function fromJson($items, StrictJson $delegate, JsonPath $path)
{
$mapped_items = [];
foreach ($items as $idx => $item) {
$mapped_items[] = $delegate->mapDecoded($item, $this->type, $context->withArrayIndex($idx));
$mapped_items[] = $delegate->mapDecoded($item, $this->type, $path->withArrayIndex($idx));
}
return $mapped_items;
}
Expand Down
4 changes: 2 additions & 2 deletions src/InvalidConfigurationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

class InvalidConfigurationException extends RuntimeException
{
public function __construct(string $message, JsonContext $context, Throwable $previous = null)
public function __construct(string $message, JsonPath $path, Throwable $previous = null)
{
$message = $message . ' at path ' . $context->__toString();
$message = $message . ' at path ' . $path->__toString();
parent::__construct($message, 0, $previous);
}
}
44 changes: 0 additions & 44 deletions src/JsonContext.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/JsonFormatException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

class JsonFormatException extends Exception
{
public function __construct($message, JsonContext $context, Throwable $previous = null)
public function __construct($message, JsonPath $path, Throwable $previous = null)
{
$message = $message . ' at path ' . $context->__toString();
$message = $message . ' at path ' . $path->__toString();
parent::__construct($message, 0, $previous);
}
}
48 changes: 48 additions & 0 deletions src/JsonPath.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php declare(strict_types=1);

namespace Burba\StrictJson;

/**
* Represents the current state of JSON decoding. Used to identify where in the JSON errors occurred
*/
class JsonPath
{
private $path;

private function __construct(string $path = '$')
{
$this->path = $path;
}

/**
* A JsonPath at the root of the JSON
* @return JsonPath
*/
public static function root()
{
return new JsonPath();
}

/**
* @param int $index The array index
* @return JsonPath A new JsonPath that represents indexing into the array of the current path
*/
public function withArrayIndex(int $index)
{
return new JsonPath($this->path . "[$index]");
}

/**
* @param string $property_name
* @return JsonPath A new JsonPath that represents accessing a property of the current path
*/
public function withProperty(string $property_name)
{
return new JsonPath($this->path . ".$property_name");
}

public function __toString()
{
return $this->path == '$' ? '<json_root>' : $this->path;
}
}
Loading

0 comments on commit 6903103

Please sign in to comment.