Check that the spec matches and overlay help messaged.

The resulting SpecResult instance should have more user-friendly
messages. This allows one to use Specs for validation on a website or
even an API.

@param array $input

@return SpecResult
179293
public function check(array $input)
    {
        $result = $this->spec->check($input);

        return new SpecResult(
            $result->getMissing(),
            Arr::walkCopy(
                $result->getFailed(),
                function ($key, $value, &$array, $path) {
                    $array[$key] = Std::coalesce(
                        Std::firstBias(
                            Arr::dotGet(
                                $this->messages,
                                Std::nonempty($path, $key)
                            ) !== null,
                            [Arr::dotGet(
                                $this->messages,
                                Std::nonempty($path, $key)
                            )],
                            null
                        ),
                        Std::firstBias(
                            $value instanceof AbstractConstraint,
                            function () use ($value) {
                                return $value->getDescription();
                            },
                            null
                        ),
                        Std::firstBias(
                            is_array($value),
                            function () use ($value) {
                                return array_map(
                                    function (AbstractConstraint $item) {
                                        return $item->getDescription();
                                    },
                                    $value
                                );
                            },
                            $value
                        )
                    );
                },
                true,
                '',
                false
            ),
            $result->getStatus()
        );
    }
1015636
private function validateStatementClass(array $input)
    {
        if (!isset($input[0])) {
            Exception::newStatementClassException();
        }

        $statementClass = $input[0];

        if (!class_exists($statementClass)) {
            Exception::newStatementClassException();
        }

        return $statementClass;
    }
492598
public function getValidator(array $input)
    {
        if (!array_key_exists($this->column, $input)) {
            return false;
        }

        $input = Arr::only($input, $this->column);

        $form = $this->buildNestedForm($this->column, $this->builder);

        $rules = $attributes = [];

        /* @var Field $field */
        foreach ($form->fields() as $field) {
            if (!$fieldRules = $field->getRules()) {
                continue;
            }

            $column = $field->column();

            if (is_array($column)) {
                foreach ($column as $key => $name) {
                    $rules[$name.$key] = $fieldRules;
                }

                $this->resetInputKey($input, $column);
            } else {
                $rules[$column] = $fieldRules;
            }

            $attributes = array_merge(
                $attributes,
                $this->formatValidationAttribute($input, $field->label(), $column)
            );
        }

        Arr::forget($rules, NestedForm::REMOVE_FLAG_NAME);

        if (empty($rules)) {
            return false;
        }

        $newRules = [];
        $newInput = [];

        foreach ($rules as $column => $rule) {
            foreach (array_keys($input[$this->column]) as $key) {
                $newRules["{$this->column}.$key.$column"] = $rule;
                if (isset($input[$this->column][$key][$column]) &&
                    is_array($input[$this->column][$key][$column])) {
                    foreach ($input[$this->column][$key][$column] as $vkey => $value) {
                        $newInput["{$this->column}.$key.{$column}$vkey"] = $value;
                    }
                }
            }
        }

        if (empty($newInput)) {
            $newInput = $input;
        }

        return Validator::make($newInput, $newRules, $this->validationMessages, $attributes);
    }
604933
protected function verify($input, $signature)
    {
        $algo = $this->algos[$this->algo];

        // HMAC SHA.
        if (\substr($this->algo, 0, 2) === 'HS') {
            return \hash_equals($this->urlSafeEncode(\hash_hmac($algo, $input, $this->key, true)), $signature);
        }

        $this->validateKey();

        $pubKey = \openssl_pkey_get_details($this->key)['key'];

        return \openssl_verify($input, $this->urlSafeDecode($signature, false), $pubKey, $algo) === 1;
    }
1011414
public function ok(&$input): bool
    {
        $input = trim((string) $input);
        $inputLabel = trim($this->fieldLabel ?: 'Field');

        // check required issue
        if ($input === '' && $this->isRequired) {
            $this->fail = sprintf('%s is required.', $inputLabel);
            return false;
        }

        // assing default to input but do not return
        // true to check also given default
        if ($input === '') {
            $input = $this->fieldDefault;
        }

        // skip if null given as default
        // that also checks given default
        if ($input === null && !$this->isRequired) {
            return true;
        }

        // valide by field type
        switch ($this->fieldType) {
            case Validation::TYPE_INT:
            case Validation::TYPE_FLOAT:
                if (!is_numeric($input)) {
                    $this->fail = sprintf('%s value must be type of %s.', $inputLabel, $this->fieldType);
                    return false;
                }

                // sanitize
                $input = ($this->fieldType == Validation::TYPE_INT) ? intval($input) : floatval($input);

                // make unsigned
                if ($this->isUnsigned) {
                    $input = abs($input);
                }

                // check limit(s)
                if ($this->limit !== null) {
                    if (is_numeric($this->limit) && strval($input) !== strval($this->limit)) {
                        $this->fail = sprintf('%s value could be only %s.', $inputLabel, $this->limit);
                        return false;
                    }
                    if ($this->limitMin !== null && $input < $this->limitMin) {
                        $this->fail = sprintf('%s value could be minimum %s.', $inputLabel, $this->limitMin);
                        return false;
                    }
                    if ($this->limitMax !== null && $input > $this->limitMax) {
                        $this->fail = sprintf('%s value could be maximum %s.', $inputLabel, $this->limitMax);
                        return false;
                    }
                }
                break;
            case Validation::TYPE_NUMERIC:
                if (!is_numeric($input)) {
                    $this->fail = sprintf('%s value must be numeric.', $inputLabel);
                    return false;
                }
                // make unsigned
                if ($this->isUnsigned) {
                    $input = abs($input);
                }
                break;
            case Validation::TYPE_STRING:
                // check regexp if provided
                if ($this->specType == 'regexp' && !preg_match($this->spec, $input)) {
                    $this->fail = sprintf('%s value did not match with given pattern.', $inputLabel);
                    return false;
                }

                // check limit(s)
                if ($this->limit !== null) {
                    $isNumericLimit = is_numeric($this->limit);
                    // should truncate?
                    if ($this->isFixed) {
                        $input = mb_substr($input, 0, intval($isNumericLimit ? $this->limit : $this->limitMax));
                    }

                    $inputLength = strlen($input);
                    if ($isNumericLimit && $inputLength !== $this->limit) {
                        $this->fail = sprintf('%s value length must be %s.', $inputLabel, $this->limit);
                        return false;
                    }
                    if ($this->limitMin !== null && $inputLength < $this->limitMin) {
                        $this->fail = sprintf('%s value minimum length could be %s.', $inputLabel, $this->limitMin);
                        return false;
                    }
                    if ($this->limitMax !== null && $inputLength > $this->limitMax) {
                        $this->fail = sprintf('%s value maximum length could be %s.', $inputLabel, $this->limitMax);
                        return false;
                    }
                }
                break;
            case Validation::TYPE_BOOL:
                // set default bool spec
                if ($this->specType != 'array') {
                    $this->spec = ['true', 'false'];
                }

                if (!in_array($input, $this->spec)) {
                    $this->fail = sprintf('%s value could be one of %s options.', $inputLabel, join(', ', $this->spec));
                    return false;
                }
                break;
            case Validation::TYPE_ENUM:
                // @todo Multi-arrays?
                if (!in_array($input, $this->spec)) {
                    $this->fail = sprintf('%s value could be one of %s options.', $inputLabel, join(', ', $this->spec));
                    return false;
                }
                break;
            case Validation::TYPE_EMAIL:
                if (!filter_var($input, FILTER_VALIDATE_EMAIL)) {
                    $this->fail = sprintf('%s value must be a valid email address.', $inputLabel);
                    return false;
                }
                break;
            case Validation::TYPE_DATE:
            case Validation::TYPE_DATETIME:
                if ($this->specType == 'regexp' && !preg_match($this->spec, $input)) {
                    $this->fail = sprintf('%s value did not match with given pattern.', $inputLabel);
                    return false;
                }

                if ($input != date($this->spec, strtotime($input))) {
                    $this->fail = sprintf('%s value is not valid date/datetime.', $inputLabel);
                    return false;
                }
                break;
            case Validation::TYPE_URL:
                if ($this->specType == 'regexp' && !preg_match($this->spec, $input)) {
                    $this->fail = sprintf('%s value did not match with given pattern.', $inputLabel);
                    return false;
                }
                if ($this->specType == 'array') {
                    // remove silly empty components (eg: path always comes even url is empty)
                    $url = array_filter((array) parse_url($input), 'strlen');
                    $components = [];
                    foreach ($this->spec as $component) {
                        if (!isset($url[$component])) {
                            $components[] = $component;
                        }
                    }
                    if (!empty($components)) {
                        $this->fail = sprintf('%s value is not a valid URL (missing components: %s).',
                            $inputLabel, join(', ', $components));
                        return false;
                    }
                    return true;
                }
                if (!filter_var($input, FILTER_VALIDATE_URL)) {
                    $this->fail = sprintf('%s value is not a valid URL.', $inputLabel);
                    return false;
                }
                break;
        }

        // seems nothing wrong
        return true;
    }
1141391
public function validate(array $input, array $ruleset)
    {
        $this->errors = array();

        foreach ($ruleset as $field => $rules) {
            #if(!array_key_exists($field, $input))
            #{
            #   continue;
            #}

            $rules = explode('|', $rules);

            if (in_array('required', $rules) || (isset($input[$field]) && trim($input[$field]) != '')) {
                foreach ($rules as $rule) {
                    $method = null;
                    $param = null;

                    if (strstr($rule, ',') !== false) {
                        // has params

                        $rule = explode(',', $rule);
                        $method = 'validate_'.$rule[0];
                        $param = $rule[1];
                        $rule = $rule[0];
                    } else {
                        $method = 'validate_'.$rule;
                    }

                    if (is_callable(array($this, $method))) {

                        $result = $this->$method($field, $input, $param);

                        if (is_array($result)) {
                            // Validation Failed

                            $this->errors[] = $result;
                        }
                    } elseif (isset($this->validation_methods[$rule])) {
                        if (isset($input[$field])) {
                            $result = call_user_func($this->validation_methods[$rule], $field, $input, $param);


                            if (!$result) {
                                // Validation Failed

                                $this->errors[] = array(
                                    'field' => $field,
                                    'value' => $input[$field],
                                    'rule' => $method,
                                    'param' => $param,
                                );
                            }
                        }
                    } else {
                        throw new Exception("Validator method '$method' does not exist.");
                    }
                }
            }
        }

        $this->compileMessage();

        return $this;
    }
1368854
public function verifySalt($input)
    {
        $valid = false;
        $matches = array ();
        if (1 === preg_match('/^\$pbkdf2(?:-(?P<digest>sha256|sha512))?\$(?P<rounds>\d+)\$(?P<salt>[\.\/0-9A-Za-z]{0,1366})\$?/', $input, $matches)) {
            $digest = $matches['digest'] ?: self::DIGEST_SHA1;
            $rounds = $matches['rounds'];
            $salt = $matches['salt'];

            $digestValid = false;
            if (in_array($digest, array (self::DIGEST_SHA1, self::DIGEST_SHA256, self::DIGEST_SHA512))) {
                $digestValid = true;
            }

            $roundsValid = false;
            if ($rounds[0] != '0' && $rounds >= 1 && $rounds <= 4294967296) {
                $roundsValid = true;
            }

            if ($digestValid && $roundsValid) {
                $valid = true;
            }
        }

        return $valid;
    }
264683
protected function validate(array $input, array &$messages)
    {
        if (!call_user_func_array($this->validationCallback, [$input, &$messages])) {
            if ($this->messageId) {
                $messages[] = new Message($this->messageId, $this->messageParameters);
            }
        }
    }
730268
public function check($input)
    {
        $this->lastValue = $input;
        $anyFailed = false;
        foreach ($this->rules as $ruleName => &$rule) {

            // at least OK
            if ($rule->check($input)) {

                // stop here if any is OK or rule is sufficient
                if ($this->matchAny || $rule->isSufficient()) {
                    return true;
                }
            }

            // if not in match any mode -> first fail stops
            elseif (!$this->matchAny) {
                $this->failedRule = &$rule;
                return false;
            }

            // at least one failed
            else {
                if (!$anyFailed) {
                    $this->failedRule = &$rule;
                }
                $anyFailed = true;
            }
        }

        // all have to work out!
        return !$anyFailed;
    }
329010
protected function handleEditable(array $input = [])
    {
        if (array_key_exists('_editable', $input)) {
            $name = $input['name'];
            $value = $input['value'];

            Arr::forget($input, ['pk', 'value', 'name']);
            Arr::set($input, $name, $value);
        }

        return $input;
    }
123783
public function execute(Input $input): Input
    {

        if (!$input->isEvaluableForTypes(...$this->types)) {
            return $input;
        }

        return $this->resolve($input);
    }
505615
public function validate($input): bool
    {
        if ($this->areAggregatorsDirty() === true) {
            $this->resetAggregators();
        }

        $this->validateSingleImplementation($input, $this->getCaptureAggregator(), $this->getErrorAggregator());
        $this->markAggregatorsAsDirty();

        $noErrors = $this->getErrorAggregator()->count() <= 0;

        return $noErrors;
    }
1282386
protected function validator($input)
    {
        $this->checkValue($input);

        if ($this->hasErrors()) {
            return '';
        }

        if (!$input) {
            if ($this->mandatory) {
                $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['mandatory'], $this->strLabel));
            }

            return '';
        } elseif (false === strpos($input, ',')) {
            return $this->multiple ? [(int) $input] : (int) $input;
        }
        $arrValue = array_map('intval', array_filter(explode(',', $input)));

        return $this->multiple ? $arrValue : $arrValue[0];
    }
524427
public function validate( $input )
    {
        // Check if we got readaccess to the provided file name at all.
        if ( !is_file( $input ) || !is_readable( $input ) )
        {
            throw new phpillowRuntimeException( 'Given image file not found: ' . $input );
        }

        // Use getimagesize to determine the filetype of the image, and compare
        // with whitelist of supported image formats.
        $imageData = getimagesize( $input );
        if ( ( $imageData === false ) ||
             ( !in_array( $imageData[2], $this->supportedImageFormats ) ) )
        {
            throw new phpillowValidationException( 'Unsupported image format provided.', array() );
        }

        // If all checks passed, we assume that this is a proper image file.
        return $input;
    }
1325422
protected function validate($input, array &$messages)
    {
        if (!preg_match(ColourStringParser::REGEX_RGBA, $input)) {
            $messages[] = new Message(self::MESSAGE);
        }
    }
1167006
public static function linkXHTMLSafe($input)
    {
        $regex = 'href="([^"]*(&(amp;){0})[^"]*)*?"';

        return preg_replace_callback(
            "#$regex#i",
            function ($m) {
                $rx = '&(?!amp;)';

                return preg_replace('#' . $rx . '#', '&amp;', $m[0]);
            },
            $input
        );
    }
109324
public function validate($input): bool
    {
        // for array
        if (\is_array($input)) {
            return \in_array($this->containsValue, $input, false);
        }
        // for string
        if (\is_string($input)) {
            return false !== mb_stripos($input, $this->containsValue, 0, mb_detect_encoding($input));
        }
        // can't compare
        return false;
    }