PHP code example of freddiegar / json-api-mapper

1. Go to this page and download the library: Download freddiegar/json-api-mapper library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

freddiegar / json-api-mapper example snippets


use FreddieGar\JsonApiMapper\JsonApiMapper;

$jsonApi = new JsonApiMapper($jsonApiResponse);

$data = $jsonApi->getData(0);
$

echo $data->getType(); // articles
echo $data->getId(); // 1

echo print_r($data->getAttributes(), true); // ['title' => 'JSON API paints my bikeshed!', 'body' => '...']
echo $data->getAttribute('created'); // 2015-05-22T14:56:29.000Z
echo $data->getAttribute('description'); // If not exist, return: null

echo print_r($data->getRelationships(), true); // ['author' => ['id' => '1', 'type' => 'people']]
echo get_class($data->getRelationship('author')); // return DataMapperInterface
echo $data->getRelationship('author')->getType(); // people
echo $data->getRelationship('author')->getId(); // 1

echo get_class($>getIncluded(0)->getType(); // people
echo $cluded->getIncluded(1); // null, it is not defined in response

$jsonApi = new JsonApiMapper($jsonApiResponse);

echo get_class($jsonApi->getErrors()); // Return ErrorsMapperInterface

$firstError = $jsonApi->getErrors(0); // Get first error

echo $firstError->getStatus(); // 422
echo print_r($firstError->getSource(), true); // ['pointer' => '/data/attributes/first-name']
echo $firstError->getTitle(); // Invalid Attribute
echo $firstError->getDetail(); // First name must contain at least three characters.

$secondError = $jsonApi->getErrors(1); // null, it is not defined in response

$dataWithIdTwo = $data->find(2); // Return DataMapperInterface if exist else null

$dataPeople = $

$dataPeopleWithIdThree = $= $dataPeople->find(3); // Return DataMapperInterface if exist else null