https://github.com/remorhaz/php-json-data
JSON Data Accessors
https://github.com/remorhaz/php-json-data
json php
Last synced: about 1 year ago
JSON representation
JSON Data Accessors
- Host: GitHub
- URL: https://github.com/remorhaz/php-json-data
- Owner: remorhaz
- License: mit
- Created: 2016-11-06T23:51:49.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2024-02-17T12:55:51.000Z (over 2 years ago)
- Last Synced: 2025-03-26T10:03:25.929Z (over 1 year ago)
- Topics: json, php
- Language: PHP
- Size: 139 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# PHP JSON Data Accessors
[](https://packagist.org/packages/remorhaz/php-json-data)
[](https://travis-ci.org/remorhaz/php-json-data)
[](https://scrutinizer-ci.com/g/remorhaz/php-json-data/?branch=master)
[](https://codecov.io/gh/remorhaz/php-json-data)
[](https://packagist.org/packages/remorhaz/php-json-data)
[](https://dashboard.stryker-mutator.io/reports/github.com/remorhaz/php-json-data/master)
[](https://packagist.org/packages/remorhaz/php-json-data)
This library provides infrastructure for JSON documents processing.
## Requirements
- PHP 8.1 or newer.
- [Internationalization functions](https://www.php.net/manual/en/book.intl.php) (ext-intl) - to compare Unicode strings.
- [JSON extension](https://www.php.net/manual/en/book.json.php) (ext-json) - to encode and decode JSON documents.
## Node Values
`ValueInterface` can be used to represent JSON document or it's part. There is a set of descendant interfaces that provide access to typed values:
- `ScalarValueInterface`
- `ArrayValueInterface`
- `ObjectValueInterface`
`NodeValueInterface` includes information about value's path in the document, so it can be used to represent values that really exist in some JSON document. Calculated values that have no paths should implement basic `ValueInterface`.
## Node Value Factories
This type of objects is intended to create node values from some source. This library includes two implementations of node value factories:
- `DecodedJson\NodeValueFactory` creates node value from raw PHP values (produced by `json_decode()` function or created manually).
- `EncodedJson\NodeValueFactory` creates node value directly from encoded JSON string.
### Example
```php
createValue('{"a":"b"}');
$value2 = DecodedJson\NodeValueFactory::create()->createValue((object) ['a' => 'b']);
```
## Event Streams
JSON document can be represented as a stream of events. These events implement descendants of `EventInterface`. Such events can be emitted by any custom JSON parser, but this library also implements standard `ValueWalker` object that converts any `NodeValueInterface` to event stream.
Value walker is also able to use _mutations_ to alter the events. There're no standard mutations, you must implement `MutationInterface` by yourself.
You can also use `EventDecoder` object to convert event stream back to `NodeValueInterface`.
## Value exporters
Library includes a set of `ValueExporterInterface` implementations that allow to export `ValueInterface` to another representation:
- `ValueEncoder` converts value to JSON-encoded string.
- `ValueDecoder` converts value to raw PHP values.
## Comparators
Library includes a set of `ComparatorInterface` implementations that provide a simple way to compare/sort JSON documents:
- `EqualValueComparator` checks for JSON documents equality. Note that objects with same properties in different order are considered equal.
- `GreaterValueComparator` compares JSON numbers and strings.
- `ContainsValueComparator` checks JSON documents either for equality or for containment. Containing object is allowed to have additional properties on any level of recursion.
## License
This library is licensed under the MIT License. Please see [LICENSE](./LICENSE) for more information.