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

88 lines
2.6 KiB

<?php
namespace model;
use DAL\ArticleGateway;
use DAL\Connection;
use DAL\FluxGateway;
use DOMDocument;
use Exception;
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): array
{
$dom = new DOMDocument();
$tabArticle = array();
if ($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;
$media = $item->getElementsByTagName('media:content');
$mediaUrl = null;
if ($media->length > 0){
$mediaUrl = $media->item(0)->getAttribute('url');
}
$tabArticle[] = new Article((int)null, $title, $date, $description, $guid, $link, $mediaUrl, $flux->getId());
}
return $tabArticle;
} else {
// En cas d'erreur lors du chargement du flux RSS, lever une exception
throw new Exception("Erreur lors du chargement du flux RSS");
}
}
/**
* @throws Exception
*/
public function parseAll($fluxes){
foreach ($fluxes as $flux){
$tabArticles[] =$this->parseArticles($flux);
}
return $tabArticles;
}
/**
* @throws Exception
*/
public function addAllArticles()
{
$this->articleGateway->removeAllArticleForParser();
$allFlux = $this->fluxGateway->findAllFlux();
var_dump($allFlux);
$allArticles = $this->parseAll($allFlux);
var_dump($allArticles);
foreach ($allArticles as $article) {
$this->articleGateway->addArticle($article);
}
return $allArticles;
}
}
$gwArt = new ArticleGateway(new Connection('mysql:host=londres.uca.local;dbname=dbrorossetto', 'rorossetto', 'tpphp'));
$gwFl = new FluxGateway(new Connection('mysql:host=londres.uca.local;dbname=dbrorossetto', 'rorossetto', 'tpphp'));
$pars = new Parser( $gwFl,$gwArt);
var_dump($pars->addAllArticles());