将Mimes字符串转为Mimes数组（逗号分隔）
@param $value
@return string[]|bool 如果至少有1个有效字符串, 返回字符串数组; 否则返回false
@throws ValidationException
559562
private static function _parseMimesArray($value)
    {
        $mimes = explode(',', $value);
        $strings = [];
        foreach ($mimes as $mime) {
            $mime = trim($mime);
            if (strlen($mime) === 0)
                continue;

            if (($pos = strpos($mime, '/')) === false) // 没有斜杠'/'. 例: jpg 或 avi
            {
                if ($mime === '*')
                    throw new ValidationException("无效的MIME“${mime}”");
                $mime = "/$mime";
            } else // 有斜杠'/'. 例: image/jpeg 或 video/*
            {
                $parts = explode('/', $mime);
                if (count($parts) !== 2)
                    throw new ValidationException("无效的MIME“${mime}”");
                $type = $parts[0];
                $ext = $parts[1];
                if (strlen($type) === 0 || strlen($ext) === 0 || $type === '*')
                    throw new ValidationException("无效的MIME“${mime}”");

                // 将形如"video/*"的转化为"video/"以方便后续处理
                if ($ext === '*')
                    $mime = "$type/";
            }

            $strings[] = $mime;
        }
        if (count($strings) === 0)
            return false;
        return $strings;
    }
1221721
public function isValid($value)
    {
        if ($this->skipValidation)
            return true;

        $datatype = $this->validation['datatype'];

        //NULL check, empty strings are considered null
        if(in_array($datatype, array('string', 'url', 'email', 'slug', 'slugwithslash', 'html', 'binary','json'))
          && strlen(trim($value)) == 0)
            $value = null;

        if ($this->validation['nullable'] === false && $value === null && $datatype != 'boolean') {
            $this->failureCode = 'nullable';
            $this->failureMessage = 'cannot be empty';
            return false;
        }

        //Nothing more to validate if the value is null...
        if ($value === null)
            return true;


        //Check date makes sense
        if ($datatype === 'date') {

            //todo: not sure how to check validity of date... it's already a DateTime instance.
            if (false) {
                $this->failureCode = 'invalid';
                $this->failureMessage = 'is an invalid date';
                return false;
            }
        }


        //Validate MIN
        $min = $this->validation['min'];

        if ($min != null) {
            if ($datatype === 'float') {
                if($value < floatval($min)) {
                    $this->failureCode = 'min';
                    $this->failureMessage = 'is less than the minimum value';
                    return false;
                }
            } else if ($datatype === 'int') {
                if($value < intval($min)) {
                    $this->failureCode = 'min';
                    $this->failureMessage = 'is less than the minimum value';
                    return false;
                }
            } else {

                if (is_string($value) && strlen($value) < intval($min)) {
                    $this->failureCode = 'minlength';
                    $this->failureMessage = 'must be at least '.($min).' characters';
                    return false;
                }
            }
        }


        //Validate MAX
        $max = $this->validation['max'];

        if ($max != null) {
            if ($datatype === 'float') {
                if($value > floatval($max)) {
                    $this->failureCode = 'max';
                    $this->failureMessage = 'is more than the maximum value';
                    return false;
                }
            } else if ($datatype === 'int') {
                if($value > intval($max)) {
                    $this->failureCode = 'max';
                    $this->failureMessage = 'is more than the maximum value';
                    return false;
                }
            } else {

                $maxbytes = intval($max);

                if(intval($max) < 255)
                {
                    // count characters

                    if (is_string($value) && StringUtils::charCount($value) > intval($max)) {
                        $this->failureCode = 'maxlength';
                        $this->failureMessage = 'must be a maximum of '.($max).' characters';
                        return false;
                    }

                    $maxbytes = 255;

                }

                // count bytes
                if (is_string($value) && StringUtils::byteCount($value) > intval($maxbytes)) {
                    $this->failureCode = 'maxlength';
                    $this->failureMessage = 'must be a maximum of '.($maxbytes).' bytes';
                    return false;
                }
            }
        }

        if($datatype === 'slug')
        {
            if(!SlugUtils::isSlug($value, false))
            {
                $this->failureCode = 'invalid';
                $this->failureMessage = 'is not a valid slug, cannot contain slashes';
                return false;
            }
        }

        if($datatype === 'slugwithslash')
        {
            if(!SlugUtils::isSlug($value, true))
            {
                $this->failureCode = 'invalid';
                $this->failureMessage = 'is not a valid slug';
                return false;
            }
        }

        if($datatype === 'url')
        {
            if(!URLUtils::isUrl($value))
            {
                $this->failureCode = 'invalid';
                $this->failureMessage = 'is not a valid URL';
                return false;
            }
        }

        if($datatype === 'email')
        {
            if(!EmailUtils::isEmailAddress($value))
            {
                $this->failureCode = 'invalid';
                $this->failureMessage = 'is not a valid email address';
                return false;
            }
        }

        if($datatype === 'json')
        {
            if(!JSONUtils::isValid($value)) //todo: get exact error message here
            {
                $this->failureCode = 'invalid';
                $this->failureMessage = 'is not a valid json string';
                return false;
            }
        }

        //Validate MATCH expression
        $match = $this->validation['match'];

        if ($match != null) {
            // Automatically escape unescaped slashes in the match before running it
            $match = preg_replace('/([^\\\\])\//', '$1\\/', $match);

            if (preg_match('/'.$match.'/s', $value) === 0) {
                $this->failureCode = 'invalid';
                //$this->failureMessage = 'is invalid (' . substr($value, 0, 255) . ')';
                $this->failureMessage = 'is invalid';
                return false;
            }
        }

        // Validate all custom functions
        foreach ( $this->validation['callback'] as $callback ) {
            if (!empty($callback) && call_user_func($callback, $value) === false) {
                $this->failureCode = $callback;
                $this->failureMessage = 'is invalid';
                return false;
            }
        }

        return true;
    }
677398
protected function _validate($value)
    {
        $ret = parent::_validate($value);

        if ($ret) {
            if (strlen($value) < self::$MIN_LENGTH) {
                $ret = false;
            }
        }

        return $ret;
    }
735805
protected function _validate($value)
    {
        $value = trim($value);
        return ((string) $value === $this->sanitize($value));
    }
1176978
public function convertFromJSCharcode($value)
    {
        $matches = array();
        // check if value matches typical charCode pattern
        if (preg_match_all('/(?:[\d+\-=\/ \*]+(?:\s?,\s?\d+)){4,}/ms', $value, $matches)) {
            $converted = '';
            $string    = implode(',', $matches[0]);
            $string    = preg_replace('/\s/', '', $string);
            $string    = preg_replace('/\w+=/', '', $string);
            $charcode  = explode(',', $string);
            foreach ($charcode as $char) {
                $char = preg_replace('/\W0/s', '', $char);
                if (preg_match_all('/\d*[+-\/\* ]\d+/', $char, $matches)) {
                    $match = preg_split('/(\W?\d+)/', implode('', $matches[0]), null, PREG_SPLIT_DELIM_CAPTURE);
                    if (array_sum($match) >= 20 && array_sum($match) <= 127) {
                        $converted .= chr(array_sum($match));
                    }
                } elseif (!empty($char) && $char >= 20 && $char <= 127) {
                    $converted .= chr($char);
                }
            }
            $value .= "\n" . $converted;
        }
        // check for octal charcode pattern
        if (preg_match_all('/(?:(?:[\\\]+\d+[ \t]*){8,})/ims', $value, $matches)) {
            $converted = '';
            $charcode  = explode('\\', preg_replace('/\s/', '', implode(',', $matches[0])));
            foreach (array_map('octdec', array_filter($charcode)) as $char) {
                if (20 <= $char && $char <= 127) {
                    $converted .= chr($char);
                }
            }
            $value .= "\n" . $converted;
        }
        // check for hexadecimal charcode pattern
        if (preg_match_all('/(?:(?:[\\\]+\w+\s*){8,})/ims', $value, $matches)) {
            $converted = '';
            $charcode  = explode('\\', preg_replace('/[ux]/', '', implode(',', $matches[0])));
            foreach (array_map('hexdec', array_filter($charcode)) as $char) {
                if (20 <= $char && $char <= 127) {
                    $converted .= chr($char);
                }
            }
            $value .= "\n" . $converted;
        }
        return $value;
    }
850270
public function validate($value) {
        $dateTime = \DateTime::createFromFormat($this->format, $value);
        if(!$dateTime instanceof \DateTime || $dateTime->format($this->format)!=$value) {
            return new ValidationResult(false, 'Invalid date');
        }

        return new ValidationResult(true);
    }
1240617
private function getSingleCharacterArray($value)
    {
        if (mb_strlen($value) == strlen($value)) {
            return str_split($value);
        }

        return preg_split('~~u', $value, -1, PREG_SPLIT_NO_EMPTY);
    }
1233806
public function isValid($value)
    {
        if (!is_scalar($value) || !is_numeric($value)) {
            $this->error(self::INVALID);

            return false;
        }

        $length = strlen($value);

        if ($length < 9) {
            $this->error(self::TOO_SHORT);

            return false;
        }

        if ($length > 9) {
            $this->error(self::TOO_LONG);

            return false;
        }

        if (!preg_match($this->pattern, $value, $matches)) {
            $this->error(self::NO_MATCH);

            return false;
        }

        // no digit group can be all 0's
        /** @var array $matches */
        foreach ($matches as $match) {
            if (0 === (int) $match) {
                $this->error(self::NO_MATCH);

                return false;
            }
        }

        // invalid first digit groups 666 or 900-999
        /** @noinspection TypeUnsafeComparisonInspection */
        if ($matches[1] == 666 || $matches[1] >= 900) {
            $this->error(self::NO_MATCH);

            return false;
        }

        // invalid for advertisements
        if ($value >= 987654320 && $value <= 987654329) {
            $this->error(self::NO_MATCH);

            return false;
        }
        // handle famous instance of 1938
        /** @noinspection TypeUnsafeComparisonInspection */
        if ($value == '078051120') {
            $this->error(self::NO_MATCH);

            return false;
        }

        return true;
    }
509857
public static function getIsValid($value)
    {
        return in_array(
            $value,
            [
                static::BOTTOM_CENTER,
                static::BOTTOM_LEFT,
                static::BOTTOM_RIGHT,
                static::LEFT_BOTTOM,
                static::LEFT_CENTER,
                static::LEFT_TOP,
                static::TOP_CENTER,
                static::TOP_LEFT,
                static::TOP_RIGHT
            ],
            false
        );
    }
963206
public function valid ($value)
    {
        $valid = true;
        $error = array();

        if ($this->minLength !== null && ! (strlen($value) >= $this->minLength)) {
            $valid = false;
            $error[] = 'minimum length';
        }

        if ($this->maxLength !== null && ! (strlen($value) <= $this->maxLength)) {
            $valid = false;
            $error[] = 'maximum length';
        }

        // var_dump(array('cur' => $value, 'min' => $this->minValue));
        if ($this->minValue !== null && (! is_numeric($value) || ! ($value >= $this->minValue))) {
            $valid = false;
            $error[] = 'minimum value';
        }

        if ($this->maxValue !== null && (! is_numeric($value) || ! ($value <= $this->maxValue))) {
            $valid = false;
            $error[] = 'maximum value';
        }

        if ($this->startsWith !== null && ! (strpos($value, $this->startsWith) === 0)) {
            $valid = false;
            $error[] = 'starts with';
        }

        if ($this->endsWith !== null && ! (strpos($value, $this->endsWith) === strlen($value)-strlen($this->endsWith))) {
            $valid = false;
            $error[] = 'ends with';
        }

        if ($this->regex !== null && ! preg_match($this->regex, $value)) {
            $valid = false;
            $error[] = 'valid custom field';
        }

        if ($this->enum !== null && is_array($this->enum)) {
            if (! in_array($value, $this->enum)) {
                $valid = false;
                $error[] = 'is value of list';
            }
        }

        if ($this->valid !== null) {
            switch ($this->valid) {
                case 'email':
                    if (! filter_var($value, FILTER_VALIDATE_EMAIL))
                        $valid = false;
                    break;
                case 'url':
                    if (! filter_var($value, FILTER_VALIDATE_URL))
                        $valid = false;
                    break;
                default:
                    $valid = false;
            }
        }
        if (! $valid) {
            $error[] = 'valid \'' . $this->valid . '\'';
        }

        return $valid === true ? true : $error;
    }
449557
public static function validateDate($value)
    {
        if (\is_string($value) && \strlen($value) > 0 && (
                false !== ($date1 = \DateTime::createFromFormat('Y-m-d\TH:i:sP', $value))
                || false !== \DateTime::createFromFormat('Y-m-d', $value)
            )
        ) {
            $format = 'Y-m-d';

            if (false !== $date1) {
                $format = 'c';
            }

            return \htmlentities((new \DateTime($value))->format($format));
        }

        return false;
    }
107090
public function validate($value)
    {
        $messenger = $this->messenger->clear();
        return array_reduce(
            $this->func,
            function (&$result, $func) use ($value, $messenger) {
                return $result && $func($value, $messenger);
            },
            true
        );
    }
822230
public static function validValue($value): bool
    {
        $enumerables = self::getCache();
        $values = array_values($enumerables);

        return \in_array($value, $values, true);
    }
142546
public static function isValid($value)
    {
        if (is_string($value)) {
            $value = [ $value ];
        }
        
        if (!is_array($value) || count($value) === 0) {
            return false;
        }

        foreach ($value as $ep) {
            if (!is_string($ep)) {
                return false;
            }
            $type = self::getType($ep);

            if ($type === null) {
                return false;
            }
        }
        return true;
    }
126182
public function normalize($value)
    {
        if (is_null($value) || $value === '') {
            return;
        }
        if (is_bool($value)) {
            return $value;
        }

        if ($value === $this->valueTrue) {
            return true;
        }

        if ($value === $this->valueFalse) {
            return false;
        }

        if (is_numeric($value)) {
            return (bool) $value;
        }

        throw new ValidationException([$this->name => 'Must be a boolean value']);
    }
879189
public function validateValue($value)
    {
        // make sure string length is limited to avoid DOS attacks
        $valid = is_string($value) && strlen($value) <= 254 && (preg_match($this->pattern, $value) || $this->allowName && preg_match($this->fullPattern, $value));
        return $valid;
    }
1159397
public function validate($value)
    {
        $value = trim($value);
        $pattern = '/^[a-z]{2}-[A-Z]{2}$/';
        return (empty($value))
                || (is_string($value) && preg_match($pattern, $value) == 1);
    }