Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd037f93

Browse files
[JsonPath] Fix support for comma separated indices
1 parentbb8505b commitd037f93

File tree

2 files changed

+119
-1
lines changed

2 files changed

+119
-1
lines changed

‎src/Symfony/Component/JsonPath/JsonCrawler.php‎

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,56 @@ private function evaluateBracket(string $expr, mixed $value): array
228228
return$this->evaluateFilter($innerExpr,$value);
229229
}
230230

231-
// quoted strings for object keys
231+
// comma-separated values, e.g. `['key1', 'key2', 123]` or `[0, 1, 'key']`
232+
if (str_contains($expr,',')) {
233+
$parts =$this->parseCommaSeparatedValues($expr);
234+
235+
$result = [];
236+
$keysIndices =array_keys($value);
237+
$isList =array_is_list($value);
238+
239+
foreach ($partsas$part) {
240+
$part =trim($part);
241+
242+
if (preg_match('/^([\'"])(.*)\1$/',$part,$matches)) {
243+
$key =stripslashes($matches[2]);
244+
245+
if ($isList) {
246+
foreach ($valueas$item) {
247+
if (\is_array($item) &&array_key_exists($key,$item)) {
248+
$result[] =$item;
249+
break;
250+
}
251+
}
252+
253+
continue;// no results here
254+
}
255+
256+
if (array_key_exists($key,$value)) {
257+
$result[] =$value[$key];
258+
}
259+
}elseif (preg_match('/^-?\d+$/',$part)) {
260+
// numeric index
261+
$index = (int)$part;
262+
if ($index <0) {
263+
$index =count($value) +$index;
264+
}
265+
266+
if ($isList &&array_key_exists($index,$value)) {
267+
$result[] =$value[$index];
268+
continue;
269+
}
270+
271+
// numeric index on a hashmap
272+
if (isset($keysIndices[$index]) &&isset($value[$keysIndices[$index]])) {
273+
$result[] =$value[$keysIndices[$index]];
274+
}
275+
}
276+
}
277+
278+
return$result;
279+
}
280+
232281
if (preg_match('/^([\'"])(.*)\1$/',$expr,$matches)) {
233282
$key =stripslashes($matches[2]);
234283

@@ -415,4 +464,44 @@ private function compare(mixed $left, mixed $right, string $operator): bool
415464
default =>false,
416465
};
417466
}
467+
468+
privatefunctionparseCommaSeparatedValues(string$expr):array
469+
{
470+
$parts = [];
471+
$current ='';
472+
$inQuotes =false;
473+
$quoteChar =null;
474+
475+
for ($i =0;$i <strlen($expr);$i++) {
476+
$char =$expr[$i];
477+
478+
if ($char ==='\\' &&$i +1 <strlen($expr)) {
479+
$current .=$char .$expr[++$i];
480+
continue;
481+
}
482+
483+
if ('"' ===$char ||"'" ===$char) {
484+
if (!$inQuotes) {
485+
$inQuotes =true;
486+
$quoteChar =$char;
487+
}elseif ($char ===$quoteChar) {
488+
$inQuotes =false;
489+
$quoteChar =null;
490+
}
491+
}elseif (!$inQuotes &&$char ===',') {
492+
$parts[] =trim($current);
493+
$current ='';
494+
495+
continue;
496+
}
497+
498+
$current .=$char;
499+
}
500+
501+
if ('' !==$current) {
502+
$parts[] =trim($current);
503+
}
504+
505+
return$parts;
506+
}
418507
}

‎src/Symfony/Component/JsonPath/Tests/JsonCrawlerTest.php‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,35 @@ public function testEscapedDoubleQuotesInFieldName()
9191
$this->assertSame(42,$result[0]);
9292
}
9393

94+
publicfunctiontestMultipleKeysAtOnce()
95+
{
96+
$crawler =newJsonCrawler(<<<JSON
97+
{"a": {"b\\"c": 42}, "b": {"c": 43}}
98+
JSON);
99+
100+
$result =$crawler->find("$['a', 'b', 3]");
101+
102+
$this->assertSame([
103+
['b"c' =>42],
104+
['c' =>43],
105+
],$result);
106+
}
107+
108+
publicfunctiontestMultipleKeysAtOnceOnArray()
109+
{
110+
$crawler =newJsonCrawler(<<<JSON
111+
[{"a": 1}, {"b": 2}, {"c": 3}, {"a,b,c": 5}, {"d": 4}]
112+
JSON);
113+
114+
$result =$crawler->find("$[0, 2, 'a,b,c', -1]");
115+
116+
$this->assertCount(4,$result);
117+
$this->assertSame(['a' =>1],$result[0]);
118+
$this->assertSame(['c' =>3],$result[1]);
119+
$this->assertSame(['a,b,c' =>5],$result[2]);
120+
$this->assertSame(['d' =>4],$result[3]);
121+
}
122+
94123
publicfunctiontestBasicNameSelector()
95124
{
96125
$result =self::getBookstoreCrawler()->find('$.store.book')[0];

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp