1 | <?php |
||
2 | declare(strict_types=1); |
||
3 | |||
4 | namespace BrenoRoosevelt\PhpAttributes; |
||
5 | |||
6 | use FlexFqcnFinder\FqcnFinderInterface; |
||
7 | use ReflectionClass; |
||
8 | use ReflectionClassConstant; |
||
9 | use ReflectionMethod; |
||
10 | use ReflectionParameter; |
||
11 | use ReflectionProperty; |
||
12 | use Attribute; |
||
13 | |||
14 | class AttributesFactory |
||
15 | { |
||
16 | /** |
||
17 | * @param object|string|object[]|string[]|array $objectOrClass |
||
18 | * @param int $target |
||
19 | * @param string|null $attribute |
||
20 | * @param int $flags |
||
21 | * @return Attributes |
||
22 | */ |
||
23 | public function from( |
||
24 | object|string|array $objectOrClass, |
||
25 | int $target = Attribute::TARGET_ALL, |
||
26 | string $attribute = null, |
||
27 | int $flags = 0, |
||
28 | ): Attributes { |
||
29 | $collection = new Attributes(); |
||
30 | |||
31 | $classes = |
||
32 | is_array($objectOrClass) ? |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
33 | array_map(fn($class) => $this->reflectionClass($class), $objectOrClass) : |
||
34 | [$this->reflectionClass($objectOrClass)]; |
||
35 | |||
36 | foreach ($classes as $objectOrClass) { |
||
37 | if (Attribute::TARGET_CLASS & $target) { |
||
38 | $collection = $collection->merge($this->fromClass($objectOrClass, $attribute, $flags)); |
||
39 | } |
||
40 | |||
41 | if (Attribute::TARGET_PROPERTY & $target) { |
||
42 | $collection = $collection->merge($this->fromProperties($objectOrClass, $attribute, $flags)); |
||
43 | } |
||
44 | |||
45 | if (Attribute::TARGET_METHOD & $target) { |
||
46 | $collection = $collection->merge($this->fromMethods($objectOrClass, $attribute, $flags)); |
||
47 | } |
||
48 | |||
49 | if (Attribute::TARGET_PARAMETER & $target) { |
||
50 | $collection = |
||
51 | $collection->merge($this->fromParameters($objectOrClass, null, $attribute, $flags)); |
||
52 | } |
||
53 | |||
54 | if (Attribute::TARGET_CLASS_CONSTANT & $target) { |
||
55 | $collection = $collection->merge($this->fromConstants($objectOrClass, $attribute, $flags)); |
||
56 | } |
||
57 | } |
||
58 | |||
59 | return $collection; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @param object|object[] $reflectionObject |
||
64 | * @param string|null $attribute |
||
65 | * @param int $flags |
||
66 | * @return Attributes |
||
67 | */ |
||
68 | public function fromReflection(object|array $reflectionObject, string $attribute = null, int $flags = 0): Attributes |
||
69 | { |
||
70 | $attributes = []; |
||
71 | $reflectionObject = is_array($reflectionObject) ? $reflectionObject : [$reflectionObject]; |
||
0 ignored issues
–
show
|
|||
72 | foreach ($reflectionObject as $object) { |
||
73 | if ($object instanceof ReflectionClass || |
||
74 | $object instanceof ReflectionMethod || |
||
75 | $object instanceof ReflectionProperty || |
||
76 | $object instanceof ReflectionParameter || |
||
77 | $object instanceof ReflectionClassConstant |
||
78 | ) { |
||
79 | foreach ($object->getAttributes($attribute, $flags) as $reflectionAttribute) { |
||
80 | $attributes[] = new ParsedAttribute($reflectionAttribute, $object); |
||
81 | } |
||
82 | } |
||
83 | } |
||
84 | |||
85 | return new Attributes(...$attributes); |
||
86 | } |
||
87 | |||
88 | public function fromClass( |
||
89 | object|string $objectOrClass, |
||
90 | string $attribute = null, |
||
91 | int $flags = 0, |
||
92 | ): Attributes { |
||
93 | $objectOrClass = $this->reflectionClass($objectOrClass); |
||
94 | return $this->fromReflection($objectOrClass, $attribute, $flags); |
||
95 | } |
||
96 | |||
97 | public function fromProperties( |
||
98 | object|string $objectOrClass, |
||
99 | string $attribute = null, |
||
100 | int $flags = 0, |
||
101 | int $filter = null |
||
102 | ): Attributes { |
||
103 | $collection = new Attributes(); |
||
104 | $objectOrClass = $this->reflectionClass($objectOrClass); |
||
105 | foreach ($objectOrClass->getProperties($filter) as $property) { |
||
106 | $collection = $collection->merge($this->fromReflection($property, $attribute, $flags)); |
||
107 | } |
||
108 | |||
109 | return $collection; |
||
110 | } |
||
111 | |||
112 | public function fromMethods( |
||
113 | object|string $objectOrClass, |
||
114 | string $attribute = null, |
||
115 | int $flags = 0, |
||
116 | int $filter = null |
||
117 | ): Attributes { |
||
118 | $collection = new Attributes(); |
||
119 | $objectOrClass = $this->reflectionClass($objectOrClass); |
||
120 | foreach ($objectOrClass->getMethods($filter) as $method) { |
||
121 | $collection = $collection->merge($this->fromReflection($method, $attribute, $flags)); |
||
122 | } |
||
123 | |||
124 | return $collection; |
||
125 | } |
||
126 | |||
127 | public function fromParameters( |
||
128 | object|string $objectOrClass, |
||
129 | string $method = null, |
||
130 | string $attribute = null, |
||
131 | int $flags = 0, |
||
132 | int $filter = null |
||
133 | ): Attributes { |
||
134 | $collection = new Attributes(); |
||
135 | $objectOrClass = $this->reflectionClass($objectOrClass); |
||
136 | $methods = is_null($method) ? $objectOrClass->getMethods($filter) : [$objectOrClass->getMethod($method)]; |
||
137 | foreach ($methods as $method) { |
||
138 | foreach ($method->getParameters() as $parameter) { |
||
139 | $collection = $collection->merge($this->fromReflection($parameter, $attribute, $flags)); |
||
140 | } |
||
141 | } |
||
142 | |||
143 | return $collection; |
||
144 | } |
||
145 | |||
146 | public function fromConstants( |
||
147 | object|string $objectOrClass, |
||
148 | string $attribute = null, |
||
149 | int $flags = 0, |
||
150 | int $filter = null |
||
151 | ): Attributes { |
||
152 | $collection = new Attributes(); |
||
153 | $objectOrClass = $this->reflectionClass($objectOrClass); |
||
154 | foreach ($objectOrClass->getReflectionConstants($filter) as $constant) { |
||
155 | $collection = $collection->merge($this->fromReflection($constant, $attribute, $flags)); |
||
156 | } |
||
157 | |||
158 | return $collection; |
||
159 | } |
||
160 | |||
161 | public function fromFqcnFinder( |
||
162 | FqcnFinderInterface $finder, |
||
163 | int $target = Attribute::TARGET_ALL, |
||
164 | string $attribute = null, |
||
165 | int $flags = 0 |
||
166 | ): Attributes { |
||
167 | return $this->from($finder->find(), $target, $attribute, $flags); |
||
168 | } |
||
169 | |||
170 | protected function reflectionClass(object|string $objectOrClass): ReflectionClass |
||
171 | { |
||
172 | if ($objectOrClass instanceof ReflectionClass) { |
||
173 | return $objectOrClass; |
||
174 | } |
||
175 | |||
176 | return new ReflectionClass($objectOrClass); |
||
177 | } |
||
178 | } |
||
179 |