Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
CommentAuthors
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
2 / 2
9
100.00% covered (success)
100.00%
1 / 1
 render
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
7
 writeCommentsAuthors
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * This file is part of PHPPresentation - A pure PHP library for reading and writing
4 * presentations documents.
5 *
6 * PHPPresentation 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/PHPPresentation/contributors.
12 *
13 * @see        https://github.com/PHPOffice/PHPPresentation
14 *
15 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16 */
17
18declare(strict_types=1);
19
20namespace PhpOffice\PhpPresentation\Writer\PowerPoint2007;
21
22use PhpOffice\Common\Adapter\Zip\ZipInterface;
23use PhpOffice\Common\XMLWriter;
24use PhpOffice\PhpPresentation\Shape\Comment;
25use PhpOffice\PhpPresentation\Shape\Comment\Author;
26
27class CommentAuthors extends AbstractDecoratorWriter
28{
29    public function render(): ZipInterface
30    {
31        /**
32         * @var Author[]
33         */
34        $arrayAuthors = [];
35        foreach ($this->getPresentation()->getAllSlides() as $oSlide) {
36            foreach ($oSlide->getShapeCollection() as $oShape) {
37                if (!($oShape instanceof Comment)) {
38                    continue;
39                }
40                $oAuthor = $oShape->getAuthor();
41                if (!($oAuthor instanceof Author)) {
42                    continue;
43                }
44                if (array_key_exists($oAuthor->getHashCode(), $arrayAuthors)) {
45                    continue;
46                }
47                $arrayAuthors[$oAuthor->getHashCode()] = $oAuthor;
48            }
49        }
50        if (!empty($arrayAuthors)) {
51            $this->getZip()->addFromString('ppt/commentAuthors.xml', $this->writeCommentsAuthors($arrayAuthors));
52        }
53
54        return $this->getZip();
55    }
56
57    /**
58     * @param Author[] $arrayAuthors
59     *
60     * @return string
61     */
62    protected function writeCommentsAuthors($arrayAuthors)
63    {
64        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
65        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
66
67        // p:cmAuthorLst
68        $objWriter->startElement('p:cmAuthorLst');
69        $objWriter->writeAttribute('xmlns:p', 'http://schemas.openxmlformats.org/presentationml/2006/main');
70
71        $idxAuthor = 0;
72        foreach ($arrayAuthors as $oAuthor) {
73            $oAuthor->setIndex($idxAuthor++);
74
75            // p:cmAuthor
76            $objWriter->startElement('p:cmAuthor');
77            $objWriter->writeAttribute('id', $oAuthor->getIndex());
78            $objWriter->writeAttribute('name', $oAuthor->getName());
79            $objWriter->writeAttribute('initials', $oAuthor->getInitials());
80            $objWriter->writeAttribute('lastIdx', '2');
81            $objWriter->writeAttribute('clrIdx', 0);
82            $objWriter->endElement();
83        }
84
85        // ## p:cmAuthorLst
86        $objWriter->endElement();
87
88        return $objWriter->getData();
89    }
90}