/** * Constructs a filter list with sorting * @param array $rawFilters * @return App_ListFilter */ public function constructFilter($rawFilters) { if (!empty($this->_whiteList)) { $fields = $this->getWhiteList(); } else { $fields = array_keys($rawFilters); } if ($this->_sortingFieldName !== null && isset($rawFilters[$this->_sortingFieldName])) { $sorting = $rawFilters[$this->_sortingFieldName]; unset($rawFilters[$this->_sortingFieldName]); } if ($this->_groupingFieldName !== null && isset($rawFilters[$this->_groupingFieldName])) { $grouping = $rawFilters[$this->_groupingFieldName]; if ($this->_groupFieldName !== null && isset($rawFilters[$this->_groupFieldName])) { $group = $rawFilters[$this->_groupFieldName]; unset($rawFilters[$this->_groupFieldName]); if ($this->_highlightingFieldName !== null && isset($rawFilters[$this->_highlightingFieldName])) { $highlighting = $rawFilters[$this->_highlightingFieldName]; unset($rawFilters[$this->_highlightingFieldName]); } } unset($rawFilters[$this->_groupingFieldName]); } $filters = array(); $blackList = $this->getBlackList(); foreach ($fields as $fieldName) { if (!in_array($fieldName, $blackList) && isset($rawFilters[$fieldName])) { // Allow multiple filters! $raw = $rawFilters[$fieldName]; $raw = is_array($raw) ? $raw : array($raw); foreach ($raw as $rawValue) { $filter = $this->_createFilter($fieldName, $rawValue); if (!$filter) { continue; } /** * Check that every date field is formatted correctly * using the YYYYMMDD or YYYY-MM-DD formats. * * (the verification is made based on the field name * having the word 'date') */ $dateRegExp = '/^(19|20)\\d\\d-?(0[1-9]|1[012])-?(0[1-9]|[12][0-9]|3[01])$/'; if (preg_match('/date/i', $fieldName)) { if ($filter instanceof App_ListFilter_BetweenFilter) { if (!preg_match($dateRegExp, $filter->getMin()) || !preg_match($dateRegExp, $filter->getMax())) { throw new \Application\Exceptions\InvalidArgumentException("Invalid '{$fieldName}' value. Expected to be between " . $filter->getMin() . " and " . $filter->getMax()); } } else { if (!preg_match($dateRegExp, $filter->getValue())) { throw new \Application\Exceptions\InvalidArgumentException("Invalid parameter value: {$fieldName}."); } } } $filter->setAllowEmpty($this->getAllowEmpty()); if ($filter->isValid()) { $filters[] = $filter; } else { // Ignoring... $this->_unusedList[] = $fieldName; } } } else { // Ignoring... $this->_unusedList[] = $fieldName; } } $listFilter = new App_ListFilter(); $listFilter->setFilters($filters); if (isset($sorting)) { $this->_setSorting($listFilter, $sorting); } if (isset($grouping) && $this->_checkGrouping($grouping)) { $listFilter->setGrouping($grouping); if (isset($group)) { $listFilter->setGroup($group); if (isset($highlighting) && $this->_checkGrouping($highlighting)) { $listFilter->setHighlighting($highlighting); } } } return $listFilter; }