Bool validator.

@param mixed $data Data to validate
@param callable $filter Custom filter
@return bool
498073
public static function validateBoolean($data, callable $filter=null) {
        if ( is_bool($data) === false ) return false;
        return self::applyFilter($data, $filter);
    }
1086605
public static function is(Validator $validator, $data, $pattern, $rule, $parameters)
    {
        $is_a_func = sprintf('is_%s', $parameters[0]);
        if (!function_exists($is_a_func)) {
            return;
        }

        foreach (Validator::getValues($data, $pattern) as $attribute => $value) {
            // As "is:<type>" is validating value type only ignore null
            if (null === $value) {
                continue;
            }
            if (call_user_func($is_a_func, $value)) {
                continue;
            }

            $validator->addError($attribute, $rule, [':type' => $parameters[0]]);
        }
    }
1305363
public function validate($data, $validators, $throw = true)
    {
        $validators = $this->getValidators($validators);
        foreach ($validators as $validator) {
            $validator = explode(':', $validator);

            $functionParams = [
                $data,
                array_splice($validator, 1),
                $throw
            ];

            if (!array_key_exists($validator[0], $this->validators)) {
                throw new ValidationException('Validator %s does not exist!', $validator[0]);
            }
            $res = $this->validators[$validator[0]]->validate(...$functionParams);

            // If validation failed and we are not throwing, return error message string
            if ($res !== true && !$throw) {
                return $res;
            }
        }

        return true;
    }
1048110
public static function validateSerialized($data, callable $filter=null) {
        $decoded = @unserialize($data);
        if ( $decoded === false && $data != @serialize(false) ) return false;
        return self::applyFilter($data, $filter);
    }
256587
public function hasFilter($filter)
    {
        $key = is_callable($filter) ? 0 : 1;

        foreach ($this->filters as $tuple) {
            if ($tuple[$key] === $filter) {
                return true;
            }
        }

        return false;
    }
235828
private static function validateFilterItem($filter, $settings)
    {
        // Convert string to filter array.
        if (!is_array($filter)) {
            $filter = ['', $filter];
        }

        // Convert string to filter array.
        if (Arr::get($settings, 'filter') !== 'boolean' && count($filter) == 1) {
            array_unshift($filter, '');
        }

        // Split the filter array into operator, value1, value2
        $operator = Arr::get($filter, 0, '');
        $value_one = Arr::get($filter, 1, false);
        $value_two = Arr::get($filter, 2, false);

        // The wild-all setting was enabled.
        // Update value with all characters wildcarded.
        if (Arr::has($settings, 'enable.wild-all')) {
            self::applyWildAll($operator, $value_one);
        }

        self::checkInlineOperator($operator, $value_one, $settings);
        self::checkNullOperator($operator, $value_one);
        self::checkEmptyOperator($operator, $value_one);

        // Defaullt operator.
        if (empty($operator)) {
            $operator = self::getDefaultOperator(Arr::get($settings, 'filter'), $operator);
        }

        // Return filter as an associative array.
        $filter = [
            'operator'  => $operator,
            'method'    => 'where',
            'arguments' => [],
            'value_one' => $value_one,
            'value_two' => $value_two,
            'settings'  => $settings,
            'positive'  => true,
        ];

        // Update filter based on the filter being used.
        $validation_method = 'filterBy'.Str::studly(Arr::get($settings, 'filter'));

        if (method_exists(__CLASS__, $validation_method)) {
            $filter = self::{$validation_method}($filter);
        }

        if ($filter === false) {
            return $filter;
        }

        // Update based on operator.
        $filter['positive'] = !(stripos($operator, '!') !== false || stripos($operator, 'NOT') !== false);

        return $filter;
    }
353087
private function applyFilter(Filter $filter)
    {
        list($filtered, $errors) = $filter->apply($this->value);
        if (count($errors) === 0) {
            return $filtered;
        }

        foreach ($errors as $error) {
            $this->paramErrors->append($this->paramName, $error);
        }

        return null;
    }
849538
def Validate(self):
    """"""
    if self.type not in filters.Filter.classes:
      raise DefinitionError("Undefined filter type %s" % self.type)
    self._filter.Validate(self.expression)
    Validate(self.hint, "Filter has invalid hint")
1327738
protected function validateFilter(array $filter, $address): void
    {
        $foundKeys = [];
        foreach (['f', 'field', 'and', 'group_and', 'or', 'group_or'] as $key) {
            if (array_key_exists($key, $filter)) {
                $foundKeys[] = $key;
            }
        }

        if (count($foundKeys) === 0) {
            throw new JoryException('A filter should contain one of the these fields: "f", "field", "and", "group_and", "or" or "group_or". (Location: '.$address.')');
        }
        if (count($foundKeys) > 1) {
            throw new JoryException('A filter cannot contain more than one of the these fields: "f", "field", "and", "group_and", "or" or "group_or". (Location: '.$address.')');
        }
        $foundKey = $foundKeys[0];

        if ($foundKey === 'f' || $foundKey === 'field') {
            // This is an actual filter, this means:

            // It is not allowed to have both an normal and minimized key, this could result in confusion
            if (array_key_exists('o', $filter) && array_key_exists('operator', $filter)) {
                throw new JoryException('A filter cannot contain both an "o" and "operator" parameter, remove one. (Location: '.$address.')');
            }
            if (array_key_exists('d', $filter) && array_key_exists('data', $filter)) {
                throw new JoryException('A filter cannot contain both an "d" and "data" parameter, remove one. (Location: '.$address.')');
            }
            // A string value in "field" (or "f") is required
            if (! is_string($filter[$foundKey])) {
                throw new JoryException('The "'.$foundKey.'" parameter should have a string value. (Location: '.$address.')');
            }
            // A string value in "operator" (or "o") is optional
            if ($this->hasArrayKey($filter, ['o', 'operator']) && ! is_string($this->getArrayValue($filter, [
                    'o',
                    'operator',
                ]))) {
                throw new JoryException('The "operator" (or "o") parameter should have a string value or be omitted. (Location: '.$address.')');
            }
            // A mixed value in "data" is optional
            // No checks needed here

            // If extra fields are present; they will be omitted by the parser
        }
        if ($foundKey === 'and' || $foundKey === 'group_and') {
            // This is a group_and filter, this means:

            // This filter should contain an array with valid subfilters
            $subFilters = $filter[$foundKey];
            if (! is_array($subFilters)) {
                throw new JoryException('The "'.$foundKey.'" parameter should hold an array with filters. (Location: '.$address.')');
            }
            foreach ($subFilters as $key => $subFilter) {
                $this->validateFilter($subFilter, $address.'(and).'.$key);
            }
        }
        if ($foundKey === 'or' || $foundKey === 'group_or') {
            // This is a group_or filter, this means:

            // This filter should contain an array with valid subfilters
            $subFilters = $filter[$foundKey];
            if (! is_array($subFilters)) {
                throw new JoryException('The "'.$foundKey.'" parameter should hold an array with filters. (Location: '.$address.')');
            }
            foreach ($subFilters as $key => $subFilter) {
                $this->validateFilter($subFilter, $address.'(or).'.$key);
            }
        }
    }
981978
public static function validateTimestamp($data, callable $filter=null) {

        return (
            (string) (int) $data === (string) $data
            && ($data <= PHP_INT_MAX)
            && ($data >= ~PHP_INT_MAX)
        ) ? self::applyFilter($data, $filter) : false;

    }
366436
public function filter(/*Callable*/ $filter)
    {
        $result = clone $this;
        foreach ($this as $object)
        {
            if (call_user_func($filter, $object) === false) {
                $result->remove($object);
            }
        }

        return $result;
    }
970149
public static function filter(array $data, $filter): array
    {
        if (!$filter) {
            return $data;
        }

        if (is_callable($filter)) {
            return array_filter($data, $filter);
        }

        if ($filter & self::FILTER_NULL) {
            return array_filter(
                $data,
                function ($value) {
                    return $value !== null;
                }
            );
        } elseif ($filter & self::FILTER_EMPTY) {
            return array_filter(
                $data,
                function ($value) {
                    return isset($value[0]);
                }
            );
        } elseif ($filter & self::FILTER_PHP) {
            return array_filter($data);
        }

        return $data;
    }
356401
protected function validateFilter($filter)
    {
        if (!isset($this->filterOptions->getFilters()[$filter])) {
            throw new Exception\FilterNotFoundException(sprintf(
                'Filter "%s" was not found', $filter
            ));
        }
        $options = $this->filterOptions->getFilters()[$filter];
        if (!isset($options['type'])) {
            throw new Exception\InvalidArgumentException(sprintf(
                'Filter type for "%s" image filter must be specified', $filter
            ));
        }
        if (!isset($options['options'])) {
            throw new Exception\InvalidArgumentException(sprintf(
                'Options for filter type "%s" must be specified', $filter
            ));
        }
    }
266943
public static function validateJson($data, callable $filter=null) {
        $decoded = json_decode($data);
        if ( is_null($decoded) ) return false;
        return self::applyFilter($data, $filter);
    }
373609
public function setFilter($filter)
    {
        if (!in_array($filter, $this->allowedFilters)) {
            throw new \Zend_Validate_Exception('Invalid param filter.');
        }

        $this->filter = (int) $filter;
        return $this;
    }
1120623
public function check($data)
    {
        $this->validAttribs   = array();
        $this->invalidAttribs = array();
        $this->missingAttribs = array();
        $this->unknownAttribs = array();
        $requiredDependent    = array();
        $seenAttrib           = array();

        foreach (U::flatten($data) as $attribName => $value) {
            $attrib = $this->dataFilter->getAttrib($attribName);
            if (!$attrib) {
                $parts = explode(U::$FLATTEN_SEPARATOR, $attribName);
                $count = count($parts);
                if ($count > 1) {

                    for ($i = $count -1; $i >= 1; $i--) {
                        $testName = join(U::$FLATTEN_SEPARATOR, array_splice($parts, 0, $i));
                        $attrib   = $this->dataFilter->getAttrib($testName. U::$FLATTEN_SEPARATOR. '*');
                        if ($attrib) {
                            break 1;
                        }
                    }
                }
            }
            $seenAttrib[$attribName] = true;

            // unknown attrib
            if (!$attrib) {
                $this->unknownAttribs[$attribName] = $this->dataFilter->applyFilter('pre', $value);
                continue;
            }

            // run pre-filters
            if ($attrib->useFilters()) {
                $value = $this->dataFilter->applyFilter('pre', $attrib->applyFilter('pre', $value));
            }

            // successfull check
            if ($attrib->check($value)) {
                $this->validAttribs[$attribName] = array(
                    'value'  => $attrib->useFilters()
                        ? $this->dataFilter->applyFilter('post', $attrib->applyFilter('post', $value))
                        : $value,
                    'attrib' => &$attrib
                );

                // determine possible dependents
                $attrib->determineDependents($value, $requiredDependent);
            }

            // checks failed
            else {
                $this->invalidAttribs[$attribName] = array(
                    'value'  => $value,
                    'attrib' => &$attrib,
                    'error'  => $attrib->getError()
                );
            }
        }

        // check now all attribs for required
        foreach ($this->dataFilter->getAttribs() as $attribName => $attrib) {

            // already seen
            if (isset($seenAttrib[$attribName])) {
                continue;
            }

            // has default
            elseif (!is_null($default = $attrib->getDefault())) {
                $this->validAttribs[$attribName] = ['value' => $default, 'attrib' => &$attrib];
            }

            // required -> missing
            elseif ($attrib->isRequired() || isset($requiredDependent[$attribName])) {
                $parts = explode(U::$FLATTEN_SEPARATOR, $attribName);
                $count = count($parts);
                if ($count > 1 && $parts[$count-1] === '*') {
                    $before = join(U::$FLATTEN_SEPARATOR, array_splice($parts, 0, $count - 1)). U::$FLATTEN_SEPARATOR;
                    $seen   = array_filter(array_keys($seenAttrib), function ($check) use ($before) {
                        return strpos($check, $before) === 0;
                    });
                    if (count($seen) > 0) {
                        continue;
                    }
                }
                $this->missingAttribs[$attribName] = array(
                    'attrib' => &$attrib,
                    'error'  => $attrib->getMissingText()
                );
            }
        }

        /*error_log("INVALID: ". count($this->invalidAttribs));
        error_log("MISSING: ". count($this->missingAttribs));*/
        return !$this->hasError();
    }
217333
public function validate($data, $throwException = true)
    {
        if (empty($rules = $this->getRules())) {
            return true;
        } else {
            $validator = Validator::make($data, $rules, $this->validationMessages);

            if ($validator->fails()) {
                $this->errors($validator->errors()->getMessages());
                if ($throwException) {
                    $errorString = DataFormatter::validationErrorsToString($this->getErrors());
                    throw new BadRequestException('Invalid data supplied.' . $errorString, null, null, $this->getErrors());
                } else {
                    return false;
                }
            } else {
                return true;
            }
        }
    }