Skip to content

Commit cb38b96

Browse files
committed
key union selector, fix #22
1 parent b8b166e commit cb38b96

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

src/Filters/IndexFilter.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ class IndexFilter extends AbstractFilter
2020
*/
2121
public function filter($collection): array
2222
{
23+
if (is_array($this->token->value)) {
24+
$result = [];
25+
foreach ($this->token->value as $value) {
26+
if (AccessHelper::keyExists($collection, $value, $this->magicIsAllowed)) {
27+
$result[] = AccessHelper::getValue($collection, $value, $this->magicIsAllowed);
28+
}
29+
}
30+
return $result;
31+
}
32+
2333
if (AccessHelper::keyExists($collection, $this->token->value, $this->magicIsAllowed)) {
2434
return [
2535
AccessHelper::getValue($collection, $this->token->value, $this->magicIsAllowed),

src/JSONPathLexer.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,16 @@ protected function createToken(string $value): JSONPathToken
213213
if (isset($matches[1])) {
214214
$tokenValue = $matches[1];
215215
$tokenValue = trim($tokenValue);
216+
217+
$possibleArray = false;
218+
if ($matches[0][0] === '"') {
219+
$possibleArray = explode('","', $tokenValue);
220+
} elseif ($matches[0][0] === "'") {
221+
$possibleArray = explode("','", $tokenValue);
222+
}
223+
if ($possibleArray !== false && count($possibleArray) > 1) {
224+
$tokenValue = $possibleArray;
225+
}
216226
} else {
217227
$tokenValue = '';
218228
}

tests/JSONPathTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,23 @@ public function testSlashesInIndex(): void
678678
self::assertEquals(["/core/img/filetypes/image.png"], $result->getData());
679679
}
680680

681+
public function testUnionWithKeys(): void
682+
{
683+
$result = (new JSONPath(
684+
json_decode(
685+
json_encode(
686+
[
687+
"key" => "value",
688+
"another" => "entry",
689+
]
690+
),
691+
false
692+
)
693+
))->find("$['key','another']");
694+
695+
self::assertEquals(["value","entry"], $result->getData());
696+
}
697+
681698
/**
682699
* @throws Exception
683700
*/

0 commit comments

Comments
 (0)