Skip to content

scallacs/array-transformer-php

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

array-transform-php

/!\ Work in progress... /!\

Usage:

Convert array to xml

$converter
    ->setTemplate('
     {% for user in users %}
    <user> 
        <firstname>{{user.firstname}}</firstname>
        <lastname>{{user.lastname}}</lastname>
    </user>
    {% endfor %}
    ')
    ->render([
        'user' => [
            'firstname' => 'John',
            'lastname' => 'Snow'
        ]
    ]);

TODO from array

You can just typecast it ?

Set the template engine

Synthaxe

[
    'KeyDestination' => 'KeySource'
]

@get()

Accessing keys

"keyDestination" => "@get(key)"

Multiple key into one

"keyDestination" => "My name is @get(firstname) @get(lastname)"

Nested keys

"keyDestination" => "My name is @get(payload[0].user.firstname)"

@forEach()

"keyDestination" => '@forEach(keyToAnIterate, "Hello @get(firstname) @get(lastname)" )'

@compute()

"keyDestination" => '@compute(10 * @get(data.a) / @get(data.b))'

@regex()

TODO

@implode(keyToIterate, renderexpression , glue)

"keyDestination" => '@compute(10 * @get(data.a) / @get(data.b))'

@customCommand()

You can define your own custom command:

"keyDestination" => '@customCommand(param1, param2)'

Custom transformer with a function

"keyDestination" => function ($data){
    // Do your magic...
    return $result;
}

Creating custom transformer

class CustomTransformer extends Transformer{

    public function apply(){
        // Access data to convert with $this->data
        // Access converter data: $this->result
        
        return 'my result';
    }
}

Example

$converter = new Converter([
    'payload' => [
        0 => [
            'user' => [
                'firsname' => 'Torstein',
                'lastname' => 'Horgmo'
            ]
        ],
        1 => [
            'user' => [
                'firsname' => 'Marcus',
                'lastname' => 'Kleveland'
            ]
        ]
    ],
    'extra': [
        'total': 20,
        'page': 1,
        'perPage': 10
    ]
]);
$convert->convert([
    'messages' => '@forEach("payload", "Hello {user.firstname} {user.lastname}!")'
    'extra.message' => '{extra.page}/@devide(@get("extra.total"), @get("extra.perPage")) ({extra.perPage} page(s))'
]);

// Will results in
[
    'messages' => [
        'Hello Torstein Horgmo!',
        'Hello Marcus Kleveland!'
    ],
    'extra' => [
        'message' => '1/2 (2 pages(s))'
    ]
]

Inspiration

Real usage

About

Transform array into another

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages