Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
51 / 51 |
|
100.00% |
11 / 11 |
CRAP | |
100.00% |
1 / 1 |
CompiledRegularExpression | |
100.00% |
51 / 51 |
|
100.00% |
11 / 11 |
15 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
createFromRegexWithoutDelimiters | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasStartOfRegexMarker | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasEndOfRegexMarker | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getMinimalPossibleLength | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
getMaximumPossibleLength | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
__toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
toCaseInsensitive | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
toDotAll | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
merge | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
removeStartAndEndMarkers | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | namespace Apie\RegexTools; |
3 | |
4 | use Apie\RegexTools\Parts\EndOfRegex; |
5 | use Apie\RegexTools\Parts\RegexPartInterface; |
6 | use Apie\RegexTools\Parts\StartOfRegex; |
7 | use Stringable; |
8 | |
9 | final class CompiledRegularExpression implements Stringable |
10 | { |
11 | /** |
12 | * @var array<int, RegexPartInterface> |
13 | */ |
14 | private array $parts; |
15 | |
16 | private function __construct(RegexPartInterface... $parts) |
17 | { |
18 | $this->parts = $parts; |
19 | } |
20 | |
21 | public static function createFromRegexWithoutDelimiters(string $regex): self |
22 | { |
23 | return new self(...(new RegexStream($regex))); |
24 | } |
25 | |
26 | public function hasStartOfRegexMarker(): bool |
27 | { |
28 | return reset($this->parts) instanceof StartOfRegex; |
29 | } |
30 | |
31 | public function hasEndOfRegexMarker(): bool |
32 | { |
33 | return end($this->parts) instanceof EndOfRegex; |
34 | } |
35 | |
36 | public function getMinimalPossibleLength(): int |
37 | { |
38 | return array_sum( |
39 | array_map( |
40 | function (RegexPartInterface $part) { |
41 | return $part->getMinimalPossibleLength(); |
42 | }, |
43 | $this->parts |
44 | ) |
45 | ); |
46 | } |
47 | |
48 | public function getMaximumPossibleLength(): ?int |
49 | { |
50 | $sum = 0; |
51 | foreach ($this->parts as $part) { |
52 | $max = $part->getMaximumPossibleLength(); |
53 | if ($max === null) { |
54 | return null; |
55 | } |
56 | $sum += $max; |
57 | } |
58 | return $sum; |
59 | } |
60 | |
61 | public function __toString(): string |
62 | { |
63 | return implode('', $this->parts); |
64 | } |
65 | |
66 | public function toCaseInsensitive(): CompiledRegularExpression |
67 | { |
68 | return new self( |
69 | ...array_map( |
70 | function (RegexPartInterface $part) { |
71 | return $part->toCaseInsensitive(); |
72 | }, |
73 | $this->parts |
74 | ) |
75 | ); |
76 | } |
77 | |
78 | public function toDotAll(): CompiledRegularExpression |
79 | { |
80 | return new self( |
81 | ...array_map( |
82 | function (RegexPartInterface $part) { |
83 | return $part->toDotAll(); |
84 | }, |
85 | $this->parts |
86 | ) |
87 | ); |
88 | } |
89 | |
90 | |
91 | public function merge(CompiledRegularExpression... $expressions): CompiledRegularExpression |
92 | { |
93 | $parts = $this->parts; |
94 | foreach ($expressions as $expression) { |
95 | foreach ($expression->parts as $part) { |
96 | $parts[] = $part; |
97 | } |
98 | } |
99 | |
100 | return new self(...$parts); |
101 | } |
102 | |
103 | public function removeStartAndEndMarkers(): CompiledRegularExpression |
104 | { |
105 | return new self( |
106 | ...array_filter( |
107 | array_map( |
108 | function (RegexPartInterface $part) { |
109 | return $part->removeStartAndEndMarkers(); |
110 | }, |
111 | $this->parts |
112 | ), |
113 | ) |
114 | ); |
115 | } |
116 | } |