diff --git a/fluxRSS/DAL/FluxGateway.php b/fluxRSS/DAL/FluxGateway.php index 8e6e41b..57ca526 100755 --- a/fluxRSS/DAL/FluxGateway.php +++ b/fluxRSS/DAL/FluxGateway.php @@ -2,6 +2,7 @@ namespace DAL; +use metier\Flux; use PDO; class FluxGateway diff --git a/fluxRSS/composer.json b/fluxRSS/composer.json index 6eaab9c..21ca7cc 100755 --- a/fluxRSS/composer.json +++ b/fluxRSS/composer.json @@ -1,7 +1,8 @@ { "require": { "twig/twig": "^3.0", - "ext-pdo": "*" + "ext-pdo": "*", + "ext-dom": "*" }, "autoload": { "psr-4": { diff --git a/fluxRSS/controleur/Controleur.php b/fluxRSS/controleur/Controleur.php index e5907f7..ca4318e 100755 --- a/fluxRSS/controleur/Controleur.php +++ b/fluxRSS/controleur/Controleur.php @@ -37,9 +37,10 @@ class Controleur } } catch (\PDOException $e) { //si erreur BD, pas le cas ici - $dVueEreur[] = 'Erreur inattendue!!! '; + $dVueEreur[] = 'Erreur PDO : ' . $e->getMessage(); + echo $twig->render('erreur.html', ['dVueEreur' => $dVueEreur]); } catch (\Exception $e2) { - $dVueEreur[] = 'Erreur inattendue!!! '; + $dVueEreur[] = 'Erreur : ' . $e2->getMessage(); echo $twig->render('erreur.html', ['dVueEreur' => $dVueEreur]); } @@ -69,9 +70,11 @@ class Controleur $age = $_POST['txtAge']; \config\Validation::val_form($nom, $age, $dVueEreur); + /* $model = new \metier\Simplemodel(); $data = $model->get_data(); - + */ + $dVue = [ 'nom' => $nom, 'age' => $age, diff --git a/fluxRSS/metier/Flux.php b/fluxRSS/metier/Flux.php index d68b0ba..a167fbe 100755 --- a/fluxRSS/metier/Flux.php +++ b/fluxRSS/metier/Flux.php @@ -4,6 +4,7 @@ namespace metier; class Flux { + private string $id; private string $flux; /** @@ -21,4 +22,12 @@ class Flux { $this->flux = $flux; } + + /** + * @return string + */ + public function getId(): string + { + return $this->id; + } } \ No newline at end of file diff --git a/fluxRSS/model/Parser.php b/fluxRSS/model/Parser.php new file mode 100644 index 0000000..6e61732 --- /dev/null +++ b/fluxRSS/model/Parser.php @@ -0,0 +1,68 @@ +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"); + } + } + + + 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); + } + } +} \ No newline at end of file