Commit b7b258a1 by Taylor Otwell

removed error collector bloat, switched to plain array. thanks mikelbring.

parent 4a5190df
......@@ -21,7 +21,7 @@ class HTML {
*/
public static function script($url)
{
return '<script type="text/javascript" src="'.trim(static::entities(URL::to_asset($url)), '.js').'.js"></script>'.PHP_EOL;
return '<script type="text/javascript" src="'.static::entities(URL::to_asset($url)).'"></script>'.PHP_EOL;
}
/**
......@@ -32,7 +32,7 @@ class HTML {
*/
public static function style($url, $media = 'all')
{
return '<link href="'.trim(static::entities(URL::to_asset($url)), '.css').'.css" rel="stylesheet" type="text/css" media="'.$media.'" />'.PHP_EOL;
return '<link href="'.static::entities(URL::to_asset($url)).'" rel="stylesheet" type="text/css" media="'.$media.'" />'.PHP_EOL;
}
/**
......
<?php namespace System\Validation;
class Error_Collector {
/**
* All of the error messages.
*
* @var array
*/
public $messages;
/**
* Create a new Error Collector instance.
*
* @return void
*/
public function __construct($messages = array())
{
$this->messages = $messages;
}
/**
* Add an error message to the collector.
*
* @param string $attribute
* @param string $message
* @return void
*/
public function add($attribute, $message)
{
$this->messages[$attribute][] = $message;
}
/**
* Determine if errors exist for an attribute.
*
* @param string $attribute
* @return bool
*/
public function has($attribute)
{
return $this->first($attribute) !== '';
}
/**
* Get the first error message for an attribute.
*
* @param string $attribute
* @return string
*/
public function first($attribute)
{
return (count($messages = $this->get($attribute)) > 0) ? $messages[0] : '';
}
/**
* Get all of the error messages for an attribute.
*
* If no attribute is specified, all of the error messages will be returned.
*
* @param string $attribute
* @return array
*/
public function get($attribute = null)
{
if (is_null($attribute))
{
$all = array();
foreach ($this->messages as $messages)
{
$all = array_merge($all, $messages);
}
return $all;
}
return (array_key_exists($attribute, $this->messages)) ? $this->messages[$attribute] : array();
}
}
\ No newline at end of file
......@@ -32,17 +32,17 @@ abstract class Rule {
/**
* Run the validation rule.
*
* @param array $attributes
* @param Error_Collector $errors
* @param array $attributes
* @param array $errors
* @return void
*/
public function validate($attributes, $errors)
public function validate($attributes, &$errors)
{
foreach ($this->attributes as $attribute)
{
if ( ! $this->check($attribute, $attributes))
{
$errors->add($attribute, $this->prepare_message($attribute));
$errors[$attribute][] = $this->prepare_message($attribute);
}
}
}
......
......@@ -10,9 +10,9 @@ class Validator {
public $attributes;
/**
* The validation error collector.
* The validation errors
*
* @var Error_Collector
* @var array
*/
public $errors;
......@@ -36,8 +36,6 @@ class Validator {
// attributes as the validation attributes.
// ---------------------------------------------------------
$this->attributes = ($target instanceof DB\Eloquent) ? $target->attributes : (array) $target;
$this->errors = new Validation\Error_Collector;
}
/**
......@@ -58,7 +56,7 @@ class Validator {
*/
public function is_valid()
{
$this->errors->messages = array();
$this->errors = array();
foreach ($this->rules as $rule)
{
......@@ -69,7 +67,7 @@ class Validator {
$rule->validate($this->attributes, $this->errors);
}
return count($this->errors->messages) == 0;
return count($this->errors) == 0;
}
/**
......
......@@ -34,16 +34,6 @@ class View {
{
$this->view = $view;
$this->data = $data;
// -----------------------------------------------------
// Every view has an error collector. This makes it
// convenient to check for any validation errors without
// worrying if the error collector is instantiated.
//
// If an error collector is in the session, it will
// be used as the error collector for the view.
// -----------------------------------------------------
$this->data['errors'] = (Config::get('session.driver') != '' and Session::has('errors')) ? Session::get('errors') : new Validation\Error_Collector;
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment