1<?php
2
3namespace dokuwiki\plugin\prosemirror\parser;
4
5class TableNode extends Node
6{
7
8    /** @var TableRowNode[] */
9    protected $tableRows = [];
10    protected $rowSpans = [];
11    protected $numCols;
12
13    public function __construct($data, Node $parent = null)
14    {
15        foreach ($data['content'] as $row) {
16            $this->tableRows[] = new TableRowNode($row, $this);
17        }
18        $this->countColNum();
19    }
20
21    /**
22     * Count the total number of columns in the table
23     *
24     * This method calculates the number of columns in the first row, by adding the colspan of each cell.
25     * This produces the correct number columns, since the first row cannot have ommited cells due to a
26     * rowspan, as every other row could have.
27     *
28     */
29    protected function countColNum() {
30        $this->numCols = $this->tableRows[0]->countCols();
31    }
32
33    /**
34     * Get the total number of columns for this table
35     *
36     * @return int
37     */
38    public function getNumTableCols() {
39        return $this->numCols;
40    }
41
42    public function toSyntax()
43    {
44        $doc = '';
45        foreach ($this->tableRows as $row) {
46            $doc .= $row->toSyntax() . "\n";
47        }
48
49        return $doc;
50    }
51
52    public function getRowSpans()
53    {
54        return $this->rowSpans;
55    }
56
57    public function setRowSpans(array $rowSpans)
58    {
59        $this->rowSpans = $rowSpans;
60    }
61}
62