Ajoute la gateway des commentaires et corrige les vues
This commit is contained in:
41
src/Silex/Gateway/CommentGateway.php
Normal file
41
src/Silex/Gateway/CommentGateway.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Silex\Gateway;
|
||||
|
||||
use DateTime;
|
||||
use PDO;
|
||||
use Silex\Model\Comment;
|
||||
|
||||
class CommentGateway
|
||||
{
|
||||
private PDO $pdo;
|
||||
|
||||
public function __construct(PDO $pdo)
|
||||
{
|
||||
$this->pdo = $pdo;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Comment[]
|
||||
*/
|
||||
public function getByNewsId(int $id): array
|
||||
{
|
||||
$req = $this->pdo->prepare('SELECT * FROM comment WHERE news_id = :id ORDER BY publication_date DESC');
|
||||
$req->bindValue(':id', $id, PDO::PARAM_INT);
|
||||
if (!$req->execute()) {
|
||||
return [];
|
||||
}
|
||||
$comments = [];
|
||||
while ($data = $req->fetch()) {
|
||||
$comments[] = $this->createComment($data);
|
||||
}
|
||||
return $comments;
|
||||
}
|
||||
|
||||
private function createComment(array $data): Comment
|
||||
{
|
||||
return new Comment(intval($data['id_comment']),intval($data['news_id']), DateTime::createFromFormat('Y-m-d H:i:s', $data['publication_date']), $data['content'], intval($data['author_id']));
|
||||
}
|
||||
}
|
@@ -49,6 +49,7 @@ class NewsGateway
|
||||
return $news;
|
||||
}
|
||||
|
||||
|
||||
private function createNews(array $data): News
|
||||
{
|
||||
return new News($data['title'], $data['content'], DateTime::createFromFormat('Y-m-d H:i:s', $data['publication_date']));
|
||||
|
Reference in New Issue
Block a user