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

Regexp::setRegexp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 1
ccs 3
cts 3
cp 1
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
  namespace Fiv\Form\Validator;
4
5
  /**
6
   * @author Ivan Shcherbak <[email protected]> 7/11/14
7
   */
8
  class Regexp extends Base {
9
10
    /**
11
     * @var string
12
     */
13
    protected $error = 'Invalid value';
14
15
    /**
16
     * @var string
17
     */
18
    protected $regexp = '';
19
20
21
    /**
22
     * @param string $regexp
23
     * @return $this
24
     */
25 1
    public function setRegexp($regexp) {
26 1
      $this->regexp = $regexp;
27 1
      return $this;
28
    }
29
30
31
    /**
32
     * @param string $error
33
     * @return $this
34
     */
35
    public function setError($error) {
36
      $this->error = $error;
37
      return $this;
38
    }
39
40
41
    /**
42
     * @param string $value
43
     * @return bool
44
     */
45 1
    public function isValid($value) {
46 1
      if (!empty($this->regexp) and !preg_match($this->regexp, $value)) {
47 1
        $this->addError($this->error);
48
      }
49
50 1
      return !$this->hasErrors();
51
    }
52
53
  }