Skip to content

Latest commit

 

History

History
96 lines (68 loc) · 2.7 KB

objects-document.md

File metadata and controls

96 lines (68 loc) · 2.7 KB

Document

Back to Navigation

Description

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

Properties

Symbols definition

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

Usage

Check if a value 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
false

Get the keys of all existing values

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(
  0 => 'meta'
)

This can be useful to get available values:

foreach($document->getKeys() as $key) {
    $value = $document->get($key);
}

Get the containing data

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. Use has() or getKeys() to check if a value exists.