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