Article into objects

This commit is contained in:
PomPom 2022-02-23 13:27:48 +01:00
parent e8e9d1ad95
commit 9f986f78f8
6 changed files with 105 additions and 31 deletions

View File

@ -4,33 +4,23 @@ class Article {
private String $nom;
private String $desc;
private DateTime $date;
private String $url;
public function __construct(String $n, String $de, DateTime $da, String $u) {
$this -> nom = $n;
$this -> desc = $de;
$this -> date = $da;
$this -> url = $u;
public function __construct(String $n, String $de, DateTime $da) {
$this->nom = $n;
$this->desc = $de;
$this->date = $da;
}
public function getArticleName() {
public function getArticleName() : String {
return $this -> nom;
}
public function getArticleDescription() {
return $this -> desc;
public function getArticleDescription() : String {
return $this->desc;
}
public function getArticleDate() {
public function getArticleDate() : DateTime {
return $this -> date;
}
public function getArticleUrl() {
return $this -> url;
}
public function __toString()
{
return '<ul><li>' .$this->getArticleName() . '</li><li>' .$this->getArticleDescription() . '</li><li>' .date_format($this->getArticleDate(), 'd M Y, H\hi') . '</li><li>' .$this->getArticleUrl() .'</ul>';
}
}

14
ArticleController.php Normal file
View File

@ -0,0 +1,14 @@
<?php
class ArticleController {
private $tabArticle = array();
public function __construct() {
}
public function createArticles() {
//lire fichiers
}
}

30
RSSArticle.php Normal file
View File

@ -0,0 +1,30 @@
<?php
class RSSArticle {
private String $nom;
private DateTime $date;
private String $url;
public function __construct(String $n, DateTime $d, String $u) {
$this -> nom = $n;
$this -> date = $d;
$this -> url = $u;
}
public function getArticleName() : String {
return $this -> nom;
}
public function getArticleDate() : DateTime {
return $this -> date;
}
public function getArticleUrl() : String {
return $this -> url;
}
public function __toString()
{
return '<ul><li>' .$this->getArticleName() . '</li><li>' .date_format($this->getArticleDate(), 'd M Y') . '</li><li>' .$this->getArticleUrl() .'</ul>';
}
}

43
RSSArticleManager.php Normal file
View File

@ -0,0 +1,43 @@
<?php
class RSSArticleManager {
private $tabURL = array(
"http://fetchrss.com/rss/6213baca199e171a5d6eacc26213babd69097e19dd230f73.xml",
"http://fetchrss.com/rss/6213baca199e171a5d6eacc26214b2ca9bb3286594306764.xml",
"http://fetchrss.com/rss/6213baca199e171a5d6eacc26214b5c20a15a160ea326d82.xml",
"http://fetchrss.com/rss/6213baca199e171a5d6eacc26214b6fd3f6eb80141637472.xml",
"http://fetchrss.com/rss/6213baca199e171a5d6eacc26214b7ac82763a37db6639c4.xml"
);
public function __construct() {
}
public function createArticles() : array {
$tabArticle = array();
foreach ($this->tabURL as $u) {
$rss = simplexml_load_file($u);
foreach ($rss->channel->item as $item){
$desc = "";
array_push($tabArticle, new RSSArticle($item->title, date_create($item->pubDate), $item->link));
}
}
return $tabArticle;
}
public function sortArticles(array $a) : array {
$tab = $a;
usort($tab, function($firstArticle, $secondArticle) { //sorting method of all articles
$testA = $firstArticle->getArticleDate();
$testB = $secondArticle->getArticleDate();
if ($testA == $testB) {
return 0;
}
return $testA > $testB ? -1 : 1;
});
return $tab;
}
}

3
articles/article.txt Normal file
View File

@ -0,0 +1,3 @@
Titre
Date
Desc

View File

@ -1,17 +1,11 @@
<?php
include './Article.php';
include './RSSArticle.php';
include './RSSArticleManager.php';
$url = "http://fetchrss.com/rss/6213baca199e171a5d6eacc26213babd69097e19dd230f73.xml"; /* insérer ici l'adresse du flux RSS de votre choix */
$rss = simplexml_load_file($url);
$tabArticle = array();
foreach ($rss->channel->item as $item){
$desc = explode("</div>", explode('<div class="content">', $item->description)[1]);
array_push($tabArticle, new Article($item->title, $desc[0], date_create($item->pubDate), $item->link));
}
$am = new RSSArticleManager();
$tabArticle = $am -> createArticles();
$tabArticle = $am -> sortArticles($tabArticle);
?>
@ -27,7 +21,7 @@ foreach ($rss->channel->item as $item){
</div>
<?php
echo sizeof($tabArticle) .' articles<br /><br />';
foreach ($tabArticle as $a) {
echo $a.'<br />';
}