Completed
Pull Request — develop (#611)
by
unknown
94:03 queued 03:08
created

DocumentLayout::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
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;
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
                break;
133
            default:
134 2
                $this->layout = self::LAYOUT_CUSTOM;
135 2
                $this->dimensionX = $pValue['cx'];
136 2
                $this->dimensionY = $pValue['cy'];
137 2
                break;
138
        }
139
140 226
        if (!$isLandscape) {
141 2
            $tmp = $this->dimensionX;
142 2
            $this->dimensionX = $this->dimensionY;
143 2
            $this->dimensionY = $tmp;
144
        }
145
146 226
        return $this;
147
    }
148
149
    /**
150
     * Get Document Layout cx
151
     *
152
     * @param string $unit
153
     * @return integer
154
     */
155 178
    public function getCX($unit = self::UNIT_EMU)
156
    {
157 178
        return $this->convertUnit($this->dimensionX, self::UNIT_EMU, $unit);
158
    }
159
160
    /**
161
     * Get Document Layout cy
162
     *
163
     * @param string $unit
164
     * @return integer
165
     */
166 178
    public function getCY($unit = self::UNIT_EMU)
167
    {
168 178
        return $this->convertUnit($this->dimensionY, self::UNIT_EMU, $unit);
169
    }
170
171
    /**
172
     * Get Document Layout cx
173
     *
174
     * @param float $value
175
     * @param string $unit
176
     * @return DocumentLayout
177
     */
178 1
    public function setCX($value, $unit = self::UNIT_EMU)
179
    {
180 1
        $this->layout = self::LAYOUT_CUSTOM;
181 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...
182 1
        return $this;
183
    }
184
185
    /**
186
     * Get Document Layout cy
187
     *
188
     * @param float $value
189
     * @param string $unit
190
     * @return DocumentLayout
191
     */
192 1
    public function setCY($value, $unit = self::UNIT_EMU)
193
    {
194 1
        $this->layout = self::LAYOUT_CUSTOM;
195 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...
196 1
        return $this;
197
    }
198
199
    /**
200
     * Convert EMUs to differents units
201
     * @param float $value
202
     * @param string $fromUnit
203
     * @param string $toUnit
204
     * @return float
205
     */
206 179
    protected function convertUnit($value, $fromUnit, $toUnit)
207
    {
208
        // Convert from $fromUnit to EMU
209
        switch ($fromUnit) {
210 179
            case self::UNIT_MILLIMETER:
211 2
                $value *= 36000;
212 2
                break;
213 179
            case self::UNIT_CENTIMETER:
214 2
                $value *= 360000;
215 2
                break;
216 179
            case self::UNIT_INCH:
217 2
                $value *= 914400;
218 2
                break;
219 179
            case self::UNIT_PIXEL:
220 2
                $value = Drawing::pixelsToEmu($value);
221 2
                break;
222 179
            case self::UNIT_POINT:
223 2
                $value *= 12700;
224 2
                break;
225 179
            case self::UNIT_EMU:
226
            default:
227
                // no changes
228
        }
229
230
        // Convert from EMU to $toUnit
231
        switch ($toUnit) {
232 179
            case self::UNIT_MILLIMETER:
233 3
                $value /= 36000;
234 3
                break;
235 178
            case self::UNIT_CENTIMETER:
236 2
                $value /= 360000;
237 2
                break;
238 178
            case self::UNIT_INCH:
239 2
                $value /= 914400;
240 2
                break;
241 178
            case self::UNIT_PIXEL:
242 3
                $value = Drawing::emuToPixels($value);
243 3
                break;
244 178
            case self::UNIT_POINT:
245 2
                $value /= 12700;
246 2
                break;
247 178
            case self::UNIT_EMU:
248
            default:
249
            // no changes
250
        }
251 179
        return $value;
252
    }
253
}
254