InlineParserMatch::oneOf()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
use League\CommonMark\Exception\InvalidArgumentException;
17
18
final class InlineParserMatch
19
{
20
    private string $regex;
21
22
    private bool $caseSensitive;
23
24 2312
    private function __construct(string $regex, bool $caseSensitive = false)
25
    {
26 2312
        $this->regex         = $regex;
27 2312
        $this->caseSensitive = $caseSensitive;
28
    }
29
30 2306
    public function caseSensitive(): self
31
    {
32 2306
        $this->caseSensitive = true;
33
34 2306
        return $this;
35
    }
36
37
    /**
38
     * @internal
39
     *
40
     * @psalm-return non-empty-string
41
     */
42 2338
    public function getRegex(): string
43
    {
44 2338
        return '/' . $this->regex . '/' . ($this->caseSensitive ? '' : 'i');
45
    }
46
47
    /**
48
     * Match the given string (case-insensitive)
49
     */
50 2312
    public static function string(string $str): self
51
    {
52 2312
        return new self(\preg_quote($str, '/'));
53
    }
54
55
    /**
56
     * Match any of the given strings (case-insensitive)
57
     */
58 2310
    public static function oneOf(string ...$str): self
59
    {
60 2310
        return new self(\implode('|', \array_map(static fn (string $str): string => \preg_quote($str, '/'), $str)));
61
    }
62
63
    /**
64
     * Match a partial regular expression without starting/ending delimiters, anchors, or flags
65
     */
66 2310
    public static function regex(string $regex): self
67
    {
68 2310
        return new self($regex);
69
    }
70
71 32
    public static function join(self ...$definitions): self
72
    {
73 32
        $regex         = '';
74 32
        $caseSensitive = null;
75 32
        foreach ($definitions as $definition) {
76 32
            $regex .= '(' . $definition->regex . ')';
77
78 32
            if ($caseSensitive === null) {
79 32
                $caseSensitive = $definition->caseSensitive;
80 32
            } elseif ($caseSensitive !== $definition->caseSensitive) {
81 2
                throw new InvalidArgumentException('Case-sensitive and case-insensitive definitions cannot be combined');
82
            }
83
        }
84
85 30
        return new self($regex, $caseSensitive ?? false);
86
    }
87
}
88