Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 33 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| OlzArticleMetadata | |
0.00% |
0 / 33 |
|
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 1 |
| getHtml | |
0.00% |
0 / 33 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\News\Components\OlzArticleMetadata; |
| 4 | |
| 5 | use Olz\Components\Common\OlzComponent; |
| 6 | |
| 7 | /** @extends OlzComponent<array<string, mixed>> */ |
| 8 | class OlzArticleMetadata extends OlzComponent { |
| 9 | public function getHtml(mixed $args): string { |
| 10 | $db = $this->dbUtils()->getDb(); |
| 11 | $data_href = $this->envUtils()->getDataHref(); |
| 12 | $base_href = $this->envUtils()->getBaseHref(); |
| 13 | $code_href = $this->envUtils()->getCodeHref(); |
| 14 | |
| 15 | $id = intval($args['id']); |
| 16 | $sql = "SELECT author_name, title, published_date, published_time, image_ids FROM news WHERE id='{$id}'"; |
| 17 | $res = $db->query($sql); |
| 18 | // @phpstan-ignore-next-line |
| 19 | if ($res->num_rows == 0) { |
| 20 | throw new \Exception("No such entry"); |
| 21 | } |
| 22 | // @phpstan-ignore-next-line |
| 23 | $row = $res->fetch_assoc(); |
| 24 | $url = "{$base_href}{$code_href}news/{$id}"; |
| 25 | $json_url = json_encode($url); |
| 26 | // @phpstan-ignore-next-line |
| 27 | $html_author = $row['author_name']; |
| 28 | $json_author = json_encode($html_author); |
| 29 | // @phpstan-ignore-next-line |
| 30 | $html_title = $row['title']; |
| 31 | $json_title = json_encode($html_title); |
| 32 | // @phpstan-ignore-next-line |
| 33 | $iso_date = $row['published_date'].'T'.$row['published_time']; |
| 34 | $json_iso_date = json_encode($iso_date); |
| 35 | // @phpstan-ignore-next-line |
| 36 | $image_ids = json_decode($row['image_ids'] ?? '[]', true); |
| 37 | $images = array_map(function ($image_id) use ($base_href, $data_href, $id) { |
| 38 | return "{$base_href}{$data_href}img/news/{$id}/img/{$image_id}"; |
| 39 | }, $image_ids); |
| 40 | $json_images = json_encode($images); |
| 41 | return <<<ZZZZZZZZZZ |
| 42 | <script type="application/ld+json"> |
| 43 | { |
| 44 | "@context": "https://schema.org", |
| 45 | "@type": "Article", |
| 46 | "identifier": {$json_url}, |
| 47 | "url": {$json_url}, |
| 48 | "author": {$json_author}, |
| 49 | "headline": {$json_title}, |
| 50 | "image": {$json_images}, |
| 51 | "datePublished": {$json_iso_date}, |
| 52 | "dateModified": {$json_iso_date} |
| 53 | } |
| 54 | </script> |
| 55 | ZZZZZZZZZZ; |
| 56 | } |
| 57 | } |