Passed
Push — 2.0 ( 040f68...c13575 )
by Colin
02:06
created

BlockStart::at()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace League\CommonMark\Parser\Block;
15
16
use League\CommonMark\Parser\Cursor;
17
use League\CommonMark\Parser\CursorState;
18
19
/**
20
 * Result object for starting parsing of a block; see static methods for constructors
21
 */
22
final class BlockStart
23
{
24
    /**
25
     * @var BlockContinueParserInterface[]
26
     *
27
     * @psalm-readonly
28
     */
29
    private array $blockParsers;
30
31
    /** @psalm-readonly-allow-private-mutation */
32
    private ?CursorState $cursorState = null;
33
34
    /** @psalm-readonly-allow-private-mutation */
35
    private bool $replaceActiveBlockParser = false;
36
37
    private bool $isAborting = false;
38
39 1406
    private function __construct(BlockContinueParserInterface ...$blockParsers)
40
    {
41 1406
        $this->blockParsers = $blockParsers;
42 1406
    }
43
44
    /**
45
     * @return BlockContinueParserInterface[]
46
     */
47 836
    public function getBlockParsers(): iterable
48
    {
49 836
        return $this->blockParsers;
50
    }
51
52 824
    public function getCursorState(): ?CursorState
53
    {
54 824
        return $this->cursorState;
55
    }
56
57 824
    public function isReplaceActiveBlockParser(): bool
58
    {
59 824
        return $this->replaceActiveBlockParser;
60
    }
61
62
    /**
63
     * @internal
64
     */
65 1394
    public function isAborting(): bool
66
    {
67 1394
        return $this->isAborting;
68
    }
69
70
    /**
71
     * Signal that we want to parse at the given cursor position
72
     *
73
     * @return $this
74
     */
75 836
    public function at(Cursor $cursor): self
76
    {
77 836
        $this->cursorState = $cursor->saveState();
78
79 836
        return $this;
80
    }
81
82
    /**
83
     * Signal that we want to replace the active block parser with this one
84
     *
85
     * @return $this
86
     */
87 130
    public function replaceActiveBlockParser(): self
88
    {
89 130
        $this->replaceActiveBlockParser = true;
90
91 130
        return $this;
92
    }
93
94
    /**
95
     * Signal that we cannot parse whatever is here
96
     *
97
     * @return null
98
     */
99 1680
    public static function none(): ?self
100
    {
101 1680
        return null;
102
    }
103
104
    /**
105
     * Signal that we'd like to register the given parser(s) so they can parse the current block
106
     */
107 836
    public static function of(BlockContinueParserInterface ...$blockParsers): self
108
    {
109 836
        return new self(...$blockParsers);
110
    }
111
112
    /**
113
     * Signal that the block parsing process should be aborted (no other block starts should be checked)
114
     *
115
     * @internal
116
     */
117 1050
    public static function abort(): self
118
    {
119 1050
        $ret             = new self();
120 1050
        $ret->isAborting = true;
121
122 1050
        return $ret;
123
    }
124
}
125