Completed
Push — master ( e1e275...3d6629 )
by Martin
20:32
created

Table::getCaption()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 3
eloc 4
nc 3
nop 0
1
<?php
2
3
/*
4
 * This is part of the webuni/commonmark-table-extension package.
5
 *
6
 * (c) Martin Hasoň <[email protected]>
7
 * (c) Webuni s.r.o. <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Webuni\CommonMark\TableExtension;
14
15
use League\CommonMark\Block\Element\AbstractBlock;
16
use League\CommonMark\ContextInterface;
17
use League\CommonMark\Cursor;
18
19
class Table extends AbstractBlock
20
{
21
    private $parser;
22
23
    public function __construct(\Closure $parser)
24
    {
25
        parent::__construct();
26
        $this->appendChild(new TableRows(TableRows::TYPE_HEAD));
27
        $this->appendChild(new TableRows(TableRows::TYPE_BODY));
28
        $this->parser = $parser;
29
    }
30
31
    public function canContain(AbstractBlock $block)
32
    {
33
        return $block instanceof TableRows || $block instanceof TableCaption;
34
    }
35
36
    public function acceptsLines()
37
    {
38
        return true;
39
    }
40
41
    public function isCode()
42
    {
43
        return false;
44
    }
45
46
    public function setCaption(TableCaption $caption = null)
47
    {
48
        $node = $this->getCaption();
49
        if ($node instanceof TableCaption) {
50
            $node->detach();
51
        }
52
53
        if ($caption instanceof TableCaption) {
54
            $this->prependChild($caption);
55
        }
56
    }
57
58
    public function getCaption()
59
    {
60
        foreach ($this->children() as $child) {
61
            if ($child instanceof TableCaption) {
62
                return $child;
63
            }
64
        }
65
    }
66
67
    public function getHead()
68
    {
69
        foreach ($this->children() as $child) {
70
            if ($child instanceof TableRows && $child->isHead()) {
71
                return $child;
72
            }
73
        }
74
    }
75
76
    public function getBody()
77
    {
78
        foreach ($this->children() as $child) {
79
            if ($child instanceof TableRows && $child->isBody()) {
80
                return $child;
81
            }
82
        }
83
    }
84
85
    public function matchesNextLine(Cursor $cursor)
86
    {
87
        return call_user_func($this->parser, $cursor);
88
    }
89
90
    public function handleRemainingContents(ContextInterface $context, Cursor $cursor)
91
    {
92
    }
93
}
94