DocumentLayout::setCY()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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
     * @var float
79
     */
80
    private $dimensionX;
81
82
    /**
83
     * Layout Y dimension
84
     * @var float
85
     */
86
    private $dimensionY;
87
88
    /**
89
     * Create a new \PhpOffice\PhpPresentation\DocumentLayout
90
     */
91 226
    public function __construct()
92
    {
93 226
        $this->setDocumentLayout(self::LAYOUT_SCREEN_4X3);
94 226
    }
95
96
    /**
97
     * Get Document Layout
98
     *
99
     * @return string
100
     */
101 177
    public function getDocumentLayout()
102
    {
103 177
        return $this->layout;
104
    }
105
106
    /**
107
     * Set Document Layout
108
     *
109
     * @param array|string $pValue
110
     * @param  boolean $isLandscape
111
     * @return \PhpOffice\PhpPresentation\DocumentLayout
112
     */
113 226
    public function setDocumentLayout($pValue = self::LAYOUT_SCREEN_4X3, $isLandscape = true)
114
    {
115
        switch ($pValue) {
116 226
            case self::LAYOUT_SCREEN_4X3:
117 3
            case self::LAYOUT_SCREEN_16X10:
118 3
            case self::LAYOUT_SCREEN_16X9:
119 3
            case self::LAYOUT_35MM:
120 3
            case self::LAYOUT_A3:
121 3
            case self::LAYOUT_A4:
122 2
            case self::LAYOUT_B4ISO:
123 2
            case self::LAYOUT_B5ISO:
124 2
            case self::LAYOUT_BANNER:
125 2
            case self::LAYOUT_LETTER:
126 2
            case self::LAYOUT_OVERHEAD:
127 226
                $this->layout = $pValue;
128 226
                $this->dimensionX = $this->dimension[$this->layout]['cx'];
129 226
                $this->dimensionY = $this->dimension[$this->layout]['cy'];
130 226
                break;
131 2
            case self::LAYOUT_CUSTOM:
132
            default:
133 2
                $this->layout = self::LAYOUT_CUSTOM;
134 2
                $this->dimensionX = $pValue['cx'];
135 2
                $this->dimensionY = $pValue['cy'];
136 2
                break;
137
        }
138
139 226
        if (!$isLandscape) {
140 2
            $tmp = $this->dimensionX;
141 2
            $this->dimensionX = $this->dimensionY;
142 2
            $this->dimensionY = $tmp;
143
        }
144
145 226
        return $this;
146
    }
147
148
    /**
149
     * Get Document Layout cx
150
     *
151
     * @param string $unit
152
     * @return integer
153
     */
154 178
    public function getCX($unit = self::UNIT_EMU)
155
    {
156 178
        return $this->convertUnit($this->dimensionX, self::UNIT_EMU, $unit);
157
    }
158
159
    /**
160
     * Get Document Layout cy
161
     *
162
     * @param string $unit
163
     * @return integer
164
     */
165 178
    public function getCY($unit = self::UNIT_EMU)
166
    {
167 178
        return $this->convertUnit($this->dimensionY, self::UNIT_EMU, $unit);
168
    }
169
170
    /**
171
     * Get Document Layout cx
172
     *
173
     * @param float $value
174
     * @param string $unit
175
     * @return DocumentLayout
176
     */
177 1
    public function setCX($value, $unit = self::UNIT_EMU)
178
    {
179 1
        $this->layout = self::LAYOUT_CUSTOM;
180 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 integer. However, the property $dimensionX is declared as type double. 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...
181 1
        return $this;
182
    }
183
184
    /**
185
     * Get Document Layout cy
186
     *
187
     * @param float $value
188
     * @param string $unit
189
     * @return DocumentLayout
190
     */
191 1
    public function setCY($value, $unit = self::UNIT_EMU)
192
    {
193 1
        $this->layout = self::LAYOUT_CUSTOM;
194 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 integer. However, the property $dimensionY is declared as type double. 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...
195 1
        return $this;
196
    }
197
198
    /**
199
     * Convert EMUs to differents units
200
     * @param float $value
201
     * @param string $fromUnit
202
     * @param string $toUnit
203
     * @return float
204
     */
205 179
    protected function convertUnit($value, $fromUnit, $toUnit)
206
    {
207
        // Convert from $fromUnit to EMU
208
        switch ($fromUnit) {
209 179
            case self::UNIT_MILLIMETER:
210 2
                $value *= 36000;
211 2
                break;
212 179
            case self::UNIT_CENTIMETER:
213 2
                $value *= 360000;
214 2
                break;
215 179
            case self::UNIT_INCH:
216 2
                $value *= 914400;
217 2
                break;
218 179
            case self::UNIT_PIXEL:
219 2
                $value = Drawing::pixelsToEmu($value);
220 2
                break;
221 179
            case self::UNIT_POINT:
222 2
                $value *= 12700;
223 2
                break;
224 179
            case self::UNIT_EMU:
225
            default:
226
                // no changes
227
        }
228
229
        // Convert from EMU to $toUnit
230
        switch ($toUnit) {
231 179
            case self::UNIT_MILLIMETER:
232 3
                $value /= 36000;
233 3
                break;
234 178
            case self::UNIT_CENTIMETER:
235 2
                $value /= 360000;
236 2
                break;
237 178
            case self::UNIT_INCH:
238 2
                $value /= 914400;
239 2
                break;
240 178
            case self::UNIT_PIXEL:
241 3
                $value = Drawing::emuToPixels($value);
242 3
                break;
243 178
            case self::UNIT_POINT:
244 2
                $value /= 12700;
245 2
                break;
246 178
            case self::UNIT_EMU:
247
            default:
248
            // no changes
249
        }
250 179
        return $value;
251
    }
252
}
253