1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Form\Attribute\Element; |
4
|
|
|
|
5
|
|
|
use Attribute; |
6
|
|
|
use Bdf\Form\AbstractElementBuilder; |
7
|
|
|
use Bdf\Form\Attribute\AttributeForm; |
8
|
|
|
use Bdf\Form\Attribute\ChildBuilderAttributeInterface; |
9
|
|
|
use Bdf\Form\Attribute\Processor\CodeGenerator\AttributesProcessorGenerator; |
10
|
|
|
use Bdf\Form\Attribute\Processor\GenerateConfiguratorStrategy; |
11
|
|
|
use Bdf\Form\Child\ChildBuilderInterface; |
12
|
|
|
use Bdf\Form\Util\ValidatorBuilderTrait; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Ignore errors generated by transformers |
16
|
|
|
* |
17
|
|
|
* If the error is ignored, when a transformer failed, the original (i.e. raw HTTP) data will be used, |
18
|
|
|
* and the standard validation process will be performed on it |
19
|
|
|
* |
20
|
|
|
* Note: this attribute is not repeatable |
21
|
|
|
* |
22
|
|
|
* This attribute is equivalent to call : |
23
|
|
|
* <code> |
24
|
|
|
* $builder->string('foo')->ignoreTransformerException(); |
25
|
|
|
* </code> |
26
|
|
|
* |
27
|
|
|
* Usage: |
28
|
|
|
* <code> |
29
|
|
|
* class MyForm extends AttributeForm |
30
|
|
|
* { |
31
|
|
|
* #[MyTransformer, IgnoreTransformerException] |
32
|
|
|
* private StringElement $foo; |
33
|
|
|
* } |
34
|
|
|
* </code> |
35
|
|
|
* |
36
|
|
|
* @see ValidatorBuilderTrait::ignoreTransformerException() The called method |
37
|
|
|
* |
38
|
|
|
* @implements ChildBuilderAttributeInterface<AbstractElementBuilder> |
39
|
|
|
*/ |
40
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY)] |
41
|
|
|
final class IgnoreTransformerException implements ChildBuilderAttributeInterface |
42
|
|
|
{ |
43
|
3 |
|
public function __construct( |
44
|
|
|
/** |
45
|
|
|
* Ignore or not transformer errors |
46
|
|
|
* |
47
|
|
|
* @readonly |
48
|
|
|
*/ |
49
|
|
|
private bool $ignore = true, |
50
|
|
|
) { |
51
|
3 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
1 |
|
public function applyOnChildBuilder(AttributeForm $form, ChildBuilderInterface $builder): void |
57
|
|
|
{ |
58
|
1 |
|
$builder->ignoreTransformerException($this->ignore); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
2 |
|
public function generateCodeForChildBuilder(string $name, AttributesProcessorGenerator $generator, AttributeForm $form): void |
65
|
|
|
{ |
66
|
2 |
|
$generator->line('$?->ignoreTransformerException(?);', [$name, $this->ignore]); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|