Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
 
3
/*
4
 * This file is part of composer/pcre.
5
 *
6
 * (c) Composer <https://github.com/composer>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
 
12
namespace Composer\Pcre;
13
 
14
class Regex
15
{
16
    /**
17
     * @param non-empty-string $pattern
18
     */
19
    public static function isMatch(string $pattern, string $subject, int $offset = 0): bool
20
    {
21
        return (bool) Preg::match($pattern, $subject, $matches, 0, $offset);
22
    }
23
 
24
    /**
25
     * @param non-empty-string $pattern
26
     * @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
27
     */
28
    public static function match(string $pattern, string $subject, int $flags = 0, int $offset = 0): MatchResult
29
    {
30
        self::checkOffsetCapture($flags, 'matchWithOffsets');
31
 
32
        $count = Preg::match($pattern, $subject, $matches, $flags, $offset);
33
 
34
        return new MatchResult($count, $matches);
35
    }
36
 
37
    /**
38
     * Variant of `match()` which returns non-null matches (or throws)
39
     *
40
     * @param non-empty-string $pattern
41
     * @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
42
     * @throws UnexpectedNullMatchException
43
     */
44
    public static function matchStrictGroups(string $pattern, string $subject, int $flags = 0, int $offset = 0): MatchStrictGroupsResult
45
    {
46
        // @phpstan-ignore composerPcre.maybeUnsafeStrictGroups
47
        $count = Preg::matchStrictGroups($pattern, $subject, $matches, $flags, $offset);
48
 
49
        return new MatchStrictGroupsResult($count, $matches);
50
    }
51
 
52
    /**
53
     * Runs preg_match with PREG_OFFSET_CAPTURE
54
     *
55
     * @param non-empty-string $pattern
56
     * @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_UNMATCHED_AS_NULL and PREG_MATCH_OFFSET are always set, no other flags are supported
57
     */
58
    public static function matchWithOffsets(string $pattern, string $subject, int $flags = 0, int $offset = 0): MatchWithOffsetsResult
59
    {
60
        $count = Preg::matchWithOffsets($pattern, $subject, $matches, $flags, $offset);
61
 
62
        return new MatchWithOffsetsResult($count, $matches);
63
    }
64
 
65
    /**
66
     * @param non-empty-string $pattern
67
     * @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
68
     */
69
    public static function matchAll(string $pattern, string $subject, int $flags = 0, int $offset = 0): MatchAllResult
70
    {
71
        self::checkOffsetCapture($flags, 'matchAllWithOffsets');
72
        self::checkSetOrder($flags);
73
 
74
        $count = Preg::matchAll($pattern, $subject, $matches, $flags, $offset);
75
 
76
        return new MatchAllResult($count, $matches);
77
    }
78
 
79
    /**
80
     * Variant of `matchAll()` which returns non-null matches (or throws)
81
     *
82
     * @param non-empty-string $pattern
83
     * @param int-mask<PREG_UNMATCHED_AS_NULL> $flags PREG_UNMATCHED_AS_NULL is always set, no other flags are supported
84
     * @throws UnexpectedNullMatchException
85
     */
86
    public static function matchAllStrictGroups(string $pattern, string $subject, int $flags = 0, int $offset = 0): MatchAllStrictGroupsResult
87
    {
88
        self::checkOffsetCapture($flags, 'matchAllWithOffsets');
89
        self::checkSetOrder($flags);
90
 
91
        // @phpstan-ignore composerPcre.maybeUnsafeStrictGroups
92
        $count = Preg::matchAllStrictGroups($pattern, $subject, $matches, $flags, $offset);
93
 
94
        return new MatchAllStrictGroupsResult($count, $matches);
95
    }
96
 
97
    /**
98
     * Runs preg_match_all with PREG_OFFSET_CAPTURE
99
     *
100
     * @param non-empty-string $pattern
101
     * @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_UNMATCHED_AS_NULL and PREG_MATCH_OFFSET are always set, no other flags are supported
102
     */
103
    public static function matchAllWithOffsets(string $pattern, string $subject, int $flags = 0, int $offset = 0): MatchAllWithOffsetsResult
104
    {
105
        self::checkSetOrder($flags);
106
 
107
        $count = Preg::matchAllWithOffsets($pattern, $subject, $matches, $flags, $offset);
108
 
109
        return new MatchAllWithOffsetsResult($count, $matches);
110
    }
111
    /**
112
     * @param string|string[] $pattern
113
     * @param string|string[] $replacement
114
     * @param string          $subject
115
     */
116
    public static function replace($pattern, $replacement, $subject, int $limit = -1): ReplaceResult
117
    {
118
        $result = Preg::replace($pattern, $replacement, $subject, $limit, $count);
119
 
120
        return new ReplaceResult($count, $result);
121
    }
122
 
123
    /**
124
     * @param string|string[] $pattern
125
     * @param ($flags is PREG_OFFSET_CAPTURE ? (callable(array<int|string, array{string|null, int<-1, max>}>): string) : callable(array<int|string, string|null>): string) $replacement
126
     * @param string          $subject
127
     * @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_OFFSET_CAPTURE is supported, PREG_UNMATCHED_AS_NULL is always set
128
     */
129
    public static function replaceCallback($pattern, callable $replacement, $subject, int $limit = -1, int $flags = 0): ReplaceResult
130
    {
131
        $result = Preg::replaceCallback($pattern, $replacement, $subject, $limit, $count, $flags);
132
 
133
        return new ReplaceResult($count, $result);
134
    }
135
 
136
    /**
137
     * Variant of `replaceCallback()` which outputs non-null matches (or throws)
138
     *
139
     * @param string $pattern
140
     * @param ($flags is PREG_OFFSET_CAPTURE ? (callable(array<int|string, array{string, int<0, max>}>): string) : callable(array<int|string, string>): string) $replacement
141
     * @param string          $subject
142
     * @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_OFFSET_CAPTURE is supported, PREG_UNMATCHED_AS_NULL is always set
143
     */
144
    public static function replaceCallbackStrictGroups($pattern, callable $replacement, $subject, int $limit = -1, int $flags = 0): ReplaceResult
145
    {
146
        $result = Preg::replaceCallbackStrictGroups($pattern, $replacement, $subject, $limit, $count, $flags);
147
 
148
        return new ReplaceResult($count, $result);
149
    }
150
 
151
    /**
152
     * @param ($flags is PREG_OFFSET_CAPTURE ? (array<string, callable(array<int|string, array{string|null, int<-1, max>}>): string>) : array<string, callable(array<int|string, string|null>): string>) $pattern
153
     * @param string $subject
154
     * @param int-mask<PREG_UNMATCHED_AS_NULL|PREG_OFFSET_CAPTURE> $flags PREG_OFFSET_CAPTURE is supported, PREG_UNMATCHED_AS_NULL is always set
155
     */
156
    public static function replaceCallbackArray(array $pattern, $subject, int $limit = -1, int $flags = 0): ReplaceResult
157
    {
158
        $result = Preg::replaceCallbackArray($pattern, $subject, $limit, $count, $flags);
159
 
160
        return new ReplaceResult($count, $result);
161
    }
162
 
163
    private static function checkOffsetCapture(int $flags, string $useFunctionName): void
164
    {
165
        if (($flags & PREG_OFFSET_CAPTURE) !== 0) {
166
            throw new \InvalidArgumentException('PREG_OFFSET_CAPTURE is not supported as it changes the return type, use '.$useFunctionName.'() instead');
167
        }
168
    }
169
 
170
    private static function checkSetOrder(int $flags): void
171
    {
172
        if (($flags & PREG_SET_ORDER) !== 0) {
173
            throw new \InvalidArgumentException('PREG_SET_ORDER is not supported as it changes the return type');
174
        }
175
    }
176
}