Completed
Pull Request — develop (#565)
by
unknown
07:15
created

PptComments   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 7
dl 0
loc 75
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 11 3
B writeSlideComments() 0 52 6
1
<?php
2
3
namespace PhpOffice\PhpPresentation\Writer\PowerPoint2007;
4
5
use PhpOffice\Common\Adapter\Zip\ZipInterface;
6
use PhpOffice\Common\Drawing as CommonDrawing;
7
use PhpOffice\Common\XMLWriter;
8
use PhpOffice\PhpPresentation\Shape\Comment;
9
use PhpOffice\PhpPresentation\Slide;
10
11
class PptComments extends AbstractDecoratorWriter
12
{
13
    /**
14
     * @return ZipInterface
15
     * @throws \Exception
16
     */
17 112
    public function render()
18
    {
19 112
        foreach ($this->getPresentation()->getAllSlides() as $numSlide => $oSlide) {
20 112
            $contentXml = $this->writeSlideComments($oSlide);
21 112
            if (empty($contentXml)) {
22 106
                continue;
23
            }
24 6
            $this->getZip()->addFromString('ppt/comments/comment'.($numSlide + 1).'.xml', $contentXml);
25
        }
26 112
        return $this->getZip();
27
    }
28
29
    /**
30
     * @param Slide $oSlide
31
     * @return string
32
     */
33 112
    protected function writeSlideComments(Slide $oSlide)
34
    {
35
        /**
36
         * @var Comment[]
37
         */
38 112
        $arrayComment = array();
39 112
        foreach ($oSlide->getShapeCollection() as $oShape) {
40 85
            if ($oShape instanceof Comment) {
41 85
                $arrayComment[] = $oShape;
42
            }
43
        }
44
45 112
        if (empty($arrayComment)) {
46 106
            return '';
47
        }
48
49 6
        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
50 6
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
51
52
        // p:cmLst
53 6
        $objWriter->startElement('p:cmLst');
54 6
        $objWriter->writeAttribute('xmlns:p', 'http://schemas.openxmlformats.org/presentationml/2006/main');
55
56 6
        foreach ($arrayComment as $idxComment => $oComment) {
57 6
            $oAuthor = $oComment->getAuthor();
58
59
            // p:cmLst > p:cm
60 6
            $objWriter->startElement('p:cm');
61 6
            $objWriter->writeAttribute('authorId', $oAuthor instanceof Comment\Author ? $oAuthor->getIndex() : 0);
62 6
            $objWriter->writeAttribute('dt', date('Y-m-d\TH:i:s.000000000', $oComment->getDate()));
63 6
            $objWriter->writeAttribute('idx', $idxComment);
64
65
            // p:cmLst > p:cm > p:pos
66
            // Uses 1/8pt for positionning
67
            // @link : https://social.msdn.microsoft.com/Forums/fr-FR/ebdc12f2-0cff-4fa8-b901-fa6e3198364e/comment-position-units
68 6
            $objWriter->startElement('p:pos');
69 6
            $objWriter->writeAttribute('x', (int) CommonDrawing::pixelsToPoints($oComment->getOffsetX() * 8));
70 6
            $objWriter->writeAttribute('y', (int) CommonDrawing::pixelsToPoints($oComment->getOffsetY() * 8));
71 6
            $objWriter->endElement();
72
73
            // p:cmLst > p:cm > p:text
74 6
            $objWriter->writeElement('p:text', $oComment->getText());
75
76
            // p:cmLst > ## p:cm
77 6
            $objWriter->endElement();
78
        }
79
80
        // ## p:cmLst
81 6
        $objWriter->endElement();
82
83 6
        return $objWriter->getData();
84
    }
85
}
86