Except::parse()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 2
nop 1
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\Parser\Parsers;
4
5
use Stratadox\Parser\Helpers\Cast;
6
use Stratadox\Parser\Parser;
7
use Stratadox\Parser\Result;
8
use Stratadox\Parser\Results\Error;
9
10
/**
11
 * Except
12
 *
13
 * Transforms a successful result into a failure if another parser also matches
14
 * the content.
15
 */
16
final class Except extends Parser
17
{
18
    public function __construct(
19
        private Parser $refuse,
20
        private Parser $parser,
21
    ) {}
22
23
    public static function for(Parser|string $refusal, Parser|string $parser): Parser
24
    {
25
        return new self(Cast::asParser($refusal), Cast::asParser($parser));
26
    }
27
28
    public function parse(string $input): Result
29
    {
30
        if ($this->refuse->parse($input)->ok()) {
31
            return Error::in($input);
32
        }
33
        return $this->parser->parse($input);
34
    }
35
}
36