What is it?
The Batch plugin can split a single line of data into multiple ones, or can merge multiple lines into a single one.
Installation
This plugin is already integrated into the Satellite package, so you can’t require it with composer.
Usage
Forking a line
fork
splits data into multiple lines.
In the following example, we have one line containing 2 fields: an id
, and images
which contains multiple values.
Instead we want to have multiple lines, each containing a single image
along with the id
.
Our example input looks like this:
[{"images": ["one.jpg", "two.jpg", "three.jpg"], "id": 15}]
We’ll use the “fork” option to split the lines for each value under images
:
batch:
fork:
foreach: '@=input["images"]'
do: '@={ id: input["id"], image: item }'
$results = [];
foreach ($input["images"] as $key => $item) {
$results[] = ["id" => $input["id"], "image" => $item];
}
The result will be:
[{"id": 15, "image": "one.jpg"}]
[{"id": 15, "image": "two.jpg"}]
[{"id": 15, "image": "three.jpg"}]
Merging multiple lines
merge
concatenates many lines traversing the pipeline into fewer lines.
This can be useful if the targeted API can handle many batches of data in a single payload, like Akeneo for example.
Our example input looks like this:
[{"foo": "bar"}]
[{"lorem": "ipsum"}]
[{"ga": "bu"}]
[{"zo": "meu"}]
[{"bla": "bli"}]
We’ll use the “merge” option. size
dictates the maximum number of lines to concatenate:
batch:
merge:
size: 2
When the number of item reaches size
, the merge will be applied to the next lines, and so on.
The output will be:
[{"foo": "bar"},{"lorem": "ipsum"}]
[{"ga": "bu"},{"zo": "meu"}]
[{"bla": "bli"}]