| Total Complexity | 9 |
| Total Lines | 60 |
| Duplicated Lines | 0 % |
| Coverage | 96.3% |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
| 1 | <?php |
||
| 11 | final class RegExp |
||
| 12 | { |
||
| 13 | private string $pattern; |
||
| 14 | |||
| 15 | 10 | public function __construct(string $pattern) |
|
| 16 | { |
||
| 17 | 10 | if (@\preg_match($pattern, '') === false) { |
|
| 18 | 1 | throw new DomainException($pattern, \preg_last_error()); |
|
| 19 | } |
||
| 20 | |||
| 21 | 9 | $this->pattern = $pattern; |
|
| 22 | 9 | } |
|
| 23 | |||
| 24 | 6 | public static function of(string $pattern): self |
|
| 25 | { |
||
| 26 | 6 | return new self($pattern); |
|
| 27 | } |
||
| 28 | |||
| 29 | 3 | public function matches(Str $string): bool |
|
| 30 | { |
||
| 31 | 3 | $value = \preg_match($this->pattern, (string) $string); |
|
| 32 | |||
| 33 | 3 | if ($value === false) { |
|
| 34 | 1 | throw new RegexException('', \preg_last_error()); |
|
| 35 | } |
||
| 36 | |||
| 37 | 2 | return (bool) $value; |
|
| 38 | } |
||
| 39 | |||
| 40 | |||
| 41 | /** |
||
| 42 | * @return Map<scalar, Str> |
||
| 43 | */ |
||
| 44 | 4 | public function capture(Str $string): Map |
|
| 45 | { |
||
| 46 | 4 | $matches = []; |
|
| 47 | 4 | $value = \preg_match($this->pattern, (string) $string, $matches); |
|
| 48 | |||
| 49 | 4 | if ($value === false) { |
|
| 50 | 1 | throw new RegexException('', \preg_last_error()); |
|
| 51 | } |
||
| 52 | |||
| 53 | 3 | $map = Map::of('scalar', Str::class); |
|
| 54 | |||
| 55 | 3 | foreach ($matches as $key => $match) { |
|
| 56 | 3 | $map = $map->put( |
|
| 57 | 3 | $key, |
|
| 58 | Str::of( |
||
| 59 | 3 | (string) $match, |
|
| 60 | 3 | (string) $string->encoding() |
|
| 61 | ) |
||
| 62 | ); |
||
| 63 | } |
||
| 64 | |||
| 65 | 3 | return $map; |
|
| 66 | } |
||
| 67 | |||
| 68 | 2 | public function __toString(): string |
|
| 71 | } |
||
| 72 | } |
||
| 73 |