public function testCheckPass() { $supplied = array('and-id-eq-value-12'); $criterion = new Criterion(); $criterion->setLogic('and')->setKey('id')->setOperand('eq')->setType('value')->setValue('12'); $filter = new Filter(); $filter->addCriterion($criterion); $expected = $filter; $requirements = array('fields' => array('id')); $actual = FilterType::check($supplied, $requirements); // Compare actual and existing params $this->assertInstanceOf(get_class($filter), $actual); $this->assertEquals($expected, $actual); }
public function testCheckFiltersWithOneFilterPass() { $supplied = array('and-id-eq-value-12'); $criterion = new Criterion(); $criterion->setLogic('and')->setKey('id')->setOperand('eq')->setType('value')->setValue('12'); $filterA = new Filter(); $filterA->addCriterion($criterion); $expected = array($filterA); $requirements = array('fields' => array('id')); $actual = PseudoTypes::checkFilters($supplied, $requirements); // Compare actual and existing params $this->assertInternalType('array', $actual); $this->assertInstanceOf(get_class($filterA), $actual[0]); $this->assertEquals($expected, $actual); }
/** * Checks if the value is of a given type and * passes the value the requirements specified. * * @param array $value Value to be checked * @param array $requirements Additional constraints * @return Filter Cleared value * @throws InvalidDataTypeException | InvalidDataValueException */ public static function check($value, array $requirements = array()) { if (!isset($requirements['fields'])) { $error = 'allowable list of fields constraint has not been specified for a filter'; throw new InvalidDataTypeException($error); } if (!is_array($value)) { $error = 'value must be a list of filters'; throw new InvalidDataTypeException($error); } // reserve space for cleared filters $filter = new Filter(); // Iterate through the list of filters and check each filter individually foreach ($value as $index => $criteria) { $criterion = self::criteriaToCriterion($criteria, $requirements, $index); $filter->addCriterion($criterion); } return $filter; }
/** * Builds SQL from filters (WHERE or HAVING parts) * * @param array $filters Array of Ucc\Data\Filter\Filter objects * @param array $fieldMap Array representing filed map * @param boolean $singleTable Marker to indicate single table queries. * @return string */ public static function getFilterSql($filters = array(), $fieldMap = array(), $singleTable = false) { $ret = array('paramiters' => array()); $havingFilters = array(); $whereFilters = array(); $table = ''; // Check if filters is a single filter if (is_a($filters, 'Ucc\\Data\\Filter\\Filter')) { $filters = array($filters); } foreach ($filters as $i => $filter) { // get Criterions $criterions = $filter->getCriterions(); $havingFilter = new Data_Filter(); $whereFilter = new Data_Filter(); foreach ($criterions as $criterion) { if (isset($fieldMap[$criterion->key()])) { // Get table name from field map $table = $fieldMap[$criterion->key()]; // Check for wildecard } elseif (isset($fieldMap['*'])) { $table = $fieldMap['*']; } if (!$singleTable || $singleTable === $table) { // Allow pseudo tables 'HAVING' if ($table == 'having') { $havingFilter->addCriterion($criterion); } else { $whereFilter->addCriterion($criterion); } } } $havingCriterions = $havingFilter->getCriterions(); if (!empty($havingCriterions)) { $havingFilters[$i] = $havingFilter; } $whereFilters[$i] = $whereFilter; } $where = Filter::filtersToSqlClause($whereFilters, $fieldMap); $having = Filter::filtersToSqlClause($havingFilters, $fieldMap); $whereStatemet = $where->getStatement(); $havingStatement = $having->getStatement(); if (!empty($whereStatemet) && $whereStatemet != '()') { $ret['where'] = 'WHERE ' . $whereStatemet; $ret['paramiters'] = array_merge($ret['paramiters'], $where->getParameters()); } if (!empty($havingStatement)) { $ret['having'] = 'HAVING ' . $havingStatement; $ret['paramiters'] = array_merge($ret['paramiters'], $having->getParameters()); } return $ret; }