Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | 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
 
16
final class InlineParserMatch
17
{
18
    private string $regex;
19
 
20
    private bool $caseSensitive;
21
 
22
    private function __construct(string $regex, bool $caseSensitive = false)
23
    {
24
        $this->regex         = $regex;
25
        $this->caseSensitive = $caseSensitive;
26
    }
27
 
28
    public function caseSensitive(): self
29
    {
30
        $this->caseSensitive = true;
31
 
32
        return $this;
33
    }
34
 
35
    /**
36
     * @internal
37
     */
38
    public function getRegex(): string
39
    {
40
        return '/' . $this->regex . '/' . ($this->caseSensitive ? '' : 'i');
41
    }
42
 
43
    /**
44
     * Match the given string (case-insensitive)
45
     */
46
    public static function string(string $str): self
47
    {
48
        return new self(\preg_quote($str, '/'));
49
    }
50
 
51
    /**
52
     * Match any of the given strings (case-insensitive)
53
     */
54
    public static function oneOf(string ...$str): self
55
    {
56
        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
    public static function regex(string $regex): self
63
    {
64
        return new self($regex);
65
    }
66
 
67
    public static function join(self ...$definitions): self
68
    {
69
        $regex         = '';
70
        $caseSensitive = null;
71
        foreach ($definitions as $definition) {
72
            $regex .= '(' . $definition->regex . ')';
73
 
74
            if ($caseSensitive === null) {
75
                $caseSensitive = $definition->caseSensitive;
76
            } elseif ($caseSensitive !== $definition->caseSensitive) {
77
                throw new \LogicException('Case-sensitive and case-insensitive defintions cannot be comined');
78
            }
79
        }
80
 
81
        return new self($regex, $caseSensitive ?? false);
82
    }
83
}