Completed
Pull Request — develop (#207)
by Franck
06:53
created

AbstractShape::setHeight()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
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
 * @link        https://github.com/PHPOffice/PHPPresentation
14
 * @copyright   2009-2015 PHPPresentation contributors
15
 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16
 */
17
18
namespace PhpOffice\PhpPresentation;
19
20
use PhpOffice\PhpPresentation\Shape\Hyperlink;
21
use PhpOffice\PhpPresentation\Style\Fill;
22
use PhpOffice\PhpPresentation\Style\Shadow;
23
24
/**
25
 * Abstract shape
26
 */
27
abstract class AbstractShape implements ComparableInterface
28
{
29
    /**
30
     * Container
31
     *
32
     * @var \PhpOffice\PhpPresentation\ShapeContainerInterface
33
     */
34
    protected $container;
35
36
    /**
37
     * Offset X
38
     *
39
     * @var int
40
     */
41
    protected $offsetX;
42
43
    /**
44
     * Offset Y
45
     *
46
     * @var int
47
     */
48
    protected $offsetY;
49
50
    /**
51
     * Width
52
     *
53
     * @var int
54
     */
55
    protected $width;
56
57
    /**
58
     * Height
59
     *
60
     * @var int
61
     */
62
    protected $height;
63
64
    /**
65
     * Fill
66
     *
67
     * @var \PhpOffice\PhpPresentation\Style\Fill
68
     */
69
    private $fill;
70
71
    /**
72
     * Border
73
     *
74
     * @var \PhpOffice\PhpPresentation\Style\Border
75
     */
76
    private $border;
77
78
    /**
79
     * Rotation
80
     *
81
     * @var int
82
     */
83
    protected $rotation;
84
85
    /**
86
     * Shadow
87
     *
88
     * @var \PhpOffice\PhpPresentation\Style\Shadow
89
     */
90
    protected $shadow;
91
92
    /**
93
     * Hyperlink
94
     *
95
     * @var \PhpOffice\PhpPresentation\Shape\Hyperlink
96
     */
97
    protected $hyperlink;
98
99
    /**
100
     * Hash index
101
     *
102
     * @var string
103
     */
104
    private $hashIndex;
105
106
    /**
107
     * Create a new self
108
     */
109 205
    public function __construct()
110
    {
111
        // Initialise values
112 205
        $this->container = null;
113 205
        $this->offsetX   = 0;
114 205
        $this->offsetY   = 0;
115 205
        $this->width     = 0;
116 205
        $this->height    = 0;
117 205
        $this->rotation  = 0;
118 205
        $this->fill      = new Style\Fill();
119 205
        $this->border    = new Style\Border();
120 205
        $this->shadow    = new Style\Shadow();
121
122 205
        $this->border->setLineStyle(Style\Border::LINE_NONE);
123 205
    }
124
125
    /**
126
     * Magic Method : clone
127
     */
128 1
    public function __clone()
129
    {
130 1
        $this->container = null;
131 1
        $this->fill      = clone $this->fill;
132 1
        $this->border    = clone $this->border;
133 1
        $this->shadow    = clone $this->shadow;
134 1
    }
135
136
    /**
137
     * Get Container, Slide or Group
138
     *
139
     * @return \PhpOffice\PhpPresentation\Container
140
     */
141
    public function getContainer()
142
    {
143
        return $this->container;
144
    }
145
146
    /**
147
     * Set Container, Slide or Group
148
     *
149
     * @param  \PhpOffice\PhpPresentation\ShapeContainerInterface $pValue
150
     * @param  bool                $pOverrideOld If a Slide has already been assigned, overwrite it and remove image from old Slide?
151
     * @throws \Exception
152
     * @return self
153
     */
154 149
    public function setContainer(ShapeContainerInterface $pValue = null, $pOverrideOld = false)
155
    {
156 149
        if (is_null($this->container)) {
157
            // Add drawing to \PhpOffice\PhpPresentation\ShapeContainerInterface
158 149
            $this->container = $pValue;
159 149
            if (!is_null($this->container)) {
160 149
                $this->container->getShapeCollection()->append($this);
161
            }
162
        } else {
163
            if ($pOverrideOld) {
164
                // Remove drawing from old \PhpOffice\PhpPresentation\ShapeContainerInterface
165
                $iterator = $this->container->getShapeCollection()->getIterator();
166
167
                while ($iterator->valid()) {
168
                    if ($iterator->current()->getHashCode() == $this->getHashCode()) {
169
                        $this->container->getShapeCollection()->offsetUnset($iterator->key());
170
                        $this->container = null;
171
                        break;
172
                    }
173
                    $iterator->next();
174
                }
175
176
                // Set new \PhpOffice\PhpPresentation\Slide
177
                $this->setContainer($pValue);
178
            } else {
179
                throw new \Exception("A \PhpOffice\PhpPresentation\ShapeContainerInterface has already been assigned. Shapes can only exist on one \PhpOffice\PhpPresentation\ShapeContainerInterface.");
180
            }
181
        }
182
183 149
        return $this;
184
    }
185
186
    /**
187
     * Get OffsetX
188
     *
189
     * @return int
190
     */
191 134
    public function getOffsetX()
192
    {
193 134
        return $this->offsetX;
194
    }
195
196
    /**
197
     * Set OffsetX
198
     *
199
     * @param  int                 $pValue
200
     * @return self
201
     */
202 64
    public function setOffsetX($pValue = 0)
203
    {
204 64
        $this->offsetX = $pValue;
205
206 64
        return $this;
207
    }
208
209
    /**
210
     * Get OffsetY
211
     *
212
     * @return int
213
     */
214 134
    public function getOffsetY()
215
    {
216 134
        return $this->offsetY;
217
    }
218
219
    /**
220
     * Set OffsetY
221
     *
222
     * @param  int                 $pValue
223
     * @return self
224
     */
225 64
    public function setOffsetY($pValue = 0)
226
    {
227 64
        $this->offsetY = $pValue;
228
229 64
        return $this;
230
    }
231
232
    /**
233
     * Get Width
234
     *
235
     * @return int
236
     */
237 126
    public function getWidth()
238
    {
239 126
        return $this->width;
240
    }
241
242
    /**
243
     * Set Width
244
     *
245
     * @param  int                 $pValue
246
     * @return self
247
     */
248 30
    public function setWidth($pValue = 0)
249
    {
250 30
        $this->width = $pValue;
251 30
        return $this;
252
    }
253
254
    /**
255
     * Get Height
256
     *
257
     * @return int
258
     */
259 126
    public function getHeight()
260
    {
261 126
        return $this->height;
262
    }
263
264
    /**
265
     * Set Height
266
     *
267
     * @param  int                 $pValue
268
     * @return self
269
     */
270 30
    public function setHeight($pValue = 0)
271
    {
272 30
        $this->height = $pValue;
273 30
        return $this;
274
    }
275
276
    /**
277
     * Set width and height with proportional resize
278
     *
279
     * @param  int                 $width
280
     * @param  int                 $height
281
     * @example $objDrawing->setWidthAndHeight(160,120);
282
     * @return self
283
     */
284 1
    public function setWidthAndHeight($width = 0, $height = 0)
285
    {
286 1
        $this->width  = $width;
287 1
        $this->height = $height;
288 1
        return $this;
289
    }
290
291
    /**
292
     * Get Rotation
293
     *
294
     * @return int
295
     */
296 55
    public function getRotation()
297
    {
298 55
        return $this->rotation;
299
    }
300
301
    /**
302
     * Set Rotation
303
     *
304
     * @param  int                 $pValue
305
     * @return self
306
     */
307 4
    public function setRotation($pValue = 0)
308
    {
309 4
        $this->rotation = $pValue;
310 4
        return $this;
311
    }
312
313
    /**
314
     * Get Fill
315
     *
316
     * @return \PhpOffice\PhpPresentation\Style\Fill
317
     */
318 103
    public function getFill()
319
    {
320 103
        return $this->fill;
321
    }
322
323
    /**
324
     * Set Fill
325
     * @param \PhpOffice\PhpPresentation\Style\Fill $pValue
326
     * @return \PhpOffice\PhpPresentation\AbstractShape
327
     */
328 1
    public function setFill(Fill $pValue = null)
329
    {
330 1
        $this->fill = $pValue;
331 1
        return $this;
332
    }
333
334
    /**
335
     * Get Border
336
     *
337
     * @return \PhpOffice\PhpPresentation\Style\Border
338
     */
339 78
    public function getBorder()
340
    {
341 78
        return $this->border;
342
    }
343
344
    /**
345
     * Get Shadow
346
     *
347
     * @return \PhpOffice\PhpPresentation\Style\Shadow
348
     */
349 78
    public function getShadow()
350
    {
351 78
        return $this->shadow;
352
    }
353
354
    /**
355
     * Set Shadow
356
     *
357
     * @param  \PhpOffice\PhpPresentation\Style\Shadow $pValue
358
     * @throws \Exception
359
     * @return self
360
     */
361 3
    public function setShadow(Shadow $pValue = null)
362
    {
363 3
        $this->shadow = $pValue;
364 3
        return $this;
365
    }
366
367
    /**
368
     * Has Hyperlink?
369
     *
370
     * @return boolean
371
     */
372 80
    public function hasHyperlink()
373
    {
374 80
        return !is_null($this->hyperlink);
375
    }
376
377
    /**
378
     * Get Hyperlink
379
     *
380
     * @return \PhpOffice\PhpPresentation\Shape\Hyperlink
381
     */
382 5
    public function getHyperlink()
383
    {
384 5
        if (is_null($this->hyperlink)) {
385 5
            $this->hyperlink = new Hyperlink();
386
        }
387 5
        return $this->hyperlink;
388
    }
389
390
    /**
391
     * Set Hyperlink
392
     *
393
     * @param  \PhpOffice\PhpPresentation\Shape\Hyperlink $pHyperlink
394
     * @throws \Exception
395
     * @return self
396
     */
397 1
    public function setHyperlink(Hyperlink $pHyperlink = null)
398
    {
399 1
        $this->hyperlink = $pHyperlink;
400 1
        return $this;
401
    }
402
403
    /**
404
     * Get hash code
405
     *
406
     * @return string Hash code
407
     */
408 67
    public function getHashCode()
409
    {
410 67
        return md5((is_object($this->container)?$this->container->getHashCode():'') . $this->offsetX . $this->offsetY . $this->width . $this->height . $this->rotation . (is_null($this->getFill()) ? '' : $this->getFill()->getHashCode()) . (is_null($this->shadow) ? '' : $this->shadow->getHashCode()) . (is_null($this->hyperlink) ? '' : $this->hyperlink->getHashCode()) . __CLASS__);
411
    }
412
413
    /**
414
     * Get hash index
415
     *
416
     * Note that this index may vary during script execution! Only reliable moment is
417
     * while doing a write of a workbook and when changes are not allowed.
418
     *
419
     * @return string Hash index
420
     */
421 65
    public function getHashIndex()
422
    {
423 65
        return $this->hashIndex;
424
    }
425
426
    /**
427
     * Set hash index
428
     *
429
     * Note that this index may vary during script execution! Only reliable moment is
430
     * while doing a write of a workbook and when changes are not allowed.
431
     *
432
     * @param string $value Hash index
433
     */
434 65
    public function setHashIndex($value)
435
    {
436 65
        $this->hashIndex = $value;
437 65
    }
438
}
439