/**
  * Validates that a value is numeric.
  *
  * <code>
  * class Person extends ActiveRecord\Model {
  *   static $validatesNumericalityOf = array(
  *     array('salary', 'greaterThan' => 19.99, 'lessThan' => 99.99)
  *   );
  * }
  * </code>
  *
  * Available options:
  *
  * <ul>
  * <li><b>onlyInteger:</b> value must be an integer (e.g. not a float)</li>
  * <li><b>even:</b> must be even</li>
  * <li><b>odd:</b> must be odd"</li>
  * <li><b>greaterThan:</b> must be greater than specified number</li>
  * <li><b>greaterThanOrEqualTo:</b> must be greater than or equal to specified number</li>
  * <li><b>equalTo:</b> ...</li>
  * <li><b>lessThan:</b> ...</li>
  * <li><b>lessThanOrEqualTo:</b> ...</li>
  * <li><b>allowBlank:</b> allow blank strings</li>
  * <li><b>allowNull:</b> allow null strings</li>
  * </ul>
  *
  * @param array $attrs Validation definition
  */
 public function validatesNumericalityOf($attrs)
 {
     $configuration = array_merge(self::$DEFAULT_VALIDATION_OPTIONS, array('onlyInteger' => false));
     // Notice that for fixnum and float columns empty strings are converted to nil.
     // Validates whether the value of the specified attribute is numeric by trying to convert it to a float with Kernel.Float
     // (if onlyInteger is false) or applying it to the regular expression /\A[+\-]?\d+\Z/ (if onlyInteger is set to true).
     foreach ($attrs as $attr) {
         $options = array_merge($configuration, $attr);
         $attribute = $options[0];
         $var = $this->model->{$attribute};
         $numericalityOptions = array_intersect_key(self::$ALL_NUMERICALITY_CHECKS, $options);
         if ($this->isNullWithOption($var, $options)) {
             continue;
         }
         $notANumberMessage = isset($options['message']) ? $options['message'] : Errors::$DEFAULT_ERROR_MESSAGES['notANumber'];
         if (true === $options['onlyInteger'] && !is_integer($var)) {
             if (!preg_match('/\\A[+-]?\\d+\\Z/', (string) $var)) {
                 $this->record->add($attribute, $notANumberMessage);
                 continue;
             }
         } else {
             if (!is_numeric($var)) {
                 $this->record->add($attribute, $notANumberMessage);
                 continue;
             }
             $var = (double) $var;
         }
         foreach ($numericalityOptions as $option => $check) {
             $optionValue = $options[$option];
             $message = isset($options['message']) ? $options['message'] : Errors::$DEFAULT_ERROR_MESSAGES[$option];
             if ('odd' != $option && 'even' != $option) {
                 $optionValue = (double) $options[$option];
                 if (!is_numeric($optionValue)) {
                     throw new ValidationsArgumentError("{$option} must be a number");
                 }
                 $message = str_replace('%d', $optionValue, $message);
                 if ('greaterThan' == $option && !($var > $optionValue)) {
                     $this->record->add($attribute, $message);
                 } elseif ('greaterThanOrEqualTo' == $option && !($var >= $optionValue)) {
                     $this->record->add($attribute, $message);
                 } elseif ('equalTo' == $option && !($var == $optionValue)) {
                     $this->record->add($attribute, $message);
                 } elseif ('lessThan' == $option && !($var < $optionValue)) {
                     $this->record->add($attribute, $message);
                 } elseif ('lessThanOrEqualTo' == $option && !($var <= $optionValue)) {
                     $this->record->add($attribute, $message);
                 }
             } else {
                 if ('odd' == $option && !Utils::isOdd($var) || 'even' == $option && Utils::isOdd($var)) {
                     $this->record->add($attribute, $message);
                 }
             }
         }
     }
 }
Example #2
0
 /**
  * Validates that a value is numeric.
  *
  * <code>
  * class Person extends Activerecord\Model {
  *   static $validatesNumericalityOf = array(
  *     array('salary', 'greater_than' => 19.99, 'less_than' => 99.99)
  *   );
  * }
  * </code>
  *
  * Available options:
  *
  * <ul>
  * <li><b>only_integer:</b> value must be an integer (e.g. not a float)</li>
  * <li><b>even:</b> must be even</li>
  * <li><b>odd:</b> must be odd"</li>
  * <li><b>greater_than:</b> must be greater than specified number</li>
  * <li><b>greater_than_or_equal_to:</b> must be greater than or equal to specified number</li>
  * <li><b>equal_to:</b> ...</li>
  * <li><b>less_than:</b> ...</li>
  * <li><b>less_than_or_equal_to:</b> ...</li>
  * <li><b>allow_blank:</b> allow blank strings</li>
  * <li><b>allow_null:</b> allow null strings</li>
  * </ul>
  *
  * @param array $attrs Validation definition
  */
 public function validatesNumericalityOf($attrs)
 {
     $configuration = \array_merge(self::$default_validation_options, ['only_integer' => false]);
     // Notice that for fixnum and float columns empty strings are converted to nil.
     // Validates whether the value of the specified attribute is numeric by trying to convert it to a float with Kernel.Float
     // (if only_integer is false) or applying it to the regular expression /\A[+\-]?\d+\Z/ (if only_integer is set to true).
     foreach ($attrs as $attr) {
         $options = \array_merge($configuration, $attr);
         $attribute = $options[0];
         $var = $this->model->{$attribute};
         $numericalityOptions = \array_intersect_key(self::$all_numericality_checks, $options);
         if ($this->isNullWithOption($var, $options)) {
             continue;
         }
         $not_a_number_message = isset($options['message']) ? $options['message'] : Errors::$default_error_messages['not_a_number'];
         if (true === $options['only_integer'] && !\is_integer($var)) {
             if (!\preg_match('/\\A[+-]?\\d+\\Z/', (string) $var)) {
                 $this->record->add($attribute, $not_a_number_message);
                 continue;
             }
         } else {
             if (!\is_numeric($var)) {
                 $this->record->add($attribute, $not_a_number_message);
                 continue;
             }
             $var = (double) $var;
         }
         foreach ($numericalityOptions as $option => $check) {
             $option_value = $options[$option];
             $message = isset($options['message']) ? $options['message'] : Errors::$default_error_messages[$option];
             if ('odd' != $option && 'even' != $option) {
                 $option_value = (double) $options[$option];
                 if (!\is_numeric($option_value)) {
                     throw new ExceptionValidation("Argument Error {$option} must be a number");
                 }
                 $message = \str_replace('%d', $option_value, $message);
                 if ('greater_than' == $option && !($var > $option_value)) {
                     $this->record->add($attribute, $message);
                 } elseif ('greater_than_or_equal_to' == $option && !($var >= $option_value)) {
                     $this->record->add($attribute, $message);
                 } elseif ('equal_to' == $option && !($var == $option_value)) {
                     $this->record->add($attribute, $message);
                 } elseif ('less_than' == $option && !($var < $option_value)) {
                     $this->record->add($attribute, $message);
                 } elseif ('less_than_or_equal_to' == $option && !($var <= $option_value)) {
                     $this->record->add($attribute, $message);
                 }
             } else {
                 if ('odd' == $option && !Utils::isOdd($var) || 'even' == $option && Utils::isOdd($var)) {
                     $this->record->add($attribute, $message);
                 }
             }
         }
     }
 }