Skip to content

Commit 2b8887f

Browse files
committed
return Datetime instances instead strings, create the Metas API
1 parent 6b7e02d commit 2b8887f

30 files changed

+175
-105
lines changed

demo/index.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,15 @@ function printCode(?string $code, bool $asHtml = true): void
233233
</tr>
234234
</table>
235235

236+
<h2>Meta data</h2>
237+
238+
<table>
239+
<tr>
240+
<th>All data collected</th>
241+
<td><?php printArray($info->getMetas()->all()); ?></td>
242+
</tr>
243+
</table>
244+
236245
<h2>Linked data</h2>
237246

238247
<table>

src/Adapters/Archive/Detectors/PublishedTime.php

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

44
namespace Embed\Adapters\Archive\Detectors;
55

6+
use Datetime;
67
use Embed\Detectors\PublishedTime as Detector;
78

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

14-
return $api->str('metadata', 'publicdate')
15-
?: $api->str('metadata', 'addeddate')
16-
?: $api->str('metadata', 'date')
15+
return $api->time('metadata', 'publicdate')
16+
?: $api->time('metadata', 'addeddate')
17+
?: $api->time('metadata', 'date')
1718
?: parent::detect();
1819
}
1920
}

src/Adapters/Gist/Detectors/PublishedTime.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33

44
namespace Embed\Adapters\Gist\Detectors;
55

6+
use Datetime;
67
use Embed\Detectors\PublishedTime as Detector;
78

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

14-
return $api->str('created_at')
15+
return $api->time('created_at')
1516
?: parent::detect();
1617
}
1718
}

src/Adapters/ImageShack/Detectors/PublishedTime.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33

44
namespace Embed\Adapters\ImageShack\Detectors;
55

6+
use Datetime;
67
use Embed\Detectors\PublishedTime as Detector;
78

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

14-
return $api->str('creation_date')
15+
return $api->time('creation_date')
1516
?: parent::detect();
1617
}
1718
}

src/ApiTrait.php

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

44
namespace Embed;
55

6+
use Datetime;
67
use Psr\Http\Message\UriInterface;
78

89
trait ApiTrait
@@ -50,6 +51,12 @@ public function str(string ...$keys): ?string
5051
return $value ? clean((string) $value) : null;
5152
}
5253

54+
public function strAll(string ...$keys): array
55+
{
56+
$all = (array) $this->get(...$keys);
57+
return array_filter(array_map(fn ($value) => clean($value), $all));
58+
}
59+
5360
public function html(string ...$keys): ?string
5461
{
5562
$value = $this->get(...$keys);
@@ -76,11 +83,19 @@ public function url(string ...$keys): ?UriInterface
7683
{
7784
$url = $this->str(...$keys);
7885

79-
if (!$url) {
80-
return null;
86+
return $url ? $this->extractor->resolveUri($url) : null;
87+
}
88+
89+
public function time(string ...$keys): ?Datetime
90+
{
91+
$time = $this->str(...$keys);
92+
$datetime = $time ? date_create($time) : null;
93+
94+
if (!$datetime && ctype_digit($time)) {
95+
$datetime = date_create_from_format('U', $time);
8196
}
8297

83-
return $this->extractor->resolveUri($url);
98+
return ($datetime && $datetime->getTimestamp() > 0) ? $datetime : null;
8499
}
85100

86101
abstract protected function fetchData(): array;

src/Detectors/AuthorName.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ class AuthorName extends Detector
88
public function detect(): ?string
99
{
1010
$oembed = $this->extractor->getOEmbed();
11-
$document = $this->extractor->getDocument();
11+
$metas = $this->extractor->getMetas();
1212

1313
return $oembed->str('author_name')
14-
?: $document->meta(
14+
?: $metas->str(
1515
'article:author',
1616
'book:author',
1717
'sailthru.author',

src/Detectors/AuthorUrl.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ public function detect(): ?UriInterface
1717

1818
private function detectFromTwitter(): ?UriINterface
1919
{
20-
$user = $this->extractor->getDocument()->meta('twitter:creator');
20+
$metas = $this->extractor->getMetas();
21+
$crawler = $this->extractor->getCrawler();
22+
23+
$user = $metas->str('twitter:creator');
2124

2225
return $user
23-
? $this->extractor->getCrawler()->createUri(sprintf('https://twitter.com/%s', ltrim($user, '@')))
26+
? $crawler->createUri(sprintf('https://twitter.com/%s', ltrim($user, '@')))
2427
: null;
2528
}
2629
}

src/Detectors/Code.php

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,20 @@ private function detectFromEmbed(): ?EmbedCode
3333

3434
private function detectFromOpenGraph(): ?EmbedCode
3535
{
36-
$document = $this->extractor->getDocument();
36+
$metas = $this->extractor->getMetas();
3737

38-
$url = $document->meta('og:video:secure_url', 'og:video:url', 'og:video');
38+
$url = $metas->url('og:video:secure_url', 'og:video:url', 'og:video');
3939

4040
if (!$url) {
4141
return null;
4242
}
4343

44-
if (!($path = parse_url($url, PHP_URL_PATH)) || !($type = pathinfo($path, PATHINFO_EXTENSION))) {
45-
$type = $document->meta('og:video_type');
44+
if (!($type = pathinfo($url->getPath(), PATHINFO_EXTENSION))) {
45+
$type = $metas->str('og:video_type');
4646
}
4747

48-
$width = $document->meta('twitter:player:width');
49-
$height = $document->meta('twitter:player:height');
48+
$width = $metas->int('twitter:player:width');
49+
$height = $metas->int('twitter:player:height');
5050

5151
switch ($type) {
5252
case 'swf':
@@ -77,25 +77,21 @@ private function detectFromOpenGraph(): ?EmbedCode
7777
]);
7878
}
7979

80-
return new EmbedCode(
81-
$code,
82-
$width ? (int) $width : null,
83-
$height ? (int) $height : null
84-
);
80+
return new EmbedCode($code, $width, $height);
8581
}
8682

8783
private function detectFromTwitter(): ?EmbedCode
8884
{
89-
$document = $this->extractor->getDocument();
85+
$metas = $this->extractor->getMetas();
9086

91-
$url = $document->meta('twitter:player');
87+
$url = $metas->url('twitter:player');
9288

9389
if (!$url) {
9490
return null;
9591
}
9692

97-
$width = $document->meta('twitter:player:width');
98-
$height = $document->meta('twitter:player:height');
93+
$width = $metas->int('twitter:player:width');
94+
$height = $metas->int('twitter:player:height');
9995

10096
$code = html('iframe', [
10197
'src' => $url,
@@ -105,10 +101,6 @@ private function detectFromTwitter(): ?EmbedCode
105101
'allowTransparency' => 'true',
106102
]);
107103

108-
return new EmbedCode(
109-
$code,
110-
$width ? (int) $width : null,
111-
$height ? (int) $height : null
112-
);
104+
return new EmbedCode($code, $width, $height);
113105
}
114106
}

src/Detectors/Description.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ class Description extends Detector
88
public function detect(): ?string
99
{
1010
$oembed = $this->extractor->getOEmbed();
11-
$document = $this->extractor->getDocument();
11+
$metas = $this->extractor->getMetas();
1212
$ld = $this->extractor->getLinkedData();
1313

1414
return $oembed->str('description')
15-
?: $document->meta('og:description', 'twitter:description', 'lp:description', 'description')
15+
?: $metas->str('og:description', 'twitter:description', 'lp:description', 'description')
1616
?: $ld->str('description');
1717
}
1818
}

src/Detectors/Image.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ public function detect(): ?UriInterface
1111
{
1212
$oembed = $this->extractor->getOEmbed();
1313
$document = $this->extractor->getDocument();
14+
$metas = $this->extractor->getMetas();
1415
$ld = $this->extractor->getLinkedData();
1516

1617
return $oembed->url('image')
1718
?: $oembed->url('thumbnail')
1819
?: $oembed->url('thumbnail_url')
19-
?: $document->metaUrl('og:image', 'og:image:url', 'og:image:secure_url', 'twitter:image', 'twitter:image:src', 'lp:image')
20+
?: $metas->url('og:image', 'og:image:url', 'og:image:secure_url', 'twitter:image', 'twitter:image:src', 'lp:image')
2021
?: $document->link('image_src')
2122
?: $ld->url('image', 'url');
2223
}

0 commit comments

Comments
 (0)