|
5 | 5 | namespace Laminas\Code\Generator;
|
6 | 6 |
|
7 | 7 | use Laminas\Code\Generator\AttributeGenerator\AttributeAssembler;
|
8 |
| -use Laminas\Code\Generator\AttributeGenerator\AttributeAssemblerBag; |
9 |
| -use Laminas\Code\Generator\AttributeGenerator\AttributeAssemblerFactory; |
10 |
| -use Laminas\Code\Generator\AttributeGenerator\AttributeBuilder; |
| 8 | +use Laminas\Code\Generator\AttributeGenerator\AttributePrototype; |
| 9 | +use Laminas\Code\Generator\AttributeGenerator\AttributeWithArgumentsAssembler; |
| 10 | +use Laminas\Code\Generator\AttributeGenerator\SimpleAttributeAssembler; |
| 11 | +use ReflectionAttribute; |
11 | 12 | use ReflectionClass;
|
12 | 13 |
|
13 |
| -class AttributeGenerator extends AbstractGenerator |
| 14 | +final class AttributeGenerator implements GeneratorInterface |
14 | 15 | {
|
15 |
| - private function __construct(private AttributeAssemblerBag $attributeAssemblerBag) |
| 16 | + private array $assemblers; |
| 17 | + |
| 18 | + private function __construct(AttributeAssembler ...$assembler) |
16 | 19 | {
|
| 20 | + $this->assemblers = $assembler; |
17 | 21 | }
|
18 | 22 |
|
19 | 23 | public function generate(): string
|
20 | 24 | {
|
21 |
| - $generatedAttributes = array_map(fn(AttributeAssembler $attributeAssembler) => $attributeAssembler->__toString(), |
22 |
| - $this->attributeAssemblerBag->toArray(), |
| 25 | + $generatedAttributes = array_map(fn(AttributeAssembler $attributeAssembler) => $attributeAssembler->assemble(), |
| 26 | + $this->assemblers, |
23 | 27 | );
|
24 | 28 |
|
25 |
| - return implode(self::LINE_FEED, $generatedAttributes); |
| 29 | + return implode(AbstractGenerator::LINE_FEED, $generatedAttributes); |
26 | 30 | }
|
27 | 31 |
|
28 |
| - public static function fromReflection(ReflectionClass $reflectionClass): self |
| 32 | + public static function fromPrototype(AttributePrototype ...$attributePrototype): self |
29 | 33 | {
|
30 |
| - return new self(AttributeAssemblerFactory::createForClassByReflection($reflectionClass)); |
| 34 | + $assemblers = []; |
| 35 | + |
| 36 | + foreach ($attributePrototype as $prototype) { |
| 37 | + $assemblers[] = self::negotiateAssembler($prototype); |
| 38 | + } |
| 39 | + |
| 40 | + return new self(...$assemblers); |
31 | 41 | }
|
32 | 42 |
|
33 |
| - public static function fromBuilder(AttributeBuilder $attributeBuilder): self |
| 43 | + public static function fromReflection(ReflectionClass $reflectionClass): self |
34 | 44 | {
|
35 |
| - return new self(AttributeAssemblerFactory::createForClassFromBuilder($attributeBuilder)); |
| 45 | + $attributes = $reflectionClass->getAttributes(); |
| 46 | + $assemblers = []; |
| 47 | + |
| 48 | + foreach ($attributes as $attribute) { |
| 49 | + $assembler = self::negotiateAssembler($attribute); |
| 50 | + |
| 51 | + $assemblers[] = $assembler; |
| 52 | + } |
| 53 | + |
| 54 | + return new self(...$assemblers); |
36 | 55 | }
|
37 | 56 |
|
38 | 57 | public static function fromArray(array $definitions): self
|
39 | 58 | {
|
40 |
| - $builder = new AttributeBuilder(); |
| 59 | + $assemblers = []; |
41 | 60 |
|
42 | 61 | foreach ($definitions as $definition) {
|
43 | 62 | @list($attributeName, $attributeArguments) = $definition;
|
44 | 63 |
|
45 |
| - if (!isset($attributeArguments)) { |
46 |
| - $attributeArguments = []; |
47 |
| - } |
| 64 | + $prototype = new AttributePrototype($attributeName, $attributeArguments ?? []); |
| 65 | + |
| 66 | + $assemblers[] = self::negotiateAssembler($prototype); |
| 67 | + } |
| 68 | + |
| 69 | + return new self(...$assemblers); |
| 70 | + } |
| 71 | + |
| 72 | + private static function negotiateAssembler(ReflectionAttribute|AttributePrototype $reflectionPrototype): AttributeAssembler |
| 73 | + { |
| 74 | + $hasArguments = !empty($reflectionPrototype->getArguments()); |
48 | 75 |
|
49 |
| - $builder->add($attributeName, $attributeArguments); |
| 76 | + if ($hasArguments) { |
| 77 | + return new AttributeWithArgumentsAssembler($reflectionPrototype); |
50 | 78 | }
|
51 | 79 |
|
52 |
| - return self::fromBuilder($builder); |
| 80 | + return new SimpleAttributeAssembler($reflectionPrototype); |
53 | 81 | }
|
54 | 82 | }
|
0 commit comments