Skip to content

Commit 6e4e714

Browse files
[JsonPath] Make JsonCrawler stateless
1 parent c6baf1b commit 6e4e714

File tree

3 files changed

+89
-91
lines changed

3 files changed

+89
-91
lines changed

src/Symfony/Component/JsonPath/JsonCrawler.php

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,36 +38,29 @@ final class JsonCrawler implements JsonCrawlerInterface
3838
'value' => true,
3939
];
4040

41-
/**
42-
* @param resource|string $raw
43-
*/
44-
public function __construct(
45-
private readonly mixed $raw,
46-
) {
47-
if (!\is_string($raw) && !\is_resource($raw)) {
48-
throw new InvalidArgumentException(\sprintf('Expected string or resource, got "%s".', get_debug_type($raw)));
41+
public function find(string|JsonPath $query, mixed $data): array
42+
{
43+
if (!\is_string($data) && !\is_resource($data)) {
44+
throw new InvalidArgumentException(\sprintf('Expected string or resource, got "%s".', get_debug_type($data)));
4945
}
50-
}
5146

52-
public function find(string|JsonPath $query): array
53-
{
54-
return $this->evaluate(\is_string($query) ? new JsonPath($query) : $query);
47+
return $this->evaluate(\is_string($query) ? new JsonPath($query) : $query, $data);
5548
}
5649

57-
private function evaluate(JsonPath $query): array
50+
private function evaluate(JsonPath $query, mixed $data): array
5851
{
5952
try {
6053
$tokens = JsonPathTokenizer::tokenize($query);
61-
$json = $this->raw;
54+
$json = $data;
6255

63-
if (\is_resource($this->raw)) {
56+
if (\is_resource($data)) {
6457
if (!class_exists(Splitter::class)) {
6558
throw new \LogicException('The JsonStreamer package is required to evaluate a path against a resource. Try running "composer require symfony/json-streamer".');
6659
}
6760

6861
$simplified = JsonPathUtils::findSmallestDeserializableStringAndPath(
6962
$tokens,
70-
$this->raw,
63+
$data,
7164
);
7265

7366
$tokens = $simplified['tokens'];

src/Symfony/Component/JsonPath/JsonCrawlerInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
interface JsonCrawlerInterface
2323
{
2424
/**
25+
* @param string|resource $data The JSON string or a stream resource containing the JSON string
26+
*
2527
* @return list<array|string|float|int|bool|null>
2628
*
2729
* @throws InvalidArgumentException When the JSON string provided to the crawler cannot be decoded
2830
* @throws JsonCrawlerException When a syntax error occurs in the provided JSON path
2931
*/
30-
public function find(string|JsonPath $query): array;
32+
public function find(string|JsonPath $query, mixed $data): array;
3133
}

0 commit comments

Comments
 (0)