ArticleRepository
in package
ArticleRepository - Data Mapper for Article entities
This repository implements the Repository Pattern and Data Mapper Pattern, providing a clean separation between the domain layer (Article entities) and the database layer (PDO).
Uses ArticleFactory to transform PDO arrays into Article entities.
Tags
Table of Contents
Properties
- $pdo : PDO
- PDO database connection instance
Methods
- __construct() : mixed
- Constructor - Dependency Injection of PDO connection
- count() : int
- Count total number of articles
- delete() : bool
- Delete an article by its ID
- findById() : Article|null
- Find an article by its unique identifier
- findBySlug() : Article|null
- Find an article by its SEO-friendly slug
- findLatestArticles() : array<int, Article>
- Retrieve latest articles ordered by creation date (descending)
- findPaginated() : array<int, Article>
- Retrieve paginated articles ordered by creation date (descending)
- save() : bool
- Save an article (insert or update)
- generateUniqueSlug() : string
- Generate a unique slug from a title using SlugGenerator
- generateUniqueSlugForUpdate() : string
- Generate a unique slug for an article update
- insert() : bool
- Insert a new article into the database
- slugExists() : bool
- Check if a slug already exists in the database
- slugExistsExcludingId() : bool
- Check if a slug exists excluding a specific article ID
- update() : bool
- Update an existing article in the database
Properties
$pdo
PDO database connection instance
private
PDO
$pdo
Methods
__construct()
Constructor - Dependency Injection of PDO connection
public
__construct(PDO $pdo) : mixed
Following best practices (CM4 Slide 22), the PDO connection is injected via constructor to facilitate testing and respect the Dependency Inversion Principle.
Parameters
- $pdo : PDO
-
Database connection instance
count()
Count total number of articles
public
count() : int
Return values
int —Total article count
delete()
Delete an article by its ID
public
delete(int $id) : bool
Parameters
- $id : int
-
Article ID to delete
Return values
bool —True if deletion succeeded, false otherwise
findById()
Find an article by its unique identifier
public
findById(int $id) : Article|null
Parameters
- $id : int
-
Article ID
Return values
Article|null —Article entity or null if not found
findBySlug()
Find an article by its SEO-friendly slug
public
findBySlug(string $slug) : Article|null
Parameters
- $slug : string
-
URL-friendly slug
Return values
Article|null —Article entity or null if not found
findLatestArticles()
Retrieve latest articles ordered by creation date (descending)
public
findLatestArticles(int $limit) : array<int, Article>
Parameters
- $limit : int
-
Maximum number of articles to retrieve
Return values
array<int, Article> —Array of latest Article entities
findPaginated()
Retrieve paginated articles ordered by creation date (descending)
public
findPaginated(int $offset, int $limit) : array<int, Article>
Parameters
- $offset : int
-
Starting offset
- $limit : int
-
Number of articles to retrieve
Return values
array<int, Article> —Array of Article entities
save()
Save an article (insert or update)
public
save(Article $article) : bool
If the article has no ID (null), it will be inserted. If the article has an ID, it will be updated.
This method implements the "smart save" pattern recommended for clean controller code.
Parameters
- $article : Article
-
Article entity to save
Return values
bool —True if save succeeded, false otherwise
generateUniqueSlug()
Generate a unique slug from a title using SlugGenerator
private
generateUniqueSlug(string $title) : string
Parameters
- $title : string
-
Title to convert
Return values
string —Unique slug
generateUniqueSlugForUpdate()
Generate a unique slug for an article update
private
generateUniqueSlugForUpdate(string $title, int $excludeId) : string
Parameters
- $title : string
-
Title to convert
- $excludeId : int
-
ID of the article to exclude from uniqueness check
Return values
string —Unique slug
insert()
Insert a new article into the database
private
insert(Article $article) : bool
Parameters
- $article : Article
-
Article entity to insert
Return values
bool —True if insertion succeeded, false otherwise
slugExists()
Check if a slug already exists in the database
private
slugExists(string $slug) : bool
Parameters
- $slug : string
-
Slug to check
Return values
bool —True if exists, false otherwise
slugExistsExcludingId()
Check if a slug exists excluding a specific article ID
private
slugExistsExcludingId(string $slug, int $excludeId) : bool
Parameters
- $slug : string
-
Slug to check
- $excludeId : int
-
ID of the article to exclude from check
Return values
bool —True if exists, false otherwise
update()
Update an existing article in the database
private
update(Article $article) : bool
Parameters
- $article : Article
-
Article entity to update
Return values
bool —True if update succeeded, false otherwise