|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Mikemirten\Component\JsonApi\Mapper\Definition\AnnotationProcessor; |
|
5
|
|
|
|
|
6
|
|
|
use Mikemirten\Component\JsonApi\Mapper\Definition\Annotation\Link as LinkAnnotation; |
|
7
|
|
|
use Mikemirten\Component\JsonApi\Mapper\Definition\Link; |
|
8
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
|
9
|
|
|
use Doctrine\Common\Annotations\Reader; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Abstract processor |
|
13
|
|
|
* Contains shared methods and annotations registration |
|
14
|
|
|
* |
|
15
|
|
|
* @package Mikemirten\Component\JsonApi\Mapper\Definition\AnnotationProcessor |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract class AbstractProcessor implements AnnotationProcessorInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Pattern of "resource" parameter of link annotation |
|
21
|
|
|
*/ |
|
22
|
|
|
const RESOURCE_PATTERN = '~^(?<repository>[a-z_][a-z0-9_]*)\.(?<link>[a-z_][a-z0-9_]*)$~i'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Annotation classes ha been registered. |
|
26
|
|
|
* |
|
27
|
|
|
* @var bool |
|
28
|
|
|
*/ |
|
29
|
|
|
static private $annotationsRegistered = false; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Doctrine annotation reader |
|
33
|
|
|
* |
|
34
|
|
|
* @var Reader |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $reader; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* AnnotationDefinitionProvider constructor. |
|
40
|
|
|
* |
|
41
|
|
|
* @param Reader $reader |
|
42
|
|
|
*/ |
|
43
|
28 |
|
public function __construct(Reader $reader) |
|
44
|
|
|
{ |
|
45
|
28 |
|
self::registerAnnotations(); |
|
46
|
|
|
|
|
47
|
28 |
|
$this->reader = $reader; |
|
48
|
28 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Register annotation classes. |
|
52
|
|
|
* Supports a medieval-aged way of "autoloading" for the Doctrine Annotation library. |
|
53
|
|
|
*/ |
|
54
|
28 |
|
static protected function registerAnnotations() |
|
55
|
|
|
{ |
|
56
|
28 |
|
if (self::$annotationsRegistered === false) { |
|
57
|
1 |
|
AnnotationRegistry::registerFile(__DIR__ . '/../Annotation/ResourceIdentifier.php'); |
|
|
|
|
|
|
58
|
1 |
|
AnnotationRegistry::registerFile(__DIR__ . '/../Annotation/Relationship.php'); |
|
|
|
|
|
|
59
|
1 |
|
AnnotationRegistry::registerFile(__DIR__ . '/../Annotation/Attribute.php'); |
|
|
|
|
|
|
60
|
1 |
|
AnnotationRegistry::registerFile(__DIR__ . '/../Annotation/Link.php'); |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
1 |
|
self::$annotationsRegistered = true; |
|
63
|
|
|
} |
|
64
|
28 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Create link by link's annotation |
|
68
|
|
|
* |
|
69
|
|
|
* @param LinkAnnotation $annotation |
|
70
|
|
|
* @return Link |
|
71
|
|
|
*/ |
|
72
|
6 |
|
protected function createLink(LinkAnnotation $annotation): Link |
|
73
|
|
|
{ |
|
74
|
6 |
|
if (! preg_match(self::RESOURCE_PATTERN, $annotation->resource, $matches)) { |
|
75
|
|
|
throw new \LogicException(sprintf('Invalid resource definition: "%s"', $annotation->resource)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
6 |
|
$link = new Link( |
|
79
|
6 |
|
$annotation->name, |
|
80
|
6 |
|
$matches['repository'], |
|
81
|
6 |
|
$matches['link'] |
|
82
|
|
|
); |
|
83
|
|
|
|
|
84
|
6 |
|
$link->setParameters($annotation->parameters); |
|
85
|
6 |
|
$link->setMetadata($annotation->metadata); |
|
86
|
|
|
|
|
87
|
6 |
|
return $link; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Resolve getter of related object |
|
92
|
|
|
* |
|
93
|
|
|
* @param \ReflectionProperty $property |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
22 |
|
protected function resolveGetter(\ReflectionProperty $property) |
|
97
|
|
|
{ |
|
98
|
22 |
|
$name = $property->getName(); |
|
99
|
22 |
|
$class = $property->getDeclaringClass(); |
|
100
|
|
|
|
|
101
|
22 |
View Code Duplication |
foreach (['get', 'is'] as $prefix) |
|
|
|
|
|
|
102
|
|
|
{ |
|
103
|
22 |
|
$getter = $prefix . ucfirst($name); |
|
104
|
|
|
|
|
105
|
22 |
|
if ($class->hasMethod($getter) && $class->getMethod($getter)->isPublic()) { |
|
106
|
22 |
|
return $getter; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
throw new \LogicException(sprintf( |
|
111
|
|
|
'Getter-method for the property "%s" cannot be resolved automatically. ' . |
|
112
|
|
|
'Probably there is no get%2$s() or is%2$s() method or it is not public.', |
|
113
|
|
|
$name, ucfirst($name) |
|
114
|
|
|
)); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Resolve getter of related object |
|
119
|
|
|
* |
|
120
|
|
|
* @param \ReflectionProperty $property |
|
121
|
|
|
* @return string | null |
|
122
|
|
|
*/ |
|
123
|
18 |
|
protected function resolveSetter(\ReflectionProperty $property) |
|
124
|
|
|
{ |
|
125
|
18 |
|
$name = $property->getName(); |
|
126
|
18 |
|
$class = $property->getDeclaringClass(); |
|
127
|
|
|
|
|
128
|
18 |
|
$setter = 'set' . ucfirst($name); |
|
129
|
|
|
|
|
130
|
18 |
|
if ($class->hasMethod($setter) && $class->getMethod($setter)->isPublic()) { |
|
131
|
2 |
|
return $setter; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
} |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.