|
| 1 | +FastMap Config, a fluent interface for configuring your mappings |
| 2 | +=== |
| 3 | + |
| 4 | +Example 1: configure mapping for an array |
| 5 | +--- |
| 6 | + |
| 7 | +```php |
| 8 | +<?php |
| 9 | + |
| 10 | +use Kiboko\Component\ETL\Config\ArrayBuilder; |
| 11 | +use Kiboko\Component\ETL\FastMap\Compiler; |
| 12 | +use Kiboko\Component\ETL\FastMap\PropertyAccess\EmptyPropertyPath; |
| 13 | +use Symfony\Component\ExpressionLanguage\ExpressionFunction; |
| 14 | + |
| 15 | +$input = [ |
| 16 | + 'customer' => [ |
| 17 | + 'firstName' => 'John', |
| 18 | + 'lastName' => 'Doe', |
| 19 | + |
| 20 | + ], |
| 21 | + 'items' => [ |
| 22 | + [ |
| 23 | + 'sku' => '123456', |
| 24 | + 'price' => [ |
| 25 | + 'value' => 123.45, |
| 26 | + 'currency' => 'EUR', |
| 27 | + ], |
| 28 | + 'weight' => [ |
| 29 | + 'value' => 123.45, |
| 30 | + 'KILOGRAM', |
| 31 | + ], |
| 32 | + ], |
| 33 | + [ |
| 34 | + 'sku' => '234567', |
| 35 | + 'price' => [ |
| 36 | + 'value' => 23.45, |
| 37 | + 'currency' => 'EUR', |
| 38 | + ], |
| 39 | + 'weight' => [ |
| 40 | + 'value' => 13.45, |
| 41 | + 'KILOGRAM', |
| 42 | + ], |
| 43 | + ], |
| 44 | + ], |
| 45 | + 'shippings' => [ |
| 46 | + [ |
| 47 | + 'sku' => '123456', |
| 48 | + 'price' => [ |
| 49 | + 'value' => 123.45, |
| 50 | + 'currency' => 'EUR', |
| 51 | + ], |
| 52 | + ], |
| 53 | + [ |
| 54 | + 'sku' => '234567', |
| 55 | + 'price' => [ |
| 56 | + 'value' => 23.45, |
| 57 | + 'currency' => 'EUR', |
| 58 | + ], |
| 59 | + ], |
| 60 | + ], |
| 61 | +]; |
| 62 | + |
| 63 | +$compiler = new Compiler\Compiler(new Compiler\Strategy\Spaghetti()); |
| 64 | + |
| 65 | +$interpreter = new Symfony\Component\ExpressionLanguage\ExpressionLanguage(); |
| 66 | +$interpreter->addFunction(ExpressionFunction::fromPhp('array_merge', 'merge')); |
| 67 | +$interpreter->addFunction(new ExpressionFunction( |
| 68 | + 'price', |
| 69 | + function (array $context, float $value, string $currency) { |
| 70 | + return sprintf('\sprintf("%%s %%s", \number_format(%s, 2), "%s")', $value, addslashes($currency)); |
| 71 | + }, |
| 72 | + function (array $context, float $value, string $currency) { |
| 73 | + return sprintf('%s %s', number_format($value, 2), $currency); |
| 74 | + } |
| 75 | +)); |
| 76 | + |
| 77 | +$mapper = (new ArrayBuilder(null, $interpreter)) |
| 78 | + ->children() |
| 79 | + ->constant('[type]', 'ORDER') |
| 80 | + ->copy('[customer][first_name]', '[customer][firstName]') |
| 81 | + ->copy('[customer][last_name]', '[customer][lastName]') |
| 82 | + ->list('[products]', 'merge( input["items"], input["shippings"] )') |
| 83 | + ->children() |
| 84 | + ->copy('[sku]', '[sku]') |
| 85 | + ->expression('[price]', 'price( input["price"]["value"], input["price"]["currency"] )') |
| 86 | + ->end() |
| 87 | + ->end() |
| 88 | + ->end() |
| 89 | + ->getMapper(); |
| 90 | + |
| 91 | +$compiler->compile( |
| 92 | + Compiler\StandardCompilationContext::build(new EmptyPropertyPath(), __DIR__, 'Foo\\ArraySpaghettiMapper'), |
| 93 | + $mapper |
| 94 | +); |
| 95 | + |
| 96 | +var_dump($mapper($input, [], new EmptyPropertyPath())); |
| 97 | +``` |
| 98 | + |
| 99 | +Example 2: configure mapping for an object |
| 100 | +--- |
| 101 | + |
| 102 | +```php |
| 103 | +<?php |
| 104 | + |
| 105 | +use Kiboko\Component\ETL\Config\CompositeBuilder;use Kiboko\Component\ETL\Config\ObjectBuilder; |
| 106 | +use Kiboko\Component\ETL\FastMap\Compiler; |
| 107 | +use Kiboko\Component\ETL\FastMap\PropertyAccess\EmptyPropertyPath; |
| 108 | +use Symfony\Component\ExpressionLanguage\ExpressionFunction; |
| 109 | + |
| 110 | +class Order |
| 111 | +{ |
| 112 | + public ?Customer $customer = null; |
| 113 | + public iterable $products; |
| 114 | + |
| 115 | + public function __construct() |
| 116 | + { |
| 117 | + $this->products = []; |
| 118 | + } |
| 119 | +} |
| 120 | +class Customer |
| 121 | +{ |
| 122 | + public string $firstName; |
| 123 | + public string $lastName; |
| 124 | +} |
| 125 | +class Product |
| 126 | +{ |
| 127 | + public string $sku; |
| 128 | + public ?Price $price = null; |
| 129 | +} |
| 130 | +class Price |
| 131 | +{ |
| 132 | + public float $amount; |
| 133 | + public string $currency; |
| 134 | +} |
| 135 | + |
| 136 | +$mapper = (new ObjectBuilder(Order::class, null, $interpreter)) |
| 137 | + ->children() |
| 138 | + ->object('customer', 'input["customer"]', Customer::class) |
| 139 | + ->children() |
| 140 | + ->merge( |
| 141 | + (new CompositeBuilder(null, $interpreter)) |
| 142 | + ->copy('firstName', '[firstName]') |
| 143 | + ->copy('lastName', '[lastName]') |
| 144 | + ) |
| 145 | + ->end() |
| 146 | + ->end() |
| 147 | + ->collection('products', 'merge( input["items"], input["shippings"] )', Product::class) |
| 148 | + ->children() |
| 149 | + ->copy('sku', '[sku]') |
| 150 | + ->object('price', 'input["price"]', Price::class) |
| 151 | + ->children() |
| 152 | + ->expression('amount', 'input["value"]') |
| 153 | + ->expression('currency', 'input["currency"]') |
| 154 | + ->end() |
| 155 | + ->end() |
| 156 | + ->end() |
| 157 | + ->end() |
| 158 | + ->end() |
| 159 | + ->getMapper(); |
| 160 | + |
| 161 | +var_dump($mapper($input, [], new EmptyPropertyPath())); |
| 162 | + |
| 163 | +$compiler->compile( |
| 164 | + Compiler\StandardCompilationContext::build(new EmptyPropertyPath(), __DIR__, 'Foo\\ObjectSpaghettiMapper'), |
| 165 | + $mapper |
| 166 | +); |
| 167 | +``` |
0 commit comments