Skip to content

Commit a9dee48

Browse files
committed
Simplify toHtml
1 parent 5f4c524 commit a9dee48

1 file changed

Lines changed: 14 additions & 50 deletions

File tree

src/OpenGraph/OpenGraph.php

Lines changed: 14 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -232,56 +232,20 @@ public function getAudios(): array
232232
*/
233233
public function toHtml(): string
234234
{
235-
$html = [];
236-
237-
if (null !== $this->title) {
238-
$html[] = self::renderMetaTag('og:title', $this->title);
239-
}
240-
241-
$html[] = self::renderMetaTag('og:type', $this->type->getType());
242-
243-
if (null !== $this->url) {
244-
$html[] = self::renderMetaTag('og:url', $this->url);
245-
}
246-
247-
if (null !== $this->description) {
248-
$html[] = self::renderMetaTag('og:description', $this->description);
249-
}
250-
251-
if (null !== $this->determiner) {
252-
$html[] = self::renderMetaTag('og:determiner', $this->determiner);
253-
}
254-
255-
if (null !== $this->locale) {
256-
$html[] = self::renderMetaTag('og:locale', $this->locale);
257-
}
258-
259-
foreach ($this->localeAlternates as $localeAlternate) {
260-
$html[] = self::renderMetaTag('og:locale:alternate', $localeAlternate);
261-
}
262-
263-
if (null !== $this->siteName) {
264-
$html[] = self::renderMetaTag('og:site_name', $this->siteName);
265-
}
266-
267-
foreach ($this->images as $image) {
268-
$html[] = $image->toHtml();
269-
}
270-
271-
foreach ($this->videos as $video) {
272-
$html[] = $video->toHtml();
273-
}
274-
275-
foreach ($this->audios as $audio) {
276-
$html[] = $audio->toHtml();
277-
}
278-
279-
$typeHtml = $this->type->toHtml();
280-
if ('' !== $typeHtml) {
281-
$html[] = $typeHtml;
282-
}
283-
284-
return implode("\n", $html);
235+
return implode("\n", array_filter([
236+
self::renderMetaTag('og:title', $this->title),
237+
self::renderMetaTag('og:type', $this->type->getType()),
238+
self::renderMetaTag('og:url', $this->url),
239+
self::renderMetaTag('og:description', $this->description),
240+
self::renderMetaTag('og:determiner', $this->determiner),
241+
self::renderMetaTag('og:locale', $this->locale),
242+
...array_map(static fn (string $locale): ?string => self::renderMetaTag('og:locale:alternate', $locale), $this->localeAlternates),
243+
self::renderMetaTag('og:site_name', $this->siteName),
244+
...array_map(static fn (Image $image): string => $image->toHtml(), $this->images),
245+
...array_map(static fn (Video $video): string => $video->toHtml(), $this->videos),
246+
...array_map(static fn (Audio $audio): string => $audio->toHtml(), $this->audios),
247+
$this->type->toHtml(),
248+
], static fn (?string $value): bool => null !== $value && '' !== $value));
285249
}
286250

287251
public static function renderMetaTag(string $property, bool|float|int|string|null $content): ?string

0 commit comments

Comments
 (0)