Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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 <colinodell@gmail.com>
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
 
991 lars 16
use League\CommonMark\Exception\InvalidArgumentException;
17
 
148 lars 18
final class InlineParserMatch
19
{
20
    private string $regex;
21
 
22
    private bool $caseSensitive;
23
 
24
    private function __construct(string $regex, bool $caseSensitive = false)
25
    {
26
        $this->regex         = $regex;
27
        $this->caseSensitive = $caseSensitive;
28
    }
29
 
30
    public function caseSensitive(): self
31
    {
32
        $this->caseSensitive = true;
33
 
34
        return $this;
35
    }
36
 
37
    /**
38
     * @internal
39
     */
40
    public function getRegex(): string
41
    {
42
        return '/' . $this->regex . '/' . ($this->caseSensitive ? '' : 'i');
43
    }
44
 
45
    /**
46
     * Match the given string (case-insensitive)
47
     */
48
    public static function string(string $str): self
49
    {
50
        return new self(\preg_quote($str, '/'));
51
    }
52
 
53
    /**
54
     * Match any of the given strings (case-insensitive)
55
     */
56
    public static function oneOf(string ...$str): self
57
    {
58
        return new self(\implode('|', \array_map(static fn (string $str): string => \preg_quote($str, '/'), $str)));
59
    }
60
 
61
    /**
62
     * Match a partial regular expression without starting/ending delimiters, anchors, or flags
63
     */
64
    public static function regex(string $regex): self
65
    {
66
        return new self($regex);
67
    }
68
 
69
    public static function join(self ...$definitions): self
70
    {
71
        $regex         = '';
72
        $caseSensitive = null;
73
        foreach ($definitions as $definition) {
74
            $regex .= '(' . $definition->regex . ')';
75
 
76
            if ($caseSensitive === null) {
77
                $caseSensitive = $definition->caseSensitive;
78
            } elseif ($caseSensitive !== $definition->caseSensitive) {
991 lars 79
                throw new InvalidArgumentException('Case-sensitive and case-insensitive definitions cannot be combined');
148 lars 80
            }
81
        }
82
 
83
        return new self($regex, $caseSensitive ?? false);
84
    }
85
}