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;
|
|
|
|
|
|
|
|
|
|
|
2023-12-18 15:01:09 +08:00
|
|
|
use function json_validate;
|
|
|
|
|
|
2022-01-09 14:01:11 +08:00
|
|
|
class TypesOfValidator extends BaseValidator
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
2023-12-18 15:01:09 +08:00
|
|
|
const string JSON = 'json';
|
|
|
|
|
const string FLOAT = 'float';
|
|
|
|
|
const string ARRAY = 'array';
|
2023-12-12 15:35:34 +08:00
|
|
|
const string STRING = 'string';
|
|
|
|
|
const string INTEGER = 'integer';
|
2023-09-12 20:52:18 +08:00
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
|
public string $method;
|
|
|
|
|
|
2023-07-07 17:24:03 +08:00
|
|
|
/**
|
2023-12-18 15:01:09 +08:00
|
|
|
* @param string $field
|
|
|
|
|
* @param mixed $value
|
2023-07-07 17:24:03 +08:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2023-12-19 14:51:13 +08:00
|
|
|
public function trigger(mixed $value): bool
|
2023-09-12 20:52:18 +08:00
|
|
|
{
|
2023-12-18 15:01:09 +08:00
|
|
|
return match ($this->method) {
|
|
|
|
|
self::INTEGER => $this->integerFormat($value),
|
|
|
|
|
self::FLOAT => $this->floatFormat($value),
|
|
|
|
|
self::JSON => $this->jsonFormat($value),
|
|
|
|
|
self::STRING => $this->stringFormat($value),
|
|
|
|
|
self::ARRAY => $this->arrayFormat($value),
|
|
|
|
|
};
|
2023-09-12 20:52:18 +08:00
|
|
|
}
|
2022-01-09 14:01:11 +08:00
|
|
|
|
2023-07-07 17:24:03 +08:00
|
|
|
/**
|
2023-12-18 15:01:09 +08:00
|
|
|
* @param string|null $value
|
2023-07-07 17:24:03 +08:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2023-12-18 15:01:09 +08:00
|
|
|
public function jsonFormat(?string $value): bool
|
2023-09-12 20:52:18 +08:00
|
|
|
{
|
2023-12-18 15:01:09 +08:00
|
|
|
return json_validate($value);
|
2023-09-12 20:52:18 +08:00
|
|
|
}
|
2022-01-09 14:01:11 +08:00
|
|
|
|
2023-07-07 17:24:03 +08:00
|
|
|
/**
|
2023-12-18 15:01:09 +08:00
|
|
|
* @param mixed $value
|
2023-07-07 17:24:03 +08:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2023-12-18 15:01:09 +08:00
|
|
|
public function arrayFormat(mixed $value): bool
|
2023-09-12 20:52:18 +08:00
|
|
|
{
|
2023-12-18 15:01:09 +08:00
|
|
|
return is_array($value);
|
2023-09-12 20:52:18 +08:00
|
|
|
}
|
2022-01-09 14:01:11 +08:00
|
|
|
|
2023-07-07 17:24:03 +08:00
|
|
|
/**
|
2023-12-18 15:01:09 +08:00
|
|
|
* @param mixed $value
|
2023-07-07 17:24:03 +08:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2023-12-18 15:01:09 +08:00
|
|
|
public function stringFormat(mixed $value): bool
|
2023-09-12 20:52:18 +08:00
|
|
|
{
|
2023-12-18 15:01:09 +08:00
|
|
|
return is_string($value);
|
2023-09-12 20:52:18 +08:00
|
|
|
}
|
2022-01-09 14:01:11 +08:00
|
|
|
|
2023-07-07 17:24:03 +08:00
|
|
|
/**
|
2023-12-18 15:01:09 +08:00
|
|
|
* @param mixed $value
|
2023-07-07 17:24:03 +08:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2023-12-18 15:01:09 +08:00
|
|
|
public function integerFormat(mixed $value): bool
|
2023-09-12 20:52:18 +08:00
|
|
|
{
|
2023-12-18 15:01:09 +08:00
|
|
|
return (int)$value == $value;
|
2023-09-12 20:52:18 +08:00
|
|
|
}
|
2022-01-09 14:01:11 +08:00
|
|
|
|
2023-07-07 17:24:03 +08:00
|
|
|
/**
|
2023-12-18 15:01:09 +08:00
|
|
|
* @param mixed $value
|
2023-07-07 17:24:03 +08:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2023-12-18 15:01:09 +08:00
|
|
|
public function floatFormat(mixed $value): bool
|
2023-09-12 20:52:18 +08:00
|
|
|
{
|
2023-12-18 15:01:09 +08:00
|
|
|
return (float)$value == $value;
|
2023-09-12 20:52:18 +08:00
|
|
|
}
|
2022-01-09 14:01:11 +08:00
|
|
|
|
|
|
|
|
}
|