Passed
Push — main ( 8bb0e7...efbdfd )
by Colin
05:29 queued 03:15
created

InlineParserMatch   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 20
c 1
b 0
f 0
dl 0
loc 66
ccs 25
cts 25
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A regex() 0 3 1
A getRegex() 0 3 2
A join() 0 15 4
A oneOf() 0 3 1
A string() 0 3 1
A caseSensitive() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace League\CommonMark\Parser\Inline;
15
16
final class InlineParserMatch
17
{
18
    private string $regex;
19
20
    private bool $caseSensitive;
21
22 3147
    private function __construct(string $regex, bool $caseSensitive = false)
23
    {
24 3147
        $this->regex         = $regex;
25 3147
        $this->caseSensitive = $caseSensitive;
26 3147
    }
27
28 3141
    public function caseSensitive(): self
29
    {
30 3141
        $this->caseSensitive = true;
31
32 3141
        return $this;
33
    }
34
35
    /**
36
     * @internal
37
     */
38 3186
    public function getRegex(): string
39
    {
40 3186
        return '/' . $this->regex . '/' . ($this->caseSensitive ? '' : 'i');
41
    }
42
43
    /**
44
     * Match the given string (case-insensitive)
45
     */
46 3147
    public static function string(string $str): self
47
    {
48 3147
        return new self(\preg_quote($str, '/'));
49
    }
50
51
    /**
52
     * Match any of the given strings (case-insensitive)
53
     */
54 3147
    public static function oneOf(string ...$str): self
55
    {
56 3147
        return new self(\implode('|', \array_map(static fn (string $str): string => \preg_quote($str, '/'), $str)));
57
    }
58
59
    /**
60
     * Match a partial regular expression without starting/ending delimiters, anchors, or flags
61
     */
62 3147
    public static function regex(string $regex): self
63
    {
64 3147
        return new self($regex);
65
    }
66
67 45
    public static function join(self ...$definitions): self
68
    {
69 45
        $regex         = '';
70 45
        $caseSensitive = null;
71 45
        foreach ($definitions as $definition) {
72 45
            $regex .= '(' . $definition->regex . ')';
73
74 45
            if ($caseSensitive === null) {
75 45
                $caseSensitive = $definition->caseSensitive;
76 45
            } elseif ($caseSensitive !== $definition->caseSensitive) {
77 3
                throw new \LogicException('Case-sensitive and case-insensitive defintions cannot be comined');
78
            }
79
        }
80
81 42
        return new self($regex, $caseSensitive ?? false);
82
    }
83
}
84