70, 'description' => 200 ]; public ?string $url = null; public ?string $title = null; public ?string $description = null; public ?string $keywords = null; public ?string $image = null; protected array $social = []; public function setSocial(string $name, string $value): void { $this->social[$name] = $value; } public function getHtml(): string { $tags = []; $add_og_twitter = function ($key, $value) use (&$tags) { foreach (['og', 'twitter'] as $social) { if ($social == 'twitter' && isset(self::TWITTER_LIMITS[$key])) { if (mb_strlen($value) > self::TWITTER_LIMITS[$key]) $value = mb_substr($value, 0, self::TWITTER_LIMITS[$key] - 3).'...'; } $tags[] = [ $social == 'twitter' ? 'name' : 'property' => $social.':'.$key, 'content' => $value ]; } }; foreach (['url', 'title', 'image'] as $k) { if ($this->$k !== null) $add_og_twitter($k, $this->$k); } foreach (['description', 'keywords'] as $k) { if ($this->$k !== null) { $add_og_twitter($k, $this->$k); $tags[] = ['name' => $k, 'content' => $this->$k]; } } if (!empty($this->social)) { foreach ($this->social as $key => $value) { if (str_starts_with($key, 'og:')) { $tags[] = ['property' => $key, 'content' => $value]; } else { logWarning("unsupported meta: $key => $value"); } } } return implode('', array_map(function (array $item): string { $s = ' $v) $s .= ' '.htmlescape($k).'="'.htmlescape($v).'"'; $s .= '/>'; $s .= "\n"; return $s; }, $tags)); } }