Failed Conditions
Pull Request — master (#4447)
by Owen
17:43 queued 17s
created

Dimension::toUnit()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
ccs 8
cts 8
cp 1
crap 3
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Helper;
4
5
use PhpOffice\PhpSpreadsheet\Exception;
6
use PhpOffice\PhpSpreadsheet\Shared\Drawing;
7
use PhpOffice\PhpSpreadsheet\Style\Font;
8
9
class Dimension
10
{
11
    public const UOM_CENTIMETERS = 'cm';
12
    public const UOM_MILLIMETERS = 'mm';
13
    public const UOM_INCHES = 'in';
14
    public const UOM_PIXELS = 'px';
15
    public const UOM_POINTS = 'pt';
16
    public const UOM_PICA = 'pc';
17
18
    /**
19
     * Based on 96 dpi.
20
     */
21
    const ABSOLUTE_UNITS = [
22
        self::UOM_CENTIMETERS => 96.0 / 2.54,
23
        self::UOM_MILLIMETERS => 96.0 / 25.4,
24
        self::UOM_INCHES => 96.0,
25
        self::UOM_PIXELS => 1.0,
26
        self::UOM_POINTS => 96.0 / 72,
27
        self::UOM_PICA => 96.0 * 12 / 72,
28
    ];
29
30
    /**
31
     * Based on a standard column width of 8.54 units in MS Excel.
32
     */
33
    const RELATIVE_UNITS = [
34
        'em' => 10.0 / 8.54,
35
        'ex' => 10.0 / 8.54,
36
        'ch' => 10.0 / 8.54,
37
        'rem' => 10.0 / 8.54,
38
        'vw' => 8.54,
39
        'vh' => 8.54,
40
        'vmin' => 8.54,
41
        'vmax' => 8.54,
42
        '%' => 8.54 / 100,
43
    ];
44
45
    /**
46
     * @var float|int If this is a width, then size is measured in pixels (if is set)
47
     *                   or in Excel's default column width units if $unit is null.
48
     *                If this is a height, then size is measured in pixels ()
49
     *                   or in points () if $unit is null.
50
     */
51
    protected float|int $size;
52
53
    protected ?string $unit = null;
54
55
    public function __construct(string $dimension)
56
    {
57
        $size = 0.0;
58
        $unit = '';
59 88
        $sscanf = sscanf($dimension, '%[1234567890.]%s');
60
        if (is_array($sscanf)) {
61 88
            $size = (float) ($sscanf[0] ?? 0.0);
62
            $unit = strtolower($sscanf[1] ?? '');
63
        }
64 88
65
        // If a UoM is specified, then convert the size to pixels for internal storage
66 88
        if (isset(self::ABSOLUTE_UNITS[$unit])) {
67 88
            $size *= self::ABSOLUTE_UNITS[$unit];
68 88
            $this->unit = self::UOM_PIXELS;
69
        } elseif (isset(self::RELATIVE_UNITS[$unit])) {
70
            $size *= self::RELATIVE_UNITS[$unit];
71 88
            $size = round($size, 4);
72 71
        }
73 71
74 25
        $this->size = $size;
75 5
    }
76 5
77
    public function width(): float
78
    {
79 88
        return (float) ($this->unit === null)
80
            ? $this->size
81
            : round(Drawing::pixelsToCellDimension((int) $this->size, new Font(false)), 4);
82 60
    }
83
84 60
    public function height(): float
85 7
    {
86 60
        return (float) ($this->unit === null)
87
            ? $this->size
88
            : $this->toUnit(self::UOM_POINTS);
89 3
    }
90
91 3
    public function toUnit(string $unitOfMeasure): float
92 1
    {
93 3
        $unitOfMeasure = strtolower($unitOfMeasure);
94
        if (!array_key_exists($unitOfMeasure, self::ABSOLUTE_UNITS)) {
95
            throw new Exception("{$unitOfMeasure} is not a vaid unit of measure");
96 75
        }
97
98 75
        $size = $this->size;
99 75
        if ($this->unit === null) {
100 1
            $size = Drawing::cellDimensionToPixels($size, new Font(false));
101
        }
102
103 74
        return $size / self::ABSOLUTE_UNITS[$unitOfMeasure];
104 74
    }
105
}
106