CallBackValidator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 47
ccs 0
cts 16
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A setErrorMessage() 0 4 1
A isValid() 0 11 2
1
<?php
2
3
  namespace Fiv\Form\Validator;
4
5
  /**
6
   *
7
   * @author Vitaly Sevastyanov
8
   */
9
  class CallBackValidator extends BaseValidator {
10
11
    /**
12
     * @var callable
13
     */
14
    protected $callback;
15
16
    /**
17
     * @var string|null
18
     */
19
    protected $errorMessage;
20
21
22
    /**
23
     * @param callable $callback
24
     */
25
    public function __construct(callable $callback) {
26
      $this->callback = $callback;
27
    }
28
29
30
    /**
31
     * @param string $errorMessage
32
     * @return $this
33
     */
34
    public function setErrorMessage($errorMessage) {
35
      $this->errorMessage = $errorMessage;
36
      return $this;
37
    }
38
39
40
    /**
41
     * @param string $value
42
     * @return mixed
43
     */
44
    public function isValid($value) {
45
      $this->errors = [];
46
47
      $result = call_user_func($this->callback, $value);
48
      if ($result) {
49
        return true;
50
      }
51
52
      $this->addError($this->errorMessage);
53
      return false;
54
    }
55
  }