Пример #1
0
 /**
  * Validates the parameters value and returns the result.
  * @see ParamDefinition::validateValue
  *
  * @since 0.5
  *
  * @param $value mixed
  * @param $param IParam
  * @param $definitions array of IParamDefinition
  * @param $params array of IParam
  * @param ValidatorOptions $options
  *
  * @return boolean
  */
 protected function validateValue($value, IParam $param, array $definitions, array $params, ValidatorOptions $options)
 {
     if ($options->isStringlyTyped()) {
         if (!is_string($value)) {
             return false;
         }
         $isNegative = false;
         if ($this->allowNegatives && strpos($value, '-') === 0) {
             $value = substr($value, 1);
             $isNegative = true;
         }
         if (ctype_digit((string) $value)) {
             $value = (int) $value;
             if ($isNegative) {
                 $value = -$value;
             }
         } else {
             return false;
         }
     } elseif (!is_int($value) && !is_float($value) || !$this->allowNegatives && $value < 0) {
         return false;
     }
     if (is_float($value)) {
         if ((int) $value == $value) {
             $value = (int) $value;
         } else {
             return false;
         }
     }
     return parent::validateValue($value, $param, $definitions, $params, $options);
 }
Пример #2
0
 /**
  * Validates the parameters value and returns the result.
  * @see ParamDefinition::validateValue
  *
  * @since 0.5
  *
  * @param $value mixed
  * @param $param IParam
  * @param $definitions array of IParamDefinition
  * @param $params array of IParam
  * @param ValidatorOptions $options
  *
  * @return boolean
  */
 protected function validateValue($value, IParam $param, array $definitions, array $params, ValidatorOptions $options)
 {
     if ($options->isStringlyTyped()) {
         if (preg_match('/^(-)?\\d+((\\.|,)\\d+)?$/', $value)) {
             $value = (double) $value;
         } else {
             return false;
         }
     } elseif (!is_float($value) && !is_int($value)) {
         return false;
     }
     return parent::validateValue($value, $param, $definitions, $params, $options);
 }