AllOrNothing   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 6
c 1
b 0
f 0
dl 0
loc 18
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
A in() 0 3 1
A parse() 0 9 2
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
 * All-or-Nothing
12
 *
13
 * Matches the entire result or nothing at all.
14
 * Used to control where the error occurs. No effect on successful results.
15
 */
16
final class AllOrNothing extends Parser
17
{
18
    public function __construct(private Parser $parser) {}
19
20
    public static function in(Parser|string $parser): Parser
21
    {
22
        return new self(Cast::asParser($parser));
23
    }
24
25
    public function parse(string $input): Result
26
    {
27
        $result = $this->parser->parse($input);
28
29
        if ($result->ok()) {
30
            return $result;
31
        }
32
33
        return Error::in($input);
34
    }
35
}
36