feat: support object values in array dot helpers#10226
Conversation
f6c68af to
da3a0d9
Compare
|
Hi there, syawqy! 👋 Thank you for sending this PR! We expect the following in all Pull Requests (PRs).
Important We expect all code changes or bug-fixes to be accompanied by one or more tests added to our test suite to prove the code works. If pull requests do not comply with the above, they will likely be closed. Since we are a team of volunteers, we don't have any more time to work See https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/pull_request.md Sincerely, the mergeable bot 🤖 |
|
Thanks for the PR. I think object handling in array helper's dot syntax is not being implemented anywhere in the |
michalsn
left a comment
There was a problem hiding this comment.
Thank you for sending a PR. Unfortunately, it cannot be merged in its current form.
Since this would be an enhancement rather than a bug fix, it should target the 4.8 branch. Also, as mentioned above, support for arrays of objects should be considered consistently across the other array methods/helpers that currently support only arrays of arrays.
da3a0d9 to
ed4c702
Compare
|
Thanks for the review. I have updated the PR to target the The PR now covers object values for Tests and analysis passed:
|
ed4c702 to
178926b
Compare
|
Docs have been updated to describe object property support for the array dot read operations and |
| @@ -56,16 +56,16 @@ The following functions are available: | |||
| .. note:: Prior to v4.2.0, ``dot_array_search('foo.bar.baz', ['foo' => ['bar' => 23]])`` returned ``23`` | |||
| due to a bug. v4.2.0 and later returns ``null``. | |||
|
|
|||
There was a problem hiding this comment.
I think we should explicitly mention in a note something like: Prior to v4.8.0, Only array of arrays were supported. Support for objects was added in v4.8.0
|
Also, in a general sense, I am quite unsure if helper methods like |
|
That concern makes sense. I agree the docs should be explicit that this is new in v4.8.0 and limited to read-style dot traversal. My reasoning for keeping object support here is that this does not turn the array helper into a general object mutation API. It only allows dot-path reads to traverse public object properties in the same way they traverse array keys. This matches common inputs such as Also, the Array Helper already documents object support for |
I guess you're right, we could add support for objects in ArrayHelper. Well actually, most of the time, it'll be used with array of objects and that is mostly intended, so, it would be okay. |
| return array_key_exists($key, $data); | ||
| } | ||
|
|
||
| return array_key_exists($key, get_object_vars($data)); |
There was a problem hiding this comment.
We can't rely on get_object_vars() as it ignores magic properties, __get(), __isset(), toRawArray(), toArray(), and ArrayAccess-style objects.
Description
Fixes #10225.
This PR updates
array_group_by()so it can group rows that are objects, such as rows returned byjson_decode()or object-based result sets.Previously, the internal grouping helper required each row to be an array and used
dot_array_search(), which also only accepts arrays. That caused a type error when grouping an array ofstdClassobjects.The change keeps the public helper API unchanged and adds internal dot-path lookup support for array/object rows. Grouped rows are preserved as their original type, so object rows remain objects in the result.
Regression tests were added for:
stdClassrows by a direct property.Checklist: