LazyRecord\CollectionFilter\CollectionFilter::apply PHP Method

apply() public method

public apply ( array $args )
$args array
    public function apply(array $args)
    {
        $c = $this->collection;
        foreach ($this->validFields as $fieldName => $t) {
            if (!isset($args[$fieldName])) {
                continue;
            }
            $requestValues = (array) $args[$fieldName];
            // Conditions that takes array
            if ($t == self::Range) {
                if (count($requestValues) != 2) {
                    throw new Exception('require 2 request values for the range filter.');
                }
                $c->where()->group()->between($fieldName, $requestValues[0], $requestValues[1])->ungroup();
                continue;
            }
            if ($t == self::InSet) {
                $c->where()->in($fieldName, $requestValues);
                continue;
            }
            $where = $c->where();
            $hasParams = false;
            foreach ($requestValues as $idx => $requestValue) {
                if (isset($this->validValues[$fieldName])) {
                    $validValues = $this->validValues[$fieldName];
                    if (!$this->validateValue($validValues, $requestValue)) {
                        continue;
                    }
                }
                if ($idx == 0) {
                    $where->group();
                }
                $hasParams = true;
                switch ($t) {
                    case self::Contains:
                        $where->or()->like($fieldName, '%' . $requestValue . '%');
                        break;
                    case self::StartsWith:
                        $where->or()->like($fieldName, $requestValue . '%');
                        break;
                    case self::EndsWith:
                        $where->or()->like($fieldName, '%' . $requestValue);
                        break;
                    case self::Greater:
                        $where->or()->greaterThan($fieldName, $requestValue);
                        break;
                    case self::Lesser:
                        $where->or()->lesserThan($fieldName, $requestValue);
                        break;
                    case self::Equal:
                        $where->or()->equal($fieldName, $requestValue);
                        break;
                }
            }
            if ($hasParams) {
                $expr->ungroup();
            }
        }
        return $c;
    }