initial commit of validation classes.
This commit is contained in:
70
system/validation/error_collector.php
Normal file
70
system/validation/error_collector.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user