Skip to content

Commit 6b7e02d

Browse files
committed
use UriInterface instead strings
1 parent aab39dc commit 6b7e02d

27 files changed

+113
-77
lines changed

src/Adapters/Archive/Api.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Api
1111

1212
protected function fetchData(): array
1313
{
14-
$this->endpoint = (string) $this->extractor->getUri()->withQuery('output=json');
14+
$this->endpoint = $this->extractor->getUri()->withQuery('output=json');
1515

1616
return $this->fetchJSON($this->endpoint);
1717
}

src/Adapters/Gist/Api.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Api
1212
protected function fetchData(): array
1313
{
1414
$uri = $this->extractor->getUri();
15-
$this->endpoint = (string) $uri->withPath($uri->getPath().'.json');
15+
$this->endpoint = $uri->withPath($uri->getPath().'.json');
1616

1717
return $this->fetchJSON($this->endpoint);
1818
}

src/Adapters/Gist/Detectors/AuthorUrl.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@
44
namespace Embed\Adapters\Gist\Detectors;
55

66
use Embed\Detectors\AuthorUrl as Detector;
7+
use Psr\Http\Message\UriInterface;
78

89
class AuthorUrl extends Detector
910
{
10-
public function detect(): ?string
11+
public function detect(): ?UriInterface
1112
{
1213
$api = $this->extractor->getApi();
1314
$owner = $api->str('owner');
1415

1516
if ($owner) {
16-
return "https://github.com/{$owner}";
17+
return $this->extractor->getCrawler()->createUri("https://github.com/{$owner}");
1718
}
1819

1920
return parent::detect();

src/Adapters/ImageShack/Api.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ protected function fetchData(): array
2929
return [];
3030
}
3131

32-
$this->endpoint = "https://api.imageshack.com/v2/images/{$id}";
32+
$this->endpoint = $this->extractor->getCrawler()->createUri("https://api.imageshack.com/v2/images/{$id}");
3333
$data = $this->fetchJSON($this->endpoint);
3434
return $data['result'] ?? [];
3535
}

src/Adapters/ImageShack/Detectors/AuthorUrl.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@
44
namespace Embed\Adapters\ImageShack\Detectors;
55

66
use Embed\Detectors\AuthorUrl as Detector;
7+
use Psr\Http\Message\UriInterface;
78

89
class AuthorUrl extends Detector
910
{
10-
public function detect(): ?string
11+
public function detect(): ?UriInterface
1112
{
1213
$api = $this->extractor->getApi();
1314
$owner = $api->str('owner', 'username');
1415

1516
if ($owner) {
16-
return "https://imageshack.com/{$owner}";
17+
return $this->extractor->getCrawler()->createUri("https://imageshack.com/{$owner}");
1718
}
1819

1920
return parent::detect();

src/Adapters/ImageShack/Detectors/Image.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
namespace Embed\Adapters\ImageShack\Detectors;
55

66
use Embed\Detectors\Image as Detector;
7+
use Psr\Http\Message\UriInterface;
78

89
class Image extends Detector
910
{
10-
public function detect(): ?string
11+
public function detect(): ?UriInterface
1112
{
1213
$api = $this->extractor->getApi();
1314

src/Adapters/Wikipedia/Api.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ protected function fetchData(): array
2121

2222
$titles = getDirectory($uri->getPath(), 1);
2323

24-
$this->endpoint = (string) $uri
24+
$this->endpoint = $uri
2525
->withPath('/w/api.php')
2626
->withQuery(http_build_query([
2727
'action' => 'query',

src/Adapters/Youtube/Detectors/Feeds.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
use Embed\Detectors\Feeds as Detector;
77
use function Embed\getDirectory;
88
use function Embed\match;
9+
use Psr\Http\Message\UriInterface;
910

1011
class Feeds extends Detector
1112
{
13+
/**
14+
* @return UriInterface[]
15+
*/
1216
public function detect(): array
1317
{
1418
return parent::detect()
@@ -24,7 +28,8 @@ private function fallback(): array
2428
}
2529

2630
$id = getDirectory($uri->getPath(), 1);
31+
$feed = $this->extractor->getCrawler()->createUri("https://www.youtube.com/feeds/videos.xml?channel_id={$id}");
2732

28-
return ["https://www.youtube.com/feeds/videos.xml?channel_id={$id}"];
33+
return [$feed];
2934
}
3035
}

src/ApiTrait.php

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,18 @@
33

44
namespace Embed;
55

6-
use Exception;
6+
use Psr\Http\Message\UriInterface;
77

88
trait ApiTrait
99
{
1010
private Extractor $extractor;
1111
private array $data;
12-
private ?string $endpoint;
1312

1413
public function __construct(Extractor $extractor)
1514
{
1615
$this->extractor = $extractor;
1716
}
1817

19-
public function getEndpoint(): ?string
20-
{
21-
return $this->endpoint;
22-
}
23-
2418
public function all(): array
2519
{
2620
if (!isset($this->data)) {
@@ -78,31 +72,16 @@ public function int(string ...$keys): ?int
7872
return $value ? (int) $value : null;
7973
}
8074

81-
public function url(string ...$keys): ?string
75+
public function url(string ...$keys): ?UriInterface
8276
{
8377
$url = $this->str(...$keys);
8478

8579
if (!$url) {
8680
return null;
8781
}
8882

89-
$uri = $this->extractor->getCrawler()->createUri($url);
90-
91-
return (string) resolveUri($this->extractor->getUri(), $uri);
83+
return $this->extractor->resolveUri($url);
9284
}
9385

9486
abstract protected function fetchData(): array;
95-
96-
private function fetchJSON(string $url): array
97-
{
98-
$crawler = $this->extractor->getCrawler();
99-
$request = $crawler->createRequest('GET', $url);
100-
$response = $crawler->sendRequest($request);
101-
102-
try {
103-
return json_decode((string) $response->getBody(), true) ?: [];
104-
} catch (Exception $exception) {
105-
return [];
106-
}
107-
}
10887
}

src/Detectors/AuthorUrl.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,24 @@
33

44
namespace Embed\Detectors;
55

6+
use Psr\Http\Message\UriInterface;
7+
68
class AuthorUrl extends Detector
79
{
8-
public function detect(): ?string
10+
public function detect(): ?UriInterface
911
{
1012
$oembed = $this->extractor->getOEmbed();
1113

1214
return $oembed->url('author_url')
1315
?: $this->detectFromTwitter();
1416
}
1517

16-
private function detectFromTwitter(): ?string
18+
private function detectFromTwitter(): ?UriINterface
1719
{
1820
$user = $this->extractor->getDocument()->meta('twitter:creator');
1921

20-
return $user ? sprintf('https://twitter.com/%s', ltrim($user, '@')) : null;
22+
return $user
23+
? $this->extractor->getCrawler()->createUri(sprintf('https://twitter.com/%s', ltrim($user, '@')))
24+
: null;
2125
}
2226
}

0 commit comments

Comments
 (0)