Pattern::validate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Linio\Component\Input\Constraint;
6
7
class Pattern extends Constraint
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $pattern;
13
14
    public function __construct(string $pattern, string $errorMessage = null)
15
    {
16
        $this->pattern = $pattern;
17
18
        $this->setErrorMessage($errorMessage ?? 'Required pattern does not match');
19
    }
20
21
    public function validate($content): bool
22
    {
23
        if (!is_scalar($content)) {
24
            return false;
25
        }
26
27
        if (!$content) {
28
            return false;
29
        }
30
31
        return (bool) preg_match($this->pattern, $content);
32
    }
33
}
34