Download the PHP package consistence/consistence-jms-serializer without Composer
On this page you can find all versions of the php package consistence/consistence-jms-serializer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download consistence/consistence-jms-serializer
More information about consistence/consistence-jms-serializer
Files in consistence/consistence-jms-serializer
Package consistence-jms-serializer
Short Description Integration of Consistence library with JMS Serializer
License MIT
Informations about the package consistence-jms-serializer
Integration of Consistence library with JMS Serializer
This library provides integration of Consistence value objects for JMS Serializer so that you can use them in your serialization mappings.
For now, the only integration which is needed is for Enums, see the examples below.
Usage
Enums represent predefined set of values and of course, you will want to serialize and deserialize these values as well. Since Enums
are objects and you only want to (de)serialize represented value, there has to be some mapping.
You can see it in this example where you want to (de)serialize sex for your User
s:
Now you can use the Sex
enum in your User
object. Type for (de)serialization is specified as enum<Your\Enum\Class>
:
Now everything is ready to be used, when you serialize the object, only female
will be returned as the value representing the enum:
And when you deserialize the object, you will receive the Sex
enum object again:
This means that the objects API is symmetrical (you get the same type as you set) and you can start benefiting from Enums advantages such as being sure, that what you get is already a valid value and having the possibility to define methods on top of the represented values.
Nullable by default
Both serialization and deserialization will accept null
values, there is no special mapping for that (by Jms Serializer
s convention), so if you are expecting a non-null value you have to enforce this by other means - either by restricting this in the API of your objects or by validation (needed especially for deserialization).
Invalid values
While serializing, there should be no invalid values, because Enums guarantee that the instance contains only valid values.
While deserializing, there can be an invalid value given, an exception will be thrown:
If you are using this in an API, make sure you will catch this exception and send the consumer a response detailing this error, you can also write a custom message, the available values are listed in InvalidEnumValueException::getAvailableValues()
.
XML support
Unlike in JSON, in XML value types cannot be inferred directly from the values. So if you need to deserialize XML, you have to provide this type manually. You can do this by writing the type in the type definition - for the above example it would be string
:
Special support for mapped MultiEnums
Since the (de)serialization works only with the value the enum is representing, then in case of MultiEnums this would mean outputting the value of the internal bit mask. This could be useful if both the client and server use the same Enum objects, but otherwise this breaks the abstraction and is less readable for a human consumer as well.
If you are using a MultiEum mapped to a single Enum there is a handy solution provided, if you add to your mapping enum<Your\Enum\Class, as_single>
- notice the new as_single
parameter, then the value of MultiEnum
will be serialized as a collection of single Enum
values:
Deserialization then again works symmetrically - giving an array of single Enum
values will produce a MultiEnum
instance:
Installation
If you are using Symfony, you can use
consistence/consistence-jms-serializer-symfony
, which will take care of the integration.
1) Install package consistence/consistence-jms-serializer
with Composer:
2) Register serialization handler:
That's all, you are good to go!