Skip to content

Commit 843d759

Browse files
authored
Merge pull request #27 from SG5/union-keys
key union selector, fix #22
2 parents 36879d5 + cb38b96 commit 843d759

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
@@ -683,6 +683,23 @@ public function testSlashesInIndex(): void
683683
self::assertEquals(["/core/img/filetypes/image.png"], $result->getData());
684684
}
685685

686+
public function testUnionWithKeys(): void
687+
{
688+
$result = (new JSONPath(
689+
json_decode(
690+
json_encode(
691+
[
692+
"key" => "value",
693+
"another" => "entry",
694+
]
695+
),
696+
false
697+
)
698+
))->find("$['key','another']");
699+
700+
self::assertEquals(["value","entry"], $result->getData());
701+
}
702+
686703
/**
687704
* @throws Exception
688705
*/

0 commit comments

Comments
 (0)