ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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  'ch' => true, 'rem' => true, 'vw' => true, 'vh' => true,
36  'vmin' => true, 'vmax' => true
37  );
38 
43  public function __construct($n = '0', $u = false)
44  {
45  $this->n = (string) $n;
46  $this->unit = $u !== false ? (string) $u : false;
47  }
48 
54  public static function make($s)
55  {
56  if ($s instanceof HTMLPurifier_Length) {
57  return $s;
58  }
59  $n_length = strspn($s, '1234567890.+-');
60  $n = substr($s, 0, $n_length);
61  $unit = substr($s, $n_length);
62  if ($unit === '') {
63  $unit = false;
64  }
65  return new HTMLPurifier_Length($n, $unit);
66  }
67 
72  protected function validate()
73  {
74  // Special case:
75  if ($this->n === '+0' || $this->n === '-0') {
76  $this->n = '0';
77  }
78  if ($this->n === '0' && $this->unit === false) {
79  return true;
80  }
81  if (!ctype_lower($this->unit)) {
82  $this->unit = strtolower($this->unit);
83  }
84  if (!isset(HTMLPurifier_Length::$allowedUnits[$this->unit])) {
85  return false;
86  }
87  // Hack:
89  $result = $def->validate($this->n, false, false);
90  if ($result === false) {
91  return false;
92  }
93  $this->n = $result;
94  return true;
95  }
96 
101  public function toString()
102  {
103  if (!$this->isValid()) {
104  return false;
105  }
106  return $this->n . $this->unit;
107  }
108 
113  public function getN()
114  {
115  return $this->n;
116  }
117 
122  public function getUnit()
123  {
124  return $this->unit;
125  }
126 
131  public function isValid()
132  {
133  if ($this->isValid === null) {
134  $this->isValid = $this->validate();
135  }
136  return $this->isValid;
137  }
138 
146  public function compareTo($l)
147  {
148  if ($l === false) {
149  return false;
150  }
151  if ($l->unit !== $this->unit) {
152  $converter = new HTMLPurifier_UnitConverter();
153  $l = $converter->convert($l, $this->unit);
154  if ($l === false) {
155  return false;
156  }
157  }
158  return $this->n - $l->n;
159  }
160 }
161 
162 // vim: et sw=4 sts=4
$isValid
Whether or not this length is valid.
Definition: Length.php:26
Class for converting between different unit-lengths as specified by CSS.
$result
$n
String numeric magnitude.
Definition: Length.php:14
compareTo($l)
Compares two lengths, and returns 1 if greater, -1 if less and 0 if equal.
Definition: Length.php:146
Validates a number as defined by the CSS spec.
Definition: Number.php:6
static $allowedUnits
Array Lookup array of units recognized by CSS 3 array.
Definition: Length.php:32
if(! $in) print Initializing normalization quick check tables n
$s
Definition: pwgen.php:45
static make($s)
Definition: Length.php:54
validate()
Validates the number and unit.
Definition: Length.php:72
getUnit()
Retrieves string unit.
Definition: Length.php:122
__construct($n='0', $u=false)
Definition: Length.php:43
Represents a measurable length, with a string numeric magnitude and a unit.
Definition: Length.php:7
getN()
Retrieves string numeric magnitude.
Definition: Length.php:113
global $l
Definition: afr.php:30
$unit
String unit.
Definition: Length.php:20
$def
Definition: croninfo.php:21
isValid()
Returns true if this length unit is valid.
Definition: Length.php:131
toString()
Returns string representation of number.
Definition: Length.php:101