Passed
Push — master ( a9840c...5ab0c2 )
by Todd
03:34
created

Regex::validate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 2
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Logikos\Util\Config\Field\Validation\Validator;
4
5
use Logikos\Util\Config\Field\Validation\Validator;
6
7
class Regex implements Validator {
8
  private $pattern;
9
  private $message;
10
11
  /**
12
   * @param string $pattern
13
   * @param string $description
14
   * @throws Exception
15
   */
16 14
  public function __construct($pattern, $description = null) {
17 14
    if (!$this->isValidRegexPattern($pattern))
18 2
      throw new Exception("Invalid Regex pattern: {$pattern}");
19 12
    $this->pattern = $pattern;
20 12
    $this->message = $description;
21 12
  }
22
23 1
  public function getPattern() {
24 1
    return $this->pattern;
25
  }
26
27 10
  public function validate($value) : bool {
28 10
    return preg_match($this->pattern, $value) !== 0;
29
  }
30
31 14
  protected function isValidRegexPattern($pattern) {
32 14
    return @preg_match($pattern, null) !== false;
33
  }
34
35 6
  public function getDescription() {
36 6
    return $this->message;
37
  }
38
}