StringValidationTrait::addValidator()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
  declare(strict_types=1);
4
5
  namespace Fiv\Form\Validation\StringValidation;
6
7
  use Fiv\Form\Elements\StringDataElementInterface;
8
  use Fiv\Form\Validation\ValidationResult;
9
  use Fiv\Form\Validation\ValidationResultInterface;
10
11
  trait StringValidationTrait {
12
13
    /**
14
     * @var StringValidatorInterface[]
15
     */
16
    private $validators = [];
17
18
19
    public function addValidator(StringValidatorInterface $validator) : self {
20
      $this->validators[] = $validator;
21
      return $this;
22
    }
23
24
25
26
    public function validate() : ValidationResultInterface {
27
28
      $validationResult = new ValidationResult();
29
      foreach ($this->validators as $validator) {
30
        /** @var StringDataElementInterface $this */
31
        $result = $validator->validate($this);
32
        $validationResult->merge($result);
33
      }
34
35
      return $validationResult;
36
    }
37
  }