Generating EDI files with custom XML schemas pt. 3
In Part 2 we put off implementation of a couple of classes and methods for later and marked them with
TODO. In our export processor class
Magently\Dropshipping\ExportProcessor\Edi we use
contentGeneratorFactory that should be injected in the constructor. Its type is
Magently\Dropshipping\ContentGenerator\EdiFactory and it will help us to create instances of
Magently\Dropshipping\ContentGenerator\Edi. The latter is a simple class:
It uses yet another factory for creating a
Magently\Dropshipping\ContentGenerator\FileGenerator\Edi instance where the actual file content is generated. In the code above you can see that the file generator object has two public methods:
writeContent. Here is how they are implemented:
orderHeaderFieldReader. This is the virtual type
Magently\Dropshipping\ContentGenerator\FileGenerator\Edi\FieldReader\OrderHeader that we created in Part 2 and can now inject into the file generator object through di.xml:
writeRow method called in
writeOrderHeaderRow does what its name implies. It’s implemented this way:
There are two new elements there.
self::DATA_SEPARATOR is a separator of choice, for example “
mergeConfig is a private method that handles the
mapping attribute that we used in our XML schema. Here’s its implementation:
Now you can go to the admin panel, check some orders and select Generate EDI File. The downloaded file should look similar to this:
The P1 package row has been added the same way as the header row (using edi_order_package.xml file).
We’ve covered lots of ground creating this feature. EDI files can follow many standards specific to industries and regions. You could define different kinds of rows by creating XML configuration files and injecting them by DI. The code presented was rather verbose, but it was written with extensibility in mind. You could make it even more so by leveraging more interfaces.