Simplified
This commit is contained in:
@@ -1,53 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Repositories;
|
||||
|
||||
use App\Models\Post;
|
||||
|
||||
/**
|
||||
* Interface décrivant les opérations sur les posts.
|
||||
*/
|
||||
interface PostRepositoryInterface
|
||||
{
|
||||
/**
|
||||
* Retourne tous les posts triés par id descendant.
|
||||
*
|
||||
* @return Post[] Tableau d'objets Post
|
||||
*/
|
||||
public function allDesc(): array;
|
||||
|
||||
/**
|
||||
* Trouve un post par son id.
|
||||
*
|
||||
* @param int $id
|
||||
* @return Post|null
|
||||
*/
|
||||
public function find(int $id): ?Post;
|
||||
|
||||
/**
|
||||
* Crée un post.
|
||||
*
|
||||
* @param Post $post Instance contenant les données à insérer (id ignoré)
|
||||
* @return int id inséré
|
||||
*/
|
||||
public function create(Post $post): int;
|
||||
|
||||
/**
|
||||
* Met à jour un post.
|
||||
*
|
||||
* @param int $id
|
||||
* @param Post $post Données à mettre à jour (id ignoré)
|
||||
* @return void
|
||||
*/
|
||||
public function update(int $id, Post $post): void;
|
||||
|
||||
/**
|
||||
* Supprime un post.
|
||||
*
|
||||
* @param int $id
|
||||
* @return void
|
||||
*/
|
||||
public function delete(int $id): void;
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Repositories;
|
||||
|
||||
use Medoo\Medoo;
|
||||
use App\Models\Post;
|
||||
|
||||
/**
|
||||
* Repository pour "post" basé sur Medoo.
|
||||
*
|
||||
* Cette implémentation convertit les lignes DB en objets App\Models\Post.
|
||||
*/
|
||||
class PostRepositoryMedoo implements PostRepositoryInterface
|
||||
{
|
||||
private Medoo $db;
|
||||
|
||||
public function __construct(Medoo $db)
|
||||
{
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function allDesc(): array
|
||||
{
|
||||
$rows = $this->db->select('post', ['id', 'title', 'content'], ['ORDER' => ['id' => 'DESC']]);
|
||||
if (!is_array($rows)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return array_map(function ($r) {
|
||||
return new Post(
|
||||
(int)($r['id'] ?? 0),
|
||||
(string)($r['title'] ?? ''),
|
||||
(string)($r['content'] ?? '')
|
||||
);
|
||||
}, $rows);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function find(int $id): ?Post
|
||||
{
|
||||
$row = $this->db->get('post', ['id', 'title', 'content'], ['id' => $id]);
|
||||
|
||||
if (empty($row) || $row === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new Post(
|
||||
(int)($row['id'] ?? 0),
|
||||
(string)($row['title'] ?? ''),
|
||||
(string)($row['content'] ?? '')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function create(Post $post): int
|
||||
{
|
||||
$data = $post->toPersistableArray();
|
||||
|
||||
$this->db->insert('post', $data);
|
||||
return (int)$this->db->id();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function update(int $id, Post $post): void
|
||||
{
|
||||
$data = $post->toPersistableArray();
|
||||
$this->db->update('post', $data, ['id' => $id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function delete(int $id): void
|
||||
{
|
||||
$this->db->delete('post', ['id' => $id]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user