kozz/class-mapper

dev-master 2014-11-03 22:11 UTC

This package is not auto-updated.

Last update: 2024-12-17 03:14:52 UTC


README

Build Status Coverage Status Scrutinizer Code Quality Latest Stable Version Latest Unstable Version License

Mapping Class properties using JMS Serializer components

Why to use it

ClassMapper can only fill simple maps and Normalizer performs type casting. So it works much faster than original JMS Serializer.

Usage

Create a Map class like this

use JMS\Serializer\Annotation\Type;
class Map
{
  /**
   * @Type("integer")
   */
  public $client_id;
  /**
   * @Type("integer")
   */
  public $sender_id;
  /**
   * @Type("string")
   */
  public $order_requisite;
  /**
   * @Type("string")
   */
  public $order_warehouse;
} 

Using ClassMapper

Now let's map it

use Kozz\Components\ClassMapper\ClassMapper;

$map = new Map();
$mapper = new ClassMapper($map);
$mapper->setAttributes([
  'client_id'=>1,
  'sender_id'=>2,
  'order_requisite'=>3,
  'order_warehouse'=>4,
]);

$map->order_id; // 1 (integer)
$map->sender_id; // 2 (integer)
$map->order_requisite; // 3 (integer)
$map->order_warehouse; // 4 (integer)

Using ClassMapper and Normalizer

Normalizer is decorator for ClassMapper. It reads @Type annotations and performs type casting

use Kozz\Components\ClassMapper\ClassMapper;
use Kozz\Components\ClassMapper\Normalizer;

$map = new Map();
$mapper = new Normalizer(new ClassMapper($map));
$mapper->setAttributes([
  'client_id'=>'1str',
  'sender_id'=>'2str',
  'order_requisite'=>'3str',
  'order_warehouse'=>'4str',
]);

$map->order_id; // 1 (integer)
$map->sender_id; // 2 (integer)
$map->order_requisite; // '3str' (string)
$map->order_warehouse; // '4str' (string)