|
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
|
|
|
/** |
|
42
|
|
|
* @param \Psr\Cache\CacheItemPoolInterface $cache |
|
43
|
|
|
* @param \Mcustiel\SimpleRequest\ParserGenerator $parserGenerator |
|
44
|
|
|
*/ |
|
45
|
97 |
|
public function __construct( |
|
46
|
|
|
PsrCache $cache, |
|
47
|
|
|
ParserGenerator $parserGenerator |
|
48
|
|
|
) { |
|
49
|
97 |
|
$this->cache = $cache; |
|
50
|
97 |
|
$this->parserGenerator = $parserGenerator; |
|
51
|
97 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Main method of this class. Used to convert a request to an object of a given class by |
|
55
|
|
|
* using a requestParser. |
|
56
|
|
|
* |
|
57
|
|
|
* @param array|\stdClass $request The request to convert to an object. |
|
58
|
|
|
* @param string $className The class of the object to which the request must be converted. |
|
59
|
|
|
* @param \Mcustiel\SimpleRequest\RequestParser $requestParser The behaviour of the parser. |
|
60
|
|
|
*/ |
|
61
|
95 |
|
public function parseRequest( |
|
62
|
|
|
$request, |
|
63
|
|
|
$className, |
|
64
|
|
|
RequestParser $requestParser |
|
65
|
|
|
) { |
|
66
|
95 |
|
return $this->generateRequestParserObject($className, $requestParser) |
|
67
|
95 |
|
->parse($this->sanitizeRequestOrThrowExceptionIfInvalid($request)); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
95 |
|
private function generateRequestParserObject($className, $parser) |
|
71
|
|
|
{ |
|
72
|
95 |
|
$cacheKey = str_replace('\\', '', $className . get_class($parser)); |
|
73
|
95 |
|
$cacheItem = $this->cache->getItem($cacheKey); |
|
74
|
95 |
|
$return = $cacheItem->get(); |
|
75
|
95 |
|
if ($return === null) { |
|
76
|
93 |
|
$return = $this->parserGenerator->createRequestParser($className, $parser, $this); |
|
77
|
93 |
|
$cacheItem->set($return); |
|
78
|
93 |
|
$this->cache->save($cacheItem); |
|
79
|
93 |
|
} |
|
80
|
|
|
|
|
81
|
95 |
|
return $return; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
95 |
|
private function sanitizeRequestOrThrowExceptionIfInvalid($request) |
|
85
|
|
|
{ |
|
86
|
95 |
|
$isObject = ($request instanceof \stdClass); |
|
87
|
95 |
|
if (!is_array($request) && !$isObject) { |
|
88
|
1 |
|
throw new InvalidRequestException( |
|
89
|
|
|
'Request builder is intended to be used with arrays or instances of \\stdClass' |
|
90
|
1 |
|
); |
|
91
|
|
|
} |
|
92
|
94 |
|
return $isObject ? json_decode(json_encode($request), true) : $request; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|