Exemplo n.º 1
0
 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;
 }