Documentation

ArticleModel
in package

Responsibilities: - Read: Retrieve articles with pagination, search by slug/ID - Write: Create, update and delete articles - Automatic management of unique slugs

Tags
author

BdeLive - Group 8

version
2.0.0

Table of Contents

Properties

$pdo  : PDO

Methods

__construct()  : mixed
Constructor - PDO dependency injection
countArticles()  : int
Count total number of articles
deleteArticle()  : bool
Delete an article from the database
getArticleById()  : array<string, mixed>|null
Retrieve an article by its identifier
getArticleBySlug()  : array<string, mixed>|null
Retrieve an article by its slug
getLatestArticles()  : array<int, array<string, mixed>>
Retrieve latest articles sorted by creation date (descending)
getPaginatedArticles()  : array<int, array<string, mixed>>
Retrieve paginated articles sorted by creation date (descending)
insertArticle()  : bool
Insert a new article into the database
updateArticle()  : bool
Update an existing article in the database
generateUniqueSlug()  : string
Generate a unique slug from a title using reusable SlugGenerator
generateUniqueSlugForUpdate()  : string
Generate a unique slug for an article update
slugExists()  : bool
Check if a slug already exists in the database
slugExistsExcludingId()  : bool
Check if a slug already exists in the database, excluding a specific article ID

Properties

Methods

__construct()

Constructor - PDO dependency injection

public __construct(PDO $pdo) : mixed
Parameters
$pdo : PDO

Database connection instance

countArticles()

Count total number of articles

public countArticles() : int
Return values
int

Total number of articles

deleteArticle()

Delete an article from the database

public deleteArticle(int $articleId) : bool

Permanently deletes an article identified by its ID.

Parameters
$articleId : int

ID of the article to delete

Return values
bool

True on success, false on failure

getArticleById()

Retrieve an article by its identifier

public getArticleById(int $articleId) : array<string, mixed>|null
Parameters
$articleId : int

Unique article identifier

Return values
array<string, mixed>|null

Article data or null if not found

getArticleBySlug()

Retrieve an article by its slug

public getArticleBySlug(string $slug) : array<string, mixed>|null
Parameters
$slug : string

Unique article slug

Return values
array<string, mixed>|null

Article data or null if not found

getLatestArticles()

Retrieve latest articles sorted by creation date (descending)

public getLatestArticles([int $limit = 2 ]) : array<int, array<string, mixed>>

Retrieves a specified number of the most recent articles from the database. The query is optimized to select only required columns for performance.

Parameters
$limit : int = 2

Number of articles to retrieve (default: 2)

Return values
array<int, array<string, mixed>>

Array of articles, empty array if none found

getPaginatedArticles()

Retrieve paginated articles sorted by creation date (descending)

public getPaginatedArticles(int $offset, int $limit) : array<int, array<string, mixed>>
Parameters
$offset : int

Start offset

$limit : int

Number of articles to retrieve

Return values
array<int, array<string, mixed>>

Array of articles

insertArticle()

Insert a new article into the database

public insertArticle(string $title, string $description, string $imageUrl, string $author) : bool

Creates a new article record with all provided information. Automatically generates a unique slug from the title.

Parameters
$title : string

Article title

$description : string

Article description/content

$imageUrl : string

Cloudinary image URL

$author : string

Author full name

Return values
bool

True on success, false on failure

updateArticle()

Update an existing article in the database

public updateArticle(int $articleId, string $title, string $description, string $imageUrl, string $author) : bool

Updates information of an existing article identified by its ID. If the title changes, a new unique slug is generated. The image URL is updated only if a new image is provided.

Parameters
$articleId : int

ID of the article to update

$title : string

New article title

$description : string

New description/content

$imageUrl : string

New Cloudinary image URL (empty string to keep existing)

$author : string

New author name

Return values
bool

True on success, false on failure

generateUniqueSlug()

Generate a unique slug from a title using reusable SlugGenerator

private generateUniqueSlug(string $title) : string

If a slug already exists, appends a number to make it unique. Example: "my-article", "my-article-2", "my-article-3"

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

Similar to generateUniqueSlug but excludes the current article from the uniqueness check. Allows updating an article without changing its slug if the title did not change, or generating a new unique slug if the title changed.

Parameters
$title : string

Title to convert

$excludeId : int

ID of the article to exclude from uniqueness check

Return values
string

Unique slug

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 the slug exists, false otherwise

slugExistsExcludingId()

Check if a slug already exists in the database, 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 the check

Return values
bool

True if the slug exists (excluding the specified ID), false otherwise


        
On this page

Search results