Lazily::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 2
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\Parser\Parsers;
4
5
use RuntimeException;
6
use Stratadox\Parser\LazyContainer;
7
use Stratadox\Parser\Parser;
8
use Stratadox\Parser\Result;
9
10
/**
11
 * Lazily
12
 *
13
 * Lazy-loading proxy for the parser.
14
 */
15
final class Lazily extends Parser
16
{
17
    public function __construct(
18
        private LazyContainer $lazy,
19
        private string        $name,
20
    ) {}
21
22
    public function parse(string $input): Result
23
    {
24
        $parser = $this->lazy->factory($this->name)();
25
26
        if (!$parser instanceof Parser) {
27
            throw new RuntimeException("Missing the lazy parser `{$this->name}` for input `$input`");
28
        }
29
30
        return $parser->parse($input);
31
    }
32
}
33