Passed
Push — master ( 0b999e...3d12f2 )
by Baptiste
43s
created

RegexTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 5
dl 0
loc 27
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A the_json_path_should_not_match() 0 8 2
A the_json_path_should_match() 0 3 1
1
<?php declare(strict_types=1);
2
namespace Behapi\Json;
3
4
use InvalidArgumentException;
5
6
use Webmozart\Assert\Assert;
7
8
trait RegexTrait
9
{
10
    abstract protected function getValue(?string $path);
11
12
    /** @Then in the json, :path should match :pattern */
13
    final public function the_json_path_should_match(string $path, string $pattern): void
14
    {
15
        Assert::regex($this->getValue($path), $pattern);
16
    }
17
18
    /**
19
     * @Then in the json, :path should not match :pattern
20
     *
21
     * -----
22
     *
23
     * Note :: The body of this assertion should be replaced by a
24
     * `Assert::notRegex` as soon as the Assert's PR
25
     * https://github.com/webmozart/assert/pull/58 is merged and released.
26
     */
27
    final public function the_json_path_should_not_match(string $path, string $pattern): void
28
    {
29
        if (!preg_match($pattern, $this->getValue($path), $matches, PREG_OFFSET_CAPTURE)) {
30
            // it's all good, it is supposed not to match. :}
31
            return;
32
        }
33
34
        throw new InvalidArgumentException("The value matches {$pattern} at offset {$matches[0][1]}");
35
    }
36
}
37