Skip to content
Snippets Groups Projects
Commit d625be8e authored by Silas Meister's avatar Silas Meister
Browse files

Object-oriented News class

parent 39b26ea3
Branches
Tags
No related merge requests found
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
require_once 'Page.php'; require_once 'Page.php';
require_once 'constants.php'; require_once 'constants.php';
require_once 'data/News.php';
class Index extends Page { class Index extends Page {
...@@ -36,7 +37,17 @@ class Index extends Page { ...@@ -36,7 +37,17 @@ class Index extends Page {
$json = json_decode($fileContents, true); $json = json_decode($fileContents, true);
return $json["news"]; $news = array();
foreach ($json["news"] as $elem) {
$title = htmlspecialchars($elem["title"]);
$imgurl = htmlspecialchars($elem["imgurl"]);
$content = htmlspecialchars($elem["content"]);
$news[] = new News($title, $imgurl, $content);
}
return $news;
} }
protected function generateView():void protected function generateView():void
...@@ -107,17 +118,14 @@ class Index extends Page { ...@@ -107,17 +118,14 @@ class Index extends Page {
<div> <div>
<div class="article-content-container"> <div class="article-content-container">
EOT; EOT;
$title = $data[0]->title();
$news = $data[0]; $imgurl = $data[0]->imgurl();
$content = $data[0]->content();
$title = htmlspecialchars($news["title"]);
$imageurl = htmlspecialchars($news["imgurl"]);
$content = htmlspecialchars($news["content"]);
echo <<<EOT echo <<<EOT
<a class="article-link" href="#"></a> <a class="article-link" href="#"></a>
<img class="article-image" src="$imageurl" alt="" /> <img class="article-image" src="$imgurl" alt="" />
<div class="article-content nasa-events-container"> <div class="article-content nasa-events-container">
...@@ -146,17 +154,15 @@ EOT; ...@@ -146,17 +154,15 @@ EOT;
<div> <div>
<div class="article-content-container"> <div class="article-content-container">
EOT; EOT;
$news = $data[1]; $title = $data[1]->title();
$imgurl = $data[1]->imgurl();
$title = htmlspecialchars($news["title"]); $content = $data[1]->content();
$imageurl = htmlspecialchars($news["imgurl"]);
$content = htmlspecialchars($news["content"]);
echo <<<EOT echo <<<EOT
<a class="article-link" href="#"></a> <a class="article-link" href="#"></a>
<img class="article-image" src="$imageurl" alt="" /> <img class="article-image" src="$imgurl" alt="" />
<div class="article-content overlay"> <div class="article-content overlay">
<h4 class="overlay-title">$title</h4> <h4 class="overlay-title">$title</h4>
...@@ -169,17 +175,15 @@ EOT; ...@@ -169,17 +175,15 @@ EOT;
<div> <div>
<div class="article-content-container"> <div class="article-content-container">
EOT; EOT;
$news = $data[2]; $title = $data[2]->title();
$imgurl = $data[2]->imgurl();
$title = htmlspecialchars($news["title"]); $content = $data[2]->content();
$imageurl = htmlspecialchars($news["imgurl"]);
$content = htmlspecialchars($news["content"]);
echo <<<EOT echo <<<EOT
<a class="article-link" href="#"></a> <a class="article-link" href="#"></a>
<img class="article-half-image" src="$imageurl" alt="" /> <img class="article-half-image" src="$imgurl" alt="" />
<div class="article-triangle"></div> <div class="article-triangle"></div>
......
<?php declare(strict_types=1);
class News {
private string $title;
private string $imgurl;
private string $content;
public function __construct(string $title, string $imgurl, string $content) {
$this->title = $title;
$this->imgurl = $imgurl;
$this->content = $content;
}
public function title(): string {
return $this->title;
}
public function imgurl(): string {
return $this->imgurl;
}
public function content(): string {
return $this->content;
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment