Adding $transformer->set($path, $value)
killua-eu opened this issue · comments
Hi, would it be possible to add a setter to a function to work along with the mapper?
Can you provide an example?
Edit: For conceptual reasons, this transformer only reads the source array/object and only writes to the destination array/object. Writing to the source is not and will not be supported.
Writing to the source is actually not required. I'm working around with
$transformer = new ArrayTransformer();
$transformer->map('name.0.fn', 'name')
->map('name.0.given', 'given_name')
->map('name.0.family', 'family_name')
->map('name.0.setme', '=========', $transformer->rule()->default('use-this-value'));
$result = $transformer->toArray($claims);
instead, having the set method would just require
$transformer = new ArrayTransformer();
$transformer->map('name.0.fn', 'name')
->map('name.0.given', 'given_name')
->map('name.0.family', 'family_name')
->set('name.0.setme', 'use-this-value');
$result = $transformer->toArray($claims);
to achieve exactly the same result (i.e. echo $result['name'][0]['setme'];
to print use-this-value
).
With a tiny fix it would be possible to allow a blank string as source.
Example:
$transformer->map('bar', '', $transformer->rule()->default('default-value'));
What do you think about this way?
This would be an improvement, though I'd prefer to just extend the class with the set method as a shorthand for what you suggest (or what I have used sofar). Since the class is final, I cant really do it myself.
The set method has been added.