Giter Site home page Giter Site logo

phalcon-annotation-mapper's Introduction

Phalcon annotation mapper

Mapping data from object or associative array to object implements \Alexboo\AnnotationMapper\MapperInterface

Install

You can install it using the composer. But it need phalcon framework php extension.

composer require alexboo/phalcon-annotation-mapper

Examples

class Example extends \Alexboo\AnnotationMapper\Mapper
{
    /**
     * @Mapped(property="priceOut", type="float", precision=2)
     */
    public $price;

    /**
     * @Mapped(type="integer")
     */
    protected $count;

    /**
     * @Mapped(type="float[]")
     */
    public $array;

    /**
     * @Mapped(type="Example2[]")
     */
    public $object;

    public function setCount($value) {
        $this->count = $value;
    }
}

class Example2 extends \Alexboo\AnnotationMapper\Mapper
{
    /**
     * @Mapped(type="integer")
     */
    public $propertyOne;

    /**
     * @Mapped(type="float")
     */
    public $propertyTwo;

    /**
     * @Mapped(type="string")
     */
    public $propertyThree;
}

$donator = [
    'priceOut' => '1000.51000',
    'count' => 100,
    'array' => ['1.222','2.333','3.400000'],
    'object' => [[
        'propertyOne' => '1000.500',
        'propertyTwo' => '2000.500',
        'propertyThree' => ' aaaddd '
    ]]
];

$example = new Example();

$example->mapping($donator);


var_dump($example);

$donator = new stdClass();
$donator->priceOut = '2000.52000';
$donator->count = 200;
$donator->array = [4,5,6];
$donator->object = [[
    'propertyOne' => '1000.520',
    'propertyTwo' => '2000.520',
    'propertyThree' => ' aaaddd '
]];



$example = new Example();

$example->mapping($donator);

var_dump($example);

Additional options.

Caster float and string support additional options. For float caster you can set precision (number of digits after the decimal point). For string you can disabled or enabled (using by default) trim all string.

 // Set default precision for all properties with float type
 
 \Alexboo\AnnotationMapper\Cast\Float::setDefaultPrecision(1);
 
 class Example3 extends \Alexboo\AnnotationMapper\Mapper
 {
     /**
      * @var float $field1
      * @Mapped(type="float")
      */
     public $field1;
     /**
      * @var float $field2
      * @Mapped(type="float")
      */
     public $field2;
 }
 
 $example = new Example3();
 $example->mapping([
     'field1' => '15.123456',
     'field2' => '987.654123',
 ]);
 
 var_dump($example);
 
 // Disabled/enabled trim string data
 
 class Example4 extends \Alexboo\AnnotationMapper\Mapper
 {
     /**
      * @Mapped(type="string")
      */
     public $field1;
     /**
      * @Mapped(type="string")
      */
     public $field2;
 }
 
 \Alexboo\AnnotationMapper\Cast\String::isTrim(false);
 
 $example = new Example4();
 $example->mapping([
     'field1' => '   asdasd  asdasdasd   ',
     'field2' => '        ',
 ]);
 
 var_dump($example);
 
 \Alexboo\AnnotationMapper\Cast\String::isTrim(true);
 
 $example = new Example4();
 $example->mapping([
     'field1' => '   asdasd  asdasdasd   ',
     'field2' => '        ',
 ]);
 
 var_dump($example);
 

Create you custer

You can create custom custer if you need. Than you can specify it like type of property. Caster must implements \Alexboo\AnnotationMapper\CastCastInterface

/**
 * Cast all data to "blabla" string
 * Class BlaBlaCaster
 */
class BlaBlaCaster extends \Alexboo\AnnotationMapper\Cast\CastAbstract
{
    public function cast($value) {
        return "blabla";
    }
}

class Example5 extends \Alexboo\AnnotationMapper\Mapper
{
    /**
     * @Mapped(type="BlaBlaCaster")
     */
    public $field1;
    /**
     * @Mapped(type="\Alexboo\AnnotationMapper\Cast\Integer")
     */
    public $field2;
}

$example = new Example5();
$example->mapping([
    'field1' => 'Say something very clever!',
    'field2' => 123213213,
]);

var_dump($example);

phalcon-annotation-mapper's People

Contributors

alexboo avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.