/** * Validates the length of a value. * * <code> * class Person extends Activerecord\Model { * static $validatesLengthOf = 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 validatesLengthOf($attrs) { $configuration = \array_merge(self::$default_validation_options, ['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 ExceptionValidation('Argument Error Range unspecified. Specify the [within], [maximum], or [is] option.'); case 1: break; default: throw new ExceptionValidation('Argument Error Too many range options specified. Choose only one.'); } $attribute = $options[0]; $var = $this->model->{$attribute}; if ($this->isNullWithOption($var, $options) || $this->isBlankWithOption($var, $options)) { continue; } if ($range_options[0] == 'within' || $range_options[0] == 'in') { $range = $options[$range_options[0]]; if (!Utils::isArray('range', $range)) { throw new ExceptionValidation("Argument Error {$range_options['0']} must be an array composing a range of numbers with key [0] being less than key [1]"); } $range_options = ['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 ExceptionValidation("Argument Error {$range_option} value cannot use a signed integer."); } if (\is_float($option)) { throw new ExceptionValidation("Argument Error {$range_option} value cannot use a float for length."); } if (!($range_option == 'maximum' && \is_null($this->model->{$attribute}))) { $messageOptions = ['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); } } } } }