Conversion
Conversion PHP
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 Media Type 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
.
Available Media Type Converters
Ecotone comes with configured Media Type Converters, if you don't want to build your own.
Media Type Converter
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 beclass, scalar type, array
etc.MediaType
- Describes expected Media Type format. This can beapplication/json
,application/xml
orapplication/x-php
etc.$source
- is the actual data to be converted.
Conversion on fly using Routed Messages
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:
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 Message, 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).
Converter that matches
JSON to PHP conversion will be used to do the conversion:
How to build your own Media Type Converter
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.
PHP to PHP Conversions
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.
As you can see query
neither, command
needs to be class. It can be simple array or even a scalar, it's really up to the developer, what does fit best for him in given scenario.
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.
Conversion Array of objects
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
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
Gateway
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.
As SerializerGateway
will be automatically registered in your Dependency Container.
Last updated