Grammar::__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\Containers;
4
5
use Stratadox\Parser\Container;
6
use Stratadox\Parser\LazyContainer;
7
use Stratadox\Parser\Parser;
8
9
/**
10
 * Grammar Container
11
 *
12
 * Mixes lazy and eager containers.
13
 */
14
final class Grammar implements Container
15
{
16
    public function __construct(
17
        private Container     $eager,
18
        private LazyContainer $lazy,
19
    ) {}
20
21
    public static function container(): self
22
    {
23
        return new self(Eager::container(), Lazy::container());
24
    }
25
26
    public static function with(LazyContainer $lazy): self
27
    {
28
        return new self(Eager::container(), $lazy);
29
    }
30
31
    public function offsetGet($name): Parser
32
    {
33
        return $this->eager[$name] ?? $this->lazy[$name];
34
    }
35
36
    public function offsetSet($name, $parser): void
37
    {
38
        $this->eager[$name] = $parser;
39
    }
40
41
    public function offsetExists($name): bool
42
    {
43
        return isset($this->eager[$name]) || isset($this->lazy[$name]);
44
    }
45
46
    public function offsetUnset($name): void
47
    {
48
        unset($this->eager[$name]);
49
    }
50
51
    public function lazy(string $name, Parser $parser): void
52
    {
53
        $this->lazy[$name] = $parser;
54
    }
55
}
56