-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayConstraint.php
More file actions
101 lines (95 loc) · 3.34 KB
/
ArrayConstraint.php
File metadata and controls
101 lines (95 loc) · 3.34 KB
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
<?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\Constraint\Satisfy;
use Bdf\Form\Attribute\Processor\CodeGenerator\AttributesProcessorGenerator;
use Bdf\Form\Attribute\Processor\CodeGenerator\ObjectInstantiation;
use Bdf\Form\Child\ChildBuilderInterface;
use InvalidArgumentException;
use Nette\PhpGenerator\Literal;
use Symfony\Component\Validator\Constraint;
use function is_object;
use function is_string;
/**
* Add a constraint on the whole array element
* Use Satisfy, or directly the constraint as attribute for add a constraint on one array item
*
* This attribute is equivalent to call :
* <code>
* $builder->array('values')->arrayConstraints(MyConstraint::class, $options);
* </code>
*
* Usage:
* <code>
* class MyForm extends AttributeForm
* {
* #[ArrayConstraint(Unique::class, ['message' => 'My error'])]
* private ArrayElement $values;
*
* // or on PHP 8.1
* #[ArrayConstraint(new Unique(['message' => 'My error']))]
* private ArrayElement $values;
* }
* </code>
*
* @see Satisfy Attribute for add constraint for items
* @see ArrayElementBuilder::arrayConstraint() The called method
* @see CallbackArrayConstraint Use for a custom method validation
*
* @implements ChildBuilderAttributeInterface<ArrayElementBuilder>
* @api
*/
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
final class ArrayConstraint implements ChildBuilderAttributeInterface
{
public function __construct(
/**
* The constraint
*
* You can use a class name, and provider arguments on the next parameter,
* or directly use the constraint instance.
*
* When a constraint instance is used, in case of code generation,
* the constructor parameters will be deduced from public properties of the constraint.
* This may not work if the constraint has a complex constructor.
*
* @var class-string<Constraint>|Constraint
* @readonly
*/
private string|Constraint $constraint,
/**
* Constraint's constructor options
*
* @var mixed|null
* @readonly
*/
private mixed $options = null
) {
if (is_object($constraint) && $options !== null) {
throw new InvalidArgumentException('Cannot use options with constraint instance');
}
}
/**
* {@inheritdoc}
*/
public function applyOnChildBuilder(AttributeForm $form, ChildBuilderInterface $builder): void
{
$builder->arrayConstraint($this->constraint, $this->options);
}
/**
* {@inheritdoc}
*/
public function generateCodeForChildBuilder(string $name, AttributesProcessorGenerator $generator, AttributeForm $form): void
{
if (is_string($this->constraint)) {
$constraint = $generator->useAndSimplifyType($this->constraint);
$generator->line('$?->arrayConstraint(?::class, ?);', [$name, new Literal($constraint), $this->options]);
} else {
$constraint = ObjectInstantiation::singleArrayParameter($this->constraint)->render($generator);
$generator->line('$?->arrayConstraint(?);', [$name, $constraint]);
}
}
}