承接 suomec/ovalidator 相关项目开发

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

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

suomec/ovalidator

最新稳定版本:v1.0.3

Composer 安装命令:

composer require suomec/ovalidator

包简介

Objects validator and properties mapper for PHP

README 文档

README

This package created for mapping user input to pre-defined objects with validation schema (custom rules).

Usually you have user input as array and need to map/validate it to your value-objects. This package can help you.

First, you need a Form - class, that contains user raw data. It's /src/Form.php class.

$form = (new Form())->fromArray([
    'field_1' => 'any value',
    'field_2' => 15,
]);

Second, you need an object, with internal properties. It's just an instance of your class.

class Input
{
    public int $field_2;
}

$input = new Input();

Third, you need a set of rules to map input to that class. It's a Config. Rules are set of ->add() methods

$config = (new Config())
    ->add('field_2', 'Some description', State::Required, [
        new VInteger(),
        new VMin(10),
        new VMax(20),
    ])
;

And the last - some code for validation and map

$result = (new Mapper($form, $config))->toObject($input, new ReflectionSetter());

If you have an input: ['field_2' => 15] it's correct and $input->field_2 will have a value of 15. But for input ['field_2' => 999] $result variable will contain a list of errors (VMax check fails, because 999 > 20).

Installation

Run: composer require suomec/ovalidator

Validators

Builtin validator are described in validators.md file. You can create own validator which should implement Validator interface.

Setters

Setters are special objects which map validated input to your object. There are two default setters - Direct and Reflection-based. First setter just apply input to object via $object->$property without extended checks. Second setter checks types of properties via reflection and supported interfaces if property is another object.

Localization

Localization files are located in /etc/ folder. There is a class for quick validation run: OValidator::validateAndSet() It creates localization object from default file loc-en.php. You can pass path to your own file if needed.

Examples

You can find more examples at the /examples/ dir. There are a lot of different cases where that mapper and validator can be used. Fell free to create issue for any questions or improvements.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-08-03