From eb3a5b0dc932003933073fb645194554fc405dd8 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 22 Jun 2011 06:43:48 -0700 Subject: [PATCH 01/11] Improving validation code and comments. --- system/validator.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/system/validator.php b/system/validator.php index a3991996..1a26ca39 100644 --- a/system/validator.php +++ b/system/validator.php @@ -24,7 +24,7 @@ class Validator { public $rules = array(); /** - * Create a new Eloquent validator instance. + * Create a new Validator instance. * * @param mixed $target * @return void @@ -41,7 +41,7 @@ class Validator { } /** - * Create a new Eloquent validator instance. + * Create a new Validator instance. * * @param mixed $target * @return Validator @@ -52,7 +52,7 @@ class Validator { } /** - * Determine if the model passes all of the validation rules. + * Determine if the attributes pass all of the validation rules. * * @return bool */ From a536ceae72d1a24b130c60c4b8692a344a55507a Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 22 Jun 2011 06:52:10 -0700 Subject: [PATCH 02/11] Added default value to Lang::line()->get(). --- system/lang.php | 49 ++++++++++++++++++------------------------------- 1 file changed, 18 insertions(+), 31 deletions(-) diff --git a/system/lang.php b/system/lang.php index 6778c5eb..d74fd017 100644 --- a/system/lang.php +++ b/system/lang.php @@ -2,13 +2,6 @@ class Lang { - /** - * All of the loaded language files. - * - * @var array - */ - private static $loaded = array(); - /** * All of the loaded language lines. * @@ -55,24 +48,30 @@ class Lang { } /** - * Get the language line for a given language. + * Get the language line. * - * @param string $language + * @param mixed $default * @return string */ - public function get($language = null) + public function get($default = null) { - if (is_null($language)) - { - $language = Config::get('application.language'); - } + $language = Config::get('application.language'); list($file, $line) = $this->parse($this->key); $this->load($file, $language); + // -------------------------------------------------------------- + // If the language file did not exist, return the default value. + // -------------------------------------------------------------- + if ( ! array_key_exists($language.$file, static::$lines)) + { + return $default; + } + // -------------------------------------------------------------- // Get the language line from the appropriate file array. + // If the line doesn't exist, return the default value. // -------------------------------------------------------------- if (array_key_exists($line, static::$lines[$language.$file])) { @@ -80,7 +79,7 @@ class Lang { } else { - throw new \Exception("Language line [$line] does not exist for language [$language]"); + return $default; } // -------------------------------------------------------------- @@ -127,27 +126,15 @@ class Lang { private function load($file, $language) { // -------------------------------------------------------------- - // If we have already loaded the language file, bail out. + // If we have already loaded the language file or the file + // doesn't exist, bail out. // -------------------------------------------------------------- - if (in_array($language.$file, static::$loaded)) + if (array_key_exists($language.$file, static::$lines) or ! file_exists($path = APP_PATH.'lang/'.$language.'/'.$file.EXT)) { return; } - // -------------------------------------------------------------- - // Load the language file into the array of lines. The array - // is keyed by the language and file name. - // -------------------------------------------------------------- - if (file_exists($path = APP_PATH.'lang/'.$language.'/'.$file.EXT)) - { - static::$lines[$language.$file] = require $path; - } - else - { - throw new \Exception("Language file [$file] does not exist for language [$language]."); - } - - static::$loaded[] = $language.$file; + static::$lines[$language.$file] = require $path; } /** From 56e500419cbb520313ff01df533efa18fd383af1 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 22 Jun 2011 06:55:58 -0700 Subject: [PATCH 03/11] Improving validation code and comments. --- system/validation/rule.php | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/system/validation/rule.php b/system/validation/rule.php index c70c9c12..4932eaf5 100644 --- a/system/validation/rule.php +++ b/system/validation/rule.php @@ -5,7 +5,7 @@ use System\Lang; abstract class Rule { /** - * The attributes being validated. + * The attributes being validated by the rule. * * @var array */ @@ -22,7 +22,6 @@ abstract class Rule { * Create a new validation Rule instance. * * @param array $attributes - * @param Validator $class * @return void */ public function __construct($attributes) @@ -39,11 +38,6 @@ abstract class Rule { */ public function validate($attributes, $errors) { - if (is_null($this->message)) - { - throw new \Exception("An error message must be specified for every Eloquent validation rule."); - } - foreach ($this->attributes as $attribute) { if ( ! $this->check($attribute, $attributes)) @@ -56,18 +50,28 @@ abstract class Rule { /** * Prepare the message to be added to the error collector. * - * Attribute and size place-holders will replace with their actual values. - * * @param string $attribute * @return string */ private function prepare_message($attribute) { + if (is_null($this->message)) + { + throw new \Exception("An error message must be specified for every Eloquent validation rule."); + } + $message = $this->message; + // --------------------------------------------------------- + // Replace any place-holders with their actual values. + // + // Attribute place-holders are loaded from the language + // directory. If the line doesn't exist, the attribute + // name will be used instead. + // --------------------------------------------------------- if (strpos($message, ':attribute')) { - $message = str_replace(':attribute', Lang::line('attributes.'.$attribute)->get(), $message); + $message = str_replace(':attribute', Lang::line('attributes.'.$attribute)->get($attribute), $message); } if ($this instanceof Rules\Size_Of) From 72c3f1859fa9800bd65b202d7bb48a9583312910 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 22 Jun 2011 07:02:19 -0700 Subject: [PATCH 04/11] Improving validation code and comments. --- system/validation/rules/format_of.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/validation/rules/format_of.php b/system/validation/rules/format_of.php index c615b443..4b969dc0 100644 --- a/system/validation/rules/format_of.php +++ b/system/validation/rules/format_of.php @@ -5,7 +5,7 @@ use System\Validation\Rule; class Format_Of extends Rule { /** - * The regular expression that will be used to evaluate the attribute. + * The regular expression that will be used to validate the attribute. * * @var string */ From 9d8c5d358777d312f87decbd6bc19ce43427fbee Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 22 Jun 2011 07:03:36 -0700 Subject: [PATCH 05/11] Improving validation code and comments. --- system/validation/rules/presence_of.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/validation/rules/presence_of.php b/system/validation/rules/presence_of.php index 06c05fd5..093b30c0 100644 --- a/system/validation/rules/presence_of.php +++ b/system/validation/rules/presence_of.php @@ -12,7 +12,7 @@ class Presence_Of extends Rule { public $allow_empty = false; /** - * Indicates a null should be considered present. + * Indicates null should be considered present. * * @var bool */ From 5956b90735826172e1ace2fe44a8ea75134a3ff8 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 22 Jun 2011 07:06:03 -0700 Subject: [PATCH 06/11] Improving validation code and comments. --- system/validation/rules/size_of.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/validation/rules/size_of.php b/system/validation/rules/size_of.php index 8cefa1f6..1f7e861d 100644 --- a/system/validation/rules/size_of.php +++ b/system/validation/rules/size_of.php @@ -119,7 +119,7 @@ class Size_Of extends Rule { } /** - * Set the minimum and maximize size of the attribute. + * Set the minimum and maximum size of the attribute. * * @param int $minimum * @param int $maximum From 4d892ad3acf22c885b03bd040fad0c743f5fb7ee Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 22 Jun 2011 07:07:49 -0700 Subject: [PATCH 07/11] Improving validation code and comments. --- system/validation/rules/uniqueness_of.php | 1 - 1 file changed, 1 deletion(-) diff --git a/system/validation/rules/uniqueness_of.php b/system/validation/rules/uniqueness_of.php index 7e2a230b..a693d556 100644 --- a/system/validation/rules/uniqueness_of.php +++ b/system/validation/rules/uniqueness_of.php @@ -1,7 +1,6 @@ Date: Wed, 22 Jun 2011 07:09:02 -0700 Subject: [PATCH 08/11] Improving validation code and comments. --- system/validation/rules/with_callback.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/system/validation/rules/with_callback.php b/system/validation/rules/with_callback.php index 7ddbb75e..8630b460 100644 --- a/system/validation/rules/with_callback.php +++ b/system/validation/rules/with_callback.php @@ -5,7 +5,7 @@ use System\Validation\Rule; class With_Callback extends Rule { /** - * The callback. + * The callback that will be used to validate the attribute. * * @var function */ @@ -27,7 +27,7 @@ class With_Callback extends Rule { if ( ! is_callable($this->callback)) { - throw new \Exception("A validation callback for the [$attribute] attribute is not callable."); + throw new \Exception("The validation callback for the [$attribute] attribute is not callable."); } return call_user_func($this->callback, $attributes[$attribute]); From 9f5367f32bc3564b1ddcf9398b69d2085c934352 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 22 Jun 2011 07:26:35 -0700 Subject: [PATCH 09/11] Improving view class comments. --- system/view.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/system/view.php b/system/view.php index 0e93e51a..c4a58a88 100644 --- a/system/view.php +++ b/system/view.php @@ -95,6 +95,9 @@ class View { // We include the view into the local scope within a // try / catch block to catch any exceptions that may // occur while the view is rendering. + // + // Otherwise, a white screen of death will be shown + // when an exception occurs while rendering the view. // ----------------------------------------------------- try { @@ -111,9 +114,8 @@ class View { /** * Get the full path to the view. * - * Views are cascaded, so the application directory views - * will take precedence over the system directory's views - * of the same name. + * Views are cascaded, so the application directory views will take + * precedence over system directory views of the same name. * * @return string */ From dbeb2f4ddb6f54a37505807e3f9a0e54bfeaa027 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 22 Jun 2011 07:27:11 -0700 Subject: [PATCH 10/11] Improving view class comments. --- system/view.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/view.php b/system/view.php index c4a58a88..95b508cf 100644 --- a/system/view.php +++ b/system/view.php @@ -97,7 +97,7 @@ class View { // occur while the view is rendering. // // Otherwise, a white screen of death will be shown - // when an exception occurs while rendering the view. + // if an exception occurs while rendering the view. // ----------------------------------------------------- try { From d7aca820e521f716d18e54f33e01c466998a9ad8 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 22 Jun 2011 10:39:52 -0700 Subject: [PATCH 11/11] Clarified comments in Eloquent\Hydrator. --- system/db/eloquent/hydrator.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/system/db/eloquent/hydrator.php b/system/db/eloquent/hydrator.php index ab068fed..f4ba7e02 100644 --- a/system/db/eloquent/hydrator.php +++ b/system/db/eloquent/hydrator.php @@ -78,8 +78,9 @@ class Hydrator { // ----------------------------------------------------- // Get the relationship Eloquent model. // - // We temporarily spoof the "belongs_to" key to allow - // the query to be fetched without any problems. + // We temporarily spoof the belongs_to key to allow the + // query to be fetched without any problems, since the + // belongs_to method actually gets the attribute. // ----------------------------------------------------- $eloquent->attributes[$spoof = $include.'_id'] = 0;