swoft 自定义验证器规则

示例:
//Mapping

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

className, $this->propertyName, $annotationObject);
        return [];
    }
}

//Rule

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;
    }
}

你可能感兴趣的:(swoft 自定义验证器规则)