ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
Length.php
Go to the documentation of this file.
1<?php
2
8{
9
14 protected $n;
15
20 protected $unit;
21
26 protected $isValid;
27
32 protected static $allowedUnits = array(
33 'em' => true, 'ex' => true, 'px' => true, 'in' => true,
34 'cm' => true, 'mm' => true, 'pt' => true, 'pc' => true
35 );
36
41 public function __construct($n = '0', $u = false)
42 {
43 $this->n = (string) $n;
44 $this->unit = $u !== false ? (string) $u : false;
45 }
46
52 public static function make($s)
53 {
54 if ($s instanceof HTMLPurifier_Length) {
55 return $s;
56 }
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;
62 }
63 return new HTMLPurifier_Length($n, $unit);
64 }
65
70 protected function validate()
71 {
72 // Special case:
73 if ($this->n === '+0' || $this->n === '-0') {
74 $this->n = '0';
75 }
76 if ($this->n === '0' && $this->unit === false) {
77 return true;
78 }
79 if (!ctype_lower($this->unit)) {
80 $this->unit = strtolower($this->unit);
81 }
82 if (!isset(HTMLPurifier_Length::$allowedUnits[$this->unit])) {
83 return false;
84 }
85 // Hack:
87 $result = $def->validate($this->n, false, false);
88 if ($result === false) {
89 return false;
90 }
91 $this->n = $result;
92 return true;
93 }
94
99 public function toString()
100 {
101 if (!$this->isValid()) {
102 return false;
103 }
104 return $this->n . $this->unit;
105 }
106
111 public function getN()
112 {
113 return $this->n;
114 }
115
120 public function getUnit()
121 {
122 return $this->unit;
123 }
124
129 public function isValid()
130 {
131 if ($this->isValid === null) {
132 $this->isValid = $this->validate();
133 }
134 return $this->isValid;
135 }
136
144 public function compareTo($l)
145 {
146 if ($l === false) {
147 return false;
148 }
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;
154 }
155 }
156 return $this->n - $l->n;
157 }
158}
159
160// vim: et sw=4 sts=4
$result
global $l
Definition: afr.php:30
Validates a number as defined by the CSS spec.
Definition: Number.php:7
Represents a measurable length, with a string numeric magnitude and a unit.
Definition: Length.php:8
static $allowedUnits
Array Lookup array of units recognized by CSS 2.1 @type array.
Definition: Length.php:32
validate()
Validates the number and unit.
Definition: Length.php:70
static make($s)
Definition: Length.php:52
$isValid
Whether or not this length is valid.
Definition: Length.php:26
$unit
String unit.
Definition: Length.php:20
compareTo($l)
Compares two lengths, and returns 1 if greater, -1 if less and 0 if equal.
Definition: Length.php:144
getUnit()
Retrieves string unit.
Definition: Length.php:120
getN()
Retrieves string numeric magnitude.
Definition: Length.php:111
__construct($n='0', $u=false)
Definition: Length.php:41
$n
String numeric magnitude.
Definition: Length.php:14
toString()
Returns string representation of number.
Definition: Length.php:99
isValid()
Returns true if this length unit is valid.
Definition: Length.php:129
Class for converting between different unit-lengths as specified by CSS.