Validate a rule against a custom registered expression

@param $field
@param $value
@param $rule
@param null $message
@return bool
187240
private function validateAgainstExpression( $field, $value, $rule, $message = null )
    {
        if (preg_match($this->expressions[ $rule ], $value)) {
            return true;
        }

        if (!$message) {
            $message = $this->error_messages[ $rule ];
        }

        static::$errors[ $field ] = $message;

        return false;
    }
1062335
protected function validateSlug($value, string $parameter, array $args = [], array $rules = null, array $context = null): ?string
    {
        //$value = $this->getValue($parameter);

        if ($value === null) {
            return null;
        }

        if (!is_scalar($value)) {
            return Message::getMessage(Message::PRIMITIVE, [
                'param' => $parameter
            ]);
        }

        $value = trim((string)$value);

        if ($value === '') {
            return null;
        }

        if (!Validate::isSlug($value)) {
            return Message::getMessage(Message::SLUG, [
                'param' => $parameter,
                'value' => $value
            ]);
        }

        return null;
    }
1169667
public function rule($rule, $fields, ...$params)
    {
        if (\is_callable($rule)
            && !(\is_string($rule) && $this->hasValidator($rule))
        ) {
            $name = $this->getUniqueRuleName($fields);
            $message = $params[0] ?? null;
            $this->addInstanceRule($name, $rule, $message);
            $rule = $name;
        }

        $errors = $this->getRules();
        if (!isset($errors[$rule])) {
            $ruleMethod = 'validate' . \ucfirst($rule);
            if (!\method_exists($this, $ruleMethod)) {
                throw new \InvalidArgumentException("Rule '" . $rule . "' has not been registered with " . __CLASS__ . "::addRule().");
            }
        }

        // Ensure rule has an accompanying message
        $messages = $this->getRuleMessages();
        $message = $messages[$rule] ?? self::ERROR_DEFAULT;

        // Ensure message contains field label
        if (\strpos($message, '{field}') === false) {
            $message = '{field} ' . $message;
        }

        $this->_validations[] = [
            'rule' => $rule,
            'fields' => (array) $fields,
            'params' => (array) $params,
            'message' => '{field} ' . $message,
            'skip' => self::SKIP_CONTINUE,
        ];

        return $this;
    }
731477
public function validateDateTime($value, $errorMessage, $errorField = null)
    {
        if (!strtotime($value)) {
            $this->error($errorMessage);
            $this->_errorFields[] = $errorField;
        }
    }
38487
function message($rule, $error_message, $field = null) {
        $rule                                                 = explode(':', $rule);
        $rule                                                 = Arr::get($rule, 0);
        $this->messages[($field ? $field . '.' : '') . $rule] = $error_message;

        return $this;
    }
1326180
protected function _validate($attribute, $rule, $parameters, $value)
    {
        $srcValue = $value;

        if (isset($this->_validators[$rule])) {
            /** @noinspection VariableFunctionsUsageInspection */
            $value = call_user_func_array($this->_validators[$rule], [$value, $parameters]);
        } elseif (function_exists($rule)) {
            $value = call_user_func_array($rule, array_merge([$value], $parameters));
        } else {
            throw new FilterException(['`:name` rule is not be recognized', 'name' => $rule]);
        }

        if ($value === null) {
            if (is_string($this->_messages)) {
                if (strpos($this->_messages, '.') === false) {
                    $file = '@manaphp/Http/Validator/Messages/' . $this->_messages . '.php';
                } else {
                    $file = $this->_messages;
                }

                if (!$this->filesystem->fileExists($file)) {
                    throw new FilterException(['`:file` rule message template file is not exists', 'file' => $file]);
                }

                /** @noinspection PhpIncludeInspection */
                $this->_messages = require $this->alias->resolve($file);
            }

            if (isset($this->_messages[$rule])) {
                $message = [$this->_messages[$rule]];
            } else {
                $message = [$this->_defaultMessage];
            }

            $message['rule'] = $rule;
            $message['attribute'] = isset($this->_attributes[$attribute]) ? $this->_attributes[$attribute] : $attribute;
            $message['value'] = $srcValue;
            foreach ($parameters as $k => $parameter) {
                $message['parameters[' . $k . ']'] = $parameter;
            }

            throw new FilterException($message);
        }

        return $value;
    }
468164
protected function filterRegexp($field, $details, $label, $message) {
        if (preg_match($details['expression'], $this->getValue($field))) {
            return true;
        }
        throw new \Exception($message ? $message : $this->translate("Doesn't match expression!"));
    }
1015620
public function validate($value, Constraint $constraint)
    {
        if($value instanceof SubscriberInterface) {
            if($this->manager->exists($value, $value->getType())) {
                $this->context->buildViolation($constraint->message)
                    ->setParameter('%email%', $value->getEmail())
                    ->addViolation();
            }
        }
    }
978851
protected function validateBool($value, string $parameter, array $args = [], array $rules = null, array $context = null): ?string
    {
        //$value = $this->getValue($parameter);

        if ($value === null) {
            return null;
        }

        if (!is_scalar($value)) {
            return Message::getMessage(Message::PRIMITIVE, [
                'param' => $parameter
            ]);
        }

        if ($value === '') {
            return null;
        }

        if (!is_bool($value) && $value !== 'true' && $value !== 'false') {
            return Message::getMessage(Message::BOOL, [
                'param' => $parameter
            ]);
        }

        return null;
    }
1355082
static public function regex($value, $pattern, $message = null, $propertyPath = null)
        {
            static::string($value, $message);

            if ( ! preg_match($pattern, $value)) {
                $message = $message ?: sprintf(
                    'Value "%s" does not match expression.',
                    static::stringify($value)
                );

                throw static::createException($value, $message, static::INVALID_REGEX , $propertyPath, array('pattern' => $pattern));
            }
        }
763293
protected function validateHex($value, string $parameter, array $args = [], array $rules = null, array $context = null): ?string
    {
        //$value = $this->getValue($parameter);

        if ($value === null) {
            return null;
        }

        if (!is_scalar($value)) {
            return Message::getMessage(Message::PRIMITIVE, [
                'param' => $parameter
            ]);
        }

        $value = trim((string)$value);

        if ($value === '') {
            return null;
        }

        if ($value[0] == '-') {
            $value = substr($value, 1);
        }

        if (!ctype_xdigit($value)) {
            return Message::getMessage(Message::HEX, [
                'param' => $parameter,
                'value' => $value
            ]);
        }

        return null;
    }
89079
function ruleExists($tablename, $field = null, $value = null, $error_message = null) {
        $rule = 'exists:' . $tablename;
        if ($field) {
            $rule .= ',' . $field;
            if ($value) {
                $rule .= ',' . $value;
            }
        }

        return $this->_rule($rule, $error_message);
    }
334273
public function addValidationMessage($rule, $message)
    {
        if (($pos = strpos($rule, ':')) !== false) {
            $rule = substr($rule, 0, $pos);
        }

        $this->validationMessages[$rule] = $message;

        return $this;
    }
1339588
public function run( $field, $value , $message = null)
    {
        $this->field = $field;
        $this->message = $message;

        if(preg_match($this->pattern, $value)){
            return true;
        }

        return false;
    }
651257
protected function prepareProps($field, $rule)
    {
        $props = $messages = array();

        $props['plugins/app/js/validation'] = $this;

        // Prepare name for validator
        if (isset($this->names[$field])) {
            $props['name'] = $this->names[$field];
        }

        /**
         * Prepare messages for validator
         *
         * The messages array may look like below
         * array(
         *     // Case 1
         *     'field' => 'message',
         *     // Case 2
         *     'field2' => array(
         *         'rule' => 'message'
         *     ),
         *     // Case 2
         *     'field3' => array(
         *         'rule' => array(
         *            'option' => 'message',
         *            'option2' => 'message',
         *         )
         *     )
         * )
         *
         * In case 2, checking non-numeric offsets of strings would return true
         * in PHP 5.3, while return false in PHP 5.4, so we do NOT known
         * $messages is array or string
         * @link http://php.net/manual/en/function.isset.php
         *
         * In case 1, $messages is string
         */
        // Case 2
        if (isset($this->messages[$field][$rule]) && is_array($this->messages[$field])) {
            $messages = $this->messages[$field][$rule];
        // Case 1
        } elseif (isset($this->messages[$field]) && is_scalar($this->messages[$field])) {
            $messages = $this->messages[$field];
        }

        // Convert message to array for validator
        if (is_scalar($messages)) {
            $props['message'] =  $messages;
        } elseif (is_array($messages)) {
            foreach ($messages as $name => $message) {
                $props[$name . 'Message'] = $message;
            }
        }

        return $props;
    }
1340335
public function mapFieldRules(string $field, array $rules)
    {
        foreach ($rules as $rule) {
            //rule must be an array
            $rule = (array) $rule;

            //First element is the name of the rule
            $name = \array_shift($rule);

            //find a custom message, if any
            $message = null;
            if (isset($rule['message'])) {
                $message = $rule['message'];
                unset($rule['message']);
            }

            //Add the field and additional parameters to the rule
            $this->rule($name, $field, ...$rule);

            if (!empty($message)) {
                $this->message($message);
            }
        }
    }
587115
public function validateNotBlank($value, $errorMessage, $errorField = null)
    {
        if (Strings::isBlank($value)) {
            $this->error($errorMessage);
            $this->_errorFields[] = $errorField;
        }
    }