Remove $a = array($a) which is miscompiled by Zend OpCache.
[htmlpurifier.git] / library / HTMLPurifier / Length.php
blobbbfbe6624d6de10101ed787141d0e0ef11b75d94
1 <?php
3 /**
4 * Represents a measurable length, with a string numeric magnitude
5 * and a unit. This object is immutable.
6 */
7 class HTMLPurifier_Length
10 /**
11 * String numeric magnitude.
12 * @type string
14 protected $n;
16 /**
17 * String unit. False is permitted if $n = 0.
18 * @type string|bool
20 protected $unit;
22 /**
23 * Whether or not this length is valid. Null if not calculated yet.
24 * @type bool
26 protected $isValid;
28 /**
29 * Array Lookup array of units recognized by CSS 2.1
30 * @type array
32 protected static $allowedUnits = array(
33 'em' => true, 'ex' => true, 'px' => true, 'in' => true,
34 'cm' => true, 'mm' => true, 'pt' => true, 'pc' => true
37 /**
38 * @param string $n Magnitude
39 * @param bool|string $u Unit
41 public function __construct($n = '0', $u = false)
43 $this->n = (string) $n;
44 $this->unit = $u !== false ? (string) $u : false;
47 /**
48 * @param string $s Unit string, like '2em' or '3.4in'
49 * @return HTMLPurifier_Length
50 * @warning Does not perform validation.
52 public static function make($s)
54 if ($s instanceof HTMLPurifier_Length) {
55 return $s;
57 $n_length = strspn($s, '1234567890.+-');
58 $n = substr($s, 0, $n_length);
59 $unit = substr($s, $n_length);
60 if ($unit === '') {
61 $unit = false;
63 return new HTMLPurifier_Length($n, $unit);
66 /**
67 * Validates the number and unit.
68 * @return bool
70 protected function validate()
72 // Special case:
73 if ($this->n === '+0' || $this->n === '-0') {
74 $this->n = '0';
76 if ($this->n === '0' && $this->unit === false) {
77 return true;
79 if (!ctype_lower($this->unit)) {
80 $this->unit = strtolower($this->unit);
82 if (!isset(HTMLPurifier_Length::$allowedUnits[$this->unit])) {
83 return false;
85 // Hack:
86 $def = new HTMLPurifier_AttrDef_CSS_Number();
87 $result = $def->validate($this->n, false, false);
88 if ($result === false) {
89 return false;
91 $this->n = $result;
92 return true;
95 /**
96 * Returns string representation of number.
97 * @return string
99 public function toString()
101 if (!$this->isValid()) {
102 return false;
104 return $this->n . $this->unit;
108 * Retrieves string numeric magnitude.
109 * @return string
111 public function getN()
113 return $this->n;
117 * Retrieves string unit.
118 * @return string
120 public function getUnit()
122 return $this->unit;
126 * Returns true if this length unit is valid.
127 * @return bool
129 public function isValid()
131 if ($this->isValid === null) {
132 $this->isValid = $this->validate();
134 return $this->isValid;
138 * Compares two lengths, and returns 1 if greater, -1 if less and 0 if equal.
139 * @param HTMLPurifier_Length $l
140 * @return int
141 * @warning If both values are too large or small, this calculation will
142 * not work properly
144 public function compareTo($l)
146 if ($l === false) {
147 return false;
149 if ($l->unit !== $this->unit) {
150 $converter = new HTMLPurifier_UnitConverter();
151 $l = $converter->convert($l, $this->unit);
152 if ($l === false) {
153 return false;
156 return $this->n - $l->n;
160 // vim: et sw=4 sts=4