Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
PregMatch
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
3 / 3
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 validate
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 getInverseError
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Verja\Validator;
4
5use Verja\Error;
6use Verja\Validator;
7
8class PregMatch extends Validator
9{
10    /** @var string */
11    protected $pattern;
12
13    /**
14     * PregMatch constructor.
15     *
16     * @param string $pattern
17     */
18    public function __construct(string $pattern)
19    {
20        $this->pattern = $pattern;
21    }
22
23    /** {@inheritdoc} */
24    public function validate($value, array $context = []): bool
25    {
26        if (preg_match($this->pattern, $value)) {
27            return true;
28        }
29
30        $this->error = new Error(
31            'NO_MATCH',
32            $value,
33            sprintf('value should match "%s"', $this->pattern),
34            [ 'pattern' => $this->pattern ]
35        );
36        return false;
37    }
38
39    /** {@inheritdoc} */
40    public function getInverseError($value)
41    {
42        return new Error(
43            'MATCHES',
44            $value,
45            sprintf('value should not match "%s"', $this->pattern),
46            [ 'pattern' => $this->pattern ]
47        );
48    }
49}