Skip to content

Commit 9115387

Browse files
committed
refactor: aggregate array helper functions in their own file and namespace
1 parent 0b3d3e8 commit 9115387

File tree

6 files changed

+104
-100
lines changed

6 files changed

+104
-100
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"src/utils/is-equal.php",
3939
"src/utils/traverse-descendant.php",
4040
"src/utils/helpers.php",
41+
"src/utils/array.php",
4142
"src/utils/convert-i-regexp-to-php-regexp.php",
4243
"src/functions/function-types.php",
4344
"src/functions/search.php",

src/parsers/filter-selector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function apply_filter_selector($selector, $root_node, $json)
1313
return [];
1414
}
1515

16-
return array_filter_2(to_array($json), function ($item) use ($selector, $root_node) {
16+
return Array\filter(to_array($json), function ($item) use ($selector, $root_node) {
1717
return apply_filter_expression($selector->expr, $root_node, $item);
1818
});
1919
}

src/parsers/root.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function apply_segments(array $segments, $root_node, $node_list)
2020
$result = array_reduce(
2121
$segments,
2222
function ($result_node_list, $current_segment) use ($root_node) {
23-
return array_flat_map(
23+
return Array\flat_map(
2424
fn($node) => apply_segment($current_segment, $root_node, $node),
2525
$result_node_list,
2626
);
@@ -41,16 +41,16 @@ function apply_segment($segment, $root_node, $json)
4141
$selector_results = array_map(function ($selector) use ($root_node, $json) {
4242
return apply_selector($selector, $root_node, $json);
4343
}, $segment);
44-
$segment_result = array_flatten($selector_results);
44+
$segment_result = Array\flatten($selector_results);
4545

4646
return $segment_result;
4747
}
4848

4949
// DescendantSegment
5050
$descendant_nodes = traverse_descendant($json);
5151

52-
return array_flat_map(
53-
fn($node) => array_flat_map(
52+
return Array\flat_map(
53+
fn($node) => Array\flat_map(
5454
fn($selector) => apply_selector($selector, $root_node, $node),
5555
$segment->selectors,
5656
),
@@ -117,7 +117,7 @@ function apply_index_selector($node, $json)
117117
return [];
118118
}
119119

120-
$index = normalize_index($node->index, sizeof($json));
120+
$index = Array\normalize_index($node->index, sizeof($json));
121121

122122
return $index === null ? [] : [$json[$index]];
123123
}

src/utils.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function json_get($value, $key)
2323
*/
2424
function is_json_object($value): bool
2525
{
26-
return (is_array($value) && !array_is_list_2($value)) || $value instanceof \stdClass;
26+
return (is_array($value) && !Array\is_list($value)) || $value instanceof \stdClass;
2727
}
2828

2929
/**
@@ -39,7 +39,7 @@ function is_json_primitive($value): bool
3939
*/
4040
function is_json_array($value): bool
4141
{
42-
return is_array($value) && array_is_list_2($value);
42+
return is_array($value) && Array\is_list($value);
4343
}
4444

4545
/**

src/utils/array.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace Loilo\JsonPath\Array;
4+
5+
/**
6+
* Flatten a multi-dimensional array by a given number of levels
7+
*/
8+
function flatten(array $array, float $levels = 1): array
9+
{
10+
if ($levels <= 0) {
11+
return $array;
12+
}
13+
14+
$result = [];
15+
foreach ($array as $key => $value) {
16+
if (is_array($value)) {
17+
$result = array_merge($result, flatten($value, $levels - 1));
18+
} else {
19+
$result = array_merge($result, [$key => $value]);
20+
}
21+
}
22+
23+
return $result;
24+
}
25+
26+
/**
27+
* Map a callback over the elements of one or more arrays and flatten the result
28+
*/
29+
function flat_map(callable $callback, ...$arrays): array
30+
{
31+
if (sizeof($arrays) === 0) {
32+
return [];
33+
}
34+
35+
$new_array = [];
36+
$maxlength = max(array_map('sizeof', $arrays));
37+
for ($i = 0; $i < $maxlength; $i++) {
38+
$args =
39+
sizeof($arrays) === 1
40+
? [$arrays[0][$i]]
41+
: array_map(fn($array) => $array[$i] ?? null, $arrays);
42+
$result = call_user_func_array($callback, $args);
43+
44+
if (is_array($result)) {
45+
if (empty($result)) {
46+
continue;
47+
}
48+
49+
$new_array = array_merge($new_array, $result);
50+
} else {
51+
$new_array[] = $result;
52+
}
53+
}
54+
55+
return $new_array;
56+
}
57+
58+
/**
59+
* Filter an array and re-index it
60+
*/
61+
function filter(array $array, ?callable $callback = null): array
62+
{
63+
return array_values(array_filter($array, $callback));
64+
}
65+
66+
/**
67+
* Check if an array is a list
68+
*/
69+
function is_list(array $array): bool
70+
{
71+
if (function_exists('array_is_list')) {
72+
return \array_is_list($array);
73+
}
74+
75+
return array_keys($array) === range(0, count($array) - 1);
76+
}
77+
78+
/**
79+
* Normalize an index for a given array length to be positive
80+
* or null if it is out of bounds
81+
* An index is out of bounds if it is less than the negative length
82+
* or greater than or equal to the length
83+
*/
84+
function normalize_index($index, int $length): ?int
85+
{
86+
if ($index < -$length || $index >= $length) {
87+
return null;
88+
}
89+
90+
if ($index < 0) {
91+
$index += $length;
92+
}
93+
94+
return $index;
95+
}

src/utils/helpers.php

Lines changed: 0 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -2,102 +2,10 @@
22

33
namespace Loilo\JsonPath;
44

5-
/**
6-
* Map a callback over the elements of one or more arrays and flatten the result
7-
*/
8-
function array_flat_map(callable $callback, ...$arrays): array
9-
{
10-
if (sizeof($arrays) === 0) {
11-
return [];
12-
}
13-
14-
$new_array = [];
15-
$maxlength = max(array_map('sizeof', $arrays));
16-
for ($i = 0; $i < $maxlength; $i++) {
17-
$args =
18-
sizeof($arrays) === 1
19-
? [$arrays[0][$i]]
20-
: array_map(fn($array) => $array[$i] ?? null, $arrays);
21-
$result = call_user_func_array($callback, $args);
22-
23-
if (is_array($result)) {
24-
if (empty($result)) {
25-
continue;
26-
}
27-
28-
$new_array = array_merge($new_array, $result);
29-
} else {
30-
$new_array[] = $result;
31-
}
32-
}
33-
34-
return $new_array;
35-
}
36-
37-
/**
38-
* Flatten a multi-dimensional array by a given number of levels
39-
*/
40-
function array_flatten(array $array, float $levels = 1): array
41-
{
42-
if ($levels <= 0) {
43-
return $array;
44-
}
45-
46-
$result = [];
47-
foreach ($array as $key => $value) {
48-
if (is_array($value)) {
49-
$result = array_merge($result, array_flatten($value, $levels - 1));
50-
} else {
51-
$result = array_merge($result, [$key => $value]);
52-
}
53-
}
54-
55-
return $result;
56-
}
57-
58-
/**
59-
* Filter an array and re-indexes it
60-
*/
61-
function array_filter_2(array $array, ?callable $callback = null): array
62-
{
63-
return array_values(array_filter($array, $callback));
64-
}
65-
66-
/**
67-
* Check if an array is a list
68-
*/
69-
function array_is_list_2(array $array): bool
70-
{
71-
if (function_exists('array_is_list')) {
72-
return \array_is_list($array);
73-
}
74-
75-
return array_keys($array) === range(0, count($array) - 1);
76-
}
77-
785
/**
796
* Check if a value is a number
807
*/
818
function is_number($value): bool
829
{
8310
return is_int($value) || is_float($value);
8411
}
85-
86-
/**
87-
* Normalize an index for a given array length to be positive
88-
* or null if it is out of bounds
89-
* An index is out of bounds if it is less than the negative length
90-
* or greater than or equal to the length
91-
*/
92-
function normalize_index($index, int $length): ?int
93-
{
94-
if ($index < -$length || $index >= $length) {
95-
return null;
96-
}
97-
98-
if ($index < 0) {
99-
$index += $length;
100-
}
101-
102-
return $index;
103-
}

0 commit comments

Comments
 (0)