Files
kiri-validator/TypesOfValidator.php
T

151 lines
3.2 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
{
const JSON = 'json';
const FLOAT = 'float';
const ARRAY = 'array';
const STRING = 'string';
const INTEGER = 'integer';
const SERIALIZE = 'serialize';
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',
self::SERIALIZE => 'serialize',
];
/** @var string */
public string $method;
/**
* @return bool
*/
public function trigger(): bool
{
2022-02-18 17:16:45 +08:00
return $this->_validator($this->field, function ($field, $params, $method, $types) {
if (!in_array($method, $types)) {
return true;
}
if (!isset($params[$field])) {
return true;
}
$value = $params[$field] ?? null;
if (empty($value)) {
2022-02-25 17:12:15 +08:00
return $this->addError($field, 'This ' . $field . ' is not an empty data.');
2022-02-18 17:16:45 +08:00
}
return $this->{$method . 'Format'}($field, $value);
}, $this->params, $this->method, $this->types);
2022-01-09 14:01:11 +08:00
}
/**
2022-02-18 17:16:45 +08:00
* @param $field
2022-01-09 14:01:11 +08:00
* @param $value
* @return bool
*/
2022-02-18 17:16:45 +08:00
public function jsonFormat($field, $value): bool
2022-01-09 14:01:11 +08:00
{
if (!is_string($value) || is_numeric($value)) {
2022-02-25 17:12:15 +08:00
return $this->addError($field, 'The ' . $field . ' not is JSON data.');
2022-01-09 14:01:11 +08:00
}
if (is_null(json_decode($value))) {
2022-02-25 17:12:15 +08:00
return $this->addError($field, 'The ' . $field . ' not is JSON data.');
2022-01-09 14:01:11 +08:00
}
return true;
}
/**
2022-02-18 17:16:45 +08:00
* @param $field
2022-01-09 14:01:11 +08:00
* @param $value
* @return bool
*/
2022-02-18 17:16:45 +08:00
public function serializeFormat($field, $value): bool
2022-01-09 14:01:11 +08:00
{
if (!is_string($value) || is_numeric($value)) {
2022-02-25 17:12:15 +08:00
return $this->addError($field, 'The ' . $field . ' not is serialize data.');
2022-01-09 14:01:11 +08:00
}
if (false === swoole_unserialize($value)) {
2022-02-25 17:12:15 +08:00
return $this->addError($field, 'The ' . $field . ' not is serialize data.');
2022-01-09 14:01:11 +08:00
}
return true;
}
/**
2022-02-18 17:16:45 +08:00
* @param $field
2022-01-09 14:01:11 +08:00
* @param $value
* @return bool
*/
2022-02-18 17:16:45 +08:00
public function arrayFormat($field, $value): bool
2022-01-09 14:01:11 +08:00
{
if (!is_array($value)) {
2022-02-25 17:12:15 +08:00
return $this->addError($field, 'The ' . $field . ' not is array data.');
2022-01-09 14:01:11 +08:00
}
return true;
}
/**
2022-02-18 17:16:45 +08:00
* @param $field
2022-01-09 14:01:11 +08:00
* @param $value
* @return bool
*/
2022-02-18 17:16:45 +08:00
public function stringFormat($field, $value): bool
2022-01-09 14:01:11 +08:00
{
if (is_array($value) || is_object($value) || is_bool($value)) {
2022-02-25 17:12:15 +08:00
return $this->addError($field, 'The ' . $field . ' not is string data.');
2022-01-09 14:01:11 +08:00
}
return true;
}
/**
2022-02-18 17:16:45 +08:00
* @param $field
2022-01-09 14:01:11 +08:00
* @param $value
* @return bool
*/
2022-02-18 17:16:45 +08:00
public function integerFormat($field, $value): bool
2022-01-09 14:01:11 +08:00
{
if (!is_numeric($value)) {
2022-02-25 17:12:15 +08:00
return $this->addError($field, 'The ' . $field . ' not is number data.');
2022-01-09 14:01:11 +08:00
}
if ((int)$value != $value) {
2022-02-25 17:12:15 +08:00
return $this->addError($field, 'The ' . $field . ' not is number data.');
2022-01-09 14:01:11 +08:00
}
return true;
}
/**
2022-02-18 17:16:45 +08:00
* @param $field
2022-01-09 14:01:11 +08:00
* @param $value
* @return bool
*/
2022-02-18 17:16:45 +08:00
public function floatFormat($field, $value): bool
2022-01-09 14:01:11 +08:00
{
$trim = (float)$value;
2022-02-18 17:16:45 +08:00
if ($trim != $value) {
2022-02-25 17:12:15 +08:00
return $this->addError($field, 'The ' . $field . ' not is float data.');
2022-01-09 14:01:11 +08:00
}
return true;
}
}