-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCallbackArrayConstraint.php
108 lines (99 loc) · 3.4 KB
/
CallbackArrayConstraint.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
<?php
namespace Bdf\Form\Attribute\Aggregate;
use Attribute;
use Bdf\Form\Aggregate\ArrayElementBuilder;
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\Constraint\Closure;
use Bdf\Form\ElementBuilderInterface;
use Nette\PhpGenerator\Literal;
use Symfony\Component\Validator\Constraint;
/**
* Define a custom constraint for an array element, using a validation method
*
* This attribute is equivalent to call :
* <code>
* $builder->array('foo')->arrayConstraint([$this, 'validateFoo'], 'Foo is invalid');
* </code>
*
* Usage:
* <code>
* class MyForm extends AttributeForm
* {
* #[CustomConstraint('validateFoo', message: 'Foo is invalid')]
* private ArrayElement $foo;
*
* public function validateFoo(array $value, ElementInterface $input): bool
* {
* return count($value) % 5 > 2;
* }
* }
* </code>
*
* @see ArrayElementBuilder::arrayConstraint() The called method
* @see Constraint
* @see Closure The used constraint
* @see ArrayConstraint Use for a class constraint
*
* @implements ChildBuilderAttributeInterface<ArrayElementBuilder>
*
* @api
*/
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
final class CallbackArrayConstraint implements ChildBuilderAttributeInterface
{
public function __construct(
/**
* The method name to use as validator
* Must be a public method declared on the form class
*
* Its prototype should be :
* `public function (array $value, ElementInterface $input): bool|string|array{code: string message: string}|null`
*
* - Return true, or null (nothing) for a valid input
* - Return false for invalid input, with the default error message (or the declared one)
* - Return string for a custom error message
* - Return array with error message and code
*
* @var literal-string
* @readonly
*/
private string $methodName,
/**
* The error message to use
* This option is used only if the validator return false, in other cases,
* the message returned by the validator will be used
*
* @var string|null
* @readonly
*/
private ?string $message = null,
) {
}
/**
* {@inheritdoc}
*/
public function applyOnChildBuilder(AttributeForm $form, ChildBuilderInterface $builder): void
{
$constraint = new Closure(['callback' => [$form, $this->methodName]]);
if ($this->message !== null) {
$constraint->message = $this->message;
}
$builder->arrayConstraint($constraint);
}
/**
* {@inheritdoc}
*/
public function generateCodeForChildBuilder(string $name, AttributesProcessorGenerator $generator, AttributeForm $form): void
{
$generator->use(Closure::class, 'ClosureConstraint');
$parameters = $this->message !== null
? new Literal("['callback' => [\$form, ?], 'message' => ?]", [$this->methodName, $this->message])
: new Literal('[$form, ?]', [$this->methodName])
;
$generator->line('$?->arrayConstraint(new ClosureConstraint(?));', [$name, $parameters]);
}
}