Conversion
Conversion PHP
Last updated
Was this helpful?
Conversion PHP
Last updated
Was this helpful?
Command, queries and events are not always objects.
When they travel via different channels, coming from outside, they are mostly converted to simplified format, like JSON
or XML
.
At the level of application however we want to deal with it in PHP
format, as objects or arrays , so we can understand what are we dealing with.
Moving from one format to another requires conversion. Ecotone
does it automatically using Converters.
Media Type Converters
are responsible for converting data into expected format. It can be for example from JSON to PHP
, but it also can be other way around from PHP to JSON
.
Ecotone comes with configured Media Type Converters, if you don't want to build your own.
We need to define class for it which implements Converter
and is marked by annotation
#[MediaTypeConverter].
There are two methods matches
and convert.
Matches
tells Ecotone, if specific Converter can do conversion, by returning true/false
.
Convert
does actual conversion from one type to another.
TypeDescriptor
- Describes expected type in PHP format. This can be class, scalar type, array
etc.
MediaType
- Describes expected Media Type format. This can be application/json
, application/xml
or application/x-php
etc.
$source
- is the actual data to be converted.
Suppose we have Command Handler
endpoint, which expects PlaceOrderCommand
class.
In our HTTP Controller, we receive JSON
and we want to send it to Command Bus:
Converter that matches
JSON to PHP conversion will be used to do the conversion:
If we would like to implement JSON to PHP
converter, we could start like this:
This will tell Ecotone
that in case source media type is JSON
and target media type is PHP
, then it should use this converter.
Then you could inject into the class specific converter of your use for example Symfony Serializer
and make use of it in convert method.
Ecotone
does come with simplication for PHP level conversion. Suppose we want to send Query with scalar string type: "61cfc7ea-928f-420f-a8e1-656ae2968254"
and convert it to Uuid
on receiving side.
Let's add PHP Conversion:
Ecotone
will read parameter type of Converter
, which is string
and return type which is Uuid
and such Converter will be registered.
Conversion does work with more complicated objects, expecting more than one parameter.
In that case array can be used in order to construct the object.
Suppose we expect array of UUID's.
In order to handle such conversion, we do not need to do anything more. We have converter for string to UUID
then it will be automatically used in order to handle array of string for UUID conversion.
Ecotone
does know that parameter $orderIds
is array of UUIDs based on docblock parameter@param Uuid[]
$orderIds.
Serializer Gateway is built from simple interface
convertFromPHP
- This method is responsible for converting source PHP $data
to specific target Media Type.
convertToPHP
- This method is responsible for converting source with specific media type
to target PHP type.
Ecotone
does delay the conversion to the time, when it's actually needed. In that case it will be just before placeOrder
method will be called.
Then Payload of , which will be {"productIds": [1,2]}
with content Type application/json
will be converted to PlaceOrderCommand
and content Type application/x-php
(default for PHP types).
You may want to serialize/deserialize on the level of your application code using Converters you have already registered. In order to do that Ecotone
comes with Serializer