/** * Validates the length of a value. * * <code> * class Person extends ActiveRecord\Model { * static $validates_length_of = array( * array('name', 'within' => array(1,50)) * ); * } * </code> * * Available options: * * <ul> * <li><b>is:</b> attribute should be exactly n characters long</li> * <li><b>in/within:</b> attribute should be within an range array(min,max)</li> * <li><b>maximum/minimum:</b> attribute should not be above/below respectively</li> * <li><b>message:</b> custome error message</li> * <li><b>allow_blank:</b> allow blank strings</li> * <li><b>allow_null:</b> allow null strings. (Even if this is set to false, a null string is always shorter than a maximum value.)</li> * </ul> * * @param array $attrs Validation definition */ public function validates_length_of($attrs) { $configuration = array_merge(self::$DEFAULT_VALIDATION_OPTIONS, array('too_long' => Errors::$DEFAULT_ERROR_MESSAGES['too_long'], 'too_short' => Errors::$DEFAULT_ERROR_MESSAGES['too_short'], 'wrong_length' => Errors::$DEFAULT_ERROR_MESSAGES['wrong_length'])); foreach ($attrs as $attr) { $options = array_merge($configuration, $attr); $range_options = array_intersect(array_keys(self::$ALL_RANGE_OPTIONS), array_keys($attr)); sort($range_options); switch (sizeof($range_options)) { case 0: throw new ValidationsArgumentError('Range unspecified. Specify the [within], [maximum], or [is] option.'); case 1: break; default: throw new ValidationsArgumentError('Too many range options specified. Choose only one.'); } $attribute = $options[0]; $var = $this->model->{$attribute}; if ($this->is_null_with_option($var, $options) || $this->is_blank_with_option($var, $options)) { continue; } if ($range_options[0] == 'within' || $range_options[0] == 'in') { $range = $options[$range_options[0]]; if (!Utils::is_a('range', $range)) { throw new ValidationsArgumentError("{$range_options['0']} must be an array composing a range of numbers with key [0] being less than key [1]"); } $range_options = array('minimum', 'maximum'); $attr['minimum'] = $range[0]; $attr['maximum'] = $range[1]; } foreach ($range_options as $range_option) { $option = $attr[$range_option]; if ((int) $option <= 0) { throw new ValidationsArgumentError("{$range_option} value cannot use a signed integer."); } if (is_float($option)) { throw new ValidationsArgumentError("{$range_option} value cannot use a float for length."); } if (!($range_option == 'maximum' && is_null($this->model->{$attribute}))) { $messageOptions = array('is' => 'wrong_length', 'minimum' => 'too_short', 'maximum' => 'too_long'); if (isset($options['message'])) { $message = $options['message']; } else { $message = $options[$messageOptions[$range_option]]; } $message = str_replace('%d', $option, $message); $attribute_value = $this->model->{$attribute}; $len = strlen($attribute_value); $value = (int) $attr[$range_option]; if ('maximum' == $range_option && $len > $value) { $this->record->add($attribute, $message); } if ('minimum' == $range_option && $len < $value) { $this->record->add($attribute, $message); } if ('is' == $range_option && $len !== $value) { $this->record->add($attribute, $message); } } } } }