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
PptComments
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%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 writeSlideComments
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
6
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\Drawing as CommonDrawing;
24use PhpOffice\Common\XMLWriter;
25use PhpOffice\PhpPresentation\Shape\Comment;
26use PhpOffice\PhpPresentation\Slide;
27
28class PptComments extends AbstractDecoratorWriter
29{
30    public function render(): ZipInterface
31    {
32        foreach ($this->getPresentation()->getAllSlides() as $numSlide => $oSlide) {
33            $contentXml = $this->writeSlideComments($oSlide);
34            if (empty($contentXml)) {
35                continue;
36            }
37            $this->getZip()->addFromString('ppt/comments/comment' . ($numSlide + 1) . '.xml', $contentXml);
38        }
39
40        return $this->getZip();
41    }
42
43    protected function writeSlideComments(Slide $oSlide): string
44    {
45        /**
46         * @var Comment[]
47         */
48        $arrayComment = [];
49        foreach ($oSlide->getShapeCollection() as $oShape) {
50            if ($oShape instanceof Comment) {
51                $arrayComment[] = $oShape;
52            }
53        }
54
55        if (empty($arrayComment)) {
56            return '';
57        }
58
59        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
60        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
61
62        // p:cmLst
63        $objWriter->startElement('p:cmLst');
64        $objWriter->writeAttribute('xmlns:p', 'http://schemas.openxmlformats.org/presentationml/2006/main');
65
66        foreach ($arrayComment as $idxComment => $oComment) {
67            $oAuthor = $oComment->getAuthor();
68
69            // p:cmLst > p:cm
70            $objWriter->startElement('p:cm');
71            $objWriter->writeAttribute('authorId', $oAuthor instanceof Comment\Author ? $oAuthor->getIndex() : 0);
72            $objWriter->writeAttribute('dt', date('Y-m-d\TH:i:s.000000000', $oComment->getDate()));
73            $objWriter->writeAttribute('idx', $idxComment);
74
75            // p:cmLst > p:cm > p:pos
76            // Uses 1/8pt for positionning
77            // @link : https://social.msdn.microsoft.com/Forums/fr-FR/ebdc12f2-0cff-4fa8-b901-fa6e3198364e/comment-position-units
78            $objWriter->startElement('p:pos');
79            $objWriter->writeAttribute('x', (int) CommonDrawing::pixelsToPoints($oComment->getOffsetX() * 8));
80            $objWriter->writeAttribute('y', (int) CommonDrawing::pixelsToPoints($oComment->getOffsetY() * 8));
81            $objWriter->endElement();
82
83            // p:cmLst > p:cm > p:text
84            $objWriter->writeElement('p:text', $oComment->getText());
85
86            // p:cmLst > ## p:cm
87            $objWriter->endElement();
88        }
89
90        // ## p:cmLst
91        $objWriter->endElement();
92
93        return $objWriter->getData();
94    }
95}