1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Form\Attribute\Processor\CodeGenerator; |
4
|
|
|
|
5
|
|
|
use Bdf\Form\Transformer\TransformerInterface; |
6
|
|
|
use Nette\PhpGenerator\ClassType; |
7
|
|
|
use Nette\PhpGenerator\Method; |
8
|
|
|
use Nette\PhpGenerator\PhpNamespace; |
9
|
|
|
use Nette\PhpGenerator\Printer; |
10
|
|
|
use Nette\PhpGenerator\PromotedParameter; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Generator for a transformer class |
14
|
|
|
* |
15
|
|
|
* @see TransformerInterface The implemented interface |
16
|
|
|
*/ |
17
|
|
|
final class TransformerClassGenerator extends ClassGenerator |
18
|
|
|
{ |
19
|
|
|
private Method $toHttp; |
20
|
|
|
private Method $fromHttp; |
21
|
|
|
|
22
|
|
|
private ?Method $constructor = null; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param PhpNamespace $namespace The used namespace, for use statements, and simplify types |
26
|
|
|
* @param Printer|null $printer Code printer |
27
|
|
|
*/ |
28
|
10 |
|
public function __construct(PhpNamespace $namespace, ?Printer $printer = null) |
29
|
|
|
{ |
30
|
10 |
|
parent::__construct($namespace, new ClassType(), $printer); |
31
|
|
|
|
32
|
10 |
|
$this->implements(TransformerInterface::class); |
33
|
|
|
|
34
|
10 |
|
$this->toHttp = $this->implementsMethod(TransformerInterface::class, 'transformToHttp'); |
35
|
10 |
|
$this->fromHttp = $this->implementsMethod(TransformerInterface::class, 'transformFromHttp'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Add a new promoted property on constructor |
40
|
|
|
* |
41
|
|
|
* @param string $name The property name, without $ |
42
|
|
|
* |
43
|
|
|
* @return PromotedParameter |
44
|
|
|
*/ |
45
|
8 |
|
public function withPromotedProperty(string $name): PromotedParameter |
46
|
|
|
{ |
47
|
8 |
|
if (!$this->constructor) { |
48
|
8 |
|
$this->constructor = $this->class()->addMethod('__construct'); |
49
|
|
|
} |
50
|
|
|
|
51
|
8 |
|
return $this->constructor->addPromotedParameter($name); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the transformToHttp method builder |
56
|
|
|
* |
57
|
|
|
* @return Method |
58
|
|
|
* |
59
|
|
|
* @see TransformerInterface::transformToHttp() |
60
|
|
|
*/ |
61
|
8 |
|
public function toHttp(): Method |
62
|
|
|
{ |
63
|
8 |
|
return $this->toHttp; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get the transformFromHttp method builder |
68
|
|
|
* |
69
|
|
|
* @return Method |
70
|
|
|
* |
71
|
|
|
* @see TransformerInterface::transformFromHttp() |
72
|
|
|
*/ |
73
|
8 |
|
public function fromHttp(): Method |
74
|
|
|
{ |
75
|
8 |
|
return $this->fromHttp; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|