Completed
Push — master ( 5ab0c2...a953a5 )
by Todd
04:38
created

Field::runValidators()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 3
eloc 3
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Logikos\Util\Config\Field;
4
5
use Logikos\Util\Config\Field as FieldInterface;
6
use Logikos\Util\Config\Field\Validation\Result as ValidationResult;
7
use Logikos\Util\Config\Field\Validation\Validator;
8
9
class Field implements FieldInterface {
10
11
  protected $name;
12
  protected $messages   = [];
13
14
  /** @var Validator[] */
15
  protected $validators = [];
16
17 22
  public function __construct($name) {
18 22
    if (!$this->isValidName($name))
19 1
      throw new InvalidFieldNameException();
20 21
    $this->name = $name;
21 21
  }
22
23 4
  public static function withValidators($name, Validator ...$validators) {
24 4
    $field = new static($name);
25 4
    $field->validators = $validators;
26 4
    return $field;
27
  }
28
29 14
  public function isRequired() : bool {
30 14
    return true;
31
  }
32
33 8
  public function getName() {
34 8
    return $this->name;
35
  }
36
37 7
  public function addPattern($pattern, $description) {
38 7
    $this->addValidator(new Validator\Regex($pattern, $description));
39 6
  }
40
41 6
  public function addCallable(callable $callable, $description) {
42 6
    $this->addValidator(new Validator\Callback($callable, $description));
43 6
  }
44
45 9
  public function addValidator(Validator $validator) {
46 9
    array_push($this->validators, $validator);
47 9
  }
48
49 16
  public function validate($value): ValidationResult {
50 16
    if ($this->isRequiredOrNotEmpty($value))
51 15
      $this->runValidators($value);
52
53 16
    if ($this->isRequiredAndEmpty($value) && count($this->messages) === 0)
54 3
      $this->addMessage('Required');
55
56 16
    return $this->validationResult();
57
  }
58
59 16
  protected function isRequiredAndEmpty($value) {
60 16
    return $this->isRequired() && $this->isEmpty($value);
61
  }
62
63 16
  protected function isRequiredOrNotEmpty($value) {
64 16
    return $this->isRequired() || $this->isNotEmpty($value);
65
  }
66
67 12
  protected function addMessage($message) {
68 12
    array_push($this->messages, $message);
69 12
  }
70
71 16
  protected function validationResult() {
72 16
    $result = count($this->messages)
73 12
        ? new Validation\InvalidResult($this->messages)
74 16
        : new Validation\ValidResult();
75 16
    $this->resetMessages();
76 16
    return $result;
77
  }
78
79 16
  protected function resetMessages() {
80 16
    $this->messages = [];
81 16
  }
82
83 16
  protected function isEmpty($value) {
84 16
    return is_null($value) || $value === '';
85
  }
86
87 8
  protected function isNotEmpty($value) {
88 8
    return !$this->isEmpty($value);
89
  }
90
91 15
  protected function runValidators($value) {
92 15
    foreach ($this->validators as $validator) {
93 13
      if (!$validator->validate($value))
94 13
        $this->addMessage($validator->getDescription());
95
    }
96 15
  }
97
98 22
  private function isValidName($name) {
99 22
    return is_int($name)
100 22
        || is_string($name)
101 22
        && !empty($name);
102
  }
103
}