|
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
|
|
|
public function __construct( |
|
41
|
|
|
AnnotationReader $annotationReader, |
|
42
|
|
|
AnnotationParserFactory $annotationParserFactory |
|
43
|
|
|
) { |
|
44
|
|
|
$this->annotationReader = $annotationReader; |
|
45
|
|
|
$this->annotationParserFactory = $annotationParserFactory; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
88 |
|
public function createRequestParser( |
|
49
|
|
|
$className, |
|
50
|
|
|
$parserObject, |
|
51
|
|
|
RequestBuilder $requestBuilder |
|
52
|
88 |
|
) { |
|
53
|
88 |
|
$class = new \ReflectionClass($className); |
|
54
|
88 |
|
$parserObject->setRequestObject(new $className); |
|
55
|
|
|
foreach ($class->getProperties() as $property) { |
|
56
|
|
|
$propertyParserBuilder = new PropertyParserBuilder($property->getName()); |
|
57
|
|
|
foreach ($this->annotationReader->getPropertyAnnotations($property) as $propertyAnnotation) { |
|
58
|
|
|
$this->annotationParserFactory |
|
59
|
|
|
->getAnnotationParserFor($propertyAnnotation) |
|
60
|
|
|
->execute($propertyAnnotation, $propertyParserBuilder); |
|
61
|
86 |
|
} |
|
62
|
|
|
$parserObject->addPropertyParser($propertyParserBuilder->build($requestBuilder)); |
|
63
|
|
|
} |
|
64
|
|
|
return $parserObject; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|