定制 phramework/validate 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

phramework/validate

最新稳定版本:1.3.0

Composer 安装命令:

composer require phramework/validate

包简介

phramework's validation library

README 文档

README

phramework's validation library https://phramework.github.io/validate/

Coverage Status Build Status StyleCI Stories in Ready

Usage

Require package using composer

composer require phramework/validate

Parse an integer value

require './vendor/autoload.php';

use \Phramework\Validate\IntegerValidator;

$validator = new IntegerValidator(-1, 1);

$value = $validator->parse('0');

var_dump($value);

The above example will output:

int(0)

Parse an object of strings

$personalInformationValidator = new ObjectValidator(
    (object) [
        'name' => new StringValidator(2, 30),
        'city' => new StringValidator(2, 30),
        'age' => new IntegerValidator(1, 200),
    ],
    ['name', 'city', 'age'], //required properties
    false //no additional properties allowed
);

$personalInformation = $validationModel->parse((object) [
    'name' => 'Jane Doe',
    'city' => 'Athens',
    'age' => 28
]);

print_r($personalInformation);

The above example will output:

stdClass Object
(
    [name] => Jane Doe
    [city] => Athens,
    [age] => 28
)

Validating an array of enum strings

    /*
     * A validator that allows you to pick one or two colors between blue, green and red
     */
    $colorsValidator = new ArrayValidator(
        1, //minItems
        2, //maxItems
        (new StringValidator()) //items
            ->setEnum([
                'blue',
                'green',
                'red',
            ]),
        true //unique items
    );

    /*
     * $parsedOneItem will be validated successfully
     */
    $parsedOneItem = $colorsValidator->parse(['blue']); //will be [blue]


    /*
     * $parsedTwoItems will be validated successfully
     */
    $parsedTwoItems = $colorsValidator->parse(['blue', 'red']); //will be [blue, red]

    /*
     * $resultOfZeroItemsStatus cannot be validated true the validator requires minItems of 1
     */
    $resultOfZeroItemsStatus = $colorsValidator->validate([]);
    $resultOfZeroItemsStatus->getStatus(); // will be false because validation failed
    /** @var \Phramework\Exceptions\IncorrectParameterException $exception in this case */
    $exception = $resultOfZeroItemsStatus->getException();
    $exception->getFailure(); // will be minItems

    /*
     * $resultOfIncorrectItemsStatus cannot be validated true because "yellow" is not an allowed item
     */
    $resultOfIncorrectItemsStatus = $colorsValidator->validate(['yellow']);
    $resultOfIncorrectItemsStatus->getStatus(); // will be false because validation failed
    /** @var \Phramework\Exceptions\IncorrectParameterException $exception in this case */
    $exception = $resultOfIncorrectItemsStatus->getException();
    $exception->getFailure(); // will be items

    /*
     * Following will throw \Phramework\Exceptions\IncorrectParameterException
     * with failure maxItems because validator requires maxItems 2
     */
    $colorsValidator
        ->parse([
            'blue',
            'green',
            'red'
        ]);

Check wiki for more examples.

Development

Install dependencies

composer update

Test and lint code

composer test
composer lint

Generate documentation

composer doc

License

Copyright 2015-2019 Xenofon Spafaridis

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

统计信息

  • 总下载量: 35.49k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 3
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 4
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2015-11-26