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 * @var array
*/ */
public $messages; public $messages;
/** /**
* Create a new Errors instance. * Create a new Messages instance.
* *
* @return void * @return void
*/ */
...@@ -20,67 +20,67 @@ class Errors { ...@@ -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. * Duplicate messages will not be added.
* *
* @param string $attribute * @param string $key
* @param string $message * @param string $message
* @return void * @return void
*/ */
public function add($attribute, $message) public function add($key, $message)
{ {
// Make sure the error message is not duplicated. // Make sure the message is not duplicated.
if ( ! array_key_exists($attribute, $this->messages) or ! is_array($this->messages[$attribute]) or ! in_array($message, $this->messages[$attribute])) 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 * @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 * @param string $format
* @return string * @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 * @param string $format
* @return array * @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 $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 * @param string $format
* @return array * @return array
......
...@@ -94,7 +94,7 @@ class Validator { ...@@ -94,7 +94,7 @@ class Validator {
*/ */
public function valid() public function valid()
{ {
$this->errors = new Validation\Errors; $this->errors = new Messages;
foreach ($this->rules as $attribute => $rules) 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