示例:
//Mapping
<?php declare(strict_types=1);
namespace App\Annotation\Mapping;
use Doctrine\Common\Annotations\Annotation\Attribute;
use Doctrine\Common\Annotations\Annotation\Attributes;
use Doctrine\Common\Annotations\Annotation\Target;
use Swoft\Validator\Annotation\Mapping\Type;
/**
* Class IsInteger
*
* @since 2.0
*
* @Annotation
* @Target("PROPERTY")
* @Attributes({
* @Attribute("message", type="string"),
* @Attribute("fields", type="array")
* })
*/
class IsList extends Type
{
/**
* @var string
*/
private $message = '';
/**
* @var string
*/
private $name = '';
private $fields = [];
/**
* StringType constructor.
*
* @param array $values
*/
public function __construct(array $values)
{
if (isset($values['value'])) {
$this->message = $values['value'];
}
if (isset($values['message'])) {
$this->message = $values['message'];
}
if (isset($values['name'])) {
$this->name = $values['name'];
}
if (isset($values['fields'])) {
$fields = [];
foreach ($values['fields'] as $field) {
$field = explode(":", $field);
$fields[$field[0]] = [
'type' => $field[1],
'required' => isset($field[2]) && $field[2] ? 1 : 0
];
}
$this->fields = $fields;
}
}
/**
* @return string
*/
public function getMessage(): string
{
return $this->message;
}
public function getFields(): array
{
return $this->fields;
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
}
//Parser
<?php declare(strict_types=1);
namespace App\Annotation\Parser;
use ReflectionException;
use Swoft\Annotation\Annotation\Mapping\AnnotationParser;
use Swoft\Annotation\Annotation\Parser\Parser;
use App\Annotation\Mapping\IsList;
use Swoft\Validator\Exception\ValidatorException;
use Swoft\Validator\ValidatorRegister;
/**
* Class IsIntegerParser
*
* @AnnotationParser(IsList::class)
*/
class IsListParser extends Parser
{
/**
* @param int $type
* @param object $annotationObject
*
* @return array
* @throws ReflectionException
* @throws ValidatorException
*/
public function parse(int $type, $annotationObject): array
{
if ($type != self::TYPE_PROPERTY) {
return [];
}
ValidatorRegister::registerValidatorItem($this->className, $this->propertyName, $annotationObject);
return [];
}
}
//Rule
<?php declare(strict_types=1);
namespace App\Validator\Rule;
use App\Annotation\Mapping\IsList;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Validator\Contract\RuleInterface;
use Swoft\Validator\Exception\ValidatorException;
/**
* Class IsIntegerRule
*
* @Bean(IsList::class)
*/
class IsListRule implements RuleInterface
{
/**
* @param array $data
* @param string $propertyName
* @param object $item
* @param null $default
*
* @return array
* @throws ValidatorException
*/
public function validate(array $data, string $propertyName, $item, $default = null): array
{
$message = $item->getMessage();
if (!isset($data[$propertyName]) && $default !== null) {
$data[$propertyName] = $default;
return $data;
}
$list = $data[$propertyName];
if (!is_array($list)) {
throw new ValidatorException($message);
}
$fields = $item->getFields();
foreach ($list as $item) {
foreach ($fields as $key => $val) {
//必须参数检测
if ($val['required'] && !isset($item[$key])) {
throw new ValidatorException($message . " item[{$key}] not set");
}
//类型检测
if (isset($item[$key]) && $val['type'] != gettype($item[$key])) {
throw new ValidatorException($message . " item[{$key}] type error");
}
}
}
return $data;
}
}
网友评论