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 * This file is part of PHPWord - A pure PHP library for reading and writing
4 * word processing documents.
5 *
6 * PHPWord is free software distributed under the terms of the GNU Lesser
7 * General Public License version 3 as published by the Free Software Foundation.
8 *
9 * For the full copyright and license information, please read the LICENSE
10 * file that was distributed with this source code. For the full list of
11 * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12 *
13 * @see         https://github.com/PHPOffice/PHPWord
14 *
15 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16 */
17
18namespace PhpOffice\PhpWord\Writer\Word2007\Part;
19
20use PhpOffice\PhpWord\Element\Comment;
21use PhpOffice\PhpWord\Shared\XMLWriter;
22use PhpOffice\PhpWord\Writer\Word2007\Element\Container;
23
24/**
25 * Word2007 comments part writer: word/comments.xml.
26 */
27class Comments extends AbstractPart
28{
29    /**
30     * Comments collection to be written.
31     *
32     * @var \PhpOffice\PhpWord\Element\Comment[]
33     */
34    protected $elements;
35
36    /**
37     * Write part.
38     *
39     * @return string
40     */
41    public function write()
42    {
43        $xmlWriter = $this->getXmlWriter();
44
45        $xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
46        $xmlWriter->startElement('w:comments');
47        $xmlWriter->writeAttribute('xmlns:ve', 'http://schemas.openxmlformats.org/markup-compatibility/2006');
48        $xmlWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office');
49        $xmlWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
50        $xmlWriter->writeAttribute('xmlns:m', 'http://schemas.openxmlformats.org/officeDocument/2006/math');
51        $xmlWriter->writeAttribute('xmlns:v', 'urn:schemas-microsoft-com:vml');
52        $xmlWriter->writeAttribute('xmlns:wp', 'http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing');
53        $xmlWriter->writeAttribute('xmlns:w10', 'urn:schemas-microsoft-com:office:word');
54        $xmlWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main');
55        $xmlWriter->writeAttribute('xmlns:wne', 'http://schemas.microsoft.com/office/word/2006/wordml');
56
57        if ($this->elements !== null) {
58            foreach ($this->elements as $element) {
59                if ($element instanceof Comment) {
60                    $this->writeComment($xmlWriter, $element);
61                }
62            }
63        }
64
65        $xmlWriter->endElement(); // w:comments
66
67        return $xmlWriter->getData();
68    }
69
70    /**
71     * Write comment item.
72     */
73    protected function writeComment(XMLWriter $xmlWriter, Comment $comment): void
74    {
75        $xmlWriter->startElement('w:comment');
76        $xmlWriter->writeAttribute('w:id', $comment->getElementId());
77        $xmlWriter->writeAttribute('w:author', $comment->getAuthor());
78        if ($comment->getDate() != null) {
79            $xmlWriter->writeAttribute('w:date', $comment->getDate()->format($this->dateFormat));
80        }
81        $xmlWriter->writeAttributeIf($comment->getInitials() != null, 'w:initials', $comment->getInitials());
82
83        $containerWriter = new Container($xmlWriter, $comment);
84        $containerWriter->write();
85
86        $xmlWriter->endElement(); // w:comment
87    }
88
89    /**
90     * Set element.
91     *
92     * @param \PhpOffice\PhpWord\Element\Comment[] $elements
93     *
94     * @return self
95     */
96    public function setElements($elements)
97    {
98        $this->elements = $elements;
99
100        return $this;
101    }
102}