You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Php_RSS/fluxRSS/model/Parser.php

61 lines
1.6 KiB

<?php
namespace model;
use DAL\ArticleGateway;
use DAL\FluxGateway;
use DOMDocument;
use metier\Article;
use metier\Flux;
class Parser
{
private FluxGateway $fluxGateway;
private ArticleGateway $articleGateway;
public function __construct(FluxGateway $fluxGateway, ArticleGateway $articleGateway){
$this->fluxGateway = $fluxGateway;
$this->articleGateway = $articleGateway;
}
public function parseArticles(Flux $flux){
$dom = new DOMDocument();
$tabArticle = array();
$dom->load($flux->getFlux());
$items = $dom->getElementsByTagName('item');
foreach ($items as $item) {
$title = $item->getElementsByTagName('title')[0]->nodeValue;
$date = $item->getElementsByTagName('pubDate')[0]->nodeValue;
$guid = $item->getElementsByTagName('guid')[0]->nodeValue;
$link = $item->getElementsByTagName('link')[0]->nodeValue;
$description = $item->getElementsByTagName('description')[0]->nodeValue;
//manque ajout de l'image
$tabArticle[] = new Article((int)Null, $title, $date, $description, $guid, $link, $description, $flux->getId());
}
return $tabArticle;
}
public function parseAll($fluxes){
foreach ($fluxes as $flux){
$tabArticles[] =$this->parseArticles($flux);
}
return $tabArticles;
}
public function addAllArticles(){
$allFlux = $this->fluxGateway->findAllFlux();
$allArticles = $this->parseAll($allFlux);
foreach ($allArticles as $article) {
$this->articleGateway->addArticle($article);
}
}
}