Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Comments
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
3 / 3
7
100.00% covered (success)
100.00%
1 / 1
 write
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
4
 writeComment
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 setElements
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
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\Word2007\Part;
20
21use PhpOffice\PhpWord\Element\Comment;
22use PhpOffice\PhpWord\Shared\XMLWriter;
23use PhpOffice\PhpWord\Writer\Word2007\Element\Container;
24
25/**
26 * Word2007 comments part writer: word/comments.xml.
27 */
28class Comments extends AbstractPart
29{
30    /**
31     * Comments collection to be written.
32     *
33     * @var Comment[]
34     */
35    protected $elements;
36
37    /**
38     * Write part.
39     *
40     * @return string
41     */
42    public function write()
43    {
44        $xmlWriter = $this->getXmlWriter();
45
46        $xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
47        $xmlWriter->startElement('w:comments');
48        $xmlWriter->writeAttribute('xmlns:ve', 'http://schemas.openxmlformats.org/markup-compatibility/2006');
49        $xmlWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office');
50        $xmlWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
51        $xmlWriter->writeAttribute('xmlns:m', 'http://schemas.openxmlformats.org/officeDocument/2006/math');
52        $xmlWriter->writeAttribute('xmlns:v', 'urn:schemas-microsoft-com:vml');
53        $xmlWriter->writeAttribute('xmlns:wp', 'http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing');
54        $xmlWriter->writeAttribute('xmlns:w10', 'urn:schemas-microsoft-com:office:word');
55        $xmlWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main');
56        $xmlWriter->writeAttribute('xmlns:wne', 'http://schemas.microsoft.com/office/word/2006/wordml');
57
58        if ($this->elements !== null) {
59            foreach ($this->elements as $element) {
60                if ($element instanceof Comment) {
61                    $this->writeComment($xmlWriter, $element);
62                }
63            }
64        }
65
66        $xmlWriter->endElement(); // w:comments
67
68        return $xmlWriter->getData();
69    }
70
71    /**
72     * Write comment item.
73     */
74    protected function writeComment(XMLWriter $xmlWriter, Comment $comment): void
75    {
76        $xmlWriter->startElement('w:comment');
77        $xmlWriter->writeAttribute('w:id', $comment->getElementId());
78        $xmlWriter->writeAttribute('w:author', $comment->getAuthor());
79        if ($comment->getDate() != null) {
80            $xmlWriter->writeAttribute('w:date', $comment->getDate()->format($this->dateFormat));
81        }
82        $xmlWriter->writeAttributeIf($comment->getInitials() != null, 'w:initials', $comment->getInitials());
83
84        $containerWriter = new Container($xmlWriter, $comment);
85        $containerWriter->write();
86
87        $xmlWriter->endElement(); // w:comment
88    }
89
90    /**
91     * Set element.
92     *
93     * @param Comment[] $elements
94     *
95     * @return self
96     */
97    public function setElements($elements)
98    {
99        $this->elements = $elements;
100
101        return $this;
102    }
103}