star/validator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

star/validator

Composer 安装命令:

composer require star/validator

包简介

Validator implementation lib

README 文档

README

Build Status

Validation tool that can be connected on any object you want to validate.

Usage

Validators

Validators are classes that will determines if the value was valid. In the following example, the YourNonEmptyValidator would make sure the name on the SomeObject cannot be empty.

// YourNonEmptyValidator.php
class YourNonEmptyValidator implements Validator
{
    /**
     * @var SomeObject
     */
    private $object;

    /**
     * @param SomeObject $object
     */
    public function __construct(SomeObject $object)
    {
        $this->object = $object;
    }

    /**
     * @param NotificationHandler $handler
     *
     * @return ValidationResult
     */
    public function validate(NotificationHandler $handler)
    {
        $name = $this->object->getName();

        if (empty($name)) {
            $handler->notifyError(new StringMessage('Name cannot be empty.'));
        }

        return $handler->createResult();
    }
}

The validators will return a ValidationResult to determine whether any constraints have failed.

NotificationHandler

The NotificationHandler are responsible to notify the user about an error.

The only supported strategy currently implemented are:

  • ExceptionNotificationHandler: Will throw a ValidationErrorException on the first error (recommended for development).
  • DeferredNotificationHandler: Will keep the trace of all errors for future use by the ValidationResult.

Example

Given you created a class that needs to be validated:

// SomeObject.php
class SomeObject
{
    /**
     * @var string
     */
    private $name;

    public function setName($name)
    {
        $this->name = $name;
    }

    public function getName()
    {
        return $this->name;
    }
}

You will need to implement a validation method in which you will put all the validators to push in the ValidationHandler.

// SomeObject.php
...
/**
 * @param NotificationHandler $handler
 *
 * @return ValidationResult
 */
public function validate(NotificationHandler $handler)
{
    $validator = new YourNonEmptyValidator($this); // This is your custom validator implementing Validator interface

    return $validator->validate($handler);
}
...

Creating the validator for your object (or using built-in validators, you can get the result from the validation.

By using the structure, validating your code will look something like this.

// Using the Exception handler on a valid object
$validObject = new SomeObject();
$validObject->setName('non-empty');
$result = $validObject->validate(new ExceptionNotificationHandler());
$result->hasErrors(); // Returns false
$result->getErrors()); // Returns empty array(), since there was no errors

// Using the Exception handler on a invalid object
$invalidObject = new SomeObject();
$invalidObject->validate(new ExceptionNotificationHandler()); // Would throw an exception on the first error (because of the handler).

// Using the deferred handler on a invalid object
$result = $invalidObject->validate(new DeferredNotificationHandler());
$result->hasErrors(); // Returns true
$result->getErrors()); // Returns empty array('Name cannot be empty.')

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-10-24

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固