Regex   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 33
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A isValid() 0 7 2
1
<?php
2
3
namespace Kontrolio\Rules\Core;
4
5
use InvalidArgumentException;
6
use Kontrolio\Rules\AbstractRule;
7
8
/**
9
 * Validation rule by a regular expression
10
 *
11
 * @package Kontrolio\Rules\Core
12
 */
13
class Regex extends AbstractRule
14
{
15
    /**
16
     * @var string
17
     */
18
    private $pattern;
19
20
    /**
21
     * @param string $pattern
22
     */
23
    public function __construct($pattern)
24
    {
25
        if (!is_string($pattern)) {
0 ignored issues
show
introduced by
The condition is_string($pattern) is always true.
Loading history...
26
            throw new InvalidArgumentException('Pattern must be a string.');
27
        }
28
29
        $this->pattern = $pattern;
30
    }
31
32
    /**
33
     * Validates input.
34
     *
35
     * @param mixed $input
36
     *
37
     * @return bool
38
     */
39
    public function isValid($input = null)
40
    {
41
        if (empty($input)) {
42
            return false;
43
        }
44
45
        return (bool) preg_match($this->pattern, $input);
46
    }
47
}
48