Skip to content

Latest commit

 

History

History
138 lines (97 loc) · 3.48 KB

objects-introduction.md

File metadata and controls

138 lines (97 loc) · 3.48 KB

Objects introduction

Back to Navigation

Object Structure

All possible objects and their hierarchical structure are listed below.

Symbols

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

All objects

  1. Document object
  2. Resource Null object
  3. Resource Identifier object
  4. Resource Item object
  5. Resource Collection object
  6. Resource Identifier Collection object
  7. Attributes object
  8. Relationship Collection object
  9. Relationship object
  10. Error Collection object
  11. Error object
  12. Error Source object
  13. Link object
  14. Document Link object
  15. Resource Item Link object
  16. Relationship Link object
  17. Error Link object
  18. Jsonapi object
  19. Meta object

Value access

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 exists
  • get($key): Get a value
  • getKeys(): Get the keys of all existing values

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('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)

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

This can be useful to get available values:

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

Get the containing data

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. Use has() or getKeys() 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."

Get the containing data as array

You can get all data as an array using the ArraySerializer.

Need more?

If you need more opportunities to get the values take a look at the Factory to inject more functionality.