| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- <?php
- namespace App\Module\Protobuf\Validators;
- use UCore\Helper\Logger;
- use UCore\Validator;
- use Uraus\App\ACCOUNT_TYPE;
- /**
- * Protobuf枚举value有效验证
- *
- * @property array $args
- */
- class EnumValidator extends Validator
- {
- /**
- * 验证传入的value 是否为 传入了Protobuf枚举的的value
- * @param mixed $value 待验证的value,
- * @param array $data
- * @return bool
- */
- public function validate(mixed $value, array $data): bool
- {
- if (empty($this->args[0])) {
- throw new \Ucore\Exception\CodeException('Missing enum class name');
- }
- $className = $this->args[0];
- if (!class_exists($className)) {
- throw new \Ucore\Exception\CodeException("Enum class {$className} not found");
- }
- if (!property_exists($className, 'valueToName') || !method_exists($className, 'name')) {
- throw new \Ucore\Exception\CodeException("Invalid protobuf enum structure: {$className}");
- }
- try {
- $className::value($value);
- return true;
- } catch (\UnexpectedValueException $e) {
- Logger::error('e:'.$e->getMessage());
- return false;
- }
- }
- }
|