Completed
Push — master ( 15d409...38a590 )
by Shcherbak
05:57 queued 13s
created

Required::isValid()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.2
cc 4
eloc 4
nc 2
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
   * @package Fiv\Form\Validator
10
   */
11
  class Required extends Base {
12
13
    /**
14
     * @var string
15
     */
16
    protected $error = 'Field is required ';
17
18
19
    /**
20
     * @param string $error
21
     * @return $this
22
     */
23
    public function setError($error) {
24
      $this->error = $error;
25
      return $this;
26
    }
27
28
29
    /**
30
     * @param string $value
31
     * @return bool
32
     */
33 1
    public function isValid($value) {
34
35 1
      if ((is_string($value) and strlen($value) === 0) or empty($value)) {
36 1
        $this->addError($this->error);
37
      }
38
39 1
      return !$this->hasErrors();
40
    }
41
42
  }