Files
kiri-validator/TypesOfValidator.php
T

126 lines
3.0 KiB
PHP
Raw Normal View History

2022-01-09 14:01:11 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/4 0004
* Time: 18:44
*/
declare(strict_types=1);
namespace validator;
class TypesOfValidator extends BaseValidator
{
2023-09-12 20:52:18 +08:00
const JSON = 'json';
const FLOAT = 'float';
const ARRAY = 'array';
const STRING = 'string';
const INTEGER = 'integer';
private ?int $min = null;
private ?int $max = null;
/** @var array */
public array $types = [
self::JSON => 'json',
self::FLOAT => 'float',
self::ARRAY => 'array',
self::STRING => 'string',
self::INTEGER => 'integer'
];
/** @var string */
public string $method;
2022-01-09 14:01:11 +08:00
2023-07-07 17:24:03 +08:00
/**
* @return bool
*/
2023-09-12 20:52:18 +08:00
public function trigger(): bool
{
return $this->_validator($this->field, function ($field, $params, $method, $types) {
if (!in_array($method, $types)) {
return true;
}
$value = $params[$field] ?? null;
return match ($method) {
self::INTEGER => $this->integerFormat($field, $value),
self::FLOAT => $this->floatFormat($field, $value),
self::JSON => $this->jsonFormat($field, $value),
self::STRING => $this->stringFormat($field, $value),
self::ARRAY => $this->arrayFormat($field, $value),
};
}, $this->params, $this->method, $this->types);
}
2022-01-09 14:01:11 +08:00
2023-07-07 17:24:03 +08:00
/**
* @param $field
* @param $value
* @return bool
*/
2023-09-12 20:52:18 +08:00
public function jsonFormat($field, $value): bool
{
2023-10-08 17:55:11 +08:00
if (!is_string($value) || is_null(json_decode($value))) {
2023-09-12 20:52:18 +08:00
return $this->addError($field, 'The ' . $field . ' not is JSON data.');
}
return true;
}
2022-01-09 14:01:11 +08:00
2023-07-07 17:24:03 +08:00
/**
* @param $field
* @param $value
* @return bool
*/
2023-09-12 20:52:18 +08:00
public function arrayFormat($field, $value): bool
{
if (!is_array($value)) {
return $this->addError($field, 'The ' . $field . ' not is array data.');
}
return true;
}
2022-01-09 14:01:11 +08:00
2023-07-07 17:24:03 +08:00
/**
* @param $field
* @param $value
* @return bool
*/
2023-09-12 20:52:18 +08:00
public function stringFormat($field, $value): bool
{
2023-07-07 17:24:03 +08:00
if (!is_string($value)) {
2023-09-12 20:52:18 +08:00
return $this->addError($field, 'The ' . $field . ' not is string data.');
}
return true;
}
2022-01-09 14:01:11 +08:00
2023-07-07 17:24:03 +08:00
/**
* @param $field
* @param $value
* @return bool
*/
2023-09-12 20:52:18 +08:00
public function integerFormat($field, $value): bool
{
if ((int)$value != $value) {
return $this->addError($field, 'The ' . $field . ' not is number data.');
}
return true;
}
2022-01-09 14:01:11 +08:00
2023-07-07 17:24:03 +08:00
/**
* @param $field
* @param $value
* @return bool
*/
2023-09-12 20:52:18 +08:00
public function floatFormat($field, $value): bool
{
$trim = (float)$value;
if ($trim != $value) {
return $this->addError($field, 'The ' . $field . ' not is float data.');
}
return true;
}
2022-01-09 14:01:11 +08:00
}