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\InvalidRequestException; |
21
|
|
|
use Psr\Cache\CacheItemPoolInterface as PsrCache; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Builds a request by parsing all the resulting object's annotations and running |
25
|
|
|
* obtained filters and validators against the request. |
26
|
|
|
* |
27
|
|
|
* @author mcustiel |
28
|
|
|
*/ |
29
|
|
|
class RequestBuilder |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var \Psr\Cache\CacheItemPoolInterface |
33
|
|
|
*/ |
34
|
|
|
private $cache; |
35
|
|
|
/** |
36
|
|
|
* @var ParserGenerator |
37
|
|
|
*/ |
38
|
|
|
private $parserGenerator; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Class constructor. |
42
|
|
|
* |
43
|
|
|
* @param \stdClass $cacheConfig |
|
|
|
|
44
|
|
|
* Config parameters for cache. By default cache is activated and saves files |
45
|
|
|
* under system's temp dir. This parameter is used to set alternative options. |
46
|
|
|
* |
47
|
|
|
*/ |
48
|
89 |
|
public function __construct( |
49
|
|
|
PsrCache $cache, |
50
|
|
|
ParserGenerator $parserGenerator |
51
|
|
|
) { |
52
|
89 |
|
$this->cache = $cache; |
53
|
89 |
|
$this->parserGenerator = $parserGenerator; |
54
|
89 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Main method of this class. Used to convert a request to an object of a given class by |
58
|
|
|
* using a requestParser. |
59
|
|
|
* |
60
|
|
|
* @param array|\stdClass $request The request to convert to an object. |
61
|
|
|
* @param string $className The class of the object to which the request must be converted. |
62
|
|
|
* @param string $behaviour The behaviour of the parser. |
|
|
|
|
63
|
|
|
*/ |
64
|
89 |
|
public function parseRequest( |
65
|
|
|
$request, |
66
|
|
|
$className, |
67
|
|
|
RequestParser $requestParser |
68
|
|
|
) { |
69
|
89 |
|
return $this->generateRequestParserObject($className, $requestParser) |
70
|
89 |
|
->parse($this->sanitizeRequestOrThrowExceptionIfInvalid($request)); |
71
|
|
|
} |
72
|
|
|
|
73
|
89 |
|
private function generateRequestParserObject($className, $parser) |
74
|
|
|
{ |
75
|
89 |
|
$cacheKey = str_replace('\\', '', $className . get_class($parser)); |
76
|
89 |
|
$cacheItem = $this->cache->getItem($cacheKey); |
77
|
89 |
|
$return = $cacheItem->get(); |
78
|
89 |
|
if ($return === null) { |
79
|
87 |
|
$return = $this->parserGenerator->createRequestParser($className, $parser, $this); |
80
|
87 |
|
$cacheItem->set($return); |
81
|
87 |
|
$this->cache->save($cacheItem); |
82
|
87 |
|
} |
83
|
|
|
|
84
|
89 |
|
return $return; |
85
|
|
|
} |
86
|
|
|
|
87
|
89 |
|
private function sanitizeRequestOrThrowExceptionIfInvalid($request) |
88
|
|
|
{ |
89
|
89 |
|
$isObject = ($request instanceof \stdClass); |
90
|
89 |
|
if (!is_array($request) && !$isObject) { |
91
|
1 |
|
throw new InvalidRequestException( |
92
|
|
|
'Request builder is intended to be used with arrays or instances of \\stdClass' |
93
|
1 |
|
); |
94
|
|
|
} |
95
|
88 |
|
return $isObject ? json_decode(json_encode($request), true) : $request; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.