1<?php
2
3namespace dokuwiki\plugin\wordimport\docx;
4
5/**
6 * A heading
7 *
8 * This is a paragraph with a specific style indicating a heading
9 */
10class Heading extends AbstractParagraph
11{
12    /** @var int header level 1 to 5 */
13    protected $level = 1;
14    /** @var string The text of the heading */
15    protected $text = '';
16
17    /**
18     * Headers use a style ID that points to a style in style.xml. That style has a name like "heading 1"
19     * We extract the number from that name to get the level of the heading.
20     *
21     * @inheritdoc
22     */
23    public function parse()
24    {
25        $this->text = (string) $this->p->xpath('w:r/w:t')[0];
26        $style = $this->p->xpath('w:pPr/w:pStyle');
27        $styleID = $style[0]->attributes('w', true)->val;
28        $this->level =  substr($this->docx->getStyles()->getStyleName($styleID), -1); // translates to "heading X"
29        if ($this->level < 1) $this->level = 1;
30        if ($this->level > 5) $this->level = 5;
31    }
32
33    /** @inheritdoc  */
34    public function __toString(): string
35    {
36        return str_pad('', 7 - $this->level, '=') . ' ' . $this->text . ' ' . str_pad('', 7 - $this->level, '=');
37    }
38}
39