1717/**
1818 * @author GeLo <geloen.eric@gmail.com>
1919 */
20- interface MultiHttpAdapterException extends Exception
20+ class MultiHttpAdapterException extends \Exception implements Exception
2121{
22+ /**
23+ * @var HttpAdapterException[]
24+ */
25+ private $ exceptions ;
26+
27+ /**
28+ * @var ResponseInterface[]
29+ */
30+ private $ responses ;
31+
32+ /**
33+ * @param HttpAdapterException[] $exceptions
34+ * @param ResponseInterface[] $responses
35+ */
36+ public function __construct (array $ exceptions = [], array $ responses = [])
37+ {
38+ parent ::__construct ('An error occurred when sending multiple requests. ' );
39+
40+ $ this ->setExceptions ($ exceptions );
41+ $ this ->setResponses ($ responses );
42+ }
43+
2244 /**
2345 * Returns all exceptions
2446 *
2547 * @return HttpAdapterException[]
2648 */
27- public function getExceptions ();
49+ public function getExceptions ()
50+ {
51+ return $ this ->exceptions ;
52+ }
2853
2954 /**
3055 * Checks if a specific exception exists
@@ -33,61 +58,94 @@ public function getExceptions();
3358 *
3459 * @return boolean TRUE if there is the exception else FALSE.
3560 */
36- public function hasException (HttpAdapterException $ exception );
61+ public function hasException (HttpAdapterException $ exception )
62+ {
63+ return array_search ($ exception , $ this ->exceptions , true ) !== false ;
64+ }
3765
3866 /**
3967 * Checks if any exception exists
4068 *
4169 * @return boolean
4270 */
43- public function hasExceptions ();
71+ public function hasExceptions ()
72+ {
73+ return !empty ($ this ->exceptions );
74+ }
4475
4576 /**
4677 * Sets the exceptions
4778 *
4879 * @param HttpAdapterException[] $exceptions
4980 */
50- public function setExceptions (array $ exceptions );
81+ public function setExceptions (array $ exceptions )
82+ {
83+ $ this ->clearExceptions ();
84+ $ this ->addExceptions ($ exceptions );
85+ }
5186
5287 /**
5388 * Adds an exception
5489 *
5590 * @param HttpAdapterException $exception
5691 */
57- public function addException (HttpAdapterException $ exception );
92+ public function addException (HttpAdapterException $ exception )
93+ {
94+ $ this ->exceptions [] = $ exception ;
95+ }
5896
5997 /**
6098 * Adds some exceptions
6199 *
62100 * @param HttpAdapterException[] $exceptions
63101 */
64- public function addExceptions (array $ exceptions );
102+ public function addExceptions (array $ exceptions )
103+ {
104+ foreach ($ exceptions as $ exception ) {
105+ $ this ->addException ($ exception );
106+ }
107+ }
65108
66109 /**
67110 * Removes an exception
68111 *
69112 * @param HttpAdapterException $exception
70113 */
71- public function removeException (HttpAdapterException $ exception );
114+ public function removeException (HttpAdapterException $ exception )
115+ {
116+ unset($ this ->exceptions [array_search ($ exception , $ this ->exceptions , true )]);
117+ $ this ->exceptions = array_values ($ this ->exceptions );
118+ }
72119
73120 /**
74121 * Removes some exceptions
75122 *
76123 * @param HttpAdapterException[] $exceptions
77124 */
78- public function removeExceptions (array $ exceptions );
125+ public function removeExceptions (array $ exceptions )
126+ {
127+ foreach ($ exceptions as $ exception ) {
128+ $ this ->removeException ($ exception );
129+ }
130+ }
79131
80132 /**
81133 * Clears all exceptions
82134 */
83- public function clearExceptions ();
135+ public function clearExceptions ()
136+ {
137+ $ this ->exceptions = [];
138+ }
84139
85140 /**
86141 * Returns all responses
87142 *
88143 * @return ResponseInterface[]
89144 */
90- public function getResponses ();
145+ public function getResponses ()
146+ {
147+ return $ this ->responses ;
148+ }
91149
92150 /**
93151 * Checks if a specific response exists
@@ -96,52 +154,82 @@ public function getResponses();
96154 *
97155 * @return boolean
98156 */
99- public function hasResponse (ResponseInterface $ response );
157+ public function hasResponse (ResponseInterface $ response )
158+ {
159+ return array_search ($ response , $ this ->responses , true ) !== false ;
160+ }
100161
101162 /**
102163 * Checks if any response exists
103164 *
104165 * @return boolean
105166 */
106- public function hasResponses ();
167+ public function hasResponses ()
168+ {
169+ return !empty ($ this ->responses );
170+ }
107171
108172 /**
109173 * Sets the responses
110174 *
111175 * @param ResponseInterface[] $responses
112176 */
113- public function setResponses (array $ responses );
177+ public function setResponses (array $ responses )
178+ {
179+ $ this ->clearResponses ();
180+ $ this ->addResponses ($ responses );
181+ }
114182
115183 /**
116184 * Adds a response
117185 *
118186 * @param ResponseInterface $response
119187 */
120- public function addResponse (ResponseInterface $ response );
188+ public function addResponse (ResponseInterface $ response )
189+ {
190+ $ this ->responses [] = $ response ;
191+ }
121192
122193 /**
123194 * Adds some responses
124195 *
125196 * @param ResponseInterface[] $responses
126197 */
127- public function addResponses (array $ responses );
198+ public function addResponses (array $ responses )
199+ {
200+ foreach ($ responses as $ response ) {
201+ $ this ->addResponse ($ response );
202+ }
203+ }
128204
129205 /**
130206 * Removes a response
131207 *
132208 * @param ResponseInterface $response
133209 */
134- public function removeResponse (ResponseInterface $ response );
210+ public function removeResponse (ResponseInterface $ response )
211+ {
212+ unset($ this ->responses [array_search ($ response , $ this ->responses , true )]);
213+ $ this ->responses = array_values ($ this ->responses );
214+ }
135215
136216 /**
137217 * Removes some responses
138218 *
139219 * @param ResponseInterface[] $responses
140220 */
141- public function removeResponses (array $ responses );
221+ public function removeResponses (array $ responses )
222+ {
223+ foreach ($ responses as $ response ) {
224+ $ this ->removeResponse ($ response );
225+ }
226+ }
142227
143228 /**
144229 * Clears all responses
145230 */
146- public function clearResponses ();
231+ public function clearResponses ()
232+ {
233+ $ this ->responses = [];
234+ }
147235}
0 commit comments