Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
44 / 44
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractElement
100.00% covered (success)
100.00%
44 / 44
100.00% covered (success)
100.00%
4 / 4
17
100.00% covered (success)
100.00%
1 / 1
 writeCommentRangeStart
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
4
 writeCommentRangeEnd
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
6
 writeCommentRangeEndElement
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 replaceTabs
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
6
1<?php
2
3/**
4 * This file is part of PHPWord - A pure PHP library for reading and writing
5 * word processing documents.
6 *
7 * PHPWord is free software distributed under the terms of the GNU Lesser
8 * General Public License version 3 as published by the Free Software Foundation.
9 *
10 * For the full copyright and license information, please read the LICENSE
11 * file that was distributed with this source code. For the full list of
12 * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
13 *
14 * @see         https://github.com/PHPOffice/PHPWord
15 *
16 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17 */
18
19namespace PhpOffice\PhpWord\Writer\ODText\Element;
20
21use PhpOffice\PhpWord\Element\Comment;
22use PhpOffice\PhpWord\Writer\Word2007\Element\AbstractElement as Word2007AbstractElement;
23
24/**
25 * Abstract element writer.
26 *
27 * @since 0.11.0
28 */
29abstract class AbstractElement extends Word2007AbstractElement
30{
31    protected function writeCommentRangeStart(): void
32    {
33        if ($this->getElement()->getCommentsRangeStart() === null) {
34            return;
35        }
36
37        foreach ($this->getElement()->getCommentsRangeStart()->getItems() as $comment) {
38            $this->getXmlWriter()->startElement('office:annotation');
39            $this->getXmlWriter()->writeAttribute('office:name', $comment->getElementId());
40            $this->getXmlWriter()->writeElement('dc:creator', $comment->getAuthor());
41            if ($comment->getDate() !== null) {
42                $this->getXmlWriter()->writeElement('dc:date', $comment->getDate()->format('Y-m-d\\TH:i:s\\Z'));
43            }
44
45            $containerWriter = new Container($this->getXmlWriter(), $comment);
46            $containerWriter->write();
47            $this->getXmlWriter()->endElement();
48        }
49    }
50
51    protected function writeCommentRangeEnd(): void
52    {
53        $element = $this->getElement();
54        $comments = $element->getCommentsRangeEnd();
55        if ($comments !== null) {
56            foreach ($comments->getItems() as $comment) {
57                $this->writeCommentRangeEndElement($comment);
58            }
59        }
60
61        $comments = $element->getCommentsRangeStart();
62        if ($comments !== null) {
63            foreach ($comments->getItems() as $comment) {
64                if ($comment->getEndElement() === null) {
65                    $this->writeCommentRangeEndElement($comment);
66                }
67            }
68        }
69    }
70
71    private function writeCommentRangeEndElement(Comment $comment): void
72    {
73        $this->getXmlWriter()->startElement('office:annotation-end');
74        $this->getXmlWriter()->writeAttribute('office:name', $comment->getElementId());
75        $this->getXmlWriter()->endElement();
76    }
77
78    protected function replaceTabs($text, $xmlWriter): void
79    {
80        if (preg_match('/^ +/', $text, $matches)) {
81            $num = strlen($matches[0]);
82            $xmlWriter->startElement('text:s');
83            $xmlWriter->writeAttributeIf($num > 1, 'text:c', "$num");
84            $xmlWriter->endElement();
85            $text = preg_replace('/^ +/', '', $text);
86        }
87        preg_match_all('/([\\s\\S]*?)(\\t|  +| ?$)/', $text, $matches, PREG_SET_ORDER);
88        foreach ($matches as $match) {
89            $this->writeText($match[1]);
90            if ($match[2] === '') {
91                break;
92            } elseif ($match[2] === "\t") {
93                $xmlWriter->writeElement('text:tab');
94            } elseif ($match[2] === ' ') {
95                $xmlWriter->writeElement('text:s');
96
97                break;
98            } else {
99                $num = strlen($match[2]);
100                $xmlWriter->startElement('text:s');
101                $xmlWriter->writeAttributeIf($num > 1, 'text:c', "$num");
102                $xmlWriter->endElement();
103            }
104        }
105    }
106}