-
-
Notifications
You must be signed in to change notification settings - Fork 166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Toolkit\Obj
: support array
as the $property
argument for getting multiple properties
#4268
Toolkit\Obj
: support array
as the $property
argument for getting multiple properties
#4268
Conversation
Not sure how the functionality should be $thing = new Obj(['one' => 'π', 'two' => 'Kirby']);
$properties = $thing->get(['foo', 'bar'], 'or-not');
// result: 'or-not'
// result: ['foo' => 'or-not', 'bar' => 'or-not'] |
@distantnative In my opinion, it should be $props = $thing->get(['id', 'name'], ['id'=>null]); I am open to suggestions though |
But what happens if only one of the multiple properties doesn't exist? I think the method should then return the actual values for the properties that do exist and the default only for the others that don't. What about this: $thing = new Obj(['one' => 'π', 'two' => 'Kirby']);
$thing->get(['one', 'foo', 'bar'], 'or-not'); // ['one' => 'π', 'foo' => 'or-not', 'bar' => 'or-not']
$fallback = ['one' => '1', 'foo' => 'fooo', 'bar' => 'pretty-high'];
$thing->get(['one', 'foo', 'bar'], $fallback); // ['one' => 'π', 'foo' => 'fooo', 'bar' => 'pretty-high'] |
Thinking about it, maybe we shouldn't even support the "non-array fallback" case here. Otherwise you could pass an array by accident with the expectation that this value will be used for all non-existing items. If we always handle it like in my second example and fail on non-array fallbacks, the reliability would be improved. |
@lukasbestle In that case, the whole thing would need to be quite a bit more robust, because simple merge in PHP would result in // example
$obj->get(['key'], ['key' => 'fallback']);
// naive way
A::merge($fallback, A::get($this->toArray(), $property);
// result: ['key' => null] It would need to be something completely different to this naive |
Could be something along these lines: if (is_array($fallback) !== true) {
throw new InvalidArgumentException('Fallback must be an array when getting multiple properties');
}
$result = [];
foreach ($property as $key) {
$result[$property] = $this->$key ?? $fallback[$key] ?? null;
}
return $result; |
@lukasbestle Updated as per your idea. I also updated the test, and I also added a test for the invalid fallback argument, but even if it's according to the docs/example from Kirby suite, it still doesn't validate the exception, but fail. I've no idea what's wrong with the test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PHPUnit error is indeed weird...
Can't see anything that could be wrong.
A few minor comments, maybe the resulting rerun of the CI will fix the PHPUnit error as well. ;)
To avoid multiple CI runs, you can add all suggestions you agree with to the batch and commit them at once from the GitHub UI.
Co-authored-by: Lukas Bestle <lukas@getkirby.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM π
Leaving the final review to @distantnative.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Codes OK π
Just a little thought out loud;
I have a feeling that we should do it in a new/separate method. When I saw this method, I immediately thought of the A::pluck()
method.
@afbora That is an option, but both Mainly - I've used |
@afbora |
This PR β¦
Features
Added support for getting multiple properties from
Toolkit\Obj
objects and derived objects, like so:Ready?
For review team
Because it usesA::get()
internally, getting non-existent properties in the array returns['no' => null]
, which I'm not quite sure how to handle - it makes sense from a consistency POV, but might not be expected. I didn't want to pollute the PR with more robust edge case handling.