Currently handles only JSON.
composer require andreiavrammsd/serializer
$input = '{...}';
$class = ObjectClass::class;
$serializer = Serializer\SerializerBuilder::instance()->build();
// OR
$serializer = Serializer\Factory::create();
$object = $serializer->unserialize($input, $class);
$serializer->serialize($object);
$serializer->toArray($object);
- Property: name of key in input. If not set, the variable name is used.
- Type: If set, the value will be transformed as follows
- int, float, string, bool, array: will cast value to the type.
- collection: value will be wrapped by a countable iterator with array access.
- DateTime: creates a DateTime object, formatting the value by formats given as arguments; first valid format is used.
- Fully qualified class name: the value will be parsed into the given class.
- Array of class: the value will be parsed into an array with each element parsed into the given class.
- Collection of class: the value will be parsed into a collection with each element parsed into the given class.
- Callback: A callable (function or class method) is accepted (with optional parameters). The value will be passed to the callable (with the optional parameters, if set), and the new value will be the result of the callable.
@Serializer\Property("first_name")
@Serializer\Type("int")
@Serializer\Type("float")
@Serializer\Type("string")
@Serializer\Type("bool")
@Serializer\Type("array")
@Serializer\Type("collection")
@Serializer\Type("DateTime","Y-m-d H:i", "Y-m-d")
@Serializer\Type("Entity\User")
@Serializer\Type("array[Entity\User]")
@Serializer\Type("collection[Entity\User]")
@Serializer\Callback("strtoupper")
@Serializer\Callback("substr", "0", "3")
@Serializer\Callback("[User\NameFormatter, firstName]")
@Serializer\Callback("[User\NameFormatter, lastName]", "1", "3")
@Serializer\IgnoreNull() // Ignores null values when serializing or converting to array.
- Collection: a class annotated with Collection and extending the Collection class will be a collection class with its items of the specified class type.
@Serializer\Collection("Entity\User")
- Requirements: Docker, Make
- Build and install dev container:
make build PHPVERSION=7.4
make install PHPVERSION=7.4
- Run QA tools:
make PHPVERSION=7.4
- Work inside dev container:
make run PHPVERSION=7.4
thenmake localqa
- Remove Docker image:
make clean PHPVERSION=7.4
Developed with PhpStorm