17
17
/**
18
18
* @author GeLo <[email protected] >
19
19
*/
20
- interface MultiHttpAdapterException extends Exception
20
+ class MultiHttpAdapterException extends \Exception implements Exception
21
21
{
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
+
22
44
/**
23
45
* Returns all exceptions
24
46
*
25
47
* @return HttpAdapterException[]
26
48
*/
27
- public function getExceptions ();
49
+ public function getExceptions ()
50
+ {
51
+ return $ this ->exceptions ;
52
+ }
28
53
29
54
/**
30
55
* Checks if a specific exception exists
@@ -33,61 +58,94 @@ public function getExceptions();
33
58
*
34
59
* @return boolean TRUE if there is the exception else FALSE.
35
60
*/
36
- public function hasException (HttpAdapterException $ exception );
61
+ public function hasException (HttpAdapterException $ exception )
62
+ {
63
+ return array_search ($ exception , $ this ->exceptions , true ) !== false ;
64
+ }
37
65
38
66
/**
39
67
* Checks if any exception exists
40
68
*
41
69
* @return boolean
42
70
*/
43
- public function hasExceptions ();
71
+ public function hasExceptions ()
72
+ {
73
+ return !empty ($ this ->exceptions );
74
+ }
44
75
45
76
/**
46
77
* Sets the exceptions
47
78
*
48
79
* @param HttpAdapterException[] $exceptions
49
80
*/
50
- public function setExceptions (array $ exceptions );
81
+ public function setExceptions (array $ exceptions )
82
+ {
83
+ $ this ->clearExceptions ();
84
+ $ this ->addExceptions ($ exceptions );
85
+ }
51
86
52
87
/**
53
88
* Adds an exception
54
89
*
55
90
* @param HttpAdapterException $exception
56
91
*/
57
- public function addException (HttpAdapterException $ exception );
92
+ public function addException (HttpAdapterException $ exception )
93
+ {
94
+ $ this ->exceptions [] = $ exception ;
95
+ }
58
96
59
97
/**
60
98
* Adds some exceptions
61
99
*
62
100
* @param HttpAdapterException[] $exceptions
63
101
*/
64
- public function addExceptions (array $ exceptions );
102
+ public function addExceptions (array $ exceptions )
103
+ {
104
+ foreach ($ exceptions as $ exception ) {
105
+ $ this ->addException ($ exception );
106
+ }
107
+ }
65
108
66
109
/**
67
110
* Removes an exception
68
111
*
69
112
* @param HttpAdapterException $exception
70
113
*/
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
+ }
72
119
73
120
/**
74
121
* Removes some exceptions
75
122
*
76
123
* @param HttpAdapterException[] $exceptions
77
124
*/
78
- public function removeExceptions (array $ exceptions );
125
+ public function removeExceptions (array $ exceptions )
126
+ {
127
+ foreach ($ exceptions as $ exception ) {
128
+ $ this ->removeException ($ exception );
129
+ }
130
+ }
79
131
80
132
/**
81
133
* Clears all exceptions
82
134
*/
83
- public function clearExceptions ();
135
+ public function clearExceptions ()
136
+ {
137
+ $ this ->exceptions = [];
138
+ }
84
139
85
140
/**
86
141
* Returns all responses
87
142
*
88
143
* @return ResponseInterface[]
89
144
*/
90
- public function getResponses ();
145
+ public function getResponses ()
146
+ {
147
+ return $ this ->responses ;
148
+ }
91
149
92
150
/**
93
151
* Checks if a specific response exists
@@ -96,52 +154,82 @@ public function getResponses();
96
154
*
97
155
* @return boolean
98
156
*/
99
- public function hasResponse (ResponseInterface $ response );
157
+ public function hasResponse (ResponseInterface $ response )
158
+ {
159
+ return array_search ($ response , $ this ->responses , true ) !== false ;
160
+ }
100
161
101
162
/**
102
163
* Checks if any response exists
103
164
*
104
165
* @return boolean
105
166
*/
106
- public function hasResponses ();
167
+ public function hasResponses ()
168
+ {
169
+ return !empty ($ this ->responses );
170
+ }
107
171
108
172
/**
109
173
* Sets the responses
110
174
*
111
175
* @param ResponseInterface[] $responses
112
176
*/
113
- public function setResponses (array $ responses );
177
+ public function setResponses (array $ responses )
178
+ {
179
+ $ this ->clearResponses ();
180
+ $ this ->addResponses ($ responses );
181
+ }
114
182
115
183
/**
116
184
* Adds a response
117
185
*
118
186
* @param ResponseInterface $response
119
187
*/
120
- public function addResponse (ResponseInterface $ response );
188
+ public function addResponse (ResponseInterface $ response )
189
+ {
190
+ $ this ->responses [] = $ response ;
191
+ }
121
192
122
193
/**
123
194
* Adds some responses
124
195
*
125
196
* @param ResponseInterface[] $responses
126
197
*/
127
- public function addResponses (array $ responses );
198
+ public function addResponses (array $ responses )
199
+ {
200
+ foreach ($ responses as $ response ) {
201
+ $ this ->addResponse ($ response );
202
+ }
203
+ }
128
204
129
205
/**
130
206
* Removes a response
131
207
*
132
208
* @param ResponseInterface $response
133
209
*/
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
+ }
135
215
136
216
/**
137
217
* Removes some responses
138
218
*
139
219
* @param ResponseInterface[] $responses
140
220
*/
141
- public function removeResponses (array $ responses );
221
+ public function removeResponses (array $ responses )
222
+ {
223
+ foreach ($ responses as $ response ) {
224
+ $ this ->removeResponse ($ response );
225
+ }
226
+ }
142
227
143
228
/**
144
229
* Clears all responses
145
230
*/
146
- public function clearResponses ();
231
+ public function clearResponses ()
232
+ {
233
+ $ this ->responses = [];
234
+ }
147
235
}
0 commit comments