Commit 86075c27 authored by Taylor Otwell's avatar Taylor Otwell

moved validation/errors into system/messages... not just useful for error messages.

parent f3ddadce
<?php namespace System\Validation;
<?php namespace System;
class Errors {
class Messages {
/**
* All of the error messages.
* All of the messages.
*
* @var array
*/
public $messages;
/**
* Create a new Errors instance.
* Create a new Messages instance.
*
* @return void
*/
......@@ -20,67 +20,67 @@ class Errors {
}
/**
* Add an error message to the collector.
* Add a message to the collector.
*
* Duplicate messages will not be added.
*
* @param string $attribute
* @param string $key
* @param string $message
* @return void
*/
public function add($attribute, $message)
public function add($key, $message)
{
// Make sure the error message is not duplicated.
if ( ! array_key_exists($attribute, $this->messages) or ! is_array($this->messages[$attribute]) or ! in_array($message, $this->messages[$attribute]))
// Make sure the message is not duplicated.
if ( ! array_key_exists($key, $this->messages) or ! is_array($this->messages[$key]) or ! in_array($message, $this->messages[$key]))
{
$this->messages[$attribute][] = $message;
$this->messages[$key][] = $message;
}
}
/**
* Determine if errors exist for an attribute.
* Determine if messages exist for a given key.
*
* @param string $attribute
* @param string $key
* @return bool
*/
public function has($attribute)
public function has($key)
{
return $this->first($attribute) !== '';
return $this->first($key) !== '';
}
/**
* Get the first error message for an attribute.
* Get the first message for a given key.
*
* @param string $attribute
* @param string $key
* @param string $format
* @return string
*/
public function first($attribute, $format = ':message')
public function first($key, $format = ':message')
{
return (count($messages = $this->get($attribute, $format)) > 0) ? $messages[0] : '';
return (count($messages = $this->get($key, $format)) > 0) ? $messages[0] : '';
}
/**
* Get all of the error messages for an attribute.
* Get all of the messages for a key.
*
* If no attribute is specified, all of the error messages will be returned.
* If no key is specified, all of the messages will be returned.
*
* @param string $attribute
* @param string $key
* @param string $format
* @return array
*/
public function get($attribute = null, $format = ':message')
public function get($key = null, $format = ':message')
{
if (is_null($attribute))
if (is_null($key))
{
return $this->all($format);
}
return (array_key_exists($attribute, $this->messages)) ? $this->format($this->messages[$attribute], $format) : array();
return (array_key_exists($key, $this->messages)) ? $this->format($this->messages[$key], $format) : array();
}
/**
* Get all of the error messages.
* Get all of the messages.
*
* @param string $format
* @return array
......
......@@ -94,7 +94,7 @@ class Validator {
*/
public function valid()
{
$this->errors = new Validation\Errors;
$this->errors = new Messages;
foreach ($this->rules as $attribute => $rules)
{
......
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