All possible objects and their hierarchical structure are listed below.
Symbol | Description |
---|---|
# | at least one of these properties is required |
* | zero, one or more properties |
1 | property is required |
+ | one or more properties are required |
? | property is optional |
! | property is not allowed |
- Document object
- Resource Null object
- Resource Identifier object
- Resource Item object
- Resource Collection object
- Resource Identifier Collection object
- Attributes object
- Relationship Collection object
- Relationship object
- Error Collection object
- Error object
- Error Source object
- Link object
- Document Link object
- Resource Item Link object
- Relationship Link object
- Error Link object
- Jsonapi object
- Meta object
JsonApiClient will parse a JSON API content into a hierarchical object stucture. Every object implements the Accessable
interface and has these methods for getting the values:
has($key)
: Check, if a value existsget($key)
: Get a valuegetKeys()
: Get the keys of all existing values
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('meta'));
This returns:
bool(true)
The has()
method has support for dot-notated keys:
var_dump($document->has('meta.info'));
var_dump($document->has('jsonapi.version'));
This returns:
bool(true)
bool(false)
You 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(1) {
0 => 'meta'
}
This can be useful to get available values:
foreach($document->getKeys() as $key)
{
$model->$key = $document->get($key);
}
You can get all (existing) data using the get()
method.
$meta = $document->get('meta');
// $meta contains a meta object.
Note: Using
get()
on a non-existing value will throw an AccessException. Usehas()
orgetKeys()
to check if a value exists.
The get()
method has support for dot-notated keys:
var_dump($document->get('meta.info'));
This returns:
string(28) "Testing the JsonApiClient library."
You can get all data as an array using the ArraySerializer.
If you need more opportunities to get the values take a look at the Factory to inject more functionality.