Esempio n. 1
0
 public function testParseFloat()
 {
     $this->assertEquals(10, tao_helpers_Numeric::parseFloat("10"));
     $this->assertEquals(10, tao_helpers_Numeric::parseFloat("10g"));
     $this->assertEquals(10.5, tao_helpers_Numeric::parseFloat("10.5"));
     $this->assertEquals(10.5, tao_helpers_Numeric::parseFloat("10,5"));
     $this->assertEquals(1105.5, tao_helpers_Numeric::parseFloat("1.105,5"));
 }
Esempio n. 2
0
 /**
  * Short description of method evaluate
  *
  * @access public
  * @author Jehan Bihin, <*****@*****.**>
  * @param
  *            values
  * @return boolean
  */
 public function evaluate($values)
 {
     $returnValue = (bool) false;
     $rowValue = $values;
     $value = tao_helpers_Numeric::parseFloat($rowValue);
     if (empty($rowValue)) {
         $returnValue = true;
         // no need to go further. To check if not empty, use the NotEmpty validator
         return $returnValue;
     }
     if (!is_numeric($rowValue) || $value != $rowValue) {
         $this->setMessage(__('The value of this field must be numeric'));
         $returnValue = false;
     } else {
         if ($this->hasOption('min') || $this->hasOption('max')) {
             if ($this->hasOption('min') && $this->hasOption('max')) {
                 if ($this->getOption('min') <= $value && $value <= $this->getOption('max')) {
                     $returnValue = true;
                 } else {
                     $this->setMessage(__('Invalid field range (minimum value: %1$s, maximum value: %2$s)', $this->getOption('min'), $this->getOption('max')));
                 }
             } elseif ($this->hasOption('min') && !$this->hasOption('max')) {
                 if ($this->getOption('min') <= $value) {
                     $returnValue = true;
                 } else {
                     $this->setMessage(__('Invalid field range (minimum value: %s)', $this->getOption('min')));
                 }
             } elseif (!$this->hasOption('min') && $this->hasOption('max')) {
                 if ($value <= $this->getOption('max')) {
                     $returnValue = true;
                 } else {
                     $this->setMessage(__('Invalid field range (maximum value: %s)', $this->getOption('max')));
                 }
             }
         } else {
             $returnValue = true;
         }
     }
     // Test less, greater, equal to another
     if ($returnValue && $this->hasOption('integer2_ref') && $this->getOption('integer2_ref') instanceof tao_helpers_form_FormElement) {
         $secondElement = $this->getOption('integer2_ref');
         switch ($this->getOption('comparator')) {
             case '>':
             case 'sup':
                 if ($value > $secondElement->getRawValue()) {
                     $returnValue = true;
                 } else {
                     $returnValue = false;
                 }
                 break;
             case '<':
             case 'inf':
                 if ($value < $secondElement->getRawValue()) {
                     $returnValue = true;
                 } else {
                     $returnValue = false;
                 }
                 break;
             case '=':
             case 'equal':
                 if ($value == $secondElement->getRawValue()) {
                     $returnValue = true;
                 } else {
                     $returnValue = false;
                 }
                 break;
         }
     }
     return (bool) $returnValue;
 }
Esempio n. 3
0
 /**
  * Short description of method evaluate
  *
  * @access public
  * @author Jehan Bihin, <*****@*****.**>
  * @param values
  * @return boolean
  */
 public function evaluate($values)
 {
     $returnValue = (bool) false;
     $rowValue = $values;
     $value = tao_helpers_Numeric::parseFloat($rowValue);
     if (empty($rowValue)) {
         $returnValue = true;
         // no need to go further. To check if not empty, use the NotEmpty validator
         return $returnValue;
     }
     if (!is_numeric($rowValue) || $value != $rowValue) {
         $this->message = __('The value of this field must be numeric');
         $returnValue = false;
     } else {
         if (isset($this->options['min']) || isset($this->options['max'])) {
             $this->message = __('Invalid field range');
             if (isset($this->options['min']) && isset($this->options['max'])) {
                 $this->message .= ' (' . __('minimum value: ') . $this->options['min'] . ', ' . __('maximum value: ') . $this->options['max'] . ')';
                 if ($this->options['min'] <= $value && $value <= $this->options['max']) {
                     $returnValue = true;
                 }
             } else {
                 if (isset($this->options['min']) && !isset($this->options['max'])) {
                     $this->message .= ' (' . __('minimum value: ') . $this->options['min'] . ')';
                     if ($this->options['min'] <= $value) {
                         $returnValue = true;
                     }
                 } else {
                     if (!isset($this->options['min']) && isset($this->options['max'])) {
                         $this->message .= ' (' . __('maximum value: ') . $this->options['max'] . ')';
                         if ($value <= $this->options['max']) {
                             $returnValue = true;
                         }
                     }
                 }
             }
         } else {
             $returnValue = true;
         }
     }
     // Test less, greater, equal to another
     if ($returnValue && isset($this->options['integer2_ref']) && $this->options['integer2_ref'] instanceof tao_helpers_form_FormElement) {
         $secondElement = $this->options['integer2_ref'];
         switch ($this->options['comparator']) {
             case '>':
             case 'sup':
                 if ($value > $secondElement->getRawValue()) {
                     $returnValue = true;
                 } else {
                     $returnValue = false;
                 }
                 break;
             case '<':
             case 'inf':
                 if ($value < $secondElement->getRawValue()) {
                     $returnValue = true;
                 } else {
                     $returnValue = false;
                 }
                 break;
             case '=':
             case 'equal':
                 if ($value == $secondElement->getRawValue()) {
                     $returnValue = true;
                 } else {
                     $returnValue = false;
                 }
                 break;
         }
     }
     return (bool) $returnValue;
 }