PHP code example of brash-creative / popomapper

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

    

brash-creative / popomapper example snippets



use Brash\PopoMapper\Mapper;

$mapper     = new Mapper();
$object     = $mapper->map($data, new Object());


$data   = '{
    "id": 1,
    "name": "Geoffrey",
    "purchases": [
        {
            "id": 1,
            "name": "Gromit"
        },
        {
            "id": 2,
            "name": "Whatsitsname"
        }
    ]
}';



class Client {
    /**
     * @var int
     */
    private $id;

    /**
     * @var string
     */
    private $name;

    /**
     * @var Purchase[]
     */
    private $purchases;

    /**
     * @param int $id
     *
     * @return $this
     */
    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param string $name
     *
     * @return $this
     */
    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param Purchase[] $purchases
     *
     * @return $this
     */
    public function setPurchases($purchases)
    {
        $this->purchases = $purchases;
        return $this;
    }

    /**
     * @return Purchase[]
     */
    public function getPurchases()
    {
        return $this->purchases;
    }
}



class Purchase {
    /**
     * @var int
     */
    private $id;

    /**
     * @var string
     */
    private $name;

    /**
     * @param int $id
     *
     * @return $this
     */
    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param string $name
     *
     * @return $this
     */
    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }
}



$mapper     = new Mapper();
$client     = $mapper->mapSingle($data, new Client());



$data   = '[
    {
        "id": 1,
        "name": "Client 1"
    },
    {
        "id": 2,
        "name": "Client 2"
    }
]'



$mapper     = new Mapper();
$client     = $mapper->mapMulti($data, new ArrayObject(), new Client());