1<?php
2
3/**
4 * Created by IntelliJ IDEA.
5 * User: michael
6 * Date: 7/7/17
7 * Time: 4:06 PM
8 */
9
10namespace dokuwiki\plugin\prosemirror\parser;
11
12class ListNode extends Node
13{
14    protected $parent;
15    /** @var string */
16    protected $prefix;
17    /** @var ListItemNode[] */
18    protected $listItemNodes = [];
19
20    protected $depth = 0;
21
22    public function __construct($data, Node $parent)
23    {
24        $this->parent = &$parent;
25        if (is_a($this->parent, 'dokuwiki\plugin\prosemirror\parser\ListItemNode')) {
26            $this->depth = $this->parent->getDepth() + 1;
27        }
28
29        $this->prefix = $data['type'] == 'bullet_list' ? '  *' : '  -';
30
31        foreach ($data['content'] as $listItemNode) {
32            $this->listItemNodes[] = new ListItemNode($listItemNode, $this);
33        }
34    }
35
36    public function toSyntax()
37    {
38        $doc = '';
39
40        foreach ($this->listItemNodes as $li) {
41            $liText = str_repeat('  ', $this->depth);
42            $liText .= $this->prefix;
43            $lines = $li->toSyntax();
44            if (!empty($lines) && $lines[0] !== ' ') {
45                $liText .= ' ';
46            }
47            $liText .= $lines . "\n";
48            $doc .= $liText;
49        }
50        return rtrim($doc); // blocks should __not__ end with a newline, parents must handle breaks between children
51    }
52
53    public function getDepth()
54    {
55        return $this->depth;
56    }
57}
58