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\Util\ValidatorBuilder; |
22
|
|
|
use Mcustiel\SimpleRequest\Annotation\RequestAnnotation; |
23
|
|
|
use Mcustiel\SimpleRequest\Annotation\ValidatorAnnotation; |
24
|
|
|
use Mcustiel\SimpleRequest\Annotation\FilterAnnotation; |
25
|
|
|
use Mcustiel\SimpleRequest\Util\FilterBuilder; |
26
|
|
|
use Mcustiel\SimpleRequest\Annotation\ParseAs; |
27
|
|
|
use Mcustiel\SimpleRequest\Annotation\AnnotationWithAssociatedClass; |
28
|
|
|
use Mcustiel\SimpleRequest\Strategies\AnnotationParserFactory; |
29
|
|
|
use Mcustiel\SimpleRequest\Strategies\PropertyParserBuilder; |
30
|
|
|
use Mcustiel\SimpleRequest\Strategies\Properties\PropertyParser; |
31
|
|
|
|
32
|
|
|
class ParserGenerator |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var \Doctrine\Common\Annotations\AnnotationReader |
36
|
|
|
*/ |
37
|
|
|
private $annotationReader; |
38
|
|
|
/** |
39
|
|
|
* @var \Mcustiel\SimpleRequest\Strategies\AnnotationParserFactory |
40
|
|
|
*/ |
41
|
|
|
private $annotationParserFactory; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param \Doctrine\Common\Annotations\AnnotationReader $annotationReader |
45
|
|
|
* External annotation reader instance (mostly for DI in tests). Created |
46
|
|
|
* if not is set. |
47
|
|
|
*/ |
48
|
|
|
public function __construct( |
49
|
|
|
AnnotationReader $annotationReader, |
50
|
|
|
AnnotationParserFactory $annotationParserFactory |
51
|
|
|
) { |
52
|
|
|
$this->annotationReader = $annotationReader; |
53
|
|
|
$this->annotationParserFactory = $annotationParserFactory; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function addPropertyParser(PropertyParser $propertyParser) |
57
|
|
|
{ |
58
|
|
|
$this->propertyParsers[] = $propertyParser; |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function createRequestParser( |
62
|
|
|
$className, |
63
|
|
|
$parserObject, |
64
|
|
|
RequestBuilder $requestBuilder |
65
|
|
|
) { |
66
|
|
|
$class = new \ReflectionClass($className); |
67
|
|
|
$parserObject->setRequestObject(new $className); |
68
|
|
|
foreach ($class->getProperties() as $property) { |
69
|
|
|
$propertyParserBuilder = new PropertyParserBuilder($property->getName()); |
70
|
|
|
foreach ($this->annotationReader->getPropertyAnnotations($property) as $propertyAnnotation) { |
71
|
|
|
$this->annotationParserFactory |
72
|
|
|
->getAnnotationParserFor($propertyAnnotation) |
73
|
|
|
->execute($propertyAnnotation, $propertyParserBuilder); |
74
|
|
|
} |
75
|
|
|
$parserObject->addPropertyParser($propertyParserBuilder->build($requestBuilder)); |
76
|
|
|
} |
77
|
|
|
return $parserObject; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: