PHP code example of msqoor / json

1. Go to this page and download the library: Download msqoor/json 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/ */

    

msqoor / json example snippets


$factory = new Json\Factory();
$documentBuilder = new Json\Document\Builder($factory);

$dataBuilder = $documentBuilder->getDataCollectionBuilder();

$documentLinksBuilder = $documentBuilder->getLinksCollectionBuilder();
$documentMetaBuilder = $documentBuilder->getMetaCollectionBuilder();
$documentIncludedBuilder = $documentBuilder->getIncludedCollectionBuilder();

// building the document data
$dataBuilder
    ->setId(1)
    ->setAttributes([
        'firstName' => 'Foo',
        'lastName' => 'Bar',
        'age' => 20,
        'active' => true
    ])
    ->setType('person');

// setting data relationships
$dataRelationshipsBuilder = $dataBuilder->getRelationshipsCollectionBuilder();
$dataRelationshipsBuilder->setName('friends');

$dataRelationshipsDataBuilder = $dataRelationshipsBuilder->getDataCollectionBuilder();
$dataRelationshipsDataBuilder
    ->setId(2)
    ->setType('person')
    ->setAttributes([
        'firstName' => 'Foo 1',
        'lastName' => 'Bar',
        'age' => 20,
        'active' => true
    ]);
$dataRelationshipsDataBuilder->addData()->addToParent(); // added data to relationships

$dataRelationshipsLinksBuilder = $dataRelationshipsBuilder->getLinksCollectionBuilder();
$dataRelationshipsLinksBuilder
    ->setName('self')
    ->setHref('http://www.facebook.com/2');
$dataRelationshipsLinksBuilder->addLink()->addToParent(); // added links to relationships

$dataRelationshipsMetaBuilder = $dataRelationshipsBuilder->getMetaCollectionBuilder();
$dataRelationshipsMetaBuilder
    ->setName('createdAt')
    ->setValue('19 July');
$dataRelationshipsMetaBuilder->addMeta()->addToParent(); // added meta to relationships

$dataRelationshipsBuilder->addRelationships()->addToParent(); // added relationships to data

$dataBuilder->addData()->addToParent(); // added to the document
// finished relationships and added to document

// building the document links
$documentLinksBuilder
    ->setName('self')
    ->setHref('http://www.facebook.com/me')
    ->addLink();
$documentLinksBuilder
    ->setName('related')
    ->setHref('http://www.facebook.com/1');
$documentLinksMetaBuilder = $documentLinksBuilder->getMetaCollectionBuilder();
$documentLinksMetaBuilder
    ->setName('self')
    ->setValue('yes')
    ->addMeta()
    ->addToParent();
$documentLinksBuilder->addLink()->addToParent(); // added links to the document
// finished building document links and added

// building the document meta
$documentMetaBuilder->setName('postsCount')->setValue(340)->addMeta();
$documentMetaBuilder->setName('friendsCount')->setValue(500)->addMeta();

$documentMetaBuilder->addToParent();
// finished document meta and added

echo $documentBuilder->getDocument()->getAsJson();