Skip to content

[JsonPath] Add CrawlerFactoryInterface and JsonCrawlerFactory #60624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/JsonPath/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.4
---

* Add `CrawlerFactoryInterface` and `JsonCrawlerFactory`

7.3
---

Expand Down
25 changes: 25 additions & 0 deletions src/Symfony/Component/JsonPath/CrawlerFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\JsonPath;

/**
* @author Alexandre Daubois <alex.daubois@gmail.com>
*
* @experimental
*/
interface CrawlerFactoryInterface
{
/**
* @param string|resource $data
*/
public function createFromData(mixed $data): CrawlerInterface;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* @experimental
*/
interface JsonCrawlerInterface
interface CrawlerInterface
{
/**
* @return list<array|string|float|int|bool|null>
Expand Down
6 changes: 4 additions & 2 deletions src/Symfony/Component/JsonPath/JsonCrawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*
* @experimental
*/
final class JsonCrawler implements JsonCrawlerInterface
final class JsonCrawler implements CrawlerInterface
{
private const RFC9535_FUNCTIONS = [
'length' => true,
Expand All @@ -40,12 +40,14 @@ final class JsonCrawler implements JsonCrawlerInterface

/**
* @param resource|string $raw
*
* @throws \TypeError When the input is not a string or a resource
*/
public function __construct(
private readonly mixed $raw,
) {
if (!\is_string($raw) && !\is_resource($raw)) {
throw new InvalidArgumentException(\sprintf('Expected string or resource, got "%s".', get_debug_type($raw)));
throw new \TypeError(\sprintf('Expected string or resource, got "%s".', get_debug_type($raw)));
}
}

Expand Down
28 changes: 28 additions & 0 deletions src/Symfony/Component/JsonPath/JsonCrawlerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\JsonPath;

/**
* @author Alexandre Daubois <alex.daubois@gmail.com>
*
* @experimental
*/
final class JsonCrawlerFactory implements CrawlerFactoryInterface
{
/**
* @throws \TypeError When the data is not a string or a resource
*/
public function createFromData(mixed $data): JsonCrawler
{
return new JsonCrawler($data);
}
}
26 changes: 26 additions & 0 deletions src/Symfony/Component/JsonPath/Tests/JsonCrawlerFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\JsonPath\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\JsonPath\JsonCrawlerFactory;

class JsonCrawlerFactoryTest extends TestCase
{
public function testCreateCrawler()
{
$factory = new JsonCrawlerFactory();
$crawler = $factory->createFromData('{"foo": "bar"}');

$this->assertSame(['bar'], $crawler->find('$.foo'), '->createFromData() provides data to the crawler');
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/JsonPath/Tests/JsonCrawlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class JsonCrawlerTest extends TestCase
{
public function testNotStringOrResourceThrows()
{
$this->expectException(InvalidArgumentException::class);
$this->expectException(\TypeError::class);
$this->expectExceptionMessage('Expected string or resource, got "int".');

new JsonCrawler(42);
Expand Down