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\Transformer\TransformerInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Fine grain configure error triggered by transformers |
16
|
|
|
* |
17
|
|
|
* This attribute is equivalent to call : |
18
|
|
|
* <code> |
19
|
|
|
* $builder->string('foo') |
20
|
|
|
* ->transformerErrorMessage('Foo is in invalid format') |
21
|
|
|
* ->transformerErrorCode('FOO_FORMAT_ERROR') |
22
|
|
|
* ->transformerExceptionValidation([$this, 'fooTransformerExceptionValidation']) |
23
|
|
|
* ; |
24
|
|
|
* </code> |
25
|
|
|
* |
26
|
|
|
* Usage: |
27
|
|
|
* <code> |
28
|
|
|
* class MyForm extends AttributeForm |
29
|
|
|
* { |
30
|
|
|
* #[MyTransformer, TransformerError(message: 'Foo is in invalid format', code: 'FOO_FORMAT_ERROR')] |
31
|
|
|
* private StringElement $foo; |
32
|
|
|
* } |
33
|
|
|
* </code> |
34
|
|
|
* |
35
|
|
|
* @see ValidatorBuilderTrait::transformerErrorMessage() The called method when message parameter is provided |
36
|
|
|
* @see ValidatorBuilderTrait::transformerErrorCode() The called method when code parameter is provided |
37
|
|
|
* @see ValidatorBuilderTrait::transformerExceptionValidation() The called method when validationCallback parameter is provided |
38
|
|
|
* |
39
|
|
|
* @implements ChildBuilderAttributeInterface<AbstractElementBuilder> |
40
|
|
|
*/ |
41
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] |
42
|
|
|
class TransformerError implements ChildBuilderAttributeInterface |
43
|
|
|
{ |
44
|
5 |
|
public function __construct( |
45
|
|
|
/** |
46
|
|
|
* The error message to show when transformer fail |
47
|
|
|
* |
48
|
|
|
* @readonly |
49
|
|
|
*/ |
50
|
|
|
private ?string $message = null, |
51
|
|
|
/** |
52
|
|
|
* The error code to provide when transformer fail |
53
|
|
|
* |
54
|
|
|
* @readonly |
55
|
|
|
*/ |
56
|
|
|
private ?string $code = null, |
57
|
|
|
/** |
58
|
|
|
* Method name to use for validate the transformer exception |
59
|
|
|
* |
60
|
|
|
* This method must be public and declared on the form class, and follow the prototype : |
61
|
|
|
* `public function ($value, TransformerExceptionConstraint $constraint, ElementInterface $element): bool` |
62
|
|
|
* |
63
|
|
|
* If the method return false, the exception will be ignored |
64
|
|
|
* Else, the method should fill `TransformerExceptionConstraint` with error message and code to provide the custom error |
65
|
|
|
* |
66
|
|
|
* @var literal-string|null |
67
|
|
|
* @readonly |
68
|
|
|
*/ |
69
|
|
|
private ?string $validationCallback = null, |
70
|
|
|
) { |
71
|
5 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
2 |
|
public function applyOnChildBuilder(AttributeForm $form, ChildBuilderInterface $builder): void |
77
|
|
|
{ |
78
|
2 |
|
if ($this->message) { |
79
|
1 |
|
$builder->transformerErrorMessage($this->message); |
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
if ($this->code) { |
83
|
1 |
|
$builder->transformerErrorCode($this->code); |
84
|
|
|
} |
85
|
|
|
|
86
|
2 |
|
if ($this->validationCallback) { |
87
|
1 |
|
$builder->transformerExceptionValidation([$form, $this->validationCallback]); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
3 |
|
public function generateCodeForChildBuilder(string $name, AttributesProcessorGenerator $generator, AttributeForm $form): void |
95
|
|
|
{ |
96
|
3 |
|
$generator->line('$?', [$name]); |
97
|
|
|
|
98
|
3 |
|
if ($this->message) { |
99
|
2 |
|
$generator->line(' ->transformerErrorMessage(?)', [$this->message]); |
100
|
|
|
} |
101
|
|
|
|
102
|
3 |
|
if ($this->code) { |
103
|
2 |
|
$generator->line(' ->transformerErrorCode(?)', [$this->code]); |
104
|
|
|
} |
105
|
|
|
|
106
|
3 |
|
if ($this->validationCallback) { |
107
|
1 |
|
$generator->line(' ->transformerExceptionValidation([$form, ?])', [$this->validationCallback]); |
108
|
|
|
} |
109
|
|
|
|
110
|
3 |
|
$generator->line(';'); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|