承接 konsulting/laravel-rule-repository 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

konsulting/laravel-rule-repository

Composer 安装命令:

composer require konsulting/laravel-rule-repository

包简介

A package to allow rules (for example validation rules) to be attached to a model.

README 文档

README

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');

统计信息

  • 总下载量: 258
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-01-15

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固