Regex   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
eloc 11
dl 0
loc 30
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 0 2 1
A isValidRegexPattern() 0 2 1
A __construct() 0 5 2
A validate() 0 2 1
A getPattern() 0 2 1
1
<?php
2
3
namespace Logikos\Util\Validation\Validator;
4
5
use Logikos\Util\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 15
  public function __construct($pattern, $description = null) {
17 15
    if (!$this->isValidRegexPattern($pattern))
18 2
      throw new Exception("Invalid Regex pattern: {$pattern}");
19 13
    $this->pattern = $pattern;
20 13
    $this->message = $description ?? "Must match pattern: {$pattern}";
21 13
  }
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 15
  protected function isValidRegexPattern($pattern) {
32 15
    return @preg_match($pattern, null) !== false;
33
  }
34
35 7
  public function getDescription() {
36 7
    return $this->message;
37
  }
38
}