Source for file Integer.php

Documentation is available at Integer.php

  1. <?php
  2.  
  3. /**
  4.  * Validates an integer.
  5.  * @note While this class was modeled off the CSS definition, no currently
  6.  *        allowed CSS uses this type.  The properties that do are: widows,
  7.  *        orphans, z-index, counter-increment, counter-reset.  Some of the
  8.  *        HTML attributes, however, find use for a non-negative version of this.
  9.  */
  10. {
  11.     
  12.     /**
  13.      * Bool indicating whether or not negative values are allowed
  14.      */
  15.     protected $negative = true;
  16.     
  17.     /**
  18.      * Bool indicating whether or not zero is allowed
  19.      */
  20.     protected $zero = true;
  21.     
  22.     /**
  23.      * Bool indicating whether or not positive values are allowed
  24.      */
  25.     protected $positive = true;
  26.     
  27.     /**
  28.      * @param $negative Bool indicating whether or not negative values are allowed
  29.      * @param $zero Bool indicating whether or not zero is allowed
  30.      * @param $positive Bool indicating whether or not positive values are allowed
  31.      */
  32.     public function __construct(
  33.         $negative true$zero true$positive true
  34.     {
  35.         $this->negative = $negative;
  36.         $this->zero     = $zero;
  37.         $this->positive = $positive;
  38.     }
  39.     
  40.     public function validate($integer$config$context{
  41.         
  42.         $integer $this->parseCDATA($integer);
  43.         if ($integer === ''return false;
  44.         
  45.         // we could possibly simply typecast it to integer, but there are
  46.         // certain fringe cases that must not return an integer.
  47.         
  48.         // clip leading sign
  49.         if $this->negative && $integer[0=== '-' {
  50.             $digits substr($integer1);
  51.             if ($digits === '0'$integer '0'// rm minus sign for zero
  52.         elseif$this->positive && $integer[0=== '+' {
  53.             $digits $integer substr($integer1)// rm unnecessary plus
  54.         else {
  55.             $digits $integer;
  56.         }
  57.         
  58.         // test if it's numeric
  59.         if (!ctype_digit($digits)) return false;
  60.         
  61.         // perform scope tests
  62.         if (!$this->zero     && $integer == 0return false;
  63.         if (!$this->positive && $integer 0return false;
  64.         if (!$this->negative && $integer 0return false;
  65.         
  66.         return $integer;
  67.         
  68.     }
  69.     
  70. }

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