HTMLPurifier/Length.php Quellcode

Length.php
gehe zur Dokumentation dieser Datei
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




Korrekturen, Hinweise und Ergänzungen

Bitte scheuen Sie sich nicht und melden Sie, was auf dieser Seite sachlich falsch oder irreführend ist, was ergänzt werden sollte, was fehlt usw. Dazu bitte oben aus dem Menü Seite den Eintrag Support Forum wählen. Es ist eine kostenlose Anmeldung erforderlich, um Anmerkungen zu posten. Unpassende Postings, Spam usw. werden kommentarlos entfernt.