Closure::getDefaultOption()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Bdf\Form\Constraint;
4
5
use Symfony\Component\Validator\Constraint;
6
7
/**
8
 * Handle custom constraint using a callback
9
 *
10
 * <code>
11
 * // The callback may return a boolean : if it's false, use the 'message' parameter as error
12
 * new Closure([
13
 *     'callback' => function ($value, ElementInterface $element) {
14
 *         return $this->checkValue($value);
15
 *     },
16
 *     'message' => 'my error',
17
 * ]);
18
 *
19
 * // You can also return a string to define a custom message
20
 * new Closure(function ($value, ElementInterface $element) {
21
 *     if (!$this->checkValue($value)) {
22
 *         return 'my error';
23
 *     }
24
 * });
25
 *
26
 * // To define an error code, return the error as an array
27
 * new Closure(function ($value, ElementInterface $element) {
28
 *     if (!$this->checkValue($value)) {
29
 *         return ['message' => 'my error', 'code' => 'MY_ERROR'];
30
 *     }
31
 * });
32
 * </code>
33
 */
34
class Closure extends Constraint
35
{
36
    /**
37
     * @var string
38
     */
39
    public $message = 'The value is invalid';
40
41
    /**
42
     * @var callable(mixed,\Bdf\Form\ElementInterface,\Symfony\Component\Validator\Context\ExecutionContextInterface):(bool|string|array{code?: string, message?: string})
43
     */
44
    public $callback;
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 36
    public function getDefaultOption()
50
    {
51 36
        return 'callback';
52
    }
53
}
54