krubio/perfect-validation
Composer 安装命令:
composer require krubio/perfect-validation
包简介
SOLID Compliant OOP Validation
关键字:
README 文档
README
Validator Class Documentation
The Validator class is responsible for validating data using different validation strategies. It allows you to set and switch between different validation strategies based on your specific requirements.
Class Signature
class Validator { private ValidationStrategy $strategy; public function __construct(ValidationStrategy $strategy); public function setValidationStrategy(ValidationStrategy $strategy): void; public function validateData($data): mixed; public function validateDataWithErrors($data): array; }
Constructor
__construct(ValidationStrategy $strategy)
The constructor accepts a ValidationStrategy object as a parameter, which determines the specific validation strategy to use. The initial validation strategy is set during object creation.
Public Methods
setValidationStrategy(ValidationStrategy $strategy): void
This method allows you to set a new validation strategy by providing a ValidationStrategy object as a parameter. It replaces the existing validation strategy with the new one.
validateData($data): mixed
The validateData method is used to perform data validation using the current validation strategy. It accepts the data to be validated as a parameter and returns the validation result.
validateDataWithErrors($data): array
The validateDataWithErrors method performs data validation and returns an array containing the validation status and any errors, if applicable. If the strategy object has a getErrors method, the errors will be included in the array.
-
Parameters:
$data: The data to be validated.
-
Return:
- Returns the validation result, which depends on the specific validation strategy being used. It can be a boolean value (
truefor a successful validation,falseotherwise) or other data types based on the strategy implementation or an array with the validation status and errors, if any.
- Returns the validation result, which depends on the specific validation strategy being used. It can be a boolean value (
Usage Example
Here's an example that demonstrates the basic usage of the Validator class:
// Create a validator with a specific validation strategy $validator = new Validator($requiredFieldsStrategy); // Set a new validation strategy if needed $validator->setValidationStrategy($customValidationStrategy); // Perform data validation $data = $_POST; // Example data to be validated $result = $validator->validateData($data); // Perform data validation with errors $resultWithErrors = $validator->validateDataWithErrors($data); // Check the validation result if ($result === true) { echo "Validation passed! The data is valid."; } elseif (isset($resultWithErrors['errors'])) { echo "Validation failed! Errors: " . implode(', ', $resultWithErrors['errors']); } else { echo "Validation failed! Please check the data for errors."; }
In the example above:
- The
Validatorclass is instantiated with an initial validation strategy ($requiredFieldsStrategy). - If needed, you can use the
setValidationStrategymethod to switch to a different validation strategy. - The
validateDataandvalidateDataWithErrorsmethods are called with the data to be validated ($data). - The returned results are checked to determine if the validation passed or failed.
- Based on the validation results, you can perform appropriate actions or display error messages.
Note: The specific validation strategies used ($requiredFieldsStrategy and $customValidationStrategy) are not shown in the example, as they depend on your specific implementation.
krubio/perfect-validation 适用场景与选型建议
krubio/perfect-validation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 34 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 09 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「validaton」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 krubio/perfect-validation 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 krubio/perfect-validation 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 krubio/perfect-validation 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
统计信息
- 总下载量: 34
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-09-21