1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Form\Attribute\Processor\CodeGenerator; |
4
|
|
|
|
5
|
|
|
use Bdf\Form\Attribute\Processor\AttributesProcessorInterface; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use Nette\PhpGenerator\Literal; |
8
|
|
|
use Nette\PhpGenerator\Method; |
9
|
|
|
use Nette\PhpGenerator\PhpNamespace; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Utility class for generate the AttributesProcessor class |
13
|
|
|
*/ |
14
|
|
|
final class AttributesProcessorGenerator extends ClassGenerator |
15
|
|
|
{ |
16
|
|
|
private Method $method; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param non-empty-string $className The class name to generate. Must have a namespace |
|
|
|
|
20
|
|
|
* @throws InvalidArgumentException if a namespace is not provided, or if the class name is not valid |
21
|
|
|
*/ |
22
|
88 |
|
public function __construct(string $className) |
23
|
|
|
{ |
24
|
88 |
|
if (!$classNamePos = strrpos($className, '\\')) { |
25
|
1 |
|
throw new InvalidArgumentException('The class name must have a namespace'); |
26
|
|
|
} |
27
|
|
|
|
28
|
87 |
|
parent::__construct( |
29
|
87 |
|
$namespace = new PhpNamespace(substr($className, 0, $classNamePos)), |
30
|
87 |
|
$namespace->addClass(substr($className, $classNamePos + 1)) |
31
|
87 |
|
); |
32
|
|
|
|
33
|
87 |
|
$this->implements(AttributesProcessorInterface::class); |
34
|
|
|
|
35
|
87 |
|
$this->method = $this->implementsMethod(AttributesProcessorInterface::class, 'configureBuilder'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Add a line on the body method of "configureBuilder" |
40
|
|
|
* |
41
|
|
|
* @param string $line Line to add. Set empty string (default parameter) to simply add empty new line |
42
|
|
|
* @param array $args Placeholder arguments |
43
|
|
|
* |
44
|
|
|
* @return self |
45
|
|
|
*/ |
46
|
86 |
|
public function line(string $line = '', array $args = []): self |
47
|
|
|
{ |
48
|
86 |
|
$this->method->addBody($line, $args); |
49
|
|
|
|
50
|
86 |
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Create a new expression, with use class name |
55
|
|
|
* |
56
|
|
|
* @param class-string $className The class to create |
|
|
|
|
57
|
|
|
* @param array $parameters The constructor parameters |
58
|
|
|
* @param string|null $classAlias Class alias to use |
59
|
|
|
* |
60
|
|
|
* @return Literal |
61
|
|
|
*/ |
62
|
31 |
|
public function new(string $className, array $parameters, ?string $classAlias = null): Literal |
63
|
|
|
{ |
64
|
31 |
|
$className = $this->useAndSimplifyType($className, $classAlias); |
65
|
|
|
|
66
|
31 |
|
return new Literal('new ?(...?:)', [new Literal($className), $parameters]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Print the class code |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
87 |
|
public function print(): string |
75
|
|
|
{ |
76
|
87 |
|
return $this->printer()->printNamespace($this->namespace()); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|