Completed
Pull Request — develop (#565)
by
unknown
06:14
created

CommentAuthors::writeCommentsAuthors()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 0
cts 17
cp 0
rs 9.472
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace PhpOffice\PhpPresentation\Writer\PowerPoint2007;
4
5
use PhpOffice\Common\Adapter\Zip\ZipInterface;
6
use PhpOffice\Common\XMLWriter;
7
use PhpOffice\PhpPresentation\Shape\Comment;
8
use PhpOffice\PhpPresentation\Shape\Comment\Author;
9
10
class CommentAuthors extends AbstractDecoratorWriter
11
{
12
    /**
13
     * @return ZipInterface
14
     * @throws \Exception
15
     */
16 7
    public function render()
17
    {
18
        /**
19
         * @var Author[]
20
         */
21 7
        $arrayAuthors = array();
22 7
        foreach ($this->getPresentation()->getAllSlides() as $oSlide) {
23 7
            foreach ($oSlide->getShapeCollection() as $oShape) {
24 7
                if (!($oShape instanceof Comment)) {
25 7
                    continue;
26
                }
27
                $oAuthor = $oShape->getAuthor();
28
                if (!($oAuthor instanceof Author)) {
29
                    continue;
30
                }
31
                if (array_key_exists($oAuthor->getHashCode(), $arrayAuthors)) {
32
                    continue;
33
                }
34 7
                $arrayAuthors[$oAuthor->getHashCode()] = $oAuthor;
35
            }
36
        }
37 7
        if (!empty($arrayAuthors)) {
38
            $this->getZip()->addFromString('ppt/commentAuthors.xml', $this->writeCommentsAuthors($arrayAuthors));
39
        }
40
41 7
        return $this->getZip();
42
    }
43
44
    /**
45
     * @param Author[] $arrayAuthors
46
     * @return string
47
     */
48
    protected function writeCommentsAuthors($arrayAuthors)
49
    {
50
        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
51
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
52
53
        // p:cmAuthorLst
54
        $objWriter->startElement('p:cmAuthorLst');
55
        $objWriter->writeAttribute('xmlns:p', 'http://schemas.openxmlformats.org/presentationml/2006/main');
56
57
        $idxAuthor = 0;
58
        foreach ($arrayAuthors as $oAuthor) {
59
            $oAuthor->setIndex($idxAuthor++);
60
61
            // p:cmAuthor
62
            $objWriter->startElement('p:cmAuthor');
63
            $objWriter->writeAttribute('id', $oAuthor->getIndex());
64
            $objWriter->writeAttribute('name', $oAuthor->getName());
65
            $objWriter->writeAttribute('initials', $oAuthor->getInitials());
66
            $objWriter->writeAttribute('lastIdx', '2');
67
            $objWriter->writeAttribute('clrIdx', 0);
68
            $objWriter->endElement();
69
        }
70
71
        // ## p:cmAuthorLst
72
        $objWriter->endElement();
73
74
        return $objWriter->getData();
75
    }
76
}
77