| Total Complexity | 8 |
| Total Lines | 31 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php declare(strict_types=1); |
||
| 19 | final class Either extends Parser |
||
| 20 | { |
||
| 21 | /** @param Parser[] $options */ |
||
| 22 | public function __construct(private array $options) {} |
||
| 23 | |||
| 24 | public static function of(Parser|string ...$options): Parser |
||
| 25 | { |
||
| 26 | return new self(Cast::asParsers(...$options)); |
||
| 27 | } |
||
| 28 | |||
| 29 | public function parse(string $input): Result |
||
| 30 | { |
||
| 31 | $error = null; |
||
| 32 | $errorPosition = INF; |
||
| 33 | foreach ($this->options as $parser) { |
||
| 34 | $result = $parser->parse($input); |
||
| 35 | if ($result->ok()) { |
||
| 36 | return $result; |
||
| 37 | } |
||
| 38 | $pos = strlen($result->unparsed()); |
||
| 39 | if ($pos < $errorPosition) { |
||
| 40 | $errorPosition = $pos; |
||
| 41 | $error = $result; |
||
| 42 | } |
||
| 43 | } |
||
| 44 | return $error ?: Error::in($input); |
||
| 45 | } |
||
| 46 | |||
| 47 | public function or(string|Parser ...$other): Parser |
||
| 50 | } |
||
| 51 | } |
||
| 52 |