Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
73.68% |
14 / 19 |
|
75.00% |
6 / 8 |
CRAP | |
0.00% |
0 / 1 |
| RepeatToken | |
73.68% |
14 / 19 |
|
75.00% |
6 / 8 |
20.67 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __toString | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| getRegexStringLength | |
33.33% |
2 / 6 |
|
0.00% |
0 / 1 |
8.74 | |||
| getMinimalPossibleLength | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMaximumPossibleLength | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
3.14 | |||
| toCaseInsensitive | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toDotAll | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| removeStartAndEndMarkers | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Apie\RegexTools\Parts; |
| 3 | |
| 4 | final class RepeatToken implements RegexPartInterface |
| 5 | { |
| 6 | public function __construct( |
| 7 | public readonly RegexPartInterface $part, |
| 8 | public readonly ?int $minimum, |
| 9 | public readonly ?int $maximum, |
| 10 | private readonly ?string $regex = null |
| 11 | ) { |
| 12 | } |
| 13 | |
| 14 | public function __toString(): string |
| 15 | { |
| 16 | if ($this->minimum === $this->maximum && $this->minimum !== null) { |
| 17 | return $this->part . '{' . $this->minimum . '}'; |
| 18 | } |
| 19 | return $this->part . '{' . $this->minimum . ',' . $this->maximum . '}'; |
| 20 | } |
| 21 | |
| 22 | public function getRegexStringLength(): int |
| 23 | { |
| 24 | if ($this->regex) { |
| 25 | return $this->part->getRegexStringLength() + strlen($this->regex); |
| 26 | } |
| 27 | return $this->part->getRegexStringLength() |
| 28 | + 3 |
| 29 | + ($this->minimum === null ? 0 : strlen((string) $this->minimum)) |
| 30 | + ($this->maximum === null ? 0 : strlen((string) $this->maximum)); |
| 31 | } |
| 32 | |
| 33 | public function getMinimalPossibleLength(): int |
| 34 | { |
| 35 | return $this->part->getMinimalPossibleLength() * $this->minimum; |
| 36 | } |
| 37 | |
| 38 | public function getMaximumPossibleLength(): ?int |
| 39 | { |
| 40 | $max = $this->part->getMaximumPossibleLength(); |
| 41 | if ($max === null) { |
| 42 | return null; |
| 43 | } |
| 44 | return $this->maximum === null ? null : ($max * $this->maximum); |
| 45 | } |
| 46 | |
| 47 | public function toCaseInsensitive(): RegexPartInterface |
| 48 | { |
| 49 | return new RepeatToken($this->part->toCaseInsensitive(), $this->minimum, $this->maximum); |
| 50 | } |
| 51 | |
| 52 | public function toDotAll(): RegexPartInterface |
| 53 | { |
| 54 | return new RepeatToken($this->part->toDotAll(), $this->minimum, $this->maximum); |
| 55 | } |
| 56 | |
| 57 | public function removeStartAndEndMarkers(): ?RegexPartInterface |
| 58 | { |
| 59 | $part = $this->part->removeStartAndEndMarkers(); |
| 60 | return $part ? new RepeatToken($part, $this->minimum, $this->maximum) : null; |
| 61 | } |
| 62 | } |