summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Util/Dimension.php
blob: 6860fd82faddc68543d5cb849246eaa7a4144cdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */

namespace Icinga\Util;

class Dimension
{
    /**
     * Defines this dimension as nr of pixels
     */
    const UNIT_PX = "px";

    /**
     * Defines this dimension as width of 'M' in current font
     */
    const UNIT_EM = "em";

    /**
     * Defines this dimension as a percentage value
     */
    const UNIT_PERCENT = "%";

    /**
     * Defines this dimension in points
     */
    const UNIT_PT = "pt";

    /**
     * The current set value for this dimension
     *
     * @var int
     */
    private $value = 0;

    /**
     * The unit to interpret the value with
     *
     * @var string
     */
    private $unit = self::UNIT_PX;

    /**
     * Create a new Dimension object with the given size and unit
     *
     * @param int $value        The new value
     * @param string $unit      The unit to use (default: px)
     */
    public function __construct($value, $unit = self::UNIT_PX)
    {
        $this->setValue($value, $unit);
    }

    /**
     * Change the value and unit of this dimension
     *
     * @param int $value        The new value
     * @param string $unit      The unit to use (default: px)
     */
    public function setValue($value, $unit = self::UNIT_PX)
    {
        $this->value = intval($value);
        $this->unit = $unit;
    }

    /**
     * Return true when the value is > 0
     *
     * @return bool
     */
    public function isDefined()
    {
        return $this->value > 0;
    }

    /**
     * Return the underlying value without unit information
     *
     * @return int
     */
    public function getValue()
    {
        return $this->value;
    }

    /**
     * Return the unit used for the value
     *
     * @return string
     */
    public function getUnit()
    {
        return $this->unit;
    }

    /**
     * Return this value with it's according unit as a string
     *
     * @return string
     */
    public function __toString()
    {
        if (!$this->isDefined()) {
            return "";
        }
        return $this->value.$this->unit;
    }

    /**
     * Create a new Dimension object from a string containing the numeric value and the dimension (e.g. 200px, 20%)
     *
     * @param $string                       The string to parse
     *
     * @return Dimension
     */
    public static function fromString($string)
    {
        $matches = array();
        if (!preg_match_all('/^ *([0-9]+)(px|pt|em|\%) */i', $string, $matches)) {
            return new Dimension(0);
        }
        return new Dimension(intval($matches[1][0]), $matches[2][0]);
    }
}