Skip to content

Commit dabee70

Browse files
[JsonPath] Introduce JsonCrawlerFactory
1 parent 160529b commit dabee70

File tree

6 files changed

+87
-4
lines changed

6 files changed

+87
-4
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\JsonPath;
13+
14+
/**
15+
* @author Alexandre Daubois <alex.daubois@gmail.com>
16+
*
17+
* @experimental
18+
*/
19+
interface CrawlerFactoryInterface
20+
{
21+
/**
22+
* @param string|resource $data
23+
*/
24+
public function createFromData(mixed $data): CrawlerInterface;
25+
}

src/Symfony/Component/JsonPath/JsonCrawlerInterface.php renamed to src/Symfony/Component/JsonPath/CrawlerInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* @experimental
2121
*/
22-
interface JsonCrawlerInterface
22+
interface CrawlerInterface
2323
{
2424
/**
2525
* @return list<array|string|float|int|bool|null>

src/Symfony/Component/JsonPath/JsonCrawler.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*
2929
* @experimental
3030
*/
31-
final class JsonCrawler implements JsonCrawlerInterface
31+
final class JsonCrawler implements CrawlerInterface
3232
{
3333
private const RFC9535_FUNCTIONS = [
3434
'length' => true,
@@ -40,12 +40,14 @@ final class JsonCrawler implements JsonCrawlerInterface
4040

4141
/**
4242
* @param resource|string $raw
43+
*
44+
* @throws \TypeError When the input is not a string or a resource
4345
*/
4446
public function __construct(
4547
private readonly mixed $raw,
4648
) {
4749
if (!\is_string($raw) && !\is_resource($raw)) {
48-
throw new InvalidArgumentException(\sprintf('Expected string or resource, got "%s".', get_debug_type($raw)));
50+
throw new \TypeError(\sprintf('Expected string or resource, got "%s".', get_debug_type($raw)));
4951
}
5052
}
5153

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\JsonPath;
13+
14+
/**
15+
* @author Alexandre Daubois <alex.daubois@gmail.com>
16+
*
17+
* @experimental
18+
*/
19+
final class JsonCrawlerFactory implements CrawlerFactoryInterface
20+
{
21+
/**
22+
* @throws \TypeError When the data is not a string or a resource
23+
*/
24+
public function createFromData(mixed $data): JsonCrawler
25+
{
26+
return new JsonCrawler($data);
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\JsonPath\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\JsonPath\JsonCrawler;
16+
use Symfony\Component\JsonPath\JsonCrawlerFactory;
17+
18+
class JsonCrawlerFactoryTest extends TestCase
19+
{
20+
public function testCreateCrawler()
21+
{
22+
$factory = new JsonCrawlerFactory();
23+
$crawler = $factory->createFromData('{"foo": "bar"}');
24+
25+
$this->assertInstanceOf(JsonCrawler::class, $crawler);
26+
$this->assertSame(['bar'], $crawler->find('$.foo'), '->createFromData() provides data to the crawler');
27+
}
28+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class JsonCrawlerTest extends TestCase
2222
{
2323
public function testNotStringOrResourceThrows()
2424
{
25-
$this->expectException(InvalidArgumentException::class);
25+
$this->expectException(\TypeError::class);
2626
$this->expectExceptionMessage('Expected string or resource, got "int".');
2727

2828
new JsonCrawler(42);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy