digitalrevolution/symfony-console-validation
Composer 安装命令:
composer require digitalrevolution/symfony-console-validation
包简介
Symfony command input argument and option validation
README 文档
README
Symfony Console Validation
An input validation component for Symfony Console. Ease the validation of input arguments and options.
Installation
Include the library as dependency in your own project via:
composer require digitalrevolution/symfony-console-validation
Usage
- Create your own
ExampleInputclass which extends theAbstractValidatedInputclass. - Configure your own
ValidationRules. See the Validation shorthand library for more information about the rules. - Ensure the
InputValidatorclass is registered as service in your Symfony project.
use DigitalRevolution\SymfonyConsoleValidation\AbstractValidatedInput; use DigitalRevolution\SymfonyConsoleValidation\ValidationRules; class ExampleInput extends AbstractValidatedInput { public static function getValidationRules(): ValidationRules { return new ValidationRules([ 'arguments' => [ 'email' => 'required|string|email' ], 'options' => [ 'projectId' => 'int:min:1' ] ]); } public function getEmail(): string { return $this->input->getArgument('email'); } public function getProjectId(): ?int { $value = $this->input->getOption('projectId'); return $value === null ? null : (int)$value; } }
All that remains is using your ExampleInput class in your Command to validate the input.
class ExampleCommand extends Command { public function __construct(private InputValidator $inputValidator, ?string $name = null) { parent::__construct($name); } protected function execute(InputInterface $input, OutputInterface $output): int { // third argument will throw exception if input is invalid. Set to `false` if you want to handle the validation yourself. $validatedInput = $this->inputValidator->validate($input, ExampleInput::class, true); ... } }
Manual invalid input handling
The validate method will by default throw a ViolationException. To handle the violations yourself:
class ExampleCommand extends Command { ... protected function execute(InputInterface $input, OutputInterface $output): int { $validatedInput = $this->inputValidator->validate($input, ExampleInput::class, false); if ($validatedInput->isValid() === false) { $violations = $validatedInput->getViolations(); ... } ... } }
About us
At 123inkt (Part of Digital Revolution B.V.), every day more than 50 development professionals are working on improving our internal ERP and our several shops. Do you want to join us? We are looking for developers.
digitalrevolution/symfony-console-validation 适用场景与选型建议
digitalrevolution/symfony-console-validation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 52.88k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2022 年 07 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 digitalrevolution/symfony-console-validation 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 digitalrevolution/symfony-console-validation 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 52.88k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-07-04