1 <?php
2
3 /**
4 * The MIT License
5 *
6 * Copyright 2014 George Marques <george at georgemarques.com.br>.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
27 namespace Flikore\Validator\Exception;
28
29 /**
30 * An exception thrown if there are any validation error.
31 *
32 * @author George Marques <george at georgemarques.com.br>
33 * @version 0.5.2
34 * @since 0.1
35 * @license http://opensource.org/licenses/MIT MIT
36 * @copyright (c) 2014, George Marques
37 * @package Flikore\Validator
38 * @category Exceptions
39 */
40 class ValidatorException extends \Exception
41 {
42
43 /**
44 * The inner exceptions.
45 * @var ValidatorException[] The inner exceptions.
46 */
47 protected $errors = array();
48
49 /**
50 * Gets the inner exceptions from a set of validators.
51 * @return ValidatorException[] The set of exceptions.
52 */
53 public function getErrors()
54 {
55 return $this->errors;
56 }
57
58 /**
59 * Gets the inner exception for a specific key.
60 * @param string $key The key to get.
61 * @return ValidatorException The exception of that key or null if there's none.
62 */
63 public function getError($key)
64 {
65 return isset($this->errors[$key]) ? $this->errors[$key] : null;
66 }
67
68 /**
69 * Sets the inner exceptions for this exception.
70 * @param array $errors The set of exceptions.
71 */
72 public function setErrors($errors)
73 {
74 $this->errors = $errors;
75 }
76
77 /**
78 * Sets the inner exceptions for a specific key.
79 * @param ValidatorException $error The exception.
80 * @param string $key The key name.
81 */
82 public function setError($error, $key)
83 {
84 $this->errors[$key] = $error;
85 }
86
87 /**
88 * Gets all the messages as an array. If there's no inner exceptions (i.e. this is not a set error), then
89 * an array will be returned with the message being in the key 0. If the inner exceptions are also from a
90 * set, then there'll be an array of messages set to that key (i.e. this will be a nested array of messages).
91 *
92 * @return array The collection of messages.
93 */
94 public function getMessages()
95 {
96 $errors = $this->getErrors();
97
98 if(empty($errors))
99 {
100 return array(0 => $this->getMessage());
101 }
102
103 $result = array();
104
105 foreach ($errors as $key => $err)
106 {
107 $innerErrors = $err->getErrors();
108 if(empty($innerErrors))
109 {
110 $result[$key] = $err->getMessage();
111 }
112 else
113 {
114 $result[$key] = $err->getMessages();
115 }
116 }
117 return $result;
118 }
119 }
120