-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCallbackTransformer.php
183 lines (168 loc) · 6.02 KB
/
CallbackTransformer.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
namespace Bdf\Form\Attribute\Element;
use Attribute;
use Bdf\Form\Attribute\AttributeForm;
use Bdf\Form\Attribute\Child\CallbackModelTransformer;
use Bdf\Form\Attribute\ChildBuilderAttributeInterface;
use Bdf\Form\Attribute\Processor\CodeGenerator\AttributesProcessorGenerator;
use Bdf\Form\Attribute\Processor\CodeGenerator\ClassGenerator;
use Bdf\Form\Attribute\Processor\CodeGenerator\TransformerClassGenerator;
use Bdf\Form\Attribute\Processor\GenerateConfiguratorStrategy;
use Bdf\Form\Child\ChildBuilderInterface;
use Bdf\Form\ElementBuilderInterface;
use Bdf\Form\ElementInterface;
use Bdf\Form\Transformer\TransformerInterface;
use Nette\PhpGenerator\ClassType;
use Nette\PhpGenerator\Factory;
use Nette\PhpGenerator\Literal;
use Nette\PhpGenerator\Method;
use Nette\PhpGenerator\PsrPrinter;
/**
* Add a HTTP transformer on the child element, by using method
*
* Transformation to entity and to input can be separated in two different method.
* Those methods take the value and the input element as parameters, and should return the transformed value
* If dedicated methods are not used, but the unified one, the third parameter is provided :
* - on true the transformation is to the entity
* - on false the transformation is to the input
*
* This attribute is equivalent to call :
* <code>
* // For unified callback
* $builder->string('foo')->transformer([$this, 'myTransformer']);
*
* // When using two methods (toHttp: 'transformFooToHttp', fromHttp: 'transformFooFromHttp')
* $builder->string('foo')->transformer(function ($value, ElementInterface $input, bool $toPhp) {
* return $toPhp ? $this->transformFooFromHttp($value, $input) : $this->transformFooToHttp($value, $input);
* });
* </code>
*
* Usage:
* <code>
* class MyForm extends AttributeForm
* {
* #[CallbackTransformer(fromHttp: 'fooFromHttp', toHttp: 'fooToHttp')]
* private IntegerElement $foo;
*
* // With unified transformer (same as above)
* #[CallbackTransformer('barTransformer')]
* private IntegerElement $bar;
*
* public function fooFromHttp(string $value, IntegerElement $input): int
* {
* return hexdec($value);
* }
*
* public function fooToHttp(int $value, IntegerElement $input): string
* {
* return dechex($value);
* }
*
* public function barTransformer($value, IntegerElement $input, bool $toPhp)
* {
* return $toPhp ? hexdec($value) : dechex($value);
* }
* }
* </code>
*
* @implements ChildBuilderAttributeInterface<\Bdf\Form\ElementBuilderInterface>
*
* @see ElementBuilderInterface::transformer() The called method
* @see Transformer For use a transformer class as transformer
* @see CallbackModelTransformer For use transformer in same way, but for model transformer intead of http one
*
* @api
*/
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
final class CallbackTransformer implements ChildBuilderAttributeInterface
{
public function __construct(
/**
* Method name use to define the unified transformer method
* If defined, the other parameters will be ignored
*
* @var literal-string|null
* @readonly
*/
private ?string $callback = null,
/**
* Method name use to define the transformation process from http value to the input
*
* @var literal-string|null
* @readonly
*/
private ?string $fromHttp = null,
/**
* Method name use to define the transformation process from input value to http format
*
* @var literal-string|null
* @readonly
*/
private ?string $toHttp = null,
) {
}
/**
* {@inheritdoc}
*/
public function applyOnChildBuilder(AttributeForm $form, ChildBuilderInterface $builder): void
{
if ($this->callback !== null) {
$builder->transformer([$form, $this->callback]);
return;
}
$builder->transformer(new class ($form, $this->fromHttp, $this->toHttp) implements TransformerInterface {
public function __construct(
private AttributeForm $form,
private ?string $fromHttp,
private ?string $toHttp,
) {
}
/**
* {@inheritdoc}
*/
public function transformToHttp($value, ElementInterface $input)
{
if ($this->toHttp === null) {
return $value;
}
return $this->form->{$this->toHttp}($value, $input);
}
/**
* {@inheritdoc}
*/
public function transformFromHttp($value, ElementInterface $input)
{
if ($this->fromHttp === null) {
return $value;
}
return $this->form->{$this->fromHttp}($value, $input);
}
});
}
/**
* {@inheritdoc}
*/
public function generateCodeForChildBuilder(string $name, AttributesProcessorGenerator $generator, AttributeForm $form): void
{
if ($this->callback !== null) {
$generator->line('$?->transformer([$form, ?]);', [$name, $this->callback]);
return;
}
$transformer = new TransformerClassGenerator($generator->namespace(), $generator->printer());
$transformer->withPromotedProperty('form')->setPrivate();
if ($this->toHttp !== null) {
$transformer->toHttp()->setBody('return $this->form->?($value, $input);', [$this->toHttp]);
} else {
$transformer->toHttp()->setBody('return $value;');
}
if ($this->fromHttp !== null) {
$transformer->fromHttp()->setBody('return $this->form->?($value, $input);', [$this->fromHttp]);
} else {
$transformer->fromHttp()->setBody('return $value;');
}
$generator->line(
'$?->transformer(new class ($form) ?);',
[$name, new Literal($transformer->generateClass())]
);
}
}