Skip to content

Commit 178926b

Browse files
committed
feat: support object values in array dot helpers
1 parent 1a3987f commit 178926b

6 files changed

Lines changed: 206 additions & 81 deletions

File tree

‎system/Helpers/Array/ArrayHelper.php‎

Lines changed: 72 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ final class ArrayHelper
3434
*
3535
* @used-by dot_array_search()
3636
*
37-
* @param string $index The index as dot array syntax.
37+
* @param string $index The index as dot array syntax.
38+
* @param array<array-key, mixed>|object $array
3839
*
39-
* @return array|bool|int|object|string|null
40+
* @return array<array-key, mixed>|bool|int|object|string|null
4041
*/
41-
public static function dotSearch(string $index, array $array)
42+
public static function dotSearch(string $index, array|object $array)
4243
{
4344
return self::arraySearchDot(self::convertToArray($index), $array);
4445
}
@@ -78,9 +79,12 @@ private static function convertToArray(string $index): array
7879
*
7980
* @used-by dotSearch()
8081
*
81-
* @return array|bool|float|int|object|string|null
82+
* @param list<string> $indexes
83+
* @param array<array-key, mixed>|object $array
84+
*
85+
* @return array<array-key, mixed>|bool|float|int|object|string|null
8286
*/
83-
private static function arraySearchDot(array $indexes, array $array)
87+
private static function arraySearchDot(array $indexes, array|object $array)
8488
{
8589
// If index is empty, returns null.
8690
if ($indexes === []) {
@@ -90,7 +94,7 @@ private static function arraySearchDot(array $indexes, array $array)
9094
// Grab the current index
9195
$currentIndex = array_shift($indexes);
9296

93-
if (! isset($array[$currentIndex]) && $currentIndex !== '*') {
97+
if (! self::valueExists($array, $currentIndex) && $currentIndex !== '*') {
9498
return null;
9599
}
96100

@@ -99,7 +103,7 @@ private static function arraySearchDot(array $indexes, array $array)
99103
$answer = [];
100104

101105
foreach ($array as $value) {
102-
if (! is_array($value)) {
106+
if (! is_array($value) && ! is_object($value)) {
103107
return null;
104108
}
105109

@@ -119,12 +123,14 @@ private static function arraySearchDot(array $indexes, array $array)
119123
// If this is the last index, make sure to return it now,
120124
// and not try to recurse through things.
121125
if ($indexes === []) {
122-
return $array[$currentIndex];
126+
return self::value($array, $currentIndex);
123127
}
124128

129+
$value = self::value($array, $currentIndex);
130+
125131
// Do we need to recursively search this value?
126-
if (is_array($array[$currentIndex]) && $array[$currentIndex] !== []) {
127-
return self::arraySearchDot($indexes, $array[$currentIndex]);
132+
if ((is_array($value) && $value !== []) || is_object($value)) {
133+
return self::arraySearchDot($indexes, $value);
128134
}
129135

130136
// Otherwise, not found.
@@ -136,9 +142,9 @@ private static function arraySearchDot(array $indexes, array $array)
136142
*
137143
* If wildcard `*` is used, all items for the key after it must have the key.
138144
*
139-
* @param array<array-key, mixed> $array
145+
* @param array<array-key, mixed>|object $array
140146
*/
141-
public static function dotHas(string $index, array $array): bool
147+
public static function dotHas(string $index, array|object $array): bool
142148
{
143149
self::ensureValidWildcardPattern($index);
144150

@@ -154,10 +160,10 @@ public static function dotHas(string $index, array $array): bool
154160
/**
155161
* Recursively check key existence by dot path, including wildcard support.
156162
*
157-
* @param array<array-key, mixed> $array
158-
* @param list<string> $indexes
163+
* @param array<array-key, mixed>|object $array
164+
* @param list<string> $indexes
159165
*/
160-
private static function hasByDotPath(array $array, array $indexes): bool
166+
private static function hasByDotPath(array|object $array, array $indexes): bool
161167
{
162168
if ($indexes === []) {
163169
return true;
@@ -167,27 +173,29 @@ private static function hasByDotPath(array $array, array $indexes): bool
167173

168174
if ($currentIndex === '*') {
169175
foreach ($array as $item) {
170-
if (! is_array($item) || ! self::hasByDotPath($item, $indexes)) {
176+
if ((! is_array($item) && ! is_object($item)) || ! self::hasByDotPath($item, $indexes)) {
171177
return false;
172178
}
173179
}
174180

175181
return true;
176182
}
177183

178-
if (! array_key_exists($currentIndex, $array)) {
184+
if (! self::keyExists($array, $currentIndex)) {
179185
return false;
180186
}
181187

182188
if ($indexes === []) {
183189
return true;
184190
}
185191

186-
if (! is_array($array[$currentIndex])) {
192+
$value = self::value($array, $currentIndex);
193+
194+
if (! is_array($value) && ! is_object($value)) {
187195
return false;
188196
}
189197

190-
return self::hasByDotPath($array[$currentIndex], $indexes);
198+
return self::hasByDotPath($value, $indexes);
191199
}
192200

193201
/**
@@ -333,13 +341,16 @@ public static function groupBy(array $array, array $indexes, bool $includeEmpty
333341

334342
/**
335343
* Recursively attach $row to the $indexes path of values found by
336-
* `dot_array_search()`.
344+
* dot syntax.
337345
*
338346
* @used-by groupBy()
347+
*
348+
* @param array<array-key, mixed>|object $row
349+
* @param list<string> $indexes
339350
*/
340351
private static function arrayAttachIndexedValue(
341352
array $result,
342-
array $row,
353+
array|object $row,
343354
array $indexes,
344355
bool $includeEmpty,
345356
): array {
@@ -349,7 +360,7 @@ private static function arrayAttachIndexedValue(
349360
return $result;
350361
}
351362

352-
$value = dot_array_search($index, $row);
363+
$value = self::dotSearch($index, $row);
353364

354365
if (! is_scalar($value)) {
355366
$value = '';
@@ -447,6 +458,42 @@ public static function sortValuesByNatural(array &$array, $sortByIndex = null):
447458
});
448459
}
449460

461+
/**
462+
* @param array<array-key, mixed>|object $data
463+
*/
464+
private static function keyExists(array|object $data, string $key): bool
465+
{
466+
if (is_array($data)) {
467+
return array_key_exists($key, $data);
468+
}
469+
470+
return array_key_exists($key, get_object_vars($data));
471+
}
472+
473+
/**
474+
* @param array<array-key, mixed>|object $data
475+
*/
476+
private static function valueExists(array|object $data, string $key): bool
477+
{
478+
if (is_array($data)) {
479+
return isset($data[$key]);
480+
}
481+
482+
return isset(get_object_vars($data)[$key]);
483+
}
484+
485+
/**
486+
* @param array<array-key, mixed>|object $data
487+
*/
488+
private static function value(array|object $data, string $key): mixed
489+
{
490+
if (is_array($data)) {
491+
return $data[$key];
492+
}
493+
494+
return get_object_vars($data)[$key];
495+
}
496+
450497
/**
451498
* Throws exception for invalid wildcard patterns.
452499
*/
@@ -606,7 +653,7 @@ private static function projectByDotPath(
606653
$currentIndex = array_shift($indexes);
607654

608655
if ($currentIndex === '*') {
609-
if (! is_array($source)) {
656+
if (! is_array($source) && ! is_object($source)) {
610657
return;
611658
}
612659

@@ -617,10 +664,10 @@ private static function projectByDotPath(
617664
return;
618665
}
619666

620-
if (! is_array($source) || ! array_key_exists($currentIndex, $source)) {
667+
if ((! is_array($source) && ! is_object($source)) || ! self::keyExists($source, $currentIndex)) {
621668
return;
622669
}
623670

624-
self::projectByDotPath($source[$currentIndex], $indexes, $result, [...$prefix, $currentIndex]);
671+
self::projectByDotPath(self::value($source, $currentIndex), $indexes, $result, [...$prefix, $currentIndex]);
625672
}
626673
}

‎system/Helpers/array_helper.php‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020
* Searches an array through dot syntax. Supports
2121
* wildcard searches, like foo.*.bar
2222
*
23-
* @return array|bool|int|object|string|null
23+
* @param array<array-key, mixed>|object $array
24+
*
25+
* @return array<array-key, mixed>|bool|int|object|string|null
2426
*/
25-
function dot_array_search(string $index, array $array)
27+
function dot_array_search(string $index, array|object $array)
2628
{
2729
return ArrayHelper::dotSearch($index, $array);
2830
}
@@ -32,9 +34,9 @@ function dot_array_search(string $index, array $array)
3234
/**
3335
* Checks if an array key exists using dot syntax.
3436
*
35-
* @param array<array-key, mixed> $array
37+
* @param array<array-key, mixed>|object $array
3638
*/
37-
function dot_array_has(string $index, array $array): bool
39+
function dot_array_has(string $index, array|object $array): bool
3840
{
3941
return ArrayHelper::dotHas($index, $array);
4042
}

‎tests/system/Helpers/ArrayHelperTest.php‎

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use CodeIgniter\Test\CIUnitTestCase;
1818
use PHPUnit\Framework\Attributes\DataProvider;
1919
use PHPUnit\Framework\Attributes\Group;
20+
use stdClass;
2021
use ValueError;
2122

2223
/**
@@ -381,6 +382,59 @@ public function testArrayDotIgnoresLastWildcard(): void
381382
$this->assertSame(['baz' => 23], dot_array_search('foo.bar.*', $data));
382383
}
383384

385+
public function testArrayDotWithObjectValues(): void
386+
{
387+
$data = [
388+
'user' => (object) [
389+
'profile' => (object) [
390+
'name' => 'Jane',
391+
],
392+
],
393+
];
394+
395+
$this->assertSame('Jane', dot_array_search('user.profile.name', $data));
396+
$this->assertTrue(dot_array_has('user.profile.name', $data));
397+
$this->assertFalse(dot_array_has('user.profile.email', $data));
398+
}
399+
400+
public function testArrayDotWildcardWithObjectValues(): void
401+
{
402+
$data = [
403+
'users' => [
404+
(object) ['name' => 'John'],
405+
(object) ['name' => 'Maria'],
406+
],
407+
];
408+
409+
$this->assertSame(['John', 'Maria'], dot_array_search('users.*.name', $data));
410+
$this->assertTrue(dot_array_has('users.*.name', $data));
411+
}
412+
413+
public function testArrayDotOnlyWithObjectValues(): void
414+
{
415+
$data = [
416+
'users' => [
417+
(object) [
418+
'id' => 1,
419+
'name' => 'John',
420+
],
421+
(object) [
422+
'id' => 2,
423+
'name' => 'Maria',
424+
],
425+
],
426+
];
427+
428+
$expected = [
429+
'users' => [
430+
['id' => 1],
431+
['id' => 2],
432+
],
433+
];
434+
435+
$this->assertSame($expected, dot_array_only($data, 'users.*.id'));
436+
}
437+
384438
/**
385439
* @param int|string $key
386440
* @param array|string|null $expected
@@ -1501,4 +1555,66 @@ public static function provideArrayGroupByExcludeEmpty(): iterable
15011555
],
15021556
];
15031557
}
1558+
1559+
/**
1560+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/10225
1561+
*/
1562+
public function testArrayGroupByWithObjectRows(): void
1563+
{
1564+
$json = <<<'JSON'
1565+
[
1566+
{ "id": 1, "name": "Giraffe", "group": "Mammals" },
1567+
{ "id": 2, "name": "Zebra", "group": "Mammals" },
1568+
{ "id": 3, "name": "Crow", "group": "Birds" }
1569+
]
1570+
JSON;
1571+
$data = json_decode($json);
1572+
1573+
$this->assertIsArray($data);
1574+
1575+
$actual = array_group_by($data, ['group']);
1576+
1577+
$this->assertSame(
1578+
[
1579+
'Mammals' => [$data[0], $data[1]],
1580+
'Birds' => [$data[2]],
1581+
],
1582+
$actual,
1583+
);
1584+
$this->assertInstanceOf(stdClass::class, $actual['Mammals'][0]);
1585+
}
1586+
1587+
public function testArrayGroupByWithNestedObjectRows(): void
1588+
{
1589+
$data = [
1590+
(object) [
1591+
'id' => 1,
1592+
'hr' => (object) [
1593+
'department' => 'Engineering',
1594+
],
1595+
],
1596+
(object) [
1597+
'id' => 2,
1598+
'hr' => (object) [
1599+
'department' => 'Marketing',
1600+
],
1601+
],
1602+
(object) [
1603+
'id' => 3,
1604+
'hr' => (object) [
1605+
'department' => 'Engineering',
1606+
],
1607+
],
1608+
];
1609+
1610+
$actual = array_group_by($data, ['hr.department']);
1611+
1612+
$this->assertSame(
1613+
[
1614+
'Engineering' => [$data[0], $data[2]],
1615+
'Marketing' => [$data[1]],
1616+
],
1617+
$actual,
1618+
);
1619+
}
15041620
}

‎user_guide_src/source/changelogs/v4.8.0.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,9 @@ Helpers and Functions
255255
- :doc:`Array Helper </helpers/array_helper>` gained five new dot-path functions:
256256
:php:func:`dot_array_has()`, :php:func:`dot_array_set()`, :php:func:`dot_array_unset()`,
257257
:php:func:`dot_array_only()`, and :php:func:`dot_array_except()`.
258+
- :doc:`Array Helper </helpers/array_helper>` dot-path read operations now support object properties
259+
in :php:func:`dot_array_search()`, :php:func:`dot_array_has()`, :php:func:`dot_array_only()`,
260+
and :php:func:`array_group_by()`.
258261

259262
HTTP
260263
====

0 commit comments

Comments
 (0)