Coder Social home page Coder Social logo

konsulting / laravel-rule-repository Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 0.0 40 KB

A package to centralise the storage and organisation of validation rules, whilst avoiding storing them on the model or controller.

Home Page: https://www.klever.co.uk

License: MIT License

PHP 100.00%
laravel laravel-validation model-view-controller validation

laravel-rule-repository's Introduction

Laravel Rule Repository

Build Status Code Coverage Scrutinizer Code Quality

A package to allow rules (for example validation rules) to be attached to a model, whilst avoiding storing them on the model itself or in a controller.

Installation

Install via Composer:

composer require konsulting/laravel-rule-repository

Usage

Each model under validation should have its own validation repository attached to it, containing default validation rules and, optionally, validation rules for different states. You may, for example, require different validation rules when updating the model as opposed to creating it.

Creating the repository

The validation repository must implement Contracts\RuleRepository, and as such must contain a default() method which returns an array of default rules. It may also contain any number of 'state' methods which contain differing rules.

Note:

  • State methods names should use camel case.
  • State-specific rules will be merged (non-recursively) with the default rules when they are retrieved.

Extending the AbstractRepository class

The AbstractRepository class is provided with some helper functions to make defining rules easier. This class may be extended instead of directly implementing the interface.

Sometimes it's useful to append or prepend a rule to an existing list of rules, e.g. making values required only on model creation. This is possible with the following methods:

AbstractRepository::prependRule(string $rule, array $baseRules);
AbstractRepository::prependRules(array $rules, array $baseRules);

AbstractRepository::appendRule(string $rule, array $baseRules);
AbstractRepository::appendRules(array $rules, array $baseRules);

Example repository:

use Konsulting\Laravel\RuleRepository\AbstractRepository;

class UserRuleRepository extends AbstractRepository
{
    public function default() : array
    {
        return [
            'name'          => 'string',
            'email'         => 'string|email',
            'date_of_birth' => 'date',
        ];
    }
    
    public function create() : array
    {
        return $this->prependRule('required', $this->default());
    }
}

Attaching the repository to the model

The model to attach the rules to should use the RuleRepositoryTrait trait. It is recommended (but not required) that the model implements the interface Contracts\HasRuleRepositories.

The static property $ruleRepositories should be initialised to an associative array of repository class paths, with the repository name as the key.

use Konsulting\Laravel\RuleRepository\Contracts\HasRuleRepository;
use Konsulting\Laravel\RuleRepository\RuleRepositoryTrait;

class User extends Model implements HasRuleRepository
{
    use RuleRepositoryTrait;

    protected static $ruleRepositories = [
        'validation' => UserValidationRepository::class;
    ];
}

Retrieving validation rules

The model's validation rules can be retrieved using the static method getRules($name, $state = 'default').

To return the default rules for the validation repository:

User::getRules('validation');

// or

User::getRules('validation', 'default');

To get state-specific rules:

User::getRules('validation', 'update');

User::getRules('validation', $state);

States may be named in either camel-case or snake-case.

Using magic methods

The RepositoryMagicMethods trait may also be added to the model to allow rules to be retrieved with a more concise syntax. Rules may be retrieved using:

Model::{$repositoryName . 'Rules'}($state = 'default');

For example:

User::validationRules();

User::validationRules('update');

laravel-rule-repository's People

Contributors

keoghan avatar php-shift avatar rdarcy1 avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.