function field_comparison($check1, $operator, $field2) { foreach ($check1 as $key => $value1) { $value2 = $this->data[$this->alias][$field2]; if (!Validation::comparison($value1, $operator, $value2)) { return false; } } return true; }
public function compareWithField($validationFields = array(), $operator = null, $compareFieldName = '') { if (!isset($this->data[$this->name][$compareFieldName])) { throw new CakeException(sprintf('Can\'t compare to the non-existing field "%s" of model %s.'), $compareFieldName, $this->name); } $compareTo = $this->data[$this->name][$compareFieldName]; foreach ($validationFields as $key => $value) { if (!Validation::comparison($value, $operator, $compareTo)) { return false; } } return true; }
/** * testComparisonAsArray method * * @return void */ public function testComparisonAsArray() { $this->assertTrue(Validation::comparison(array('check1' => 7, 'operator' => 'is greater', 'check2' => 6))); $this->assertTrue(Validation::comparison(array('check1' => 7, 'operator' => '>', 'check2' => 6))); $this->assertTrue(Validation::comparison(array('check1' => 6, 'operator' => 'is less', 'check2' => 7))); $this->assertTrue(Validation::comparison(array('check1' => 6, 'operator' => '<', 'check2' => 7))); $this->assertTrue(Validation::comparison(array('check1' => 7, 'operator' => 'greater or equal', 'check2' => 7))); $this->assertTrue(Validation::comparison(array('check1' => 7, 'operator' => '>=', 'check2' => 7))); $this->assertTrue(Validation::comparison(array('check1' => 7, 'operator' => 'greater or equal', 'check2' => 6))); $this->assertTrue(Validation::comparison(array('check1' => 7, 'operator' => '>=', 'check2' => 6))); $this->assertTrue(Validation::comparison(array('check1' => 6, 'operator' => 'less or equal', 'check2' => 7))); $this->assertTrue(Validation::comparison(array('check1' => 6, 'operator' => '<=', 'check2' => 7))); $this->assertTrue(Validation::comparison(array('check1' => 7, 'operator' => 'equal to', 'check2' => 7))); $this->assertTrue(Validation::comparison(array('check1' => 7, 'operator' => '==', 'check2' => 7))); $this->assertTrue(Validation::comparison(array('check1' => 7, 'operator' => 'not equal', 'check2' => 6))); $this->assertTrue(Validation::comparison(array('check1' => 7, 'operator' => '!=', 'check2' => 6))); $this->assertFalse(Validation::comparison(array('check1' => 6, 'operator' => 'is greater', 'check2' => 7))); $this->assertFalse(Validation::comparison(array('check1' => 6, 'operator' => '>', 'check2' => 7))); $this->assertFalse(Validation::comparison(array('check1' => 7, 'operator' => 'is less', 'check2' => 6))); $this->assertFalse(Validation::comparison(array('check1' => 7, 'operator' => '<', 'check2' => 6))); $this->assertFalse(Validation::comparison(array('check1' => 6, 'operator' => 'greater or equal', 'check2' => 7))); $this->assertFalse(Validation::comparison(array('check1' => 6, 'operator' => '>=', 'check2' => 7))); $this->assertFalse(Validation::comparison(array('check1' => 6, 'operator' => 'greater or equal', 'check2' => 7))); $this->assertFalse(Validation::comparison(array('check1' => 6, 'operator' => '>=', 'check2' => 7))); $this->assertFalse(Validation::comparison(array('check1' => 7, 'operator' => 'less or equal', 'check2' => 6))); $this->assertFalse(Validation::comparison(array('check1' => 7, 'operator' => '<=', 'check2' => 6))); $this->assertFalse(Validation::comparison(array('check1' => 7, 'operator' => 'equal to', 'check2' => 6))); $this->assertFalse(Validation::comparison(array('check1' => 7, 'operator' => '==', 'check2' => 6))); $this->assertFalse(Validation::comparison(array('check1' => 7, 'operator' => 'not equal', 'check2' => 7))); $this->assertFalse(Validation::comparison(array('check1' => 7, 'operator' => '!=', 'check2' => 7))); }
/** * Retorna o texto já aplicadas as suas condições * * @param string $col a coluna que será comparada * @param array $conditions array com as condições * @param integer $row O número da linha no array de dados * @return string O texto formatado e aplicado as condições */ private function applyConditions($col, $conditions, $row) { //valor dessa linha e desse campo $curValue = $this->data[$row][$col]; foreach ($conditions as $key => $cond) { $params = $conditions[$key]; //Se for acima da versão 0.2 //if((SPAGHETTI_VERSION >= 0.2)): $comparison = Validation::comparison($curValue, $params['operator'], $params['comparator']); //Se for a versão 0.1, não suportará operadores de comparação //else: // $comparison = $curValue == $params['comparator']; //endif; //Se a comparação foi promissora if ($comparison) { //Se existir um label //$alt = isset($params['alt']) ? $params['alt'] : ''; $label = isset($params['label']) ? $this->isImage($params['label'], $params) : $curValue; //Se um link foi definido $return = isset($params['href']) ? $this->link($label, $params['href']) : $label; //Se uma class de linha tiver definida if (isset($params['rowClass'])) { $this->rowClass[$row] = array_unset($params, 'rowClass'); //$this->rowClass[$row] = $params['rowClass']; } //Se uma classe de célula estiver definida if (isset($params['cellClass'])) { $this->cellClass[$col][$row] = array_unset($params, 'cellClass'); // $this->cellClass[$col][$row] = $params['cellClass']; } $matched = true; break; } } //Se após o foreach achou-se o valor if ($matched === true) { return $this->extract($return, $row); } else { return $curValue; } }
/** * @testdox valid should return false to number equal other number */ public function testInvalidComparison() { $value1 = 3.14; $value2 = 42; $this->assertFalse(Validation::comparison($value1, 'equal', $value2)); }
/** * Test comparison casting values before comparisons. * * @return void */ public function testComparisonTypeChecks() { $this->assertFalse(Validation::comparison('\\x028', '>=', 1), 'hexish encoding fails'); $this->assertFalse(Validation::comparison('0b010', '>=', 1), 'binary string data fails'); $this->assertFalse(Validation::comparison('0x01', '>=', 1), 'hex string data fails'); $this->assertFalse(Validation::comparison('0x1', '>=', 1), 'hex string data fails'); $this->assertFalse(Validation::comparison('\\x028', '>=', 1.5), 'hexish encoding fails'); $this->assertFalse(Validation::comparison('0b010', '>=', 1.5), 'binary string data fails'); $this->assertFalse(Validation::comparison('0x02', '>=', 1.5), 'hex string data fails'); }
/** * Image size validation method * * @param mixed $check * @param array $options is an array with key width or height and a value of array * with two options, operator and value. For example: * array('height' => array('==', 100)) will only be true if the image has a * height of exactly 100px. See the CakePHP core class and method * Validation::comparison for all operators. * @return boolean true * @see Validation::comparison() * @throws \InvalidArgumentException */ public function validateImageSize($check, $options) { if (!isset($options['height']) && !isset($options['width'])) { throw new \InvalidArgumentException(__d('file_storage', 'Invalid image size validation parameters')); } if (is_string($check)) { $imageFile = $check; } else { $check = array_values($check); $check = $check[0]; if (is_array($check) && isset($check['tmp_name'])) { $imageFile = $check['tmp_name']; } else { $imageFile = $check; } } $imageSizes = $this->getImageSize($imageFile); if (isset($options['height'])) { $height = Validation::comparison($imageSizes[1], $options['height'][0], $options['height'][1]); } else { $height = true; } if (isset($options['width'])) { $width = Validation::comparison($imageSizes[0], $options['width'][0], $options['width'][1]); } else { $width = true; } if ($height === false || $width === false) { return false; } return true; }
/** * Retorna o texto já aplicadas as suas condições * * @param string $col a coluna que será comparada * @param array $conditions array com as condições * @param integer $row O número da linha no array de dados * @return string O texto formatado e aplicado as condições */ private function applyConditions($col, $conditions, $row) { //valor dessa linha e desse campo $curValue = $this->data[$row][$col]; foreach ($conditions as $key => $cond) { $params = $conditions[$key]; $comparison = Validation::comparison($curValue, $params['operator'], $params['comparator']); //Se a comparação foi promissora if ($comparison) { //Se existir um label $label = isset($params['label']) ? $this->isImage($params['label'], $params) : $curValue; //Se um link foi definido $return = isset($params['href']) ? $this->link($label, $params['href']) : $label; //Se uma class de linha tiver definida if (isset($params['rowClass'])) { $this->rowClass[$row] = array_unset($params, 'rowClass'); } //Se uma classe de célula estiver definida if (isset($params['cellClass'])) { $this->cellClass[$col][$row] = array_unset($params, 'cellClass'); } $matched = true; break; } } //Se após o foreach achou-se o valor if ($matched === true) { return $this->extract($return, $row); } else { return $curValue; } }
/** * Used to compare 2 datetime values. * * @param object &$model use model * @param string|array $check datetime string * @param string $operator Can be either a word or operand * is greater >, is less <, greater or equal >= * less or equal <=, is less <, equal to ==, not equal != * @param string $compare compare datetime string * @return bool Success */ public function checkDateComp(&$model, $check, $operator, $compare) { // 比較対象がないので比較する必要なし if (Validation::blank($model->data['Registration'][$compare])) { return true; } $check2 = strtotime($model->data['Registration'][$compare]); foreach ($check as $val) { if (Validation::blank($val)) { continue; } $check1 = strtotime($val); $ret = Validation::comparison($check1, $operator, $check2); if (!$ret) { return false; } } return true; }