PHP code example of alevikzs / phmap

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

    

alevikzs / phmap example snippets


class Tree {

    private $height;
    private $name;
    private $branch;

    public function getHeight() {
        return $this->height;
    }

    public function setHeight($height) {
        $this->height = $height;
    }

    public function getName() {
        return $this->name;
    }

    public function setName($name) {
        $this->name = $name;
    }

    public function getBranch() {
        return $this->branch;
    }

    /**
     * @mapper(class="\Tests\Dummy\Branch")
     */
    public function setBranch(Branch $branch) {
        $this->branch = $branch;
    }
    
}

class Branch {

    private $length;
    private $leaves;
    
    public function getLength() {
        return $this->length;
    }

    public function setLength($length) {
        $this->length = $length;
    }

    public function getLeaves() {
        return $this->leaves;
    }

    /**
     * @mapper(class="\Tests\Dummy\Leaf", isArray=true)
     */
    public function setLeaves(array $leaves) {
        $this->leaves = $leaves;
    }

}

class Leaf {

    private $height;
    private $width;
    
    public function getHeight() {
        return $this->height;
    }

    public function setHeight($height) {
        $this->height = $height;
    }

    public function getWidth() {
        return $this->width;
    }

    public function setWidth($width) {
        $this->width = $width;
    }

}

$result = (new \PhMap\Wrapper\Json($json, 'Tree'))->map();

$result = (new \PhMap\Mapper\Structure\Associative($array, 'Tree'))->map();

$result = (new \PhMap\Mapper\Structure\Object($object, 'Tree'))->map();

$result = (new \PhMap\Wrapper\Smart($value, 'Tree'))->map();

new \PhMap\Wrapper\Json($json, 'Tree', \PhMap\Mapper::MEMORY_ANNOTATION_ADAPTER);

new \PhMap\Wrapper\Smart($json, 'Tree', \PhMap\Mapper::FILES_ANNOTATION_ADAPTER);

new \PhMap\Mapper\Structure\Associative($array, 'Tree', \PhMap\Mapper::APC_ANNOTATION_ADAPTER);

new \PhMap\Mapper\Structure\Object($object, 'Tree', \PhMap\Mapper::X_CACHE_ANNOTATION_ADAPTER);

$tree = new Tree();

$result = (new \PhMap\Mapper\Smart($json, $tree))->map();

$mapper = new \PhMap\Mapper\Structure\Object($tree, 'Tree', \PhMap\Mapper::X_CACHE_ANNOTATION_ADAPTER)
$result = $mapper->map();

$mapper->setInputObject($branch)
    ->setOutputObject(new Branch())
    ->setAnnotationAdapterType(Mapper::MEMORY_ANNOTATION_ADAPTER);
$result = $mapper->map();

$mapper = new \PhMap\Mapper\Structure\Object($tree, 'Tree')

$transforms = (new \PhMap\Transforms())
    ->add(
        (new \PhMap\Transform())
            ->setInputFieldName('nameIn')
            ->setOutputFieldName('name')
    )
    ->add(
        (new \PhMap\Transform())
            ->setInputFieldName('branchIn')
            ->setOutputFieldName('branch')
            ->setTransforms(
                (new \PhMap\Transforms())->add(
                    (new \PhMap\Transform())
                        ->setInputFieldName('leavesIn')
                        ->setOutputFieldName('leaves')
                )
            )
    );

$result = $mapper->setTransforms($transforms)->map();

$mapper = new \PhMap\Mapper\Structure\Object($tree, 'Tree')

$attributes = [
    'name',
    'branch.length'
];

$result = $mapper->setSkipAttributes($attributes)->map();

$result = $mapper->disableValidation()->map();

class Tree {

   use \PhMap\MapperTrait;
   
   //other class declaration
   
}

$tree = new Tree();
$result = $tree->mapper($json)->map();

$result = Tree::staticMapper($json)->map();
setSkipAttributes(array $attributes)