-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSatisfy.php
77 lines (72 loc) · 2.07 KB
/
Satisfy.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
<?php
namespace Bdf\Form\Attribute\Constraint;
use Attribute;
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 Nette\PhpGenerator\Literal;
use Symfony\Component\Validator\Constraint;
/**
* Define a custom constraint for an element, using a validation method
*
* Note: prefer use directly the constraint as attribute
*
* This attribute is equivalent to call :
* <code>
* $builder->integer('foo')->satisfy(MyConstraint::class, $options);
* </code>
*
* Usage:
* <code>
* class MyForm extends AttributeForm
* {
* #[Satisfy(MyConstraint::class, ['foo' => 'bar'])]
* private IntegerElement $foo;
* }
* </code>
*
* @implements ChildBuilderAttributeInterface<\Bdf\Form\ElementBuilderInterface>
*
* @see ElementBuilderInterface::satisfy() The called method
* @see Constraint
*
* @api
*/
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
class Satisfy implements ChildBuilderAttributeInterface
{
public function __construct(
/**
* The constraint class name
*
* @var class-string<Constraint>
* @readonly
*/
private string $constraint,
/**
* Constraint's constructor options
*
* @var array|null|string
* @readonly
*/
private mixed $options = null
) {
}
/**
* {@inheritdoc}
*/
public function applyOnChildBuilder(AttributeForm $form, ChildBuilderInterface $builder): void
{
$builder->satisfy($this->constraint, $this->options);
}
/**
* {@inheritdoc}
*/
public function generateCodeForChildBuilder(string $name, AttributesProcessorGenerator $generator, AttributeForm $form): void
{
$type = $generator->useAndSimplifyType($this->constraint);
$generator->line('$?->satisfy(?::class, ?);', [$name, new Literal($type), $this->options]);
}
}