1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of php-simple-request. |
4
|
|
|
* |
5
|
|
|
* php-simple-request is free software: you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
7
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* php-simple-request is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License |
16
|
|
|
* along with php-simple-request. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
namespace Mcustiel\SimpleRequest; |
19
|
|
|
|
20
|
|
|
use Mcustiel\SimpleRequest\Exception\InvalidValueException; |
21
|
|
|
use Mcustiel\SimpleRequest\Exception\InvalidRequestException; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Request parser object that returns the parsing errors for all the |
25
|
|
|
* invalid properties. |
26
|
|
|
* |
27
|
|
|
* @author mcustiel |
28
|
|
|
*/ |
29
|
|
|
class AllErrorsRequestParser extends RequestParser |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Parses a request and returns a response object containing the converted object |
33
|
|
|
* and the list of errors. |
34
|
|
|
* |
35
|
|
|
* @param array|\stdClass $request |
36
|
|
|
* |
37
|
|
|
* @return ParserResponse |
38
|
|
|
*/ |
39
|
94 |
|
public function parse($request) |
40
|
|
|
{ |
41
|
94 |
|
$object = clone $this->requestObject; |
42
|
94 |
|
$invalidValues = []; |
43
|
|
|
|
44
|
94 |
|
foreach ($this->propertyParsers as $propertyParser) { |
45
|
|
|
try { |
46
|
94 |
|
$this->setProperty($request, $object, $propertyParser); |
|
|
|
|
47
|
94 |
|
} catch (InvalidValueException $e) { |
48
|
62 |
|
$invalidValues[$propertyParser->getName()] = $e->getMessage(); |
49
|
|
|
} |
50
|
94 |
|
} |
51
|
94 |
|
$this->checkIfRequestIsValidOrThrowException($invalidValues); |
52
|
|
|
|
53
|
32 |
|
return $object; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Checks if there are invalid values in the request, in that case it throws |
58
|
|
|
* an exception. |
59
|
|
|
* |
60
|
|
|
* @param array $invalidValues |
61
|
|
|
* |
62
|
|
|
* @throws \Mcustiel\SimpleRequest\Exception\InvalidRequestException |
63
|
|
|
*/ |
64
|
94 |
|
private function checkIfRequestIsValidOrThrowException($invalidValues) |
65
|
|
|
{ |
66
|
94 |
|
if (!empty($invalidValues)) { |
67
|
62 |
|
$exception = new InvalidRequestException('Errors occurred while parsing the request'); |
68
|
62 |
|
$exception->setErrors($invalidValues); |
69
|
62 |
|
throw $exception; |
70
|
|
|
} |
71
|
32 |
|
} |
72
|
|
|
} |
73
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.