/
TransformerCollector.php
128 lines (105 loc) · 3.97 KB
/
TransformerCollector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php declare(strict_types=1);
namespace ApiGen\Reflection;
use ApiGen\Element\Contract\ReflectionCollector\ReflectionCollectorCollectorInterface;
use ApiGen\Reflection\Contract\Reflection\Partial\AccessLevelInterface;
use ApiGen\Reflection\Contract\Reflection\Partial\AnnotationsInterface;
use ApiGen\Reflection\Contract\Transformer\TransformerInterface;
use ApiGen\Reflection\Contract\TransformerCollectorAwareInterface;
use ApiGen\Reflection\Contract\TransformerCollectorInterface;
use ApiGen\Reflection\Exception\UnsupportedReflectionClassException;
final class TransformerCollector implements TransformerCollectorInterface
{
/**
* @var TransformerInterface[]
*/
private $transformers = [];
/**
* @var ReflectionCollectorCollectorInterface
*/
private $reflectionCollectorCollector;
public function __construct(ReflectionCollectorCollectorInterface $reflectionCollectorCollector)
{
$this->reflectionCollectorCollector = $reflectionCollectorCollector;
}
public function addTransformer(TransformerInterface $transformer): void
{
$this->transformers[] = $transformer;
}
/**
* @param object[] $reflections
* @return object[]
*/
public function transformGroup(array $reflections): array
{
$elements = [];
foreach ($reflections as $name => $reflection) {
$transformedReflection = $this->transformSingle($reflection);
if ($this->shouldSkipReflection($transformedReflection)) {
continue;
}
$elements[$transformedReflection->getName()] = $transformedReflection;
}
// @todo: sort here!, before ElementSorter
uasort($elements, function ($firstElement, $secondElement) {
return strcmp($firstElement->getName(), $secondElement->getName());
});
return $elements;
}
/**
* @param object $reflection
* @return object
*/
public function transformSingle($reflection)
{
foreach ($this->transformers as $transformer) {
if (! $transformer->matches($reflection)) {
continue;
}
$newReflection = $transformer->transform($reflection);
if ($newReflection instanceof TransformerCollectorAwareInterface) {
$newReflection->setTransformerCollector($this);
}
$this->reflectionCollectorCollector->processReflection($newReflection);
return $newReflection;
}
throw new UnsupportedReflectionClassException(sprintf(
'Reflection class "%s" is not yet supported. Register new transformer implementing "%s".',
is_object($reflection) ? get_class($reflection) : 'constant',
TransformerInterface::class
));
}
/**
* @param object $transformedReflection
*/
private function hasAllowedAccessLevel($transformedReflection): bool
{
if (! $transformedReflection instanceof AccessLevelInterface) {
return true;
}
// hardcoded @todo make service-like and using Configuration
if ($transformedReflection->isPublic() || $transformedReflection->isProtected()) {
return true;
}
return false;
}
/**
* @param object $transformedReflection
*/
private function shouldSkipReflection($transformedReflection): bool
{
// also ! $this->reflection->isInternal();, remove isDocumented()
// @let decide voters if element is passed?
// here alreay 2 conditions
if ($transformedReflection instanceof AnnotationsInterface
&& $transformedReflection->hasAnnotation('internal')
) {
return true;
}
// $this->configuration->getVisibilityLevels()
// @todo: here is the place to filter out public/protected etc - use service!
if (! $this->hasAllowedAccessLevel($transformedReflection)) {
return true;
}
return false;
}
}