-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransformerError.php
114 lines (103 loc) · 3.68 KB
/
TransformerError.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
<?php
namespace Bdf\Form\Attribute\Element;
use Attribute;
use Bdf\Form\AbstractElementBuilder;
use Bdf\Form\Attribute\AttributeForm;
use Bdf\Form\Attribute\ChildBuilderAttributeInterface;
use Bdf\Form\Attribute\Processor\CodeGenerator\AttributesProcessorGenerator;
use Bdf\Form\Attribute\Processor\GenerateConfiguratorStrategy;
use Bdf\Form\Child\ChildBuilderInterface;
use Bdf\Form\Transformer\TransformerInterface;
/**
* Fine grain configure error triggered by transformers
*
* This attribute is equivalent to call :
* <code>
* $builder->string('foo')
* ->transformerErrorMessage('Foo is in invalid format')
* ->transformerErrorCode('FOO_FORMAT_ERROR')
* ->transformerExceptionValidation([$this, 'fooTransformerExceptionValidation'])
* ;
* </code>
*
* Usage:
* <code>
* class MyForm extends AttributeForm
* {
* #[MyTransformer, TransformerError(message: 'Foo is in invalid format', code: 'FOO_FORMAT_ERROR')]
* private StringElement $foo;
* }
* </code>
*
* @see ValidatorBuilderTrait::transformerErrorMessage() The called method when message parameter is provided
* @see ValidatorBuilderTrait::transformerErrorCode() The called method when code parameter is provided
* @see ValidatorBuilderTrait::transformerExceptionValidation() The called method when validationCallback parameter is provided
*
* @implements ChildBuilderAttributeInterface<AbstractElementBuilder>
*
* @api
*/
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
class TransformerError implements ChildBuilderAttributeInterface
{
public function __construct(
/**
* The error message to show when transformer fail
*
* @readonly
*/
private ?string $message = null,
/**
* The error code to provide when transformer fail
*
* @readonly
*/
private ?string $code = null,
/**
* Method name to use for validate the transformer exception
*
* This method must be public and declared on the form class, and follow the prototype :
* `public function ($value, TransformerExceptionConstraint $constraint, ElementInterface $element): bool`
*
* If the method return false, the exception will be ignored
* Else, the method should fill `TransformerExceptionConstraint` with error message and code to provide the custom error
*
* @var literal-string|null
* @readonly
*/
private ?string $validationCallback = null,
) {
}
/**
* {@inheritdoc}
*/
public function applyOnChildBuilder(AttributeForm $form, ChildBuilderInterface $builder): void
{
if ($this->message !== null) {
$builder->transformerErrorMessage($this->message);
}
if ($this->code !== null) {
$builder->transformerErrorCode($this->code);
}
if ($this->validationCallback !== null) {
$builder->transformerExceptionValidation([$form, $this->validationCallback]);
}
}
/**
* {@inheritdoc}
*/
public function generateCodeForChildBuilder(string $name, AttributesProcessorGenerator $generator, AttributeForm $form): void
{
$generator->line('$?', [$name]);
if ($this->message !== null) {
$generator->line(' ->transformerErrorMessage(?)', [$this->message]);
}
if ($this->code !== null) {
$generator->line(' ->transformerErrorCode(?)', [$this->code]);
}
if ($this->validationCallback !== null) {
$generator->line(' ->transformerExceptionValidation([$form, ?])', [$this->validationCallback]);
}
$generator->line(';');
}
}