Completed
Pull Request — develop (#362)
by Franck
13:14
created

AbstractWriter::iterateCollection()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6

Importance

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