PHP code example of apimatic / jsonmapper

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

    

apimatic / jsonmapper example snippets



    
    new JsonMapper();
    $contactObject = $mapper->map($jsonContact, new Contact());
    


    
    new JsonMapper();
    $contactsArray = $mapper->mapArray(
        $jsonContacts, new ArrayObject(), 'Contact'
    );
    


    
    class Contact
    {
        /**
         * Full name
         * @var string
         */
        public $name;

        /**
         * @var Address
         */
        public $address;
    }
    


    
    class Address
    {
        public $street;
        public $city;

        public function getGeoCoords()
        {
            //do something with the $street and $city
        }
    }
    


    
    $json = json_decode(file_get_contents('http://example.org/bigbang.json'));
    $mapper = new JsonMapper();
    $contact = $mapper->map($json, new Contact());

    echo "Geo coordinates for " . $contact->name . ": "
        . var_export($contact->address->getGeoCoords(), true);
    


    $mapper = new JsonMapper();
    $contactObject = $mapper->mapClass($jsonContact, 'Contact')


    $mapper = new JsonMapper();
    $contactsArray = $mapper->mapClassArray($jsonContacts, 'Contact')


    $mapper = new JsonMapper();
    $contactObject = $mapper->mapFor($value, 'oneOf(string,Contact)')


    /**
     * @var \my\application\model\Person
     * @maps person_object
     */
    public $person


    /**
     * @var DateTime
     */
    public $date


    /**
     * @factory MyUtilityClass::createDate
     */
    public $date


    $jm = new JsonMapper();
    $jm->bExceptionOnUndefinedProperty = true;
    $jm->map(...)


    $jm = new JsonMapper();
    $mapper->sAdditionalPropertiesCollectionMethod = 'addAdditionalProperty';
    $jm->map(...)


    /**
     * @var string
     * @


    $jm = new JsonMapper();
    $jm->bExceptionOnMissingData = true;
    $jm->map(...)


    json_decode($jsonString, true)

By default, JsonMapper will throw an exception because ``map()`` .. code:: php

    $jm = new JsonMapper();
    $jm->bEnforceMapType = false;
    $jm->map(...)


    
    /**
     * @discriminator type
     * @discriminatorType person
     */
    class Person
    {
        public $name;
        public $age;
        public $type;
    }

Your local ``Employee`` class:

.. code:: php

    
    /**
     * @discriminator type
     * @discriminatorType employee
     */
    class Employee extends Person
    {
        public $employeeId;
    }

Your application code:

.. code:: php

    $mapper = new JsonMapper();
    $mapper->arChildClasses['Person'] = ['Employee'];
    $mapper->arChildClasses['Employee'] = [];
    $person = $mapper->mapClass($json, 'Person')