Build Constraint objects from given filter values

@param string []|string

@return Constraint[]|Constraint
346476
protected function buildConstraints($values)
    {
        if (is_array($values)) {
            $constraints = [];
            foreach ($values as $value) {
                $constraints[] = Constraint::make($value);
            }
            return $constraints;
        } else {
            return Constraint::make($values);
        }
    }
143189
protected function getFilterResolver(string $filter): array
    {
        $class = $filter;
        $method = 'filter';

        $position = \strpos($filter, '@');

        if ($position === 0) {
            $method = 'filter'.\ucwords(\substr($filter, 1));

            return [$this, $method];
        }

        if ($position !== false) {
            list($class, $method) = \explode('@', $filter, 2);
        }

        return $this->makeFilterResolver($class, $method);
    }
355556
function build_rules(filters, arr) {
    var rules = [];
    for(var i=0; i<arr.length; i++) {
        var r = arr[i], rule
        if(!contains_expr(r)) throw OptError('Rule MUST contain an option.');
        switch(r.length) {
            case 1:
                rule = build_rule(filters, r[0]);
                break;
            case 2:
                var expr = LONG_SWITCH_RE.test(r[0]) ? 0 : 1;
                var alias = expr == 0 ? -1 : 0;
                var desc = alias == -1 ? 1 : -1;
                rule = build_rule(filters, r[alias], r[expr], r[desc]);
                break;
            case 3:
                rule = build_rule(filters, r[0], r[1], r[2]);
                break;
            default:
            case 0:
                continue;
        }
        rules.push(rule)
    }
    return rules;
}
1250537
protected static function buildFilterArray($filter, $params = null)
    {
        if (empty($filter)) {
            return [];
        }

        if (is_array($filter)) {
            // assume client knows correct usage of Mongo query language
            return static::toMongoObjects($filter);
        }

        // handle logical operators first
        $logicalOperators = DbLogicalOperators::getDefinedConstants();
        foreach ($logicalOperators as $logicalOp) {
            if (DbLogicalOperators::NOT_STR === $logicalOp) {
                // NOT(a = 1)  or NOT (a = 1)format
                if ((0 === stripos($filter, $logicalOp . '(')) || (0 === stripos($filter, $logicalOp . '('))) {
                    $parts = trim(substr($filter, 3));
                    $parts = static::buildFilterArray($parts, $params);

                    return [static::localizeOperator($logicalOp) => $parts];
                }
            } else {
                // (a = 1) AND (b = 2) format
                $paddedOp = ') ' . $logicalOp . ' (';
                if (false !== $pos = stripos($filter, $paddedOp)) {
                    $left = trim(substr($filter, 0, $pos));
                    $right = trim(substr($filter, $pos + strlen($paddedOp)));
                    $left = static::buildFilterArray($left, $params);
                    $right = static::buildFilterArray($right, $params);

                    return [static::localizeOperator($logicalOp) => [$left, $right]];
                }
                // (a = 1)AND(b = 2) format
                $paddedOp = ')' . $logicalOp . '(';
                if (false !== $pos = stripos($filter, $paddedOp)) {
                    $left = trim(substr($filter, 0, $pos));
                    $right = trim(substr($filter, $pos + strlen($paddedOp)));
                    $left = static::buildFilterArray($left, $params);
                    $right = static::buildFilterArray($right, $params);

                    return [static::localizeOperator($logicalOp) => [$left, $right]];
                }
            }
        }

        $filter = trim(trim($filter, '()'));

        // the rest should be comparison operators
        // Note: order matters here!
        $sqlOperators = DbComparisonOperators::getParsingOrder();
        foreach ($sqlOperators as $sqlOp) {
            $paddedOp = static::padOperator($sqlOp);
            if (false !== $pos = stripos($filter, $paddedOp)) {
                $field = trim(substr($filter, 0, $pos));
                $negate = false;
                if (false !== strpos($field, ' ')) {
                    $parts = explode(' ', $field);
                    if ((count($parts) > 2) || (0 !== strcasecmp($parts[1], trim(DbLogicalOperators::NOT_STR)))) {
                        // invalid field side of operator
                        throw new BadRequestException('Invalid or unparsable field in filter request.');
                    }
                    $field = $parts[0];
                    $negate = true;
                }

                $value = trim(substr($filter, $pos + strlen($paddedOp)));
                if (DbComparisonOperators::requiresValueList($sqlOp)) {
                    $value = trim($value, '()[]');
                    $parsed = [];
                    foreach (explode(',', $value) as $each) {
                        $parsed[] = static::determineValue($each, $field, $params);
                    }
                    $value = $parsed;
                } elseif (DbComparisonOperators::requiresNoValue($sqlOp)) {
                    switch ($sqlOp) {
                        case DbComparisonOperators::IS_NULL:
                            return [$field => null];
                        case DbComparisonOperators::IS_NOT_NULL:
                            return [$field => ['$ne' => null]];
                        case DbComparisonOperators::DOES_EXIST:
                            return [$field => ['$exists' => true]];
                        case DbComparisonOperators::DOES_NOT_EXIST:
                            return [$field => ['$exists' => false]];
                    }
                } else {
                    $value = static::determineValue($value, $field, $params);
                    if ('$eq' === static::localizeOperator($sqlOp)) {
                        // prior to 3.0
                        if ($negate) {
                            return [$field => ['$ne' => $value]];
                        }

                        return [$field => $value];
                    } elseif (DbComparisonOperators::LIKE === $sqlOp) {
//			WHERE name LIKE "%Joe%"	(array("name" => new Regex("/Joe/")));
//			WHERE name LIKE "Joe%"	(array("name" => new Regex("/^Joe/")));
//			WHERE name LIKE "%Joe"	(array("name" => new Regex("/Joe$/")));
                        if ('%' == $value[strlen($value) - 1]) {
                            if ('%' == $value[0]) {
                                $value = trim($value, '%');
                            } else {
                                $value = '^' . rtrim($value, '%');
                            }
                        } else {
                            if ('%' == $value[0]) {
                                $value = trim($value, '%') . '$';
                            }
                        }

                        return [$field => new Regex($value, '')];
                    } elseif (DbComparisonOperators::CONTAINS === $sqlOp) {
                        return [$field => new Regex($value, '')];
                    } elseif (DbComparisonOperators::STARTS_WITH === $sqlOp) {
                        return [$field => new Regex('^' . $value, '')];
                    } elseif (DbComparisonOperators::ENDS_WITH === $sqlOp) {
                        return [$field => new Regex($value . '$', '')];
                    }
                }

                if ($negate) {
                    $value = ['$not' => $value];
                }

                return [$field => [static::localizeOperator($sqlOp) => $value]];
            }
        }

        return $filter;
    }
119688
protected function buildRegex(array $tokens, array $constraints)
    {
        $groupIndex      = 1;
        $regex           = '';
        $quotedDelimiter = preg_quote($this->delimiter);

        foreach ($tokens as $token) {
            switch ($token[static::TYPE]) {
                case 'literal':
                    $regex .= preg_quote($token[static::LITERAL]);
                    break;

                case 'parameter':
                    $groupName = '?P<param' . $groupIndex . '>';

                    if (isset($constraints[$token[static::NAME]])) {
                        $regex .= '(' . $groupName . $constraints[$token[static::NAME]] . ')';
                    } elseif ($token[static::DELIMITERS] === null) {
                        $regex .= '(' . $groupName . '[^' . $quotedDelimiter . ']+)';
                    } else {
                        $regex .= '(' . $groupName . '[^' . $token[static::DELIMITERS] . ']+)';
                    }

                    $this->paramMap['param' . $groupIndex] = $token[static::NAME];
                    ++$groupIndex;
                    break;

                case 'optional-start':
                    $regex .= '(?:';
                    break;

                case 'optional-end':
                    $regex .= ')?';
                    break;
            }
        }

        return $regex;
    }
451415
public function guessTypeFromConstraintList(array $constraintList) : ?TypeDoc
    {
        $doc = $abstractTypeFound = null;
        foreach ($constraintList as $constraint) {
            $doc = $this->guessTypeFromConstraint($constraint);
            if (null !== $doc) {
                if ($this->isAbstractType($doc)) {
                     // Abstract type => continue to see if better type can be found
                     $abstractTypeFound = $doc;
                     $doc = null;
                } else {
                     break;
                }
            }
        }
        // Try to fallback on abstractType if found
        if (null === $doc && null !== $abstractTypeFound) {
            $doc = $abstractTypeFound;
        }

        return $doc;
    }
527358
protected function _buildDefinition($columns, $constraints)
    {
        foreach ($columns as $name => $field) {
            $field['name'] = $name;
            $field = $this->dialect()->field($field);

            if (!empty($field['serial'])) {
                $primary = $name;
            }
            $result[] = $this->dialect()->column($field);
        }

        foreach ($constraints as $constraint) {
            if (!isset($constraint['type'])) {
                throw new SqlException("Missing contraint type.");
            }
            $type = $constraint['type'];
            if ($meta = $this->dialect()->constraint($type, $constraint, ['schemas' => ['' => $this]])) {
                $result[] = $meta;
            }
            if ($type === 'primary') {
                $primary = null;
            }
        }
        if (isset($primary)) {
            $result[] = $this->dialect()->constraint('primary', ['column' => $primary]);
        }

        return ' (' . join(', ', array_filter($result)) . ')';
    }
681209
protected function _constraints(array $constraints) {
		foreach ($constraints as &$value) {
			if (is_string($value)) {
				$value = (object) $this->name($value);
			} elseif (is_array($value)) {
				$value = $this->_constraints($value);
			}
		}
		return $constraints;
	}
163762
protected function getConstraintsFromfields(array $fields)
    {
        $constraints = [];

        foreach ($fields as $field) {
            if ($field->hasForeignConstraint()) {
                $constraints[] = $field->getForeignConstraint();
            }
        }

        return $constraints;
    }
1346285
private String buildConstraintExpr(String device_name, String attribute, String event_name, String[] filters) {
        String str =
                "$domain_name == \'" + device_name.toLowerCase() + "/" +
                        attribute.toLowerCase() + "\'" + " and $event_name == \'" +
                        event_name + "\'";
        if (filters != null && filters.length != 0) {
            str += " and ((";
            for (String filter : filters)
                str += filter;
            str += " ) and $forced_event > 0.5 )";
        }
        return str;
    }
878957
protected function createAllOfConstraint(Value\ArrayValue $value)
    {
        $schemas = array();
        foreach ($value as $subValue) {
            $schemas[] = $this->createSchema($subValue);
        }

        return new Generic\AllOfConstraint($schemas);
    }
863205
public function matchConstraints(array $constraints)
    {
        foreach ($constraints as $constraint) {
            $result = true;
            if (isset($constraints['host_match'])) {
                $result = $result && $this->hostMatch($constraints['host_match']);
            }

            if (isset($constraints['host'])) {
                $result = $result && $this->hostEqual($constraints['host']);
            }

            if (isset($constraints['request_method'])) {
                $result = $result && $this->requestMethodEqual($constraints['request_method']);
            }

            if (isset($constraints['path_match'])) {
                $result = $result && $this->pathMatch($constraints['path_match']);
            }

            if (isset($constraints['path'])) {
                $result = $result && $this->pathEqual($constraints['path']);
            }

            // If it matches all constraints, we simply return true and skip other constraints
            if ($result) {
                return true;
            }
            // try next one
        }

        return false;
    }
102242
protected function transformConstraintArray(array $constraints)
    {
        $self = $this;

        return array_map(
            function (ConstraintInterface $constraint) use ($self) {
                return $constraint->accept($self);
            },
            $constraints
        );
    }
1359933
public function buildFilters($options) {
        if (empty($options['filters']) || !is_array($options['filters'])) return [];
        return array_filter(array_map(function($filter) {
            if (empty($this->config['filters'][$filter])) return;
            return new $this->config['filters'][$filter];
        }, $options['filters']));
    }
1365673
private function filtersDefaultOperators(array $filters): array
    {
        foreach ($filters as $key => $filter) {
            // give the filter default operators, according to its type
            if (
                (!array_key_exists('operators', $filter)) ||
                (empty($filter['operators']))
            ) {
                $builderType = $filter['type'];

                switch ($builderType) {
                    case 'string':
                        $filter['operators'] = [
                            'equal', 'not_equal', 'is_null', 'is_not_null',
                            'begins_with', 'not_begins_with', 'contains', 'not_contains', 'ends_with', 'not_ends_with', 'is_empty', 'is_not_empty', // specific to strings
                        ];

                        break;
                    case 'integer':
                    case 'double':
                    case 'date':
                    case 'time':
                    case 'datetime':
                        $filter['operators'] = [
                            'equal', 'not_equal', 'is_null', 'is_not_null',
                            'less', 'less_or_equal', 'greater', 'greater_or_equal', 'between', 'not_between', // specific to numbers and dates
                        ];

                        break;
                    case 'boolean':
                        $filter['operators'] = [
                            'equal', 'not_equal', 'is_null', 'is_not_null',
                        ];

                        break;
                }
            }
            $filters[$key] = $filter;
        }

        return $filters;
    }
984117
protected static function splitConstraints($version)
    {
        $found   = [];
        $pattern = '/(?<!^|as|[=>< ,]) *(?<!-)[, ](?!-) *(?!,|as|$)/';

        foreach (preg_split('/\s*\|\|?\s*/', trim($version)) as $constraints) {
            $andConstraints = preg_split($pattern, $constraints);

            foreach ($andConstraints as $constraint) {
                $found[] = $constraint;
            }
        }

        return $found;
    }
604412
public static function create(array $constraints): ValidationClassMappingBuilderInterface
    {
        $self = new self();
        $self->constraints = [];
        foreach ($constraints as $constraint) {
            $self->addConstraint($constraint);
        }

        return $self;
    }