1 | <?php |
||
18 | class InputValidation |
||
19 | { |
||
20 | /** |
||
21 | * @var InputModel input model |
||
22 | */ |
||
23 | protected $model; |
||
24 | |||
25 | /** |
||
26 | * @var string[] map where field name => display name override |
||
27 | */ |
||
28 | protected $titles = []; |
||
29 | |||
30 | /** |
||
31 | * The given input/model is assumed to be valid |
||
32 | * |
||
33 | * @param InputModel|array|null $model the form input to be validated |
||
34 | */ |
||
35 | 18 | public function __construct($model) |
|
41 | |||
42 | /** |
||
43 | * Produce a title for a given Field. |
||
44 | * |
||
45 | * @param FieldInterface $field |
||
46 | * |
||
47 | * @return string label |
||
48 | * |
||
49 | * @see setLabel() |
||
50 | * @see Field::getLabel() |
||
51 | */ |
||
52 | 15 | public function getLabel(FieldInterface $field) |
|
53 | { |
||
54 | 15 | return array_key_exists($field->getName(), $this->titles) |
|
55 | 1 | ? $this->titles[$field->getName()] |
|
56 | 15 | : $field->getLabel(); |
|
57 | } |
||
58 | |||
59 | /** |
||
60 | * Override the display name used when referring to a given Field in error messages |
||
61 | * |
||
62 | * @param FieldInterface $field |
||
63 | * @param string $title display name override |
||
64 | * |
||
65 | * @return $this |
||
66 | * |
||
67 | * @see getLabel() |
||
68 | */ |
||
69 | 1 | public function setLabel(FieldInterface $field, $title) |
|
70 | { |
||
71 | 1 | $this->titles[$field->getName()] = $title; |
|
72 | |||
73 | 1 | return $this; |
|
74 | } |
||
75 | |||
76 | /** |
||
77 | * Check the basic constraints defined by the Field itself, validating for e.g. required input, |
||
78 | * data-types and value ranges. |
||
79 | * |
||
80 | * @param FieldInterface|FieldInterface[] $field Field(s) to check |
||
81 | * |
||
82 | * @return $this |
||
83 | * |
||
84 | * @see Field::createValidators() |
||
85 | */ |
||
86 | 2 | public function check($field) |
|
97 | |||
98 | /** |
||
99 | * Validates one or more Fields using one or more given Validators. |
||
100 | * |
||
101 | * Consider calling {@see check()} first to validate the Field's basic constraints. |
||
102 | * |
||
103 | * @param FieldInterface|FieldInterface[] $field Field(s) to validate |
||
104 | * @param ValidatorInterface|ValidatorInterface[] $validator one or more Validators to apply |
||
105 | * |
||
106 | * @return $this |
||
107 | * |
||
108 | * @see check() |
||
109 | */ |
||
110 | 18 | public function validate($field, $validator) |
|
126 | } |
||
127 |