Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
15 / 15
PregMatch
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
3 / 3
4
100.00% covered (success)
100.00%
15 / 15
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 validate
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
8 / 8
 getInverseError
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
5 / 5
<?php
namespace Verja\Validator;
use Verja\Error;
use Verja\Validator;
class PregMatch extends Validator
{
    /** @var string */
    protected $pattern;
    /**
     * PregMatch constructor.
     *
     * @param string $pattern
     */
    public function __construct(string $pattern)
    {
        $this->pattern = $pattern;
    }
    /** {@inheritdoc} */
    public function validate($value, array $context = []): bool
    {
        if (preg_match($this->pattern, $value)) {
            return true;
        }
        $this->error = new Error(
            'NO_MATCH',
            $value,
            sprintf('value should match "%s"', $this->pattern),
            [ 'pattern' => $this->pattern ]
        );
        return false;
    }
    /** {@inheritdoc} */
    public function getInverseError($value)
    {
        return new Error(
            'MATCHES',
            $value,
            sprintf('value should not match "%s"', $this->pattern),
            [ 'pattern' => $this->pattern ]
        );
    }
}