|
| 1 | +# XML Data Mapper |
| 2 | +A small library to generate xml from source data |
| 3 | + |
| 4 | +# Installation |
| 5 | + |
| 6 | +`composer require alaa-almaliki/xml-data-mapper` |
| 7 | + |
| 8 | +# Example |
| 9 | + |
| 10 | +see [Example](https://github.com/alaa-almaliki/xml-data-mapper/tree/master/example) |
| 11 | + |
| 12 | +To run the example run the command `php main.php` |
| 13 | + |
| 14 | +### How it works |
| 15 | + |
| 16 | +There is one class to be extended: |
| 17 | + |
| 18 | +code |
| 19 | + |
| 20 | +```php |
| 21 | +// Check example folder to fully understand how it works |
| 22 | + |
| 23 | +// the class order represents xml mode which contains xml text node |
| 24 | + |
| 25 | +class Order extends \Xml\Data\Subject |
| 26 | +{ |
| 27 | + protected $nodeName = 'order'; // the node name is name of the xml node that contains text nodes |
| 28 | + |
| 29 | + /** |
| 30 | + * add callbacks |
| 31 | + */ |
| 32 | + public function addCallbacks() |
| 33 | + { |
| 34 | + // you can manupilate the values presented in the xml |
| 35 | + $this->storage->registerCallback('first_name', function ($value) { |
| 36 | + return strtoupper($value); |
| 37 | + }); |
| 38 | + |
| 39 | + $this->storage->registerCallback('last_name', function ($value) { |
| 40 | + return strtoupper($value); |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @return array |
| 46 | + */ |
| 47 | + public function getMappedAttributes() |
| 48 | + { |
| 49 | + return [ |
| 50 | + 'order_number' => 'order_id', // The mapping is done as 'xml_attribute' => 'data_source_attribute' |
| 51 | + 'order_date' => 'created_at', |
| 52 | + 'customer_id' => 'customer_id', |
| 53 | + 'first_name' => 'customer_firstname', |
| 54 | + 'last_name' => 'customer_lastname', |
| 55 | + ]; |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +Generating the XML |
| 61 | +```php |
| 62 | +// Data source - where the keys are used for mapping - see class order above |
| 63 | +$data = [ |
| 64 | + 'order' => [ |
| 65 | + 'order_id' => '1', |
| 66 | + 'created_at' => '2018-03-04', |
| 67 | + 'customer_id' => '123', |
| 68 | + 'customer_firstname' => 'Alaa', |
| 69 | + 'customer_lastname' => 'Al-Maliki', |
| 70 | + 'shipping_address' => [ |
| 71 | + 'full_name' => 'Alaa Al-Maliki', |
| 72 | + 'street' => '123 Wightfield Road', |
| 73 | + 'city' => 'Manachester', |
| 74 | + 'country' => 'United Kingdom', |
| 75 | + 'postcode' => 'AB1 CD2' |
| 76 | + ], |
| 77 | + 'billing_address' => [ |
| 78 | + 'full_name' => 'John Doe', |
| 79 | + 'street' => '456 Crimson Avenue', |
| 80 | + 'city' => 'London', |
| 81 | + 'country' => 'United Kingdom', |
| 82 | + 'postcode' => 'BQ1 DF2' |
| 83 | + ], |
| 84 | + 'items' => [ |
| 85 | + [ |
| 86 | + 'item_id' => '1', |
| 87 | + 'qty' => 2, |
| 88 | + 'name' => 'Blue Jeans', |
| 89 | + 'sku' => 'BJ-123', |
| 90 | + 'tax_code' => 'UK1', |
| 91 | + 'total' => 12.98 |
| 92 | + ], |
| 93 | + [ |
| 94 | + 'item_id' => '2', |
| 95 | + 'qty' => 1, |
| 96 | + 'name' => 'Small T-shirt', |
| 97 | + 'sku' => 'SS-456', |
| 98 | + 'tax_code' => 'UK1', |
| 99 | + 'total' => 32.56 |
| 100 | + ] |
| 101 | + ], |
| 102 | + 'payment' => [ |
| 103 | + 'method' => 'by_card', |
| 104 | + 'amount_paid' => 45.54, |
| 105 | + ] |
| 106 | + ] |
| 107 | +]; |
| 108 | + |
| 109 | +$order = new Order(new \Xml\Data\CallbackStorage()); // order node |
| 110 | +$order->setData($data['order']); |
| 111 | +$lines = []; |
| 112 | + |
| 113 | +foreach ($data['order']['items'] as $item) { |
| 114 | + $orderLine = new Line(new \Xml\Data\CallbackStorage()); // order line node |
| 115 | + $orderLine->setData($item); |
| 116 | + $lines[] = $orderLine; |
| 117 | +} |
| 118 | + |
| 119 | +$shippingAddress = new ShippingAddress(new \Xml\Data\CallbackStorage()); // shipping address node |
| 120 | +$shippingAddress->setData($data['order']['shipping_address']); |
| 121 | + |
| 122 | +$billingAddress = new BillingAddress(new \Xml\Data\CallbackStorage()); // billing address node |
| 123 | +$billingAddress->setData($data['order']['billing_address']); |
| 124 | + |
| 125 | +$payment = new Payment(new \Xml\Data\CallbackStorage()); // payment node |
| 126 | +$payment->setData($data['order']['payment']); |
| 127 | + |
| 128 | +$rootSubject = new RootSubject(new \Xml\Data\CallbackStorage()); // orders node - root |
| 129 | + |
| 130 | +$rootSubject->addSubject($order); // add order node to the root node which is named order |
| 131 | +$order->addSubjects($lines, 'lines'); // add lines nodes to the order node |
| 132 | +$order->addSubject($billingAddress); // add billing address node to the order node |
| 133 | +$order->addSubject($shippingAddress); // add shipping address node to the order node |
| 134 | +$order->addSubject($payment); // add payment node to the order node |
| 135 | + |
| 136 | +// generate the xml file |
| 137 | +\Xml\Data\Mapper::convert($rootSubject) |
| 138 | + ->toXml() |
| 139 | + ->addNamespace('xsi', "http://www.w3.org/1999/xhtml") |
| 140 | + ->addNamespace('xmlns', "http://www.w3.org/2000/10/XMLSchema") |
| 141 | + ->withOpenTag() |
| 142 | + ->write(__DIR__ . '/resource/order.xml'); |
| 143 | +``` |
| 144 | +# Running unit tests |
| 145 | +`vendor/bin/phpunit -c phpunit.xml.dist` |
| 146 | + |
| 147 | +# Contributions |
| 148 | +Feel free to contribute if any bugs found or enhancements need to be done |
| 149 | + |
| 150 | +# License |
| 151 | +MIT |
0 commit comments