Passed
Push — master ( a9840c...5ab0c2 )
by Todd
03:34
created

Field::resetMessages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 2
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Logikos\Util\Config\Field;
4
5
use Innmind\Immutable\Stream;
0 ignored issues
show
Bug introduced by
The type Innmind\Immutable\Stream was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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