PHP code example of runz0rd / mapper-php

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

    

runz0rd / mapper-php example snippets


class SomeGuy {

    /**
     * @ublic $name;    

    /**
     * @rule limit(1,150)
     * @var integer
     */
    public $age;    

    /**
     * @var boolean
     */
    public $isEmployed;    
    
    /**
     * @name custom-desc
     * @var string
     */
    public $description
}

$model->mapFromArray($myArray);
$model->mapFromJson($myJson);
$model->mapFromXml($myXml);
$model->mapFromObject($myObject);

$model = new Model();
$mapper = new ModelMapper();
$mapper->map($sourceObject, $model);

$myArray = $model->toArray();
$myJson = $model->toJson();
$myXml = $model->toXml();
$myObject = $model->toObject();

$mapper = new ModelMapper();
$myObject = $mapper->unmap($myModel);

$model->validate('createAction');

$model->validate();

$model = new Model();
$validator = new ModelValidator();
$validator->validate($model, 'myAction');

/**
 * @rule email
 */
public $email;

/**
 * @rule limit(0,99)
 */
public $value;

use Common\ModelReflection\ModelProperty;
use Validator\IRule;
use Validator\ModelValidatorException;

class LimitRule implements IRule {

    function getNames() {
        return ['limit'];
    }

    function validate(ModelProperty $property, array $params = []) {
        if($property->getPropertyValue() < $params[0] || $property->getPropertyValue() > $params[1]) {
            throw new ModelValidatorException('Value is not between '.$params[0].' and '.$params[1]);
        }
    }
}

$model = new Model();
$myCustomRule = new MyCustomRule();
$validator = new ModelValidator();
$validator->useRule($myCustomRule);
$validator->loadRules('/path/to/rules/');
$validator->validate($model, 'myAction');

composer