Completed
Push — tab-handling ( ecbd21...d44572 )
by Colin
10:02 queued 07:31
created

AbstractBlock::parent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
9
 *  - (c) John MacFarlane
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace League\CommonMark\Block\Element;
16
17
use League\CommonMark\ContextInterface;
18
use League\CommonMark\Cursor;
19
use League\CommonMark\Node\Node;
20
use League\CommonMark\Util\ArrayCollection;
21
22
/**
23
 * Block-level element
24
 */
25
abstract class AbstractBlock extends Node
26
{
27
    /**
28
     * Used for storage of arbitrary data.
29
     *
30
     * @var array
31
     */
32
    public $data = [];
33
34
    /**
35
     * @var ArrayCollection|string[]
36
     */
37
    protected $strings;
38
39
    /**
40
     * @var string
41
     */
42
    protected $finalStringContents = '';
43
44
    /**
45
     * @var bool
46
     */
47
    protected $open = true;
48
49
    /**
50
     * @var bool
51
     */
52
    protected $lastLineBlank = false;
53
54
    /**
55
     * @var int
56
     */
57
    protected $startLine;
58
59
    /**
60
     * @var int
61
     */
62
    protected $endLine;
63
64
    /**
65
     * Constructor
66
     */
67 1971
    public function __construct()
68
    {
69 1971
        $this->strings = new ArrayCollection();
70 1971
    }
71
72
    /**
73
     * @param Node|null $node
74
     */
75 1863
    protected function setParent(Node $node = null)
76
    {
77 1863
        if ($node && !$node instanceof self) {
78
            throw new \InvalidArgumentException('Parent of block must also be block (can not be inline)');
79
        }
80
81 1863
        parent::setParent($node);
82 1863
    }
83
84
    /**
85
     * @return bool
86
     */
87 1863
    public function isContainer()
88
    {
89 1863
        return true;
90
    }
91
92
    /**
93
     * @return bool
94
     */
95 1863
    public function hasChildren()
96
    {
97 1863
        return !is_null($this->firstChild);
98
    }
99
100
    /**
101
     * Returns true if this block can contain the given block as a child node
102
     *
103
     * @param AbstractBlock $block
104
     *
105
     * @return bool
106
     */
107
    abstract public function canContain(AbstractBlock $block);
108
109
    /**
110
     * Returns true if block type can accept lines of text
111
     *
112
     * @return bool
113
     */
114
    abstract public function acceptsLines();
115
116
    /**
117
     * Whether this is a code block
118
     *
119
     * @return bool
120
     */
121
    abstract public function isCode();
122
123
    /**
124
     * @param Cursor $cursor
125
     *
126
     * @return bool
127
     */
128
    abstract public function matchesNextLine(Cursor $cursor);
129
130
    /**
131
     * @param ContextInterface $context
132
     * @param Cursor           $cursor
133
     */
134
    public function handleRemainingContents(ContextInterface $context, Cursor $cursor)
135
    {
136
        // create paragraph container for line
137
        $context->addBlock(new Paragraph());
138
        $cursor->advanceToFirstNonSpace();
139
        $context->getTip()->addLine($cursor->getRemainder());
140
    }
141
142
    /**
143
     * @param int $startLine
144
     *
145
     * @return $this
146
     */
147 1869
    public function setStartLine($startLine)
148
    {
149 1869
        $this->startLine = $startLine;
150 1869
        if (empty($this->endLine)) {
151 1869
            $this->endLine = $startLine;
152 1869
        }
153
154 1869
        return $this;
155
    }
156
157
    /**
158
     * @return int
159
     */
160
    public function getStartLine()
161
    {
162
        return $this->startLine;
163
    }
164
165
    public function setEndLine($endLine)
166
    {
167
        $this->endLine = $endLine;
168
169
        return $this;
170
    }
171
172
    /**
173
     * @return int
174
     */
175
    public function getEndLine()
176
    {
177
        return $this->endLine;
178
    }
179
180
    /**
181
     * Whether the block ends with a blank line
182
     *
183
     * @return bool
184
     */
185 642
    public function endsWithBlankLine()
186
    {
187 642
        return $this->lastLineBlank;
188
    }
189
190
    /**
191
     * @param bool $blank
192
     */
193 1863
    public function setLastLineBlank($blank)
194
    {
195 1863
        $this->lastLineBlank = $blank;
196 1863
    }
197
198
    /**
199
     * Determines whether the last line should be marked as blank
200
     *
201
     * @param Cursor $cursor
202
     * @param int    $currentLineNumber
203
     *
204
     * @return bool
205
     */
206 1638
    public function shouldLastLineBeBlank(Cursor $cursor, $currentLineNumber)
207
    {
208 1638
        return $cursor->isBlank();
209
    }
210
211
    /**
212
     * @return string[]
213
     */
214 1758
    public function getStrings()
215
    {
216 1758
        return $this->strings->toArray();
217
    }
218
219
    /**
220
     * @param string $line
221
     */
222 1851
    public function addLine($line)
223
    {
224 1851
        if (!$this->acceptsLines()) {
225
            throw new \LogicException('You cannot add lines to a block which cannot accept them');
226
        }
227
228 1851
        $this->strings->add($line);
229 1851
    }
230
231
    /**
232
     * Whether the block is open for modifications
233
     *
234
     * @return bool
235
     */
236 1032
    public function isOpen()
237
    {
238 1032
        return $this->open;
239
    }
240
241
    /**
242
     * Finalize the block; mark it closed for modification
243
     *
244
     * @param ContextInterface $context
245
     * @param int              $endLineNumber
246
     */
247 1863
    public function finalize(ContextInterface $context, $endLineNumber)
248
    {
249 1863
        if (!$this->open) {
250
            return; // TODO: Throw AlreadyClosedException?
251
        }
252
253 1863
        $this->open = false;
254 1863
        $this->endLine = $endLineNumber;
255
256 1863
        $context->setTip($context->getTip()->parent());
257 1863
    }
258
259
    /**
260
     * @return string
261
     */
262 1833
    public function getStringContent()
263
    {
264 1833
        return $this->finalStringContents;
265
    }
266
267
    /**
268
     * @param string $key
269
     * @param mixed  $default
270
     *
271
     * @return mixed
272
     */
273 1830
    public function getData($key, $default = null)
274
    {
275 1830
        return array_key_exists($key, $this->data) ? $this->data[$key] : $default;
276
    }
277
}
278