PHP code example of zahran / data-mapper

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

    

zahran / data-mapper example snippets




iner = \Zahran\Mapper\Container::getInstance();
$container->add([
    'cast_type.boolean' => new \Zahran\Mapper\CastType\Boolean(),
    'cast_type.date' => new \Zahran\Mapper\CastType\Date(),
    'cast_type.integer' => new \Zahran\Mapper\CastType\Integer(),
    'cast_type.string' => new \Zahran\Mapper\CastType\Stringify(),
    'cast_type.float' => new \Zahran\Mapper\CastType\FloatingPointNumber(),
    'condition.contains' => new \Zahran\Mapper\Condition\Contains(),
    'condition.eq' => new \Zahran\Mapper\Condition\Equals(),
    'condition.gt' => new \Zahran\Mapper\Condition\GreaterThan(),
    'condition.gte' => new \Zahran\Mapper\Condition\GreaterThanOrEquals(),
    'condition.in' => new \Zahran\Mapper\Condition\Inset(),
    'condition.not_in' => new \Zahran\Mapper\Condition\NotInset(),
    'condition.lt' => new \Zahran\Mapper\Condition\LessThan(),
    'condition.lte' => new \Zahran\Mapper\Condition\LessThanOrEquals(),
    'condition.neq' => new \Zahran\Mapper\Condition\NotEquals(),
    'condition.notnull' => new \Zahran\Mapper\Condition\NotNullable(),
    'condition.null' => new \Zahran\Mapper\Condition\Nullable(),
    'condition.is_numeric' => new \Zahran\Mapper\Condition\IsNumeric(),
    'condition.is_string' => new \Zahran\Mapper\Condition\IsString(),
    'condition.is_boolean' => new \Zahran\Mapper\Condition\IsBoolean(),
    'condition.is_float' => new \Zahran\Mapper\Condition\IsFloat(),
    'condition.is_double' => new \Zahran\Mapper\Condition\IsDouble(),
    'mutator.multiply' => new \Zahran\Mapper\Mutator\Multiply(),
    'helper.util' => new \Zahran\Mapper\Helper\Util(),
    'factory.cast_type' => new \Zahran\Mapper\Factory\CastTypeFactory(),
    'factory.condition' => new \Zahran\Mapper\Factory\ConditionFactory(),
    'factory.mutator' => new \Zahran\Mapper\Factory\MutatorFactory(),
]);
$mapper = new \Zahran\Mapper\DataMapper(
    new \Zahran\Mapper\Factory\AttributeFactory(),
    new \Zahran\Mapper\DataModifier()
);
$output = $mapper->map(
    file_get_contents('./path/to/data.json'),
    file_get_contents('./path/to/mappings.json')
);



namespace Your\Vendor\Name;

use Zahran\Mapper\Contract\CastType as CastTypeInterface;
use Zahran\Mapper\Model\CastType;

class MyCustomCastType implements CastTypeInterface
{
    /**
     * @var CastType
     */
    protected $model;

    public function setModel(CastType $model): CastTypeInterface
    {
        $this->model = $model;
        return $this;
    }

    public function cast($originalValue): bool
    {
        // implement your logic here.
    }
}



\Zahran\Mapper\Container::getInstance()->add(
    'cast_type.{type}', 
    new \Your\Vendor\Name\MyCustomCastType()
);



namespace Your\Vendor\Name;

use Zahran\Mapper\Contract\Condition as ConditionInterface;
use Zahran\Mapper\Model\Condition;

class MyCustomCondition implements ConditionInterface
{
    /**
     * @var Condition
     */
    protected $model;

    public function setModel(Condition $model): ConditionInterface
    {
        $this->model = $model;
        return $this;
    }

    public function apply($originalValue)
    {
        // use $originalValue to compare against it anything you want.
        // use $this->model->getThen() to get the return value if the condition is true.
        // use $this->model->getOtherwise() to get the return value if the condition is false.
    }
}




\Zahran\Mapper\Container::getInstance()->add(
    'condition.{condition_type}', 
    new \Your\Vendor\Name\MyCustomCondition()
);



namespace Your\Vendor\Name;

use Zahran\Mapper\Contract\Mutator as MutatorInterface;
use Zahran\Mapper\Model\Mutator;

class Multiply implements MutatorInterface
{
    /**
     * @var Mutator
     */
    protected $model;

    public function setModel(Mutator $model): MutatorInterface
    {
        $this->model = $model;
        return $this;
    }

    public function apply($originalValue, array $arguments = [])
    {
        // add your logic here
    }
}





\Zahran\Mapper\Container::getInstance()->add(
    'mutator.{name}', 
    new \Your\Vendor\Name\MyCustomMutator()
);