Source for file Length.php

Documentation is available at Length.php

  1. <?php
  2.  
  3. /**
  4.  * Represents a measurable length, with a string numeric magnitude
  5.  * and a unit. This object is immutable.
  6.  */
  7. {
  8.     
  9.     /**
  10.      * String numeric magnitude.
  11.      */
  12.     protected $n;
  13.     
  14.     /**
  15.      * String unit. False is permitted if $n = 0.
  16.      */
  17.     protected $unit;
  18.     
  19.     /**
  20.      * Whether or not this length is valid. Null if not calculated yet.
  21.      */
  22.     protected $isValid;
  23.     
  24.     /**
  25.      * Lookup array of units recognized by CSS 2.1
  26.      */
  27.     protected static $allowedUnits array(
  28.         'em' => true'ex' => true'px' => true'in' => true,
  29.         'cm' => true'mm' => true'pt' => true'pc' => true
  30.     );
  31.     
  32.     /**
  33.      * @param number $n Magnitude
  34.      * @param string $u Unit
  35.      */
  36.     public function __construct($n '0'$u false{
  37.         $this->n = (string) $n;
  38.         $this->unit = $u !== false ? (string) $u false;
  39.     }
  40.     
  41.     /**
  42.      * @param string $s Unit string, like '2em' or '3.4in'
  43.      * @warning Does not perform validation.
  44.      */
  45.     static public function make($s{
  46.         if ($s instanceof HTMLPurifier_Lengthreturn $s;
  47.         $n_length strspn($s'1234567890.+-');
  48.         $n substr($s0$n_length);
  49.         $unit substr($s$n_length);
  50.         if ($unit === ''$unit false;
  51.         return new HTMLPurifier_Length($n$unit);
  52.     }
  53.     
  54.     /**
  55.      * Validates the number and unit.
  56.      */
  57.     protected function validate({
  58.         // Special case:
  59.         if ($this->n === '+0' || $this->n === '-0'$this->n = '0';
  60.         if ($this->n === '0' && $this->unit === falsereturn true;
  61.         if (!ctype_lower($this->unit)) $this->unit = strtolower($this->unit);
  62.         if (!isset(HTMLPurifier_Length::$allowedUnits[$this->unit])) return false;
  63.         // Hack:
  64.         $def new HTMLPurifier_AttrDef_CSS_Number();
  65.         $result $def->validate($this->nfalsefalse);
  66.         if ($result === falsereturn false;
  67.         $this->n = $result;
  68.         return true;
  69.     }
  70.     
  71.     /**
  72.      * Returns string representation of number.
  73.      */
  74.     public function toString({
  75.         if (!$this->isValid()) return false;
  76.         return $this->n . $this->unit;
  77.     }
  78.     
  79.     /**
  80.      * Retrieves string numeric magnitude.
  81.      */
  82.     public function getN({return $this->n;}
  83.     
  84.     /**
  85.      * Retrieves string unit.
  86.      */
  87.     public function getUnit({return $this->unit;}
  88.     
  89.     /**
  90.      * Returns true if this length unit is valid.
  91.      */
  92.     public function isValid({
  93.         if ($this->isValid === null$this->isValid = $this->validate();
  94.         return $this->isValid;
  95.     }
  96.     
  97.     /**
  98.      * Compares two lengths, and returns 1 if greater, -1 if less and 0 if equal.
  99.      * @warning If both values are too large or small, this calculation will
  100.      *           not work properly
  101.      */
  102.     public function compareTo($l{
  103.         if ($l === falsereturn false;
  104.         if ($l->unit !== $this->unit{
  105.             $converter new HTMLPurifier_UnitConverter();
  106.             $l $converter->convert($l$this->unit);
  107.             if ($l === falsereturn false;
  108.         }
  109.         return $this->n - $l->n;
  110.     }
  111.     
  112. }

Documentation generated on Thu, 19 Jun 2008 18:49:42 -0400 by phpDocumentor 1.4.2