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 8
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.2
cc 4
eloc 8
nc 3
nop 1
crap 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 2
    public function setError($error) {
23 2
      $this->error = $error;
24 2
      return $this;
25
    }
26
27
28
    /**
29
     * @param string $value
30
     * @return bool
31
     */
32 2
    public function isValid($value) {
33 2
      $this->errors = [];
34
35 2
      if (!empty($value)) {
36 2
        return true;
37
      }
38
39 2
      if (is_string($value) and $value !== '') {
40 1
        return true;
41
      }
42
43 2
      $this->addError($this->error);
44 2
      return false;
45
    }
46
47
  }