The Document object represents the Top Level of a JSON API response. You can create it using Helper\Parser.
This object implements the Accessable interface.
Property of: none
You can use the Accessable interface to access this properties.
| Key | Value | Note | |
|---|---|---|---|
| # | data | - Resource Null object - Resource Identifier object - Resource Item object - Resource Collection object |
not allowed, if 'errors' exists |
| # | errors | Error Collection object | not allowed, if 'data' exists |
| # | meta | Meta object | |
| ? | jsonapi | Jsonapi object | |
| ? | links | Document Link object | |
| ? | included | Resource Collection object | not allowed, if 'data' doesn't exist |
You can check for all possible values using the has() method.
use Art4\JsonApiClient\Helper\Parser;
$jsonapiString = '{"meta":{"info":"Testing the JsonApiClient library."}}';
$document = Parser::parseResponseString($jsonapiString);
var_dump($document->has('data'));
var_dump($document->has('errors'));
var_dump($document->has('meta'));
var_dump($document->has('jsonapi'));
var_dump($document->has('links'));
var_dump($document->has('included'));This returns:
false
false
true
false
false
falseYou can get the keys of all existing values using the getKeys() method. Assume we have the same $document like in the last example.
var_dump($document->getKeys());This returns:
array(
0 => 'meta'
)This can be useful to get available values:
foreach($document->getKeys() as $key) {
$value = $document->get($key);
}You can get all (existing) data using the get() method.
$data = $document->get('data');
$errors = $document->get('errors');
$meta = $document->get('meta');
$jsonapi = $document->get('jsonapi');
$links = $document->get('links');
$included = $document->get('included');Note: Using
get()on a non-existing value will throw an AccessException. Usehas()orgetKeys()to check if a value exists.