Completed
Pull Request — develop (#304)
by Franck
07:26
created

AbstractGraphic::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
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\Shape;
19
20
use PhpOffice\PhpPresentation\AbstractShape;
21
use PhpOffice\PhpPresentation\ComparableInterface;
22
23
/**
24
 * Abstract drawing
25
 */
26
abstract class AbstractGraphic extends AbstractShape implements ComparableInterface
27
{
28
    /**
29
     * Image counter
30
     *
31
     * @var int
32
     */
33
    private static $imageCounter = 0;
34
35
    /**
36
     * Image index
37
     *
38
     * @var int
39
     */
40
    private $imageIndex = 0;
41
42
    /**
43
     * Name
44
     *
45
     * @var string
46
     */
47
    protected $name;
48
49
    /**
50
     * Description
51
     *
52
     * @var string
53
     */
54
    protected $description;
55
56
    /**
57
     * Proportional resize
58
     *
59
     * @var boolean
60
     */
61
    protected $resizeProportional;
62
63
    /**
64
     * Slide relation ID (should not be used by user code!)
65
     *
66
     * @var string
67
     */
68
    public $relationId = null;
69
70
    /**
71
     * Create a new \PhpOffice\PhpPresentation\Slide\AbstractDrawing
72
     */
73 120
    public function __construct()
74
    {
75
        // Initialise values
76 120
        $this->name               = '';
77 120
        $this->description        = '';
78 120
        $this->resizeProportional = true;
79
80
        // Set image index
81 120
        self::$imageCounter++;
82 120
        $this->imageIndex = self::$imageCounter;
83
84
        // Initialize parent
85 120
        parent::__construct();
86 120
    }
87
    
88 1
    public function __clone()
89
    {
90 1
        parent::__clone();
91
        
92 1
        self::$imageCounter++;
93 1
        $this->imageIndex = self::$imageCounter;
94 1
    }
95
96
    /**
97
     * Get image index
98
     *
99
     * @return int
100
     */
101 46
    public function getImageIndex()
102
    {
103 46
        return $this->imageIndex;
104
    }
105
106
    /**
107
     * Get Name
108
     *
109
     * @return string
110
     */
111 55
    public function getName()
112
    {
113 55
        return $this->name;
114
    }
115
116
    /**
117
     * Set Name
118
     *
119
     * @param  string                          $pValue
120
     * @return \PhpOffice\PhpPresentation\Shape\AbstractDrawing
121
     */
122 8
    public function setName($pValue = '')
123
    {
124 8
        $this->name = $pValue;
125 8
        return $this;
126
    }
127
128
    /**
129
     * Get Description
130
     *
131
     * @return string
132
     */
133 45
    public function getDescription()
134
    {
135 45
        return $this->description;
136
    }
137
138
    /**
139
     * Set Description
140
     *
141
     * @param  string                          $pValue
142
     * @return \PhpOffice\PhpPresentation\Shape\AbstractDrawing
143
     */
144 5
    public function setDescription($pValue = '')
145
    {
146 5
        $this->description = $pValue;
147
148 5
        return $this;
149
    }
150
151
    /**
152
     * Set Width
153
     *
154
     * @param  int                             $pValue
155
     * @return \PhpOffice\PhpPresentation\Shape\AbstractGraphic
156
     */
157 46
    public function setWidth($pValue = 0)
158
    {
159
        // Resize proportional?
160 46
        if ($this->resizeProportional && $pValue != 0) {
161 9
            $ratio         = $this->height / $this->width;
162 9
            $this->height = (int) round($ratio * $pValue);
163
        }
164
165
        // Set width
166 46
        $this->width = $pValue;
167
168 46
        return $this;
169
    }
170
171
    /**
172
     * Set Height
173
     *
174
     * @param  int                             $pValue
175
     * @return \PhpOffice\PhpPresentation\Shape\AbstractGraphic
176
     */
177 46
    public function setHeight($pValue = 0)
178
    {
179
        // Resize proportional?
180 46
        if ($this->resizeProportional && $pValue != 0) {
181 9
            $ratio        = $this->width / $this->height;
182 9
            $this->width = (int) round($ratio * $pValue);
183
        }
184
185
        // Set height
186 46
        $this->height = $pValue;
187
188 46
        return $this;
189
    }
190
191
    /**
192
     * Set width and height with proportional resize
193
     * @author Vincent@luo MSN:[email protected]
194
     * @param  int $width
195
     * @param  int $height
196
     * @return \PhpOffice\PhpPresentation\Shape\AbstractGraphic
197
     */
198 2
    public function setWidthAndHeight($width = 0, $height = 0)
199
    {
200 2
        $xratio = $width / $this->width;
201 2
        $yratio = $height / $this->height;
202 2
        if ($this->resizeProportional && !($width == 0 || $height == 0)) {
203 2
            if (($xratio * $this->height) < $height) {
204 1
                $this->height = (int) ceil($xratio * $this->height);
205 1
                $this->width  = $width;
206
            } else {
207 1
                $this->width  = (int) ceil($yratio * $this->width);
208 1
                $this->height = $height;
209
            }
210
        }
211
212 2
        return $this;
213
    }
214
215
    /**
216
     * Get ResizeProportional
217
     *
218
     * @return boolean
219
     */
220 1
    public function isResizeProportional()
221
    {
222 1
        return $this->resizeProportional;
223
    }
224
225
    /**
226
     * Set ResizeProportional
227
     *
228
     * @param  boolean                         $pValue
229
     * @return \PhpOffice\PhpPresentation\Shape\AbstractDrawing
230
     */
231 28
    public function setResizeProportional($pValue = true)
232
    {
233 28
        $this->resizeProportional = $pValue;
234
235 28
        return $this;
236
    }
237
238
    /**
239
     * Get hash code
240
     *
241
     * @return string Hash code
242
     */
243 66
    public function getHashCode()
244
    {
245 66
        return md5($this->name . $this->description . parent::getHashCode() . __CLASS__);
246
    }
247
}
248