takemo101/simple-prop-check
Composer 安装命令:
composer require takemo101/simple-prop-check
包简介
Simple Prop Check
README 文档
README
The Simple Prop Check is a simple property validator.
Enjoy!
Installation
Execute the following composer command.
composer require takemo101/simple-prop-check
How to use
Please use as follows
PHP Attribute
Validate properties using PHP's Attribute feature.
<?php use Takemo101\SimplePropCheck\PropCheckFacade; use Takemo101\SimplePropCheck\Preset\String\ { Email, Between, Pattern, }; use Takemo101\SimplePropCheck\Preset\Numeric\Min; use Takemo101\SimplePropCheck\Preset\Array\TypedValue; use Takemo101\SimplePropCheck\Preset\NotEmpty; /** * You can check the value by setting Attribute to the property of the class. */ class Test { #[Email] public static string $email = 'xxx@example.com', public function __construct( #[Between(1, 10)] private string $between, // Exception message can also be set. #[Pattern('/[a]+/', 'not match')] private string $pattern, #[Min(3)] private integer $min, // Validate array values by type. #[TypedValue('integer|string')] private array $nums, #[NotEmpty] private $notEmpty = null, ) { // } } $test = new Test( 'hello', 'are', 4, [ 1, 2, 'hello', ] ); // Validate the property by passing the object to the check method. // The result is true or false. $result = PropCheckFacade::check($test); // $result == false // By passing an object to the exception method, the validation result will be returned as an exception. PropCheckFacade::exception($test); // throw exception
Property Attribute provided
The following Attribute class is available.
| attribute class | detail |
|---|---|
| Takemo101\SimplePropCheck\Preset\String\URL | Validate URL string |
| Takemo101\SimplePropCheck\Preset\String\Domain | Validate domain hostname string |
| Takemo101\SimplePropCheck\Preset\String\IP | Validate ip string |
| Takemo101\SimplePropCheck\Preset\String\Email | Validate the email address string |
| Takemo101\SimplePropCheck\Preset\String\BetweenLength | Verify the number of characters |
| Takemo101\SimplePropCheck\Preset\String\MaxLength | Verify the number of characters |
| Takemo101\SimplePropCheck\Preset\String\MinLength | Verify the number of characters |
| Takemo101\SimplePropCheck\Preset\String\Pattern | Validate regular expressions |
| Takemo101\SimplePropCheck\Preset\Array\BetweenSize | Validate the size of the array |
| Takemo101\SimplePropCheck\Preset\Array\MaxSize | Validate the size of the array |
| Takemo101\SimplePropCheck\Preset\Array\MinSize | Validate the size of the array |
| Takemo101\SimplePropCheck\Preset\Array\Unique | Verify array duplication |
| Takemo101\SimplePropCheck\Preset\Array\TypedKey | Validate the key type of the array |
| Takemo101\SimplePropCheck\Preset\Array\TypedValue | Validate the value type of the array |
| Takemo101\SimplePropCheck\Preset\Array\TypedMap | Validate array key and value types |
| Takemo101\SimplePropCheck\Preset\Array\Each | Validate the array of each data |
| Takemo101\SimplePropCheck\Preset\Numeric\Between | Verify the range of numbers |
| Takemo101\SimplePropCheck\Preset\Numeric\Max | Verify the range of numbers |
| Takemo101\SimplePropCheck\Preset\Numeric\Min | Verify the range of numbers |
| Takemo101\SimplePropCheck\Preset\Numeric\Negative | Validate negative numbers |
| Takemo101\SimplePropCheck\Preset\Numeric\Positive | Validate positive numbers |
| Takemo101\SimplePropCheck\Preset\Property\GreaterThan | Validate value is greater than other property |
| Takemo101\SimplePropCheck\Preset\Property\GreaterThanOrEqual | Validate value is greater than other property |
| Takemo101\SimplePropCheck\Preset\Property\LessThan | Validate value is less than other property |
| Takemo101\SimplePropCheck\Preset\Property\LessThanOrEqual | Validate value is less than other property |
| Takemo101\SimplePropCheck\Preset\Property\NotEquals | Validate that other properties and values are not equal |
| Takemo101\SimplePropCheck\Preset\Includes | Validate if value is included |
| Takemo101\SimplePropCheck\Preset\NotIncludes | Validate if value is not included |
| Takemo101\SimplePropCheck\Preset\NotNull | Validate null value |
| Takemo101\SimplePropCheck\Preset\NotEmpty | Validate empty value |
Customize
You can customize the Attribute class etc.
How to customize Property Attribute
First, create an Attribute class that implements AbstractValidatable or Validatable.
<?php use Takemo101\SimplePropCheck\AbstractValidatable; /** * Implement the AbstractValidatable class by extending it, * or implement the Validatable interface. * * @extends AbstractValidatable<string> */ #[Attribute(Attribute::TARGET_PROPERTY)] class MatchText extends AbstractValidatable { /** * constructor * * @param string|null $message */ public function __construct( private ?string $message = null, ) { // } /** * Verify the value of $data with the verify method. * Returns true if the value is not invalid. * * @param string $data * @return boolean returns true if the data is OK */ public function verify($data): bool { return $data == $this->text; } /** * Returns an error message if the value of the property is incorrect * * @return string */ public function message(): string { // You can use the value set by the placeholders method in the error message as a placeholder. return $this->message ?? "data dose not match :text"; } /** * Returns the value available in the error message placeholder. * * @return array<string,mixed> */ public function placeholders(): array { return [ 'text' => $this->text, // The placeholder will be ':text' ]; } /** * Return whether the value of the property is verifiable. * Basically check the value type. * * @param mixed $data * @return bool */ public function canVerified($data): bool { return is_string($data); } }
Use the created Attribute class as follows.
<?php use Takemo101\SimplePropCheck\PropCheckFacade; class Test { public function __construct( #[MatchText('hello')] private string $hello, ) { // } } $test = new Test('hi'); $result = PropCheckFacade::check($test); // $result == false $test = new Test('hello'); $result = PropCheckFacade::check($test); // $result == true
How to customize Exception Attribute
First, create an Attribute class that implements AbstractException or ExceptionFactory.
<?php use Throwable; use LogicException; use Takemo101\SimplePropCheck\Exception\AbstractException; /** * Implement the AbstractException class by extending it, * or implement the ExceptionFactory interface. */ #[Attribute(Attribute::TARGET_PROPERTY)] class TestException extends AbstractException { /** * Generate an exception in the factory method and return. * * @param string $message * @return Throwable */ public function factory(string $message): Throwable { return new LogicException("property logic error: {$message}"); } }
Use the created Attribute class as follows.
<?php use Takemo101\SimplePropCheck\PropCheckFacade; class Test { public function __construct( // You can set an exception to throw for the property you want to validate. #[MatchText('hello')] #[TestException] private string $hello, ) { // } } $test = new Test('hi'); PropCheckFacade::exception($test); // throw LogicException
About the Effect class
The Effect attribute allows you to apply a validation effect to the property of interest.
<?php use Takemo101\SimplePropCheck\Preset\NotEmpty; use Takemo101\SimplePropCheck\ { PropCheckFacade, Effect, }; class First { public function __construct( #[NotEmpty] private string $text, // Validate the object. #[Effect] private Second $second, ) {} } class Second { public function __construct( #[NotEmpty] private string $text, // Apply validation to object array. #[Effect] private array $third, ) {} } class Third { public function __construct( #[NotEmpty] private string $text, ) {} } $first = new First( 'text', new Second( 'text', [ new Third( 'text', ), new Third( 'text', ), // Invalid validation of this object new Third( '', ), ], ), ); $result = PropCheckFacade::check($first); // $result == false PropCheckFacade::exception($first); // throw exception
About the AfterCall class
The AfterCall attribute class allows you to set the method to be called after validating the value of the property.
<?php use Takemo101\SimplePropCheck\Preset\NotEmpty; use Takemo101\SimplePropCheck\ { PropCheckFacade, AfterCall, }; // Set the method name in the argument of AfterCall attribute class. #[AfterCall('print')] class CallClass { public function __construct( #[NotEmpty] private string $text, ) {} private function print(): void { echo 'call'; } } $call = new CallClass('text'); // If the value validation is successful, the specified method will be executed. $result = PropCheckFacade::check($call); // $result == true $call = new CallClass(''); // If the value validation fails, the specified method will not be executed. $result = PropCheckFacade::check($call); // $result == false
takemo101/simple-prop-check 适用场景与选型建议
takemo101/simple-prop-check 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 839 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 03 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「package」 「php」 「validation」 「attribute」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 takemo101/simple-prop-check 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 takemo101/simple-prop-check 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 takemo101/simple-prop-check 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Adds request-parameter validation to the SLIM 3.x PHP framework
A jQuery augmented PHP library for creating and validating HTML forms
Simple ASCII output of array data
A Laravel validator for delimiter-separated list of emails.
A CakePHP behavior to validate foreign keys
统计信息
- 总下载量: 839
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-03-02