Required::isValid()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
ccs 0
cts 11
cp 0
rs 9.2
cc 4
eloc 8
nc 3
nop 1
crap 20
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
  }