reynholm/laravel-repositories

This package is abandoned and no longer maintained. No replacement package was suggested.

Provides an easy way to create your laravel repositories

0.17.3 2014-09-30 16:02 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:39:09 UTC


README

Latest Stable Version Build Status Coverage Status Total Downloads License

Repository pattern for Laravel

Installation and Configuration

Add this to your composer.json

{
    "require": {
        "reynholm/laravel-repositories": "0.16.*"
    }
}

Abandoned!

This package is abandoned as you can see on packagist. I moved to python and I have no time for keeping this repo up to date with Laravel. Feel free to fork it and maintaining it yourself.

Notice

I think that you can use it even that is on an early stage, but I may make some breaking changes so if you don't want any surprises just don't put "dev-master" or an asterisk as your composer version. Be smart!

If you put your version like on the above example you will not get any breaking changes.

Usage

Simply extend the abstract class Reynholm\LaravelRepositories\Repository\LaravelRepository

Example

use Reynholm\LaravelRepositories\Repository\LaravelRepository;

class UserArrayRepository extends LaravelRepository
{
    //defaults to laravel's default connection
	//protected $connection = 'mysql';

	//If no tableName is specified it will be guessed based on a snake_case version of the CamelCase
	//class name without the repository part and pluralized.
	//Examples: UserRepository => users, CustomerHistoryLog => customer_history_logs, etc...
    //protected $tableName  = 'users';
}

Fetch Mode

You can also choose the fetcher that you like the most or create your own. You can select the one that you prefer from the LaravelRepositoryInterface constants. Example:

    class YourRepository extends LaravelRepository {
        protected $fetchMode = LaravelRepositoryInterface::FETCH_AS_LARAVEL_COLLECTION_OBJECTS;
    }

So the repository will return a Illuminate\Support\Collection with objects.

Currently implemented methods:

     /**
      * @param int $id
      * @param array $columns Restrict columns that you want to retrieve
      * @return array
      */
     public function find($id, array $columns = array());

     /**
      * @param int $id
      * @param array $columns Restrict columns that you want to retrieve
      * @return array
      * @throws EntityNotFoundException
      */
     public function findOrFail($id, array $columns = array());

     /**
      * @param array $criteria
      * Ex.:
      * array(
      *     array('name', '=', 'carlos'),
      *     array('age',  '>', 20),
      * )
      * @param array $columns Restrict columns that you want to retrieve
      * @return array
      */
     public function findOne(array $criteria, array $columns = array());

     /**
      * @param array $criteria
      * Ex.:
      * array(
      *     array('name', '=', 'carlos'),
      *     array('age',  '>', 20),
      * )
      * @param array $columns Restrict columns that you want to retrieve
      * @param integer $limit
      * @param array $orderBy
      * Ex.:
      * array(
      *     array('name', 'asc'),
      *     array('age', 'desc'),
      * )
      * @return array
      */
     public function findMany(array $criteria, array $columns = array(), $limit = 0, array $orderBy = array());

     /**
      * @param array $columns Restrict columns that you want to retrieve
      * @param int $limit
      * @param array $orderBy
      * Ex.: ['name' => 'asc', 'age' => 'desc']
      * @return array
      */
     public function findAll(array $columns = array(), $limit = 0, array $orderBy = array());

     /**
      * Get an array with the values of a given column.
      *
      * @param  string  $column
      * @param  string  $key
      * @return array
      */
     public function lists($column, $key = null);

     /**
      * @param array $data The resource that you want to create
      * @param bool $force If force is false and data is not valid error will be throwed
      * @return boolean
      * @throws DataNotValidException
      */
     public function create(array $data, $force = false);

     /**
      * @param array $data The resources that you want to create
      * @param bool $force If force is false and data is not valid error will be throwed
      * @return boolean
      * @throws DataNotValidException
      */
     public function createMany(array $data, $force = false);

     /**
      * @param array $criteria
      * Ex.:
      * array(
      *     array('name', '=', 'carlos'),
      *     array('age',  '>', 20),
      * )
      * @return int
      */
     public function count(array $criteria = array());

    /**
     * Update a resource by its id
     * @param int $id
     * @param array $data
     * @return boolean
     */
    public function update($id, array $data);

    /**
     * Update one or more resources
     * @param array $criteria
     * @param array $data
     * @return int Number of affected rows
     */
    public function updateMany(array $criteria, array $data);

     /**
      * Validates the input array and stores all the errors,
      * them, you can get them with the getErrors() method
      * @param array $data
      * @return boolean
      */
     public function validate(array $data);

     /**
      * Validates the input array and stores all the errors,
      * them, you can get them with the getErrors() method
      * Same af validate but specify the rules, instead of using the repository rules
      * @param array $data
      * @param array $rules
      * @return boolean
      */
     public function validateWithCustomRules(array $data, array $rules);

     /**
      * Validates the input array or throws exception
      * It also stores all the errors. Then you can retrieve them with the
      * getValidationErrors() method
      * @param array $data
      * @throws DataNotValidException
      * @return void
      */
     public function validateOrFail(array $data);

     /**
      * Validates the input array or throws exception
      * It also stores all the errors. Then you can retrieve them with the
      * getValidationErrors() method
      * @param array $data
      * @param array $rules
      * @throws DataNotValidException
      * @return void
      */
     public function validateWithCustomRulesOrFail(array $data, array $rules);

     /**
      * Validates a multidimensional
      * It also stores all the errors. Then you can retrieve them with the
      * getValidationErrors() method
      * @param array $data
      * @return boolean
      */
     public function validateMany(array $data);

     /**
      * Validates a multidimensional
      * It also stores all the errors. Then you can retrieve them with the
      * getValidationErrors() method
      * Same af validate but specify the rules, instead of using the repository rules
      * @param array $data
      * @param array $rules
      * @return boolean
      */
     public function validateManyWithCustomRules(array $data, array $rules);

     /**
      * Validates a multidimensional or throws exception
      * It also stores all the errors. Then you can retrieve them with the
      * getValidationErrors() method
      * @param array $data
      * @throws DataNotValidException
      */
     public function validateManyOrFail(array $data);

     /**
      * Validates a multidimensional or throws exception
      * It also stores all the errors. Then you can retrieve them with the
      * getValidationErrors() method
      * @param array $data
      * @param array $rules
      * @throws DataNotValidException
      */
     public function validateManyWithCustomRulesOrFail(array $data, array $rules);

    /**
     * Returns the errors generated by the validate methods
     * with the keys "messages" and "failed"
     * If you used validateMany it will be a multidimensional array
     * @return array
     */
    public function getValidationErrors();

    /**
    * Return the messages key from the getValidationErrors method
    * If used after validateMany it will be a multidimensional array
    * @return array
    */
    public function getValidationMessages();

    /**
    * Return the failed key from the getValidationErrors method
    * If used after validateMany it will be a multidimensional array
    * @return array
    */
    public function getValidationFailures();

     /**
      * @param int $id
      * @return boolean
      */
     public function delete($id);

     /**
      * @param int $id
      * @throw EntityNotFoundException
      */
     public function deleteOrFail($id);

     /**
      * Delete all the rows
      * @return int Number of deleted rows
      */
     public function deleteAll();

Timestamps

You can add timestamps adding the $timestamps = true property:

class DownloadRepository extends LaravelRepository
{
    protected $timestamps = true;
}

By default it will manage created_at and updated_at fields. You can override the created and updated fields using the following properties:

protected $stamp_create = 'created_at';
protected $stamp_update = 'updated_at';

Validation

You can validate your data with the validate methods. Specify the rules of your repository in the rules property:

class UserArrayRepository extends LaravelRepository {

    protected $connection = 'default';
    protected $tableName  = 'users';
    protected $rules      = array(
        'name' => 'required|min:5|unique:users',
        'age'  => 'required|integer|between:0,120',
    );
}

Examples:

if ($this->arrayRepository->validate($validData)) {
    //The data is valid
}
else {
    //The data is not valid
    //You can get the validation failed or messages this way:
    $errors = $this->arrayRepository->getValidationErrors()
    //$errors['messages'] Contains an array with the validation messages
    //$errors['failed'] Contains an array with the failed rules
}

Extending the repository with your custom methods

You can grab the builder instance with

$this->getBuilder()

to create your custom repository methods.

Example

class MyUserRepository extends LaravelRepository
{
    public function getActiveUsers()
    {
        $result = $this->getBuilder()->whereActive(true)->get();

        //Builder returns an objects array so you can use the following method
        //to convert an array of objets to array
        return $this->objectsToArray($result);
    }
}

A best practice would be to create a new interface for MyUsersRepository with all of the new methods that you are going to add.

interface MyUserRepositoryInterface
{
    /**
    * @return array
    */
    public function getActiveUsers();
}

And then implement it on your repository:

class MyUserRepository extends LaravelRepository implements MyUserRepositoryInterface
{
    /**
    * {@inheritdoc}
    */
    public function getActiveUsers()
    {
        $result = $this->getBuilder()->whereActive(true)->get();

        //In order to return the data with the current fetcher use the getFetcher() method like this:
        return $this->getFetcher()->fetchMany($result);

        //If you are returning only one entity use fetch method instead
        return $this->getFetcher()->fetch();
    }
}

So if you want to change the implementation you would need to implement the MyUsersRepositoryInterface and LaravelRepositoryInterface.

Future

More features coming soon