Completed
Push — master ( 6bcea8...250c3f )
by Shcherbak
02:41
created

Len::min()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 2
1
<?php
2
3
  namespace Fiv\Form\Validator;
4
5
  trigger_error('Deprecated', E_USER_DEPRECATED);
6
7
  /**
8
   * Check value length
9
   *
10
   * @deprecated
11
   *
12
   * @author  Ivan Shcherbak <[email protected]>
13
   * @package Fiv\Form\Validator
14
   */
15
  class Len extends BaseValidator{
16
17
    /**
18
     * @var int|null
19
     */
20
    protected $exactLen;
21
22
    /**
23
     * @var string|null
24
     */
25
    protected $exactLenError;
26
27
28
    /**
29
     * @var int|null
30
     */
31
    protected $minLen;
32
33
    /**
34
     * @var string|null
35
     */
36
    protected $minLenError;
37
38
39
    /**
40
     * @var int|null
41
     */
42
    protected $maxLen;
43
44
    /**
45
     * @var string|null
46
     */
47
    protected $maxLenError;
48
49
50
    /**
51
     * Maximum len of value
52
     *
53
     * @param $len
54
     * @param $error
55
     * @return $this
56
     */
57
    public function max($len, $error) {
58
      $this->maxLen = $len;
59
      $this->maxLenError = $error;
60
      return $this;
61
    }
62
63
64
    /**
65
     * Minimum len of value
66
     *
67
     * @param $len
68
     * @param $error
69
     * @return $this
70
     */
71
    public function min($len, $error) {
72
      $this->minLen = $len;
73
      $this->minLenError = $error;
74
      return $this;
75
    }
76
77
78
    /**
79
     * Expect exact len of value
80
     *
81
     * @param $len
82
     * @param $error
83
     * @return $this
84
     */
85
    public function exact($len, $error) {
86
      $this->exactLen = $len;
87
      $this->exactLenError = $error;
88
      return $this;
89
    }
90
91
92
    /**
93
     * Validate value
94
     * @param string $value
95
     * @return bool
96
     */
97
    public function isValid($value) {
98
99
      if ($this->exactLen !== null and mb_strlen($value, 'UTF-8') != $this->exactLen) {
100
        $this->addError(vsprintf($this->exactLenError, [$this->exactLen]));
101
      }
102
103
      if ($this->maxLen !== null and mb_strlen($value, 'UTF-8') > $this->maxLen) {
104
        $this->addError(vsprintf($this->maxLenError, [$this->maxLen]));
105
      }
106
107
      if ($this->minLen !== null and mb_strlen($value, 'UTF-8') < $this->minLen) {
108
        $this->addError(vsprintf($this->minLenError, [$this->minLen]));
109
      }
110
111
      return !$this->hasErrors();
112
    }
113
114
  }