ArticleFactory
in package
ArticleFactory - Transforms raw database data into Article entities
This factory class acts as a translator between the database layer (PDO arrays with column names) and the domain layer (Article entities).
Responsibilities:
- Hydrate Article entities from PDO result arrays
- Handle type conversions and null values
- Provide clean, type-safe entities to controllers
Design Pattern: Factory Pattern Related to: Data Mapper Pattern
Tags
Table of Contents
Methods
- createCollectionFromDatabase() : array<int, Article>
- Create multiple Article entities from database result set
- createFromDatabase() : Article
- Create an Article entity from database row data
- toDatabase() : array<string, mixed>
- Convert an Article entity back to database-ready array
Methods
createCollectionFromDatabase()
Create multiple Article entities from database result set
public
static createCollectionFromDatabase(array<int, array<string, mixed>> $dataSet) : array<int, Article>
Batch version of createFromDatabase for paginated results. Useful when retrieving multiple articles from findPaginated() or findLatestArticles().
Parameters
- $dataSet : array<int, array<string, mixed>>
-
Array of database rows
Return values
array<int, Article> —Array of hydrated Article entities
createFromDatabase()
Create an Article entity from database row data
public
static createFromDatabase(array<string, mixed> $data) : Article
Transforms a raw PDO array (from ARTICLES table) into a clean, type-safe Article entity. Handles all type conversions and ensures data integrity.
Database columns mapping:
- id → id (int|null)
- title → title (string)
- slug → slug (string)
- description → description (string)
- image_url → imageUrl (string|null)
- author → author (string)
- created_at → createdAt (string, datetime)
Parameters
- $data : array<string, mixed>
-
Raw database row from ARTICLES table
Return values
Article —Hydrated Article entity
toDatabase()
Convert an Article entity back to database-ready array
public
static toDatabase(Article $article) : array<string, mixed>
Useful for UPDATE and INSERT operations. Transforms the entity back into an array format compatible with PDO prepared statements.
Note: This method excludes the ID for INSERT operations (handled separately). Note: The created_at timestamp is excluded as it's auto-managed by database.
Parameters
- $article : Article
-
Article entity to convert
Return values
array<string, mixed> —Database-ready associative array