Initial commit
This commit is contained in:
37
src/Silex/Gateway/NewsGateway.php
Normal file
37
src/Silex/Gateway/NewsGateway.php
Normal 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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user