FieldComparisonValidatorTrait::validate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Bdf\Form\Constraint;
4
5
use Bdf\Form\ElementInterface;
6
use Bdf\Form\Util\FieldPath;
7
use Symfony\Component\Validator\Constraint;
8
9
/**
10
 * Handle FieldComparisonTrait constraints
11
 * The class must extends a subclass of AbstractComparisonValidator
12
 */
13
trait FieldComparisonValidatorTrait
14
{
15
    /**
16
     * {@inheritdoc}
17
     *
18
     * @psalm-suppress UndefinedMagicPropertyFetch
19
     * @psalm-suppress UndefinedMagicPropertyAssignment
20
     *
21
     * @return void
22
     */
23 8
    public function validate($value, Constraint $constraint)
24
    {
25
        /** @var FieldComparisonTrait $constraint */
26
        /** @var ElementInterface $element */
27 8
        $element = $this->context->getRoot()->get();
28 8
        $field = $constraint->field;
29
30 8
        if (!$field instanceof FieldPath) {
31 8
            $field = FieldPath::parse($field);
32
        }
33
34 8
        $constraint->value = $field->value($element);
35
36 8
        parent::validate($value, $constraint);
37 8
    }
38
}
39