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); }
public function testGetRegexOperands() { $criterion = new Criterion(); $this->assertTrue(is_array($criterion->getRegexOperands())); $this->assertNotEmpty($criterion->getRegexOperands()); $expected = array(Criterion::CRITERION_OP_RE); $this->assertEquals($expected, $criterion->getRegexOperands()); }
/** * Gets Criterion logic and returns it in CAPITALIZED string * @param Criterion * @return string Criterion logic */ public static function addLogic(Criterion $criterion) { return strtoupper($criterion->logic()); }
public function inNiniCriterionsProvider() { $inNiniValueAndCriterion = new Criterion(); $inNiniValueAndCriterion->setLogic('and')->setKey('foo')->setOperand('nini')->setType('value')->setValue('abc,def,xyz,123'); $inNiniValueAndClause = new Clause(); $inNiniValueAndClause->setStatement('AND `foo` NOT IN (:filter_0_0 COLLATE utf8_general_ci, :filter_0_1 COLLATE utf8_general_ci, :filter_0_2 COLLATE utf8_general_ci, :filter_0_3 COLLATE utf8_general_ci)')->setParameters(array('filter_0_0' => 'abc', 'filter_0_1' => 'def', 'filter_0_2' => 'xyz', 'filter_0_3' => '123')); $inNiniValueOrCriterion = new Criterion(); $inNiniValueOrCriterion->setLogic('or')->setKey('foo')->setOperand('nini')->setType('value')->setValue('abc,def,xyz,123'); $inNiniValueOrClause = new Clause(); $inNiniValueOrClause->setStatement('OR `foo` NOT IN (:filter_0_0 COLLATE utf8_general_ci, :filter_0_1 COLLATE utf8_general_ci, :filter_0_2 COLLATE utf8_general_ci, :filter_0_3 COLLATE utf8_general_ci)')->setParameters(array('filter_0_0' => 'abc', 'filter_0_1' => 'def', 'filter_0_2' => 'xyz', 'filter_0_3' => '123')); $inNiniTypeAndCriterion = new Criterion(); $inNiniTypeAndCriterion->setLogic('and')->setKey('foo')->setOperand('nini')->setType('field')->setValue('bar,loo,foo'); $inNiniTypeAndClause = new Clause(); $inNiniTypeAndClause->setStatement('AND `foo` NOT IN (`bar` COLLATE utf8_general_ci, `loo` COLLATE utf8_general_ci, `foo` COLLATE utf8_general_ci)'); $inNiniTypeOrCriterion = new Criterion(); $inNiniTypeOrCriterion->setLogic('or')->setKey('foo')->setOperand('nini')->setType('field')->setValue('bar,loo,foo'); $inNiniTypeOrClause = new Clause(); $inNiniTypeOrClause->setStatement('OR `foo` NOT IN (`bar` COLLATE utf8_general_ci, `loo` COLLATE utf8_general_ci, `foo` COLLATE utf8_general_ci)'); return array(array($inNiniValueAndCriterion, $inNiniValueAndClause), array($inNiniValueOrCriterion, $inNiniValueOrClause), array($inNiniTypeAndCriterion, $inNiniTypeAndClause), array($inNiniTypeOrCriterion, $inNiniTypeOrClause)); }
/** * Decides which transformation method use for given operand * * @param Criterion $criterion * @return string | false Return method name or false if method can not be determined */ public static function criterionOperandToMethod(Criterion $criterion) { switch ($criterion->op()) { // Boolean check to see if the value is set or not. case 'bool': $method = 'criterionToBool'; break; // Direct comparison checks. // Direct comparison checks. case 'eq': // equals // equals case 'ne': // does not equal // does not equal case 'eqi': // equals (case insensitive) // equals (case insensitive) case 'nei': // does not equal (case insensitive) $method = 'criterionToDirect'; break; // Relative comparison checks. // Relative comparison checks. case 'gt': // greater than // greater than case 'ge': // greater than or equal to // greater than or equal to case 'lt': // less than // less than case 'le': // less than or equal to $method = 'criterionToRelative'; break; // Wildcard comparison checks (contains/includes) // Wildcard comparison checks (contains/includes) case 'inc': // includes // includes case 'ninc': // does not include // does not include case 'inci': // includes (case insensitive) // includes (case insensitive) case 'ninci': // does not include (case insensitive) $method = 'criterionToContains'; break; // Wildcard comparison checks (begins with) // Wildcard comparison checks (begins with) case 'begins': // begins with // begins with case 'nbegins': // does not begin with // does not begin with case 'beginsi': // begins with (case insensitive) // begins with (case insensitive) case 'nbeginsi': // does not begin with (case insensitive) $method = 'criterionToBegins'; break; // Regex match // Regex match case 're': // matches regex string $method = 'criterionToRegex'; break; // Check for a list of values (match or no match). // Check for a list of values (match or no match). case 'in': // is in the list // is in the list case 'nin': // is not in the list // is not in the list case 'ini': // is in the list (case insensitive) // is in the list (case insensitive) case 'nini': // is not in the list (case insensitive) $method = 'criterionToIn'; break; default: $method = false; } return $method; }
public static function criteriaToCriterion($filter, $requirements = array(), $index = 0) { // Detect filter settings if (!is_string($filter)) { $error = 'value for index ' . $index . ' must be a string in format of' . ' {logic}-{field}-{operand}-{type}-{value}' . ' Example: and-name-eq-value-smith'; throw new InvalidDataValueException($error); } // Get filter setting from string // All filters should follow standard pattern: // {logic}-{field}-{operand}-{type}-{value} // Example: and-id-eq-value-12 $parts = explode('-', $filter, 5); // Reserve space for error message in regards to fields $filedsPatternMessage = ''; // Check for fields prerequisites if defined if (isset($requirements['fields'])) { $filedsPatternMessage = ', part 2 (field) must be one of ' . '(' . implode(', ', $requirements['fields']) . ')'; if (!in_array($parts[1], $requirements['fields'])) { // Requirements miss match $error = 'value for index ' . $index . $filedsPatternMessage; throw new InvalidDataValueException($error); } } if (!(count($parts) === 5) || !in_array($parts[0], Criterion::$criterionLogic) || !in_array($parts[2], Criterion::$criterionOperands)) { // Filter pattern does not match standard $error = 'value for index ' . $index . ', and part 1 (logic) must be one of ' . '(' . implode(', ', Criterion::$criterionLogic) . ')' . $filedsPatternMessage . ', and part 3 (operand) must be one of ' . '(' . implode(', ', Criterion::$criterionOperands) . ')'; throw new InvalidDataValueException($error); } // Check value for boolean operand is one of true ore false if (in_array($parts[2], Criterion::getBoolOperands())) { // Now that we know that this is boolean operand // let's check value is one of boolean type if (!in_array(strtolower($parts[4]), Criterion::$criterionBooleanValues)) { $error = 'value for index ' . $index . ', and part 5 (value) must be one of ' . '(' . implode(', ', Criterion::$criterionBooleanValues) . ')' . ' when using boolean operand'; throw new InvalidDataValueException($error); } } $criterion = new Criterion(); try { $criterion->setLogic($parts[0])->setKey($parts[1])->setOperand($parts[2])->setType($parts[3])->setValue($parts[4]); } catch (InvalidArgumentException $e) { $error = 'value for filter index ' . $index . ', ' . 'part 4 (type) must be one of (field or value)'; throw new InvalidDataValueException($error); } return $criterion; }