美文网首页
swoft 自定义验证器规则

swoft 自定义验证器规则

作者: 金星show | 来源:发表于2019-11-22 11:56 被阅读0次

示例:
//Mapping

<?php declare(strict_types=1);

namespace App\Annotation\Mapping;

use Doctrine\Common\Annotations\Annotation\Attribute;
use Doctrine\Common\Annotations\Annotation\Attributes;
use Doctrine\Common\Annotations\Annotation\Target;
use Swoft\Validator\Annotation\Mapping\Type;

/**
 * Class IsInteger
 *
 * @since 2.0
 *
 * @Annotation
 * @Target("PROPERTY")
 * @Attributes({
 *     @Attribute("message", type="string"),
 *     @Attribute("fields", type="array")
 * })
 */
class IsList extends Type
{
    /**
     * @var string
     */
    private $message = '';

    /**
     * @var string
     */
    private $name = '';

    private $fields = [];
    
    /**
     * StringType constructor.
     *
     * @param array $values
     */
    public function __construct(array $values)
    {
        if (isset($values['value'])) {
            $this->message = $values['value'];
        }
        if (isset($values['message'])) {
            $this->message = $values['message'];
        }
        if (isset($values['name'])) {
            $this->name = $values['name'];
        }

        if (isset($values['fields'])) {
            $fields = [];
            foreach ($values['fields'] as $field) {
                $field = explode(":", $field);
                $fields[$field[0]] = [
                    'type' => $field[1],
                    'required' => isset($field[2]) && $field[2] ? 1 : 0
                ];
            }
            $this->fields = $fields;
        }
    }

    /**
     * @return string
     */
    public function getMessage(): string
    {
        return $this->message;
    }

    public function getFields(): array 
    {
        return $this->fields;
    }

    /**
     * @return string
     */
    public function getName(): string
    {
        return $this->name;
    }
}

//Parser

<?php declare(strict_types=1);

namespace App\Annotation\Parser;

use ReflectionException;
use Swoft\Annotation\Annotation\Mapping\AnnotationParser;
use Swoft\Annotation\Annotation\Parser\Parser;
use App\Annotation\Mapping\IsList;
use Swoft\Validator\Exception\ValidatorException;
use Swoft\Validator\ValidatorRegister;

/**
 * Class IsIntegerParser
 *
 * @AnnotationParser(IsList::class)
 */
class IsListParser extends Parser
{
    /**
     * @param int $type
     * @param object $annotationObject
     *
     * @return array
     * @throws ReflectionException
     * @throws ValidatorException
     */
    public function parse(int $type, $annotationObject): array
    {
        if ($type != self::TYPE_PROPERTY) {
            return [];
        }
        ValidatorRegister::registerValidatorItem($this->className, $this->propertyName, $annotationObject);
        return [];
    }
}

//Rule

<?php declare(strict_types=1);

namespace App\Validator\Rule;

use App\Annotation\Mapping\IsList;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Validator\Contract\RuleInterface;
use Swoft\Validator\Exception\ValidatorException;

/**
 * Class IsIntegerRule
 *
 * @Bean(IsList::class)
 */
class IsListRule implements RuleInterface
{
    /**
     * @param array $data
     * @param string $propertyName
     * @param object $item
     * @param null $default
     *
     * @return array
     * @throws ValidatorException
     */
    public function validate(array $data, string $propertyName, $item, $default = null): array
    {
        $message = $item->getMessage();

        if (!isset($data[$propertyName]) && $default !== null) {
            $data[$propertyName] = $default;
            return $data;
        }
        
        $list = $data[$propertyName];
        if (!is_array($list)) {
            throw new ValidatorException($message);
        }
        
        $fields = $item->getFields();
        foreach ($list as $item) {
            foreach ($fields as $key => $val) {
                //必须参数检测
                if ($val['required'] && !isset($item[$key])) {
                    throw new ValidatorException($message . " item[{$key}] not set");
                }
                //类型检测
                if (isset($item[$key]) && $val['type'] != gettype($item[$key])) {
                    throw new ValidatorException($message . " item[{$key}] type error");
                }
            }
        }
        
        return $data;
    }
}

相关文章

  • swoft 自定义验证器规则

    示例://Mapping //Parser //Rule

  • thinkphp5.1

    一、验证器 官方文档:验证器官方文档:验证场景 验证器定义 数据验证 批量验证 抛出验证异常 自定义验证规则

  • laravel之验证

    控制器验证 $this->validate(验证参数,[验证规则],自定义错误消息提示); 首次验证失败后中止后续...

  • laravel中遇到的问题

    1、关于自定义验证规则 在自定义验证规则中,如果自定义验证规则的名称使用了驼峰结构,例:'oldpassword'...

  • layui自定义验证

    自带验证 lay-verify 自定义验证规则

  • 验证器与静态代理

    静态代理 自定义验证器 调用验证器

  • Jquery validation

    前端表单验证框架 页面引入 默认校验规则 封装后检验器 自定义rules举例(校验用户长度等) Example

  • Odin Inspector 系列教程 --- 自定义验证

    前言:前一阵子笔者写了验证器入门指南和 验证器配置文件设置与使用这次笔者将介绍自定义全局类型验证和自定义特性验证,...

  • 输入和验证

    输入和验证 [TOC] 1.使用Validate的子类定义规则 Users控制器: User验证器(验证器的名字要...

  • Laravel Validator自定义参数验证规则

    自定义参数验证规则 public Validator make(array $data, array $rules...

网友评论

      本文标题:swoft 自定义验证器规则

      本文链接:https://www.haomeiwen.com/subject/njifwctx.html