|
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
|
|
|
|