ILIAS  Release_4_1_x_branch Revision 61804
 All Data Structures Namespaces Files Functions Variables Groups Pages
Length.php
Go to the documentation of this file.
1 <?php
2 
8 {
9 
13  protected $n;
14 
18  protected $unit;
19 
23  protected $isValid;
24 
28  protected static $allowedUnits = array(
29  'em' => true, 'ex' => true, 'px' => true, 'in' => true,
30  'cm' => true, 'mm' => true, 'pt' => true, 'pc' => true
31  );
32 
37  public function __construct($n = '0', $u = false) {
38  $this->n = (string) $n;
39  $this->unit = $u !== false ? (string) $u : false;
40  }
41 
46  static public function make($s) {
47  if ($s instanceof HTMLPurifier_Length) return $s;
48  $n_length = strspn($s, '1234567890.+-');
49  $n = substr($s, 0, $n_length);
50  $unit = substr($s, $n_length);
51  if ($unit === '') $unit = false;
52  return new HTMLPurifier_Length($n, $unit);
53  }
54 
58  protected function validate() {
59  // Special case:
60  if ($this->n === '+0' || $this->n === '-0') $this->n = '0';
61  if ($this->n === '0' && $this->unit === false) return true;
62  if (!ctype_lower($this->unit)) $this->unit = strtolower($this->unit);
63  if (!isset(HTMLPurifier_Length::$allowedUnits[$this->unit])) return false;
64  // Hack:
66  $result = $def->validate($this->n, false, false);
67  if ($result === false) return false;
68  $this->n = $result;
69  return true;
70  }
71 
75  public function toString() {
76  if (!$this->isValid()) return false;
77  return $this->n . $this->unit;
78  }
79 
83  public function getN() {return $this->n;}
84 
88  public function getUnit() {return $this->unit;}
89 
93  public function isValid() {
94  if ($this->isValid === null) $this->isValid = $this->validate();
95  return $this->isValid;
96  }
97 
103  public function compareTo($l) {
104  if ($l === false) return false;
105  if ($l->unit !== $this->unit) {
106  $converter = new HTMLPurifier_UnitConverter();
107  $l = $converter->convert($l, $this->unit);
108  if ($l === false) return false;
109  }
110  return $this->n - $l->n;
111  }
112 
113 }
114 
115 // vim: et sw=4 sts=4