Let’s say for example we have the following data:
{
"apple": "red",
"pear": "green",
"orange": "orange",
"blueberry": "blue",
"strawberry": "red"
}
I need a transformer that converts a value into another based on the provided list (or return null if not found).
Based on the example data above:
- If I provide “pear”, I should get “green”.
- If I provide “strawberry”, I should get “red”.
- If I provide “pineapple”, I should get null.
The PHP equivalent to this would be:
$value = $data[$parameter] ?? null
Hi @w.vannus,
I think JmesPath could do it like this:
Could you please confirm if this solves your problem?
That’s not exactly what I meant, and perhaps I explained the issue not clear enough.
Let me try to explain it in another way, in the hopes it gets more clear:
I provide the following two variables to the transformer:
$list
: A static list of key-values, describing the colors for each type of fruit (see the example in original post)
$fruit
: A string which defines the type of fruit I supply to it. Sometimes, it contains apple
, other times pear
, etc…
Now, I want the transformer to find the color of the provided fruit from the list, and store it in a variable (for example: fruitColor
in your example).
So if I supply it apple
, it should return red
. If I supply pear
it returns green
, etc etc. If I supply pineapple
, which is not found in the list, it should return null
.
PHP function example:
function listValueTransformer(Array $list, String $key)
{
return $list[$key] ?? null;
}
$list = [
"apple" => "red",
"pear" => "green",
"orange" => "orange",
"blueberry" => "blue",
"strawberry" => "red"
];
echo listValueTransformer($list, 'apple'); //Output: 'red'
echo listValueTransformer($list, 'blueberry'); //Output: 'blue'
echo listValueTransformer($list, 'melon'); //Output: null
I hope I explained it clear enough.
Thank you for your response so far!
I’m afraid I don’t understand your question clearly. Can you send me the link to the configuration you created via DM?