Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
4 / 4
77.78% covered (warning)
77.78%
7 / 9
33.33% covered (danger)
33.33%
2 / 6
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
Regex
100.00% covered (success)
100.00%
4 / 4
77.78% covered (warning)
77.78%
7 / 9
33.33% covered (danger)
33.33%
2 / 6
100.00% covered (success)
100.00%
1 / 1
3.19
100.00% covered (success)
100.00%
1 / 1
 validate
100.00% covered (success)
100.00%
4 / 4
77.78% covered (warning)
77.78%
7 / 9
33.33% covered (danger)
33.33%
2 / 6
100.00% covered (success)
100.00%
1 / 1
3.19
1<?php
2
3declare(strict_types=1);
4
5namespace Celemas\Sire\Rule;
6
7use Celemas\Sire\Contract;
8use Celemas\Sire\Validation;
9use Override;
10
11/** @api */
12final class Regex implements Contract\Rule
13{
14    public string $message {
15        get => '{label} has an invalid format';
16    }
17
18    #[Override]
19    public function validate(Contract\Value $value, string ...$args): Contract\Validation
20    {
21        // As regex patterns could contain colons ':' and rule
22        // args are separated by colons and split at their position
23        // we need to join them again
24        $pattern = implode(':', $args);
25
26        if ($pattern === '') {
27            return Validation::invalid();
28        }
29
30        return Validation::from(preg_match($pattern, $value->value) === 1);
31    }
32}