PHP code example of consistence / consistence-jms-serializer
1. Go to this page and download the library: Download consistence/consistence-jms-serializer 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/ */
consistence / consistence-jms-serializer example snippets
namespace Consistence\JmsSerializer\Example\User;
class Sex extends \Consistence\Enum\Enum
{
public const FEMALE = 'female';
public const MALE = 'male';
}
namespace Consistence\JmsSerializer\Example\User;
use JMS\Serializer\Annotation as JMS;
class User extends \Consistence\ObjectPrototype
{
// ...
/**
* @JMS\Type("enum<Consistence\JmsSerializer\Example\User\Sex>")
* @var \Consistence\JmsSerializer\Example\User\Sex|null
*/
private $sex;
// ...
public function __construct(
// ...
Sex $sex = null
// ...
)
{
// ...
$this->sex = $sex;
// ...
}
}
namespace Consistence\JmsSerializer\Example\User;
/** @var \JMS\Serializer\Serializer $serializer */
$user = new User(Sex::get(Sex::FEMALE));
var_dump($serializer->serialize($user, 'json'));
/*
{
"sex": "female"
}
*/
namespace Consistence\JmsSerializer\Example\User;
/** @var \JMS\Serializer\Serializer $serializer */
var_dump($serializer->deserialize('{
"sex": "female"
}', User::class, 'json'));
/*
class Consistence\JmsSerializer\Example\User\User#46 (1) {
private $sex =>
class Consistence\JmsSerializer\Example\User\Sex#5 (1) {
private $value =>
string(6) "female"
}
}
*/
namespace Consistence\JmsSerializer\Example\User;
/** @var \JMS\Serializer\Serializer $serializer */
var_dump($serializer->deserialize('{
"sex": "FOO"
}', User::class, 'json'));
// \Consistence\Enum\InvalidEnumValueException: FOO [string] is not a valid value, accepted values: female, male
namespace Consistence\JmsSerializer\Example\User;
use JMS\Serializer\Annotation as JMS;
class User extends \Consistence\ObjectPrototype
{
// ...
/**
* @JMS\Type("enum<Consistence\JmsSerializer\Example\User\Sex, string>")
* @var \Consistence\JmsSerializer\Example\User\Sex|null
*/
private $sex;
// ...
}
namespace Consistence\JmsSerializer\Example\User;
use Consistence\Type\ArrayType\ArrayType;
use JMS\Serializer\Annotation as JMS;
class RoleEnum extends \Consistence\Enum\Enum
{
public const USER = 'user';
public const EMPLOYEE = 'employee';
public const ADMIN = 'admin';
}
class RolesEnum extends \Consistence\Enum\MultiEnum
{
/** @var int[] format: single Enum value (string) => MultiEnum value (int) */
private static $singleMultiMap = [
RoleEnum::USER => 1,
RoleEnum::EMPLOYEE => 2,
RoleEnum::ADMIN => 4,
];
public static function getSingleEnumClass(): string
{
return RoleEnum::class;
}
/**
* Converts value representing a value from single Enum to MultiEnum counterpart
*
* @param string $singleEnumValue
* @return int
*/
protected static function convertSingleEnumValueToValue($singleEnumValue): int
{
return ArrayType::getValue(self::$singleMultiMap, $singleEnumValue);
}
/**
* Converts value representing a value from MultiEnum to single Enum counterpart
*
* @param int $value
* @return string
*/
protected static function convertValueToSingleEnumValue(int $value): string
{
return ArrayType::getKey(self::$singleMultiMap, $value);
}
}
class User extends \Consistence\ObjectPrototype
{
// ...
/**
* @JMS\Type("enum<Consistence\JmsSerializer\Example\User\RolesEnum, as_single>")
* @var \Consistence\JmsSerializer\Example\User\RolesEnum
*/
private $roles;
// ...
public function __construct(
// ...
RolesEnum $roles
// ...
)
{
// ...
$this->roles = $roles;
// ...
}
}
$user = new User(RolesEnum::getMultiByEnums([
RoleEnum::get(RoleEnum::USER),
RoleEnum::get(RoleEnum::ADMIN),
]));
/** @var \JMS\Serializer\Serializer $serializer */
var_dump($serializer->serialize($user, 'json'));
/*
{
"roles": [
"user",
"admin"
]
}
*/
namespace Consistence\JmsSerializer\Example\User;
/** @var \JMS\Serializer\Serializer $serializer */
var_dump($serializer->deserialize('{
"roles": [
"user",
"admin"
]
}', User::class, 'json'));
/*
class Consistence\JmsSerializer\Example\User\User#48 (1) {
private $roles =>
class Consistence\JmsSerializer\Example\User\RolesEnum#37 (1) {
private $value =>
int(5)
}
}
*/
use Consistence\JmsSerializer\Enum\EnumSerializerHandler;
use JMS\Serializer\Handler\HandlerRegistry;
use JMS\Serializer\SerializerBuilder;
$serializer = SerializerBuilder::create()
->configureHandlers(function (HandlerRegistry $registry) {
$registry->registerSubscribingHandler(new EnumSerializerHandler());
})
->build();