diff --git a/system/messages.php b/system/messages.php new file mode 100644 index 00000000..f73036b4 --- /dev/null +++ b/system/messages.php @@ -0,0 +1,114 @@ +messages = $messages; + } + + /** + * Add a message to the collector. + * + * Duplicate messages will not be added. + * + * @param string $key + * @param string $message + * @return void + */ + public function add($key, $message) + { + // 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[$key][] = $message; + } + } + + /** + * Determine if messages exist for a given key. + * + * @param string $key + * @return bool + */ + public function has($key) + { + return $this->first($key) !== ''; + } + + /** + * Get the first message for a given key. + * + * @param string $key + * @param string $format + * @return string + */ + public function first($key, $format = ':message') + { + return (count($messages = $this->get($key, $format)) > 0) ? $messages[0] : ''; + } + + /** + * Get all of the messages for a key. + * + * If no key is specified, all of the messages will be returned. + * + * @param string $key + * @param string $format + * @return array + */ + public function get($key = null, $format = ':message') + { + if (is_null($key)) + { + return $this->all($format); + } + + return (array_key_exists($key, $this->messages)) ? $this->format($this->messages[$key], $format) : array(); + } + + /** + * Get all of the messages. + * + * @param string $format + * @return array + */ + public function all($format = ':message') + { + $all = array(); + + foreach ($this->messages as $messages) + { + $all = array_merge($all, $this->format($messages, $format)); + } + + return $all; + } + + /** + * Format an array of messages. + * + * @param array $messages + * @param string $format + * @return array + */ + private function format($messages, $format) + { + array_walk($messages, function(&$message, $key) use ($format) { $message = str_replace(':message', $message, $format); }); + + return $messages; + } + +} \ No newline at end of file diff --git a/system/validation/errors.php b/system/validation/errors.php deleted file mode 100644 index 6d04dc33..00000000 --- a/system/validation/errors.php +++ /dev/null @@ -1,114 +0,0 @@ -messages = $messages; - } - - /** - * Add an error message to the collector. - * - * Duplicate messages will not be added. - * - * @param string $attribute - * @param string $message - * @return void - */ - public function add($attribute, $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])) - { - $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 - * @param string $format - * @return string - */ - public function first($attribute, $format = ':message') - { - return (count($messages = $this->get($attribute, $format)) > 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 - * @param string $format - * @return array - */ - public function get($attribute = null, $format = ':message') - { - if (is_null($attribute)) - { - return $this->all($format); - } - - return (array_key_exists($attribute, $this->messages)) ? $this->format($this->messages[$attribute], $format) : array(); - } - - /** - * Get all of the error messages. - * - * @param string $format - * @return array - */ - public function all($format = ':message') - { - $all = array(); - - foreach ($this->messages as $messages) - { - $all = array_merge($all, $this->format($messages, $format)); - } - - return $all; - } - - /** - * Format an array of messages. - * - * @param array $messages - * @param string $format - * @return array - */ - private function format($messages, $format) - { - array_walk($messages, function(&$message, $key) use ($format) { $message = str_replace(':message', $message, $format); }); - - return $messages; - } - -} \ No newline at end of file diff --git a/system/validator.php b/system/validator.php index a69a6978..0e53372b 100644 --- a/system/validator.php +++ b/system/validator.php @@ -94,7 +94,7 @@ class Validator { */ public function valid() { - $this->errors = new Validation\Errors; + $this->errors = new Messages; foreach ($this->rules as $attribute => $rules) {