Skip to content

Latest commit

 

History

History
53 lines (45 loc) · 1.67 KB

compiled-mapper.md

File metadata and controls

53 lines (45 loc) · 1.67 KB

Compiled mapper

The compiled mapper is a special mapper aggregating specially-crafted mappers marked as compilable that can combine into a new native mapper and execute it.

<?php

use Kiboko\Component\ETL\FastMap\CompiledMapper;
use Kiboko\Component\ETL\FastMap\Compiler\Compiler;
use Kiboko\Component\ETL\FastMap\Compiler\StandardCompilationContext;
use Kiboko\Component\ETL\FastMap\Mapping\Composite;
use Kiboko\Component\ETL\FastMap\Mapping\Field;
use Kiboko\Component\ETL\FastMap\PropertyAccess\EmptyPropertyPath;
use Symfony\Component\PropertyAccess\PropertyPath;

$input = [
    'customerAddress' => [
        'name' => 'John P. Doe',
        'street' => 'Main Street, 42',
        'city' => 'Oblivion',
        'postcode' => '12345',
    ]   
];

$compiler = new Compiler();

$mapper = new CompiledMapper(
    $compiler,
    StandardCompilationContext::build(new EmptyPropertyPath(), 'var/mappers/', 'Foo\\BarMapper'),
    new Composite\ArrayMapper(
        new Field(
            new PropertyPath('[customer][name]'),
            new Field\ConstantValueMapper('John Doe')
        ),
        new Field(
            new PropertyPath('[address]'),
            new Field\ConcatCopyValuesMapper(
                ', ',
                new PropertyPath('[customerAddress][name]'),
                new PropertyPath('[customerAddress][street]'),
                new PropertyPath('[customerAddress][postcode]'),
                new PropertyPath('[customerAddress][city]')
            )
        )
    )
);

$output = $mapper($input, [], new PropertyPath('[address]'));

// array(2) { "customer" => array(1) { "name" => "John Doe" }, "address" => "John P. Doe, Main Street, 42, 12345, Oblivion" }