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 Doctrine\Common\Annotations\AnnotationReader; |
21
|
|
|
use Mcustiel\SimpleRequest\Strategies\AnnotationParserFactory; |
22
|
|
|
use Mcustiel\SimpleRequest\Strategies\PropertyParserBuilder; |
23
|
|
|
|
24
|
|
|
class ParserGenerator |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var \Doctrine\Common\Annotations\AnnotationReader |
28
|
|
|
*/ |
29
|
|
|
private $annotationReader; |
30
|
|
|
/** |
31
|
|
|
* @var \Mcustiel\SimpleRequest\Strategies\AnnotationParserFactory |
32
|
|
|
*/ |
33
|
|
|
private $annotationParserFactory; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param \Doctrine\Common\Annotations\AnnotationReader $annotationReader |
37
|
|
|
* External annotation reader instance (mostly for DI in tests). Created |
38
|
|
|
* if not is set. |
39
|
|
|
*/ |
40
|
97 |
|
public function __construct( |
41
|
|
|
AnnotationReader $annotationReader, |
42
|
|
|
AnnotationParserFactory $annotationParserFactory |
43
|
|
|
) { |
44
|
97 |
|
$this->annotationReader = $annotationReader; |
45
|
97 |
|
$this->annotationParserFactory = $annotationParserFactory; |
46
|
97 |
|
} |
47
|
|
|
|
48
|
93 |
|
public function createRequestParser( |
49
|
|
|
$className, |
50
|
|
|
$parserObject, |
51
|
|
|
RequestBuilder $requestBuilder |
52
|
|
|
) { |
53
|
93 |
|
$class = new \ReflectionClass($className); |
54
|
93 |
|
$parserObject->setRequestObject(new $className); |
55
|
93 |
|
foreach ($class->getProperties() as $property) { |
56
|
93 |
|
$propertyParserBuilder = new PropertyParserBuilder($property->getName()); |
57
|
93 |
|
foreach ($this->annotationReader->getPropertyAnnotations($property) as $propertyAnnotation) { |
58
|
93 |
|
$this->annotationParserFactory |
59
|
93 |
|
->getAnnotationParserFor($propertyAnnotation) |
60
|
93 |
|
->execute($propertyAnnotation, $propertyParserBuilder); |
61
|
93 |
|
} |
62
|
93 |
|
$parserObject->addPropertyParser($propertyParserBuilder->build($requestBuilder)); |
63
|
93 |
|
} |
64
|
93 |
|
return $parserObject; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|