JSON Serializer implementation.
Install:
composer.phar require phpfluent/jsonserializer:dev-masterUsage:
<?phpusePHPFluent\JSONSerializer\Serializer;
class Nested extends Serializer
{
/** * @PHPFluent\JSONSerializer\Attribute */private$array;
publicfunctionsetArray(array$array)
{
$this->array = $array;
return$this;
}
}
class MyFancyClass extends Serializer
{
/** * @PHPFluent\JSONSerializer\Attribute */private$email;
/** * @PHPFluent\JSONSerializer\Attribute */private$nested;
private$iWontBeSerialized;
publicfunctionsetEmail($email)
{
if ( ! filter_var($email, FILTER_VALIDATE_EMAIL)) {
thrownew \InvalidArgumentException("Invalid email");
}
$this->email = $email;
return$this;
}
publicfunctionsetNested(Nested$nested)
{
$this->nested = $nested;
return$this;
}
}
$nested = (newNested)->setArray(array(1, 2, 3));
$fancy = (newMyFancyClass)->setEmail("foo@bar.com")->setNested($nested);
json_encode($fancy);
/* $serialized = json_encode( (new MyFancyClass)->setEmail("foo@bar.com")->setNested( (new Nested)->setArray(array(1, 2, 3)) ); );*/Test:
cd phpfluent/jsonserializer
composer.phar install --dev
make test