guillermoandrae/php-repository

A PHP library that implements the Repository pattern.

3.0.2 2021-12-28 18:28 UTC

This package is auto-updated.

Last update: 2024-10-29 00:54:14 UTC


README

Travis Scrutinizer Scrutinizer Coverage Your Mom Approves

I use a simple implementation of the Repository pattern. Often. I've been copying and pasting the same code between projects for longer than I care to admit; I came to my senses recently, and that's why this project now exists.

Installation

Do this, then relax:

composer require guillermoandrae/php-repository

Usage

Before you do anything crazy, read up on the Repository pattern. If, after reading that, you decide you still want to move forward: keep reading.

Naming Classes

Your classes should be named according to their data type. For example, if you're dealing with widgets, your repository should be named WidgetsRepository and your model should be named WidgetModel. Note that the data type in the repository name is pluralized and singular in the model name.

Define the Namespace

By default, the RepositoryFactory class will look for repositories in the App\Repositories namespace. If you don't plan on using that namespace, you'll need to tell the factory where your repositories live. Use RepositoryFactory::setNamespace() to do that, like so:

RepositoryFactory::setNamespace('MyStuff\Repositories');

Creating Repositories

Leveraging the repository functionality is as easy as writing the following:

namespace App\Repositories; // again, use whatever you like

use Guillermoandrae\Repositories\AbstractRepository;

class WidgetsRepository extends AbstractRepository
{
    public function create(array $data): ModelInterface
    {
        // add your object creation code here
    }
}

There are a few methods you need to implement in the classes that inherit from the AbstractRepository class. Check out the RepositoryInterface and AbstractRepository classes for the most up-to-date list.

Creating Models

Creating models is straightforward:

namespace App\Repositories;

use Guillermoandrae\Models\AbstractModel;

final class WidgetModel extends AbstractModel // the "final" is optional
{
    // add your constructor and getters
}

I generally build immutable models, but how you use them is entirely up to you.

Testing

Run the following command to make sure you don't ruin the code coverage percentage:

composer check-coverage

Run the following command to make sure your code is appropriately styled:

composer check-style

Run the following command to invoke both of the above commands easily:

composer test