Completed
Pull Request — develop (#207)
by Franck
12:42 queued 05:23
created

AbstractWriter::iterateCollection()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
ccs 0
cts 0
cp 0
rs 8.8571
cc 5
eloc 13
nc 5
nop 1
crap 30
1
<?php
2
3
namespace PhpOffice\PhpPresentation\Writer;
4
5
use PhpOffice\Common\Adapter\Zip\ZipInterface;
6
use PhpOffice\PhpPresentation\PhpPresentation;
7
use PhpOffice\PhpPresentation\Shape\Drawing\AbstractDrawingAdapter;
8
use PhpOffice\PhpPresentation\Shape\Group;
9
10
abstract class AbstractWriter
11
{
12
    /**
13
     * Private unique hash table
14
     *
15
     * @var \PhpOffice\PhpPresentation\HashTable
16
     */
17
    protected $oDrawingHashTable;
18
19
    /**
20
     * Private PhpPresentation
21
     *
22
     * @var PhpPresentation
23
     */
24
    protected $oPresentation;
25
26
    /**
27
     * @var ZipInterface
28
     */
29
    protected $oZipAdapter;
30
31
    /**
32
     * Get drawing hash table
33
     *
34 157
     * @return \PhpOffice\PhpPresentation\HashTable
35
     */
36 157
    public function getDrawingHashTable()
37
    {
38
        return $this->oDrawingHashTable;
39
    }
40
41
    /**
42
     * Get PhpPresentation object
43
     *
44
     * @return PhpPresentation
45 167
     * @throws \Exception
46
     */
47 167
    public function getPhpPresentation()
48 5
    {
49
        if (empty($this->oPresentation)) {
50 162
            throw new \Exception("No PhpPresentation assigned.");
51
        }
52
        return $this->oPresentation;
53
    }
54
55
    /**
56
     * Get PhpPresentation object
57
     *
58
     * @param  PhpPresentation                       $pPhpPresentation PhpPresentation object
59
     * @throws \Exception
60 176
     * @return \PhpOffice\PhpPresentation\Writer\ODPresentation
61
     */
62 176
    public function setPhpPresentation(PhpPresentation $pPhpPresentation = null)
63 176
    {
64
        $this->oPresentation = $pPhpPresentation;
65
        return $this;
66
    }
67
68
69
    /**
70
     * @param ZipInterface $oZipAdapter
71 177
     * @return $this
72
     */
73 177
    public function setZipAdapter(ZipInterface $oZipAdapter)
74 177
    {
75
        $this->oZipAdapter = $oZipAdapter;
76
        return $this;
77
    }
78
79
    /**
80 160
     * @return ZipInterface
81
     */
82 160
    public function getZipAdapter()
83
    {
84
        return $this->oZipAdapter;
85
    }
86
87
    /**
88
     * Get an array of all drawings
89
     *
90
     * @return \PhpOffice\PhpPresentation\Shape\AbstractDrawing[] All drawings in PhpPresentation
91
     * @throws \Exception
92
     */
93
    protected function allDrawings()
94
    {
95
        // Get an array of all drawings
96
        $aDrawings  = array();
97
98
        // Loop trough PhpPresentation
99
        foreach ($this->getPhpPresentation()->getAllSlides() as $oSlide) {
100
            $arrayReturn = $this->iterateCollection($oSlide->getShapeCollection()->getIterator());
101
            $aDrawings = array_merge($aDrawings, $arrayReturn);
102
        }
103
        return $aDrawings;
104
    }
105
106
    private function iterateCollection(\ArrayIterator $oIterator)
107
    {
108
        $arrayReturn = array();
109
        if ($oIterator->count() <= 0) {
110
            return $arrayReturn;
111
        }
112
113
        while ($oIterator->valid()) {
114
            $oShape = $oIterator->current();
115
            if ($oShape instanceof AbstractDrawingAdapter) {
116
                $arrayReturn[] = $oShape;
117
            } elseif ($oShape instanceof Group) {
118
                $arrayGroup = $this->iterateCollection($oShape->getShapeCollection()->getIterator());
119
                $arrayReturn = array_merge($arrayReturn, $arrayGroup);
120
            }
121
            $oIterator->next();
122
        }
123
        return $arrayReturn;
124
    }
125
}
126