XML. At the level of application however we want to deal with it in
PHPformat, as objects or arrays.
PHPformat. In order to do that, we will need to implement
Converterinterface and mark it with
TypeDescriptor- Describes type in PHP format. This can be
class, scalar (int, string), arrayetc.
MediaType- Describes Media type format. This can be
$source- is the actual data to be converted.
matchesmethod. Which tells us, if this converter can do conversion from one type to another.
Ecotonethat in case source media type is
JSONand target media type is
PHP, then it should use this converter. Now we need to implement the convert method now. We will do it with pretty naive solution, just to proof the concept.
PHP objectsinstead of
JSON, right? In order to start sending
queriesin different format, we need to provide our handlers with routing key. So
Command and Query buseswill know, where to route the message.
convertAndSend.It takes as first argument
routing keyto which we want to send the message. The second argument describes the
formatof message we send. Third is the data to send itself, in this case command formatted as
App\Domain\Product\JsonToPHPConverterclass completely, as we won't need it anymore.
Ecotone JMSreads properties and deserializes according to type hint or docblock if it is array.
Product should be registered only with positive costWe could put constraint in
Product, validating the
Costamount. But this would assure us only in that place, that this constraint is met and we want to be sure, that the
Costis correct, whenever we make use of it, so we can avoid potential future bugs. To achieve that we will create Value Object named
Costthat will handle the validation, during the construction.
Ecotone JMSdoes provide extension points, so we can tell him, how to convert specific classes.
App\Infrastructure\Converter\CostConverter.We will put it in different namespace, to separate it from the domain.
Ecotonecan read parameter type and return type in order to know, how he can convert from scalar/array to specific class and vice versa. Let's change our command and aggregate class, so it can use the Cost directly.
$costclass property will be automatically converted from