Closure   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 1
eloc 4
c 1
b 0
f 0
dl 0
loc 18
ccs 2
cts 2
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A getDefaultOption() 0 3 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