Completed
Pull Request — develop (#321)
by Thomas
06:37
created

DocumentLayout::getCX()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
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\Common\Drawing;
21
22
/**
23
 * \PhpOffice\PhpPresentation\DocumentLayout
24
 */
25
class DocumentLayout
26
{
27
    const LAYOUT_CUSTOM = '';
28
    const LAYOUT_SCREEN_4X3 = 'screen4x3';
29
    const LAYOUT_SCREEN_16X10 = 'screen16x10';
30
    const LAYOUT_SCREEN_16X9 = 'screen16x9';
31
    const LAYOUT_35MM = '35mm';
32
    const LAYOUT_A3 = 'A3';
33
    const LAYOUT_A4 = 'A4';
34
    const LAYOUT_B4ISO = 'B4ISO';
35
    const LAYOUT_B5ISO = 'B5ISO';
36
    const LAYOUT_BANNER = 'banner';
37
    const LAYOUT_LETTER = 'letter';
38
    const LAYOUT_OVERHEAD = 'overhead';
39
40
    const UNIT_EMU = 'emu';
41
    const UNIT_CENTIMETER = 'cm';
42
    const UNIT_INCH = 'in';
43
    const UNIT_MILLIMETER = 'mm';
44
    const UNIT_PIXEL = 'px';
45
    const UNIT_POINT = 'pt';
46
47
    /**
48
     * Dimension types
49
     *
50
     * 1 px = 9525 EMU @ 96dpi (which is seems to be the default)
51
     * Absolute distances are specified in English Metric Units (EMUs),
52
     * occasionally referred to as A units; there are 360000 EMUs per
53
     * centimeter, 914400 EMUs per inch, 12700 EMUs per point.
54
     */
55
    private $dimension = array(
56
        self::LAYOUT_SCREEN_4X3 => array('cx' => 9144000, 'cy' => 6858000),
57
        self::LAYOUT_SCREEN_16X10 => array('cx' => 9144000, 'cy' => 5715000),
58
        self::LAYOUT_SCREEN_16X9 => array('cx' => 9144000, 'cy' => 5143500),
59
        self::LAYOUT_35MM => array('cx' => 10287000, 'cy' => 6858000),
60
        self::LAYOUT_A3 => array('cx' => 15120000, 'cy' => 10692000),
61
        self::LAYOUT_A4 => array('cx' => 10692000, 'cy' => 7560000),
62
        self::LAYOUT_B4ISO => array('cx' => 10826750, 'cy' => 8120063),
63
        self::LAYOUT_B5ISO => array('cx' => 7169150, 'cy' => 5376863),
64
        self::LAYOUT_BANNER => array('cx' => 7315200, 'cy' => 914400),
65
        self::LAYOUT_LETTER => array('cx' => 9144000, 'cy' => 6858000),
66
        self::LAYOUT_OVERHEAD => array('cx' => 9144000, 'cy' => 6858000),
67
    );
68
69
    /**
70
     * Layout name
71
     *
72
     * @var string
73
     */
74
    private $layout;
75
76
    /**
77
     * Layout x dimension
78
     *
79
     * @var integer
80
     */
81
    private $dimensionX;
82
83
    /**
84
     * Layout y dimension
85
     *
86
     * @var integer
87
     */
88
    private $dimensionY;
89
90
    /**
91
     * Create a new \PhpOffice\PhpPresentation\DocumentLayout
92
     */
93 199
    public function __construct()
94
    {
95 199
        $this->setDocumentLayout(self::LAYOUT_SCREEN_4X3);
96 199
    }
97
98
    /**
99
     * Get Document Layout
100
     *
101
     * @return string
102
     */
103 158
    public function getDocumentLayout()
104
    {
105 158
        return $this->layout;
106
    }
107
108
    /**
109
     * Set Document Layout
110
     *
111
     * @param array|string $pValue
112
     * @param  boolean $isLandscape
113
     * @return \PhpOffice\PhpPresentation\DocumentLayout
114
     */
115 199
    public function setDocumentLayout($pValue = self::LAYOUT_SCREEN_4X3, $isLandscape = true)
116
    {
117
        switch ($pValue) {
118 199
            case self::LAYOUT_SCREEN_4X3:
119 3
            case self::LAYOUT_SCREEN_16X10:
120 3
            case self::LAYOUT_SCREEN_16X9:
121 3
            case self::LAYOUT_35MM:
122 3
            case self::LAYOUT_A3:
123 3
            case self::LAYOUT_A4:
124 2
            case self::LAYOUT_B4ISO:
125 2
            case self::LAYOUT_B5ISO:
126 2
            case self::LAYOUT_BANNER:
127 2
            case self::LAYOUT_LETTER:
128 2
            case self::LAYOUT_OVERHEAD:
129 199
                $this->layout = $pValue;
130 199
                $this->dimensionX = $this->dimension[$this->layout]['cx'];
131 199
                $this->dimensionY = $this->dimension[$this->layout]['cy'];
132 199
                break;
133 2
            case self::LAYOUT_CUSTOM:
134
            default:
135 2
                $this->layout = self::LAYOUT_CUSTOM;
136 2
                $this->dimensionX = $pValue['cx'];
137 2
                $this->dimensionY = $pValue['cy'];
138 2
                break;
139
        }
140
141 199
        if (!$isLandscape) {
142 2
            $tmp = $this->dimensionX;
143 2
            $this->dimensionX = $this->dimensionY;
144 2
            $this->dimensionY = $tmp;
145
        }
146
147 199
        return $this;
148
    }
149
150
    /**
151
     * Get Document Layout cx
152
     *
153
     * @param string $unit
154
     * @return integer
155
     */
156 159
    public function getCX($unit = self::UNIT_EMU)
157
    {
158 159
        return $this->convertUnit($this->dimensionX, self::UNIT_EMU, $unit);
159
    }
160
161
    /**
162
     * Get Document Layout cy
163
     *
164
     * @param string $unit
165
     * @return integer
166
     */
167 159
    public function getCY($unit = self::UNIT_EMU)
168
    {
169 159
        return $this->convertUnit($this->dimensionY, self::UNIT_EMU, $unit);
170
    }
171
172
    /**
173
     * Get Document Layout cx
174
     *
175
     * @param float $value
176
     * @param string $unit
177
     * @return DocumentLayout
178
     */
179 1
    public function setCX($value, $unit = self::UNIT_EMU)
180
    {
181 1
        $this->layout = self::LAYOUT_CUSTOM;
182 1
        $this->dimensionX = $this->convertUnit($value, $unit, self::UNIT_EMU);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->convertUnit($value, $unit, self::UNIT_EMU) can also be of type double. However, the property $dimensionX is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
183 1
        return $this;
184
    }
185
186
    /**
187
     * Get Document Layout cy
188
     *
189
     * @param float $value
190
     * @param string $unit
191
     * @return DocumentLayout
192
     */
193 1
    public function setCY($value, $unit = self::UNIT_EMU)
194
    {
195 1
        $this->layout = self::LAYOUT_CUSTOM;
196 1
        $this->dimensionY = $this->convertUnit($value, $unit, self::UNIT_EMU);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->convertUnit($value, $unit, self::UNIT_EMU) can also be of type double. However, the property $dimensionY is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
197 1
        return $this;
198
    }
199
200
    /**
201
     * Convert EMUs to differents units
202
     * @param float $value
203
     * @param string $fromUnit
204
     * @param string $toUnit
205
     * @return float
206
     */
207 160
    protected function convertUnit($value, $fromUnit, $toUnit)
208
    {
209
        // Convert from $fromUnit to EMU
210
        switch ($fromUnit) {
211 160
            case self::UNIT_MILLIMETER:
212 2
                $value *= 36000;
213 2
                break;
214 160
            case self::UNIT_CENTIMETER:
215 2
                $value *= 360000;
216 2
                break;
217 160
            case self::UNIT_INCH:
218 2
                $value *= 914400;
219 2
                break;
220 160
            case self::UNIT_PIXEL:
221 2
                $value = Drawing::pixelsToEmu($value);
222 2
                break;
223 160
            case self::UNIT_POINT:
224 2
                $value *= 12700;
225 2
                break;
226 160
            case self::UNIT_EMU:
227
            default:
228
                // no changes
229
        }
230
231
        // Convert from EMU to $toUnit
232
        switch ($toUnit) {
233 160
            case self::UNIT_MILLIMETER:
234 3
                $value /= 36000;
235 3
                break;
236 159
            case self::UNIT_CENTIMETER:
237 2
                $value /= 360000;
238 2
                break;
239 159
            case self::UNIT_INCH:
240 2
                $value /= 914400;
241 2
                break;
242 159
            case self::UNIT_PIXEL:
243 3
                $value = Drawing::emuToPixels($value);
244 3
                break;
245 159
            case self::UNIT_POINT:
246 2
                $value /= 12700;
247 2
                break;
248 159
            case self::UNIT_EMU:
249
            default:
250
            // no changes
251
        }
252 160
        return $value;
253
    }
254
}
255