Skip to content

Commit 7a96171

Browse files
committed
Changed: getValuesByWildcar function to support matching a dimension for anything.
1 parent 0d1333b commit 7a96171

1 file changed

Lines changed: 68 additions & 34 deletions

File tree

src/Request.php

Lines changed: 68 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -239,54 +239,88 @@ public function applyMutation($mutations, $value)
239239
return $value;
240240
}
241241

242-
/**
242+
/**
243243
* Given an array, get all parameters which match a certain wildcard.
244244
*
245245
* @param array $data
246-
* @param string $parameter
246+
* @param string $pattern
247247
* @return array
248248
*/
249-
public function getValuesByWildcard($data, $parameter)
250-
{
249+
public function getValuesByWildcard($data, $pattern) {
251250
if (!is_array($data)) {
252251
return [];
253252
}
254253

255-
// First we want to get the furthest possible down.
256-
$parameters = explode('.', $parameter);
257-
array_shift($parameters);
258-
$end = $data;
259-
$wildcard = '';
260-
foreach ( $parameters as $var ) {
261-
262-
// We hit the wildcard.
263-
if (strpos($var, '*') !== false) {
264-
$wildcard = str_replace('*', '', $var);
265-
break;
266-
}
267-
268-
// We're not at the end and there's no wildcard.
269-
if (!isset( $end[ $var ] ) && strpos($var, '*') === false) {
270-
return [];
254+
// Split the pattern into segments
255+
$segments = explode('.', $pattern);
256+
array_shift($segments);
257+
258+
// Initialize the results
259+
$results = [];
260+
261+
// Start with the entire data array
262+
$currentData = [$data];
263+
264+
// Check if the pattern ends with an asterisk and pop the last segment
265+
$endWithAsterisk = substr(end($segments), -1) === "*";
266+
if ($endWithAsterisk) {
267+
$lastSegment = rtrim(array_pop($segments), '*');
268+
}
269+
270+
// Loop through each segment
271+
foreach ($segments as $segment) {
272+
$newData = [];
273+
274+
// Check if the segment contains an asterisk
275+
if ($segment === '*') {
276+
foreach ($currentData as $dataItem) {
277+
if (is_array($dataItem)) {
278+
foreach ($dataItem as $subItem) {
279+
$newData[] = $subItem;
280+
}
281+
}
282+
}
283+
} else {
284+
// Loop through each data item
285+
foreach ($currentData as $dataItem) {
286+
// Ensure the data item is an array and contains the segment
287+
if (is_array($dataItem) && isset($dataItem[$segment])) {
288+
$newData[] = $dataItem[$segment];
289+
}
290+
}
271291
}
272-
273-
$end = $end[ $var ];
292+
293+
// Replace the current data with the new data
294+
$currentData = $newData;
274295
}
275-
276-
// No need to continue if there is nothing to match.
277-
if (!is_array($end) || count($end) == 0) {
278-
return [];
296+
297+
if ($endWithAsterisk) {
298+
$finalData = [];
299+
foreach ($currentData as $dataItem) {
300+
if (is_array($dataItem)) {
301+
foreach ($dataItem as $key => $subItem) {
302+
if (strpos($key, $lastSegment) === 0) {
303+
$finalData[] = $subItem;
304+
}
305+
}
306+
}
307+
}
308+
$currentData = $finalData;
279309
}
280-
281-
// Based on the data that is left, find the wildcard matches.
282-
$return = [];
283-
foreach ($end as $key => $value) {
284-
if (strpos($key, $wildcard) !== false) {
285-
$return[] = $value;
310+
311+
// Loop through the current data to fetch the results
312+
foreach ($currentData as $dataItem) {
313+
if (is_array($dataItem)) {
314+
foreach ($dataItem as $value) {
315+
$results[] = $value;
316+
}
317+
} else {
318+
$results[] = $dataItem;
286319
}
287320
}
288-
289-
return $return;
321+
322+
// Return the results
323+
return $results;
290324
}
291325

292326
/**

0 commit comments

Comments
 (0)