From 9e46cadab361647de074e7cc5d727eb60e381458 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 29 Nov 2011 13:40:33 -0600 Subject: [PATCH] Minor refactoring of the Validator. Mainly code formatting and naming changes. --- laravel/validator.php | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/laravel/validator.php b/laravel/validator.php index ffa541fa..9ce05637 100644 --- a/laravel/validator.php +++ b/laravel/validator.php @@ -38,7 +38,7 @@ class Validator { * * @var Database\Connection */ - protected $connection; + protected $db; /** * The language that should be used when retrieving error messages. @@ -160,9 +160,9 @@ protected function check($attribute, $rule) $value = Arr::get($this->attributes, $attribute); - if ( ! $this->validatable($rule, $attribute, $value)) return; + $validatable = $this->validatable($rule, $attribute, $value); - if ( ! $this->{'validate_'.$rule}($attribute, $value, $parameters, $this)) + if ($validatable and ! $this->{'validate_'.$rule}($attribute, $value, $parameters, $this)) { $this->error($attribute, $rule, $parameters); } @@ -182,7 +182,18 @@ protected function check($attribute, $rule) */ protected function validatable($rule, $attribute, $value) { - return ($this->validate_required($attribute, $value) or in_array($rule, array('required', 'accepted'))); + return $this->validate_required($attribute, $value) or $this->implicit($rule); + } + + /** + * Determine if a given rule implies that the attribute is required. + * + * @param string $rule + * @return bool + */ + protected function implicit($rule) + { + return $rule == 'required' or $rule == 'accepted'; } /** @@ -401,9 +412,9 @@ protected function validate_unique($attribute, $value, $parameters) { if ( ! isset($parameters[1])) $parameters[1] = $attribute; - if (is_null($this->connection)) $this->connection = DB::connection(); + if (is_null($this->db)) $this->db = DB::connection(); - return $this->connection->table($parameters[0])->where($parameters[1], '=', $value)->count() == 0; + return $this->db->table($parameters[0])->where($parameters[1], '=', $value)->count() == 0; } /** @@ -678,7 +689,7 @@ public function speaks($language) */ public function connection(\Laravel\Database\Connection $connection) { - $this->connection = $connection; + $this->db = $connection; return $this; } @@ -697,4 +708,4 @@ public function __call($method, $parameters) throw new \BadMethodCallException("Call to undefined method [$method] on Validator instance."); } -} +} \ No newline at end of file