Initial commit

This commit is contained in:
2022-11-15 09:23:20 +01:00
commit 22127b8702
13 changed files with 369 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace Silex\Gateway;
use DateTime;
use PDO;
use Silex\Model\News;
class NewsGateway
{
private PDO $pdo;
public function __construct(PDO $pdo)
{
$this->pdo = $pdo;
}
/**
* @return News[]
*/
public function getPaginatedRecentNews(int $page = 1, int $limit = 10): array
{
$req = $this->pdo->prepare('SELECT * FROM news ORDER BY publication_date DESC LIMIT :limit OFFSET :offset;');
$req->bindValue('limit', $limit, PDO::PARAM_INT);
$req->bindValue('offset', ($page - 1) * $limit, PDO::PARAM_INT);
if (!$req->execute()) {
return [];
}
$news = [];
while ($data = $req->fetch()) {
$news[] = new News($data['title'], $data['content'], DateTime::createFromFormat('Y-m-d H:i:s', $data['publication_date']));
}
return $news;
}
}