Required   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
c 2
b 0
f 0
lcom 1
cbo 1
dl 0
loc 38
ccs 0
cts 15
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setError() 0 4 1
A isValid() 0 14 4
1
<?php
2
3
  namespace Fiv\Form\Validator;
4
5
  /**
6
   * Check if value not empty
7
   * Algorithm based on value length
8
   *
9
   */
10
  class Required extends BaseValidator {
11
12
    /**
13
     * @var string
14
     */
15
    protected $error = 'Field is required';
16
17
18
    /**
19
     * @param string $error
20
     * @return $this
21
     */
22
    public function setError($error) {
23
      $this->error = $error;
24
      return $this;
25
    }
26
27
28
    /**
29
     * @param string $value
30
     * @return bool
31
     */
32
    public function isValid($value) {
33
      $this->errors = [];
34
35
      if (!empty($value)) {
36
        return true;
37
      }
38
39
      if (is_string($value) and $value !== '') {
40
        return true;
41
      }
42
43
      $this->addError($this->error);
44
      return false;
45
    }
46
47
  }