Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
46 / 46 |
|
100.00% |
8 / 8 |
CRAP | |
100.00% |
1 / 1 |
| CaptureGroup | |
100.00% |
46 / 46 |
|
100.00% |
8 / 8 |
10 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getRegexStringLength | |
100.00% |
3 / 3 |
|
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 | |||
| toCaseInsensitive | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| toDotAll | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| removeStartAndEndMarkers | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\RegexTools\Parts; |
| 3 | |
| 4 | final class CaptureGroup implements RegexPartInterface |
| 5 | { |
| 6 | /** |
| 7 | * @param array<int, RegexPartInterface> $part |
| 8 | */ |
| 9 | public function __construct( |
| 10 | public readonly array $part |
| 11 | ) { |
| 12 | } |
| 13 | |
| 14 | public function __toString(): string |
| 15 | { |
| 16 | return '(' . implode('', $this->part) . ')'; |
| 17 | } |
| 18 | |
| 19 | public function getRegexStringLength(): int |
| 20 | { |
| 21 | return array_reduce($this->part, function (int $prevValue, RegexPartInterface $part) { |
| 22 | return $prevValue + $part->getRegexStringLength(); |
| 23 | }, 2); |
| 24 | } |
| 25 | |
| 26 | public function getMinimalPossibleLength(): int |
| 27 | { |
| 28 | return array_sum( |
| 29 | array_map( |
| 30 | function (RegexPartInterface $part) { |
| 31 | return $part->getMinimalPossibleLength(); |
| 32 | }, |
| 33 | $this->part |
| 34 | ) |
| 35 | ); |
| 36 | ; |
| 37 | } |
| 38 | |
| 39 | public function getMaximumPossibleLength(): ?int |
| 40 | { |
| 41 | $sum = 0; |
| 42 | foreach ($this->part as $part) { |
| 43 | $max = $part->getMaximumPossibleLength(); |
| 44 | if ($max === null) { |
| 45 | return null; |
| 46 | } |
| 47 | $sum += $max; |
| 48 | } |
| 49 | return $sum; |
| 50 | } |
| 51 | |
| 52 | public function toCaseInsensitive(): RegexPartInterface |
| 53 | { |
| 54 | return new CaptureGroup( |
| 55 | array_map( |
| 56 | function (RegexPartInterface $part) { |
| 57 | return $part->toCaseInsensitive(); |
| 58 | }, |
| 59 | $this->part |
| 60 | ) |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | public function toDotAll(): RegexPartInterface |
| 65 | { |
| 66 | return new CaptureGroup( |
| 67 | array_map( |
| 68 | function (RegexPartInterface $part) { |
| 69 | return $part->toDotAll(); |
| 70 | }, |
| 71 | $this->part |
| 72 | ) |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | public function removeStartAndEndMarkers(): ?RegexPartInterface |
| 77 | { |
| 78 | return new CaptureGroup( |
| 79 | array_filter( |
| 80 | array_map( |
| 81 | function (RegexPartInterface $part) { |
| 82 | return $part->removeStartAndEndMarkers(); |
| 83 | }, |
| 84 | $this->part |
| 85 | ) |
| 86 | ) |
| 87 | ); |
| 88 | } |
| 89 | } |