a tiny mvc framework for php using php-activerecord
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Refactoring validates_length_of

Benjamin 7a8aac2c efa7c44d

+32 -28
+7 -25
lib/php-activerecord/lib/Validations.php
··· 487 487 488 488 $attribute = $options[0]; 489 489 $var = $this->model->$attribute; 490 - $range_option = $range_options[0]; 491 - 492 490 if ($this->is_null_with_option($var, $options) || $this->is_blank_with_option($var, $options)) 493 491 continue; 494 - 495 - if ('within' == $range_option || 'in' == $range_option) 492 + if ($range_options[0] == 'within' || $range_options[0] == 'in') 496 493 { 497 494 $range = $options[$range_options[0]]; 498 495 499 496 if (!(Utils::is_a('range', $range))) 500 497 throw new ValidationsArgumentError("$range_option must be an array composing a range of numbers with key [0] being less than key [1]"); 501 - 502 - if (is_float($range[0]) || is_float($range[1])) 503 - throw new ValidationsArgumentError("Range values cannot use floats for length."); 504 - 505 - if ((int)$range[0] <= 0 || (int)$range[1] <= 0) 506 - throw new ValidationsArgumentError("Range values cannot use signed integers."); 507 - 508 - $too_short = isset($options['message']) ? $options['message'] : $options['too_short']; 509 - $too_long = isset($options['message']) ? $options['message'] : $options['too_long']; 510 - 511 - $too_short = str_replace('%d', $range[0], $too_short); 512 - $too_long = str_replace('%d', $range[1], $too_long); 513 - 514 - if (strlen($this->model->$attribute) < (int)$range[0]) 515 - $this->record->add($attribute, $too_short); 516 - elseif (strlen($this->model->$attribute) > (int)$range[1]) 517 - $this->record->add($attribute, $too_long); 498 + $range_options = array('minimum', 'maximum'); 499 + $attr['minimum'] = $range[0]; 500 + $attr['maximum'] = $range[1]; 518 501 } 519 - 520 - elseif ('is' == $range_option || 'minimum' == $range_option || 'maximum' == $range_option) 502 + foreach ($range_options as $range_option) 521 503 { 522 - $option = $options[$range_option]; 504 + $option = $attr[$range_option]; 523 505 524 506 if ((int)$option <= 0) 525 507 throw new ValidationsArgumentError("$range_option value cannot use a signed integer."); ··· 527 509 if (is_float($option)) 528 510 throw new ValidationsArgumentError("$range_option value cannot use a float for length."); 529 511 530 - if (!is_null($this->model->$attribute)) 512 + if (!($range_option == 'maximum' && is_null($this->model->$attribute))) 531 513 { 532 514 $messageOptions = array('is' => 'wrong_length', 'minimum' => 'too_short', 'maximum' => 'too_long'); 533 515
+25 -3
lib/php-activerecord/test/ValidatesLengthOfTest.php
··· 126 126 $this->assert_equals('is too short (minimum is 1 characters)', $book->errors->on('name')); 127 127 } 128 128 129 - public function test_invalid_null() 129 + public function test_invalid_null_within() 130 130 { 131 131 BookLength::$validates_length_of[0]['within'] = array(1, 3); 132 132 ··· 136 136 $this->assert_true($book->errors->is_invalid('name')); 137 137 $this->assert_equals('is too short (minimum is 1 characters)', $book->errors->on('name')); 138 138 } 139 + 140 + public function test_invalid_null_minimum() 141 + { 142 + BookLength::$validates_length_of[0]['minimum'] = 1; 143 + 144 + $book = new BookLength; 145 + $book->name = null; 146 + $book->save(); 147 + $this->assert_true($book->errors->is_invalid('name')); 148 + $this->assert_equals('is too short (minimum is 1 characters)', $book->errors->on('name')); 149 + 150 + } 151 + 152 + public function test_valid_null_maximum() 153 + { 154 + BookLength::$validates_length_of[0]['maximum'] = 1; 155 + 156 + $book = new BookLength; 157 + $book->name = null; 158 + $book->save(); 159 + $this->assert_false($book->errors->is_invalid('name')); 160 + } 139 161 140 162 public function test_float_as_impossible_range_option() 141 163 { ··· 145 167 try { 146 168 $book->save(); 147 169 } catch (ActiveRecord\ValidationsArgumentError $e) { 148 - $this->assert_equals('Range values cannot use floats for length.', $e->getMessage()); 170 + $this->assert_equals('maximum value cannot use a float for length.', $e->getMessage()); 149 171 } 150 172 151 173 $this->set_up(); ··· 171 193 try { 172 194 $book->save(); 173 195 } catch (ActiveRecord\ValidationsArgumentError $e) { 174 - $this->assert_equals('Range values cannot use signed integers.', $e->getMessage()); 196 + $this->assert_equals('minimum value cannot use a signed integer.', $e->getMessage()); 175 197 return; 176 198 } 177 199