Source for file Address.class.php

Documentation is available at Address.class.php

  1. <?php
  2. /**
  3.  * Gumbo Library Framework
  4.  *
  5.  * LICENSE
  6.  * This library is being released under the terms of the New BSD License.  A
  7.  * copy of the license is packaged with the software (LICENSE.txt).  If no
  8.  * copy is found, a copy of the license template can be found at:
  9.  * http://www.opensource.org/licenses/bsd-license.php
  10.  * 
  11.  * @category Gumbo
  12.  * @package Valid
  13.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  14.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  15.  * @author Michael Luster <mluster79@yahoo.com>
  16.  * @link http://sourceforge.net/projects/phpgumbo
  17.  * @version 0.0.1
  18.  */
  19.  
  20. /**
  21.  * Address Validation Class
  22.  *
  23.  * @category Gumbo
  24.  * @package Valid
  25.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  26.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  27.  * @author Michael Luster <mluster79@yahoo.com>
  28.  * @link http://sourceforge.net/projects/phpgumbo
  29.  * @desc Address Validation Class
  30.  * @version 0.0.1
  31.  */
  32.  
  33. gumbo_load ("Valid");
  34.  
  35. class Gumbo_Valid_Address extends Gumbo_Valid {
  36.     
  37.     /**
  38.      * Constructor
  39.      * @param bool $throw throw Exceptions
  40.      * @param int $level Error Level thrown
  41.      */
  42.     public function __construct ($throw=false$level=null{
  43.         parent::__construct ($throw$level);
  44.     }
  45.     
  46.     
  47.     
  48.     /**
  49.      * Returns if the value is a valid Address
  50.      * @param string $val 
  51.      * @return bool 
  52.      * @throws Gumbo_Exception
  53.      */
  54.     public function isAddress ($val{
  55.         $passed false;
  56.         
  57.         $this->turnOn ();
  58.         try {
  59.             // verify precondition
  60.             $this->isString ($val);
  61.             $this->isLessThanEqualTo ($val150);
  62.             $this->isMatch ($val"^([A-Za-z0-9[:space:]\.,_-]){0,}$");
  63.             $passed true;
  64.         catch (Exception $e{
  65.             unset ($e);
  66.         }
  67.         $this->turnOff ();
  68.         
  69.         if (!$passed && $this->isThrowing ()) {
  70.             throw new Gumbo_Exception ("Validation Failed: Address"$this->getLevel ());
  71.         }
  72.         return $passed;
  73.     }
  74.     
  75.     /**
  76.      * Returns if the value is a valid City
  77.      * @param string $val 
  78.      * @return bool 
  79.      * @throws Gumbo_Exception
  80.      */
  81.     public function isCity ($val{
  82.         $passed false;
  83.         
  84.         $this->turnOn ();
  85.         try {
  86.             // verify precondition
  87.             $this->isString ($val);
  88.             $this->isLessThanEqualTo ($val50);
  89.             $this->isMatch ($val"^([A-Za-z0-9[:space:]\.,_-]){0,}$");
  90.             $passed true;
  91.         catch (Exception $e{
  92.             $e null;
  93.         }
  94.         $this->turnOff ();
  95.         
  96.         if (!$passed && $this->isThrowing ()) {
  97.             throw new Gumbo_Exception ("Validation Failed: City"$this->getLevel ());
  98.         }
  99.         return $passed;
  100.     }
  101.     
  102.     /**
  103.      * Returns if the value is a valid State
  104.      * @param string $val 
  105.      * @return bool 
  106.      * @throws Gumbo_Exception
  107.      * @todo use Geo classes to retrieve information
  108.      */
  109.     public function isState ($val{
  110.         $passed false;
  111.         
  112.         $this->turnOn ();
  113.         try {
  114.             // verify precondition
  115.             $this->isString ($val);
  116.             $this->isLesThanEqualTo ($val40);
  117.             $this->isMatch ($val"^([A-Za-z0-9[:space:]\.,_-]){0,}$");
  118.             $passed true;
  119.         catch (Gumbo_Exception $e{
  120.             $e null;
  121.         }
  122.         $this->turnOff ();
  123.         
  124.         if (!$passed && $this->isThrowing ()) {
  125.             throw new Gumbo_Exception ("Validation Failed: State"$this->getLevel ());
  126.         }
  127.         return $passed;
  128.     }
  129.     
  130.     /**
  131.      * Returns if the value is a valid Province
  132.      * @param string $val 
  133.      * @return bool 
  134.      * @throws Gumbo_Exception
  135.      */
  136.     public function isProvince ($val{
  137.         $passed false;
  138.         
  139.         $this->turnOn ();
  140.         try {
  141.             // verify precondition
  142.             $this->isString ($val);
  143.             $this->isLesThanEqualTo ($val40);
  144.             $this->isMatch ($val"^([A-Za-z0-9[:space:]\.,_-]){0,}$");
  145.             $passed true;
  146.         catch (Gumbo_Exception $e{
  147.             $e null;
  148.         }
  149.         $this->turnOff ();
  150.         
  151.         if (!$passed && $this->isThrowing ()) {
  152.             throw new Gumbo_Exception ("Validation Failed: Province"$this->getLevel ());
  153.         }
  154.         return $passed;
  155.     }
  156.     
  157.     /**
  158.      * Returns if the value is a valid County
  159.      * @param string $val 
  160.      * @return bool 
  161.      * @throws Gumbo_Exception
  162.      */
  163.     public function isCounty ($val{
  164.         $passed false;
  165.         
  166.         $this->turnOn ();
  167.         try {
  168.             // verify precondition
  169.             $this->isString ($val);
  170.             $this->isLesThanEqualTo ($val40);
  171.             $this->isMatch ($val"^([A-Za-z0-9[:space:]\.,_-]){0,}$");
  172.             $passed true;
  173.         catch (Gumbo_Exception $e{
  174.             $e null;
  175.         }
  176.         $this->turnOff ();
  177.         
  178.         if (!$passed && $this->isThrowing ()) {
  179.             throw new Gumbo_Exception ("Validation Failed: County"$this->getLevel ());
  180.         }
  181.         return $passed;
  182.     }
  183.     
  184.     /**
  185.      * Returns if the value is a valid State Capital
  186.      * @param string $val 
  187.      * @return bool 
  188.      * @throws Gumbo_Exception
  189.      * @todo use Geo classes to determine validation
  190.      */
  191.     public function isStateCapital ($val{
  192.         $passed false;
  193.         
  194.         $this->turnOn ();
  195.         try {
  196.             // verify precondition
  197.             $this->isString ($val);
  198.             $this->isLesThanEqualTo ($val40);
  199.             $this->isMatch ($val"^([A-Za-z0-9[:space:]\.,_-]){0,}$");
  200.             $passed true;
  201.         catch (Gumbo_Exception $e{
  202.             $e null;
  203.         }
  204.         $this->turnOff ();
  205.         
  206.         if (!$passed && $this->isThrowing ()) {
  207.             throw new Gumbo_Exception ("Validation Failed: State Capital"$this->getLevel ());
  208.         }
  209.         return $passed;
  210.     }
  211.     
  212.     /**
  213.      * Returns if the value is a valid Capital
  214.      * @param string $val 
  215.      * @return bool 
  216.      * @throws Gumbo_Exception
  217.      */
  218.     public function isCapital ($val{
  219.         $passed false;
  220.         
  221.         $this->turnOn ();
  222.         try {
  223.             // verify precondition
  224.             $this->isString ($val);
  225.             $this->isLesThanEqualTo ($val40);
  226.             $this->isMatch ($val"^([A-Za-z0-9[:space:]\.,_-]){0,}$");
  227.             $passed true;
  228.         catch (Gumbo_Exception $e{
  229.             $e null;
  230.         }
  231.         $this->turnOff ();
  232.         
  233.         if (!$passed && $this->isThrowing ()) {
  234.             throw new Gumbo_Exception ("Validation Failed: Capital"$this->getLevel ());
  235.         }
  236.         return $passed;
  237.     }
  238.     
  239.     /**
  240.      * Returns if the value is a valid Country
  241.      * @param string $val 
  242.      * @return bool 
  243.      * @throws Gumbo_Exception
  244.      * @todo use Geo classes to determine validation
  245.      */
  246.     public function isCountry ($val{
  247.         $passed false;
  248.         
  249.         $this->turnOn ();
  250.         try {
  251.             // verify precondition
  252.             $this->isString ($val);
  253.             $this->isLesThanEqualTo ($val40);
  254.             $this->isMatch ($val"^([A-Za-z0-9[:space:]\.,_-]){0,}$");
  255.             $passed true;
  256.         catch (Gumbo_Exception $e{
  257.             $e null;
  258.         }
  259.         $this->turnOff ();
  260.         
  261.         if (!$passed && $this->isThrowing ()) {
  262.             throw new Gumbo_Exception ("Validation Failed: Country"$this->getLevel ());
  263.         }
  264.         return $passed;
  265.     }
  266.     
  267.     /**
  268.      * Returns if the value is a valid Continent
  269.      * @param string $val 
  270.      * @return bool 
  271.      * @throws Gumbo_Exception
  272.      * @todo use Geo classes to determine validation
  273.      */
  274.     public function isContinent ($val{
  275.         $passed false;
  276.         
  277.         $this->turnOn ();
  278.         try {
  279.             // verify precondition
  280.             $this->isString ($val);
  281.             $this->isLesThanEqualTo ($val40);
  282.             $this->isMatch ($val"^([A-Za-z0-9[:space:]\.,_-]){0,}$");
  283.             $passed true;
  284.         catch (Gumbo_Exception $e{
  285.             $e null;
  286.         }
  287.         $this->turnOff ();
  288.         
  289.         if (!$passed && $this->isThrowing ()) {
  290.             throw new Gumbo_Exception ("Validation Failed: Continent"$this->getLevel ());
  291.         }
  292.         return $passed;
  293.     }
  294.     
  295.     /**
  296.      * Returns if the value is a valid Zip code
  297.      * @param string $val 
  298.      * @return bool 
  299.      * @throws Gumbo_Exception
  300.      * @todo implement this method
  301.      */
  302.     public function isZip ($val{
  303.         $passed false;
  304.         
  305.         $this->turnOn ();
  306.         try {
  307.             // verify precondition
  308.             $this->isString ($val);
  309.             $this->isLesThanEqualTo ($val40);
  310.             $this->isMatch ($val"/^([0-9]{5})([\-]{1}[0-9]{4})?$/");
  311.             $passed true;
  312.         catch (Gumbo_Exception $e{
  313.             $e null;
  314.         }
  315.         $this->turnOff ();
  316.         
  317.         if (!$passed && $this->isThrowing ()) {
  318.             throw new Gumbo_Exception ("Validation Failed: Zip"$this->getLevel ());
  319.         }
  320.         return $passed;
  321.     }
  322.     
  323.     /**
  324.      * Returns if the value is a valid US Zip code
  325.      * @param string $val 
  326.      * @return bool 
  327.      * @throws Gumbo_Exception
  328.      */
  329.     public function isZipUS ($val{
  330.         $passed false;
  331.         $val substr ($val05);
  332.         
  333.         // @todo create a Geo US States class with the following information
  334.         $allstates array ("AK" => array ("9950099929"),"AL" => array ("3500036999"),"AR" => array ("7160072999""7550275505"),
  335.                             "AZ" => array ("8500086599"),"CA" => array ("9000096199"),"CO" => array ("8000081699"),
  336.                             "CT" => array ("0600006999"),"DC" => array ("2000020099""2020020599"),
  337.                             "DE" => array ("1970019999"),"FL" => array ("3200033999""3410034999"),
  338.                             "GA" => array ("3000031999"),"HI" => array ("9670096798""9680096899"),
  339.                             "IA" => array ("5000052999"),"ID" => array ("8320083899"),"IL" => array ("6000062999"),
  340.                             "IN" => array ("4600047999"),"KS" => array ("6600067999"),
  341.                             "KY" => array ("4000042799""4527545275"),"LA" => array ("7000071499""7174971749"),
  342.                             "MA" => array ("0100002799"),"MD" => array ("2033120331""2060021999"),
  343.                             "ME" => array ("0380103801""0380403804""0390004999"),
  344.                             "MI" => array ("4800049999"),"MN" => array ("5500056799"),"MO" => array ("6300065899"),
  345.                             "MS" => array ("3860039799"),"MT" => array ("5900059999"),"NC" => array ("2700028999"),
  346.                             "ND" => array ("5800058899"),"NE" => array ("6800069399"),
  347.                             "NH" => array ("0300003803""0380903899"),"NJ" => array ("0700008999"),
  348.                             "NM" => array ("8700088499"),"NV" => array ("8900089899"),
  349.                             "NY" => array ("0040000599""0639006390""0900014999"),
  350.                             "OH" => array ("4300045999"),"OK" => array ("7300073199""7340074999"),
  351.                             "OR" => array ("9700097999"),"PA" => array ("1500019699"),
  352.                             "RI" => array ("0280002999""0637906379"),"SC" => array ("2900029999"),
  353.                             "SD" => array ("5700057799"),"TN" => array ("3700038599""7239572395"),
  354.                             "TX" => array ("7330073399""7394973949""7500079999""8850188599"),
  355.                             "UT" => array ("8400084799"),
  356.                             "VA" => array ("2010520199""2030120301""2037020370""2200024699"),
  357.                             "VT" => array ("0500005999"),"WA" => array ("9800099499"),
  358.                             "WI" => array ("4993649936""5300054999"),"WV" => array ("2470026899"),
  359.                             "WY" => array ("8200083199") );
  360.         
  361.         // loop through the states and zip code ranges
  362.         if ((is_string ($val|| is_numeric ($val)) && strlen ($val>= 5{
  363.             foreach ($allstates as $zip{
  364.                 foreach($zip as $range{
  365.                     if (($val >= substr ($range05)) && ($val <= substr ($range,5))) {
  366.                         $passed true;
  367.                         break;
  368.                     }
  369.                 }
  370.                 if ($passed{
  371.                     break;
  372.                 }
  373.             }
  374.         }
  375.         
  376.         if (!$passed && $this->isThrowing ()) {
  377.             throw new Gumbo_Exception ("Validation Failed: US Zip"$this->getLevel ());
  378.         }
  379.         return $passed;
  380.     }
  381.     
  382. }
  383.  
  384. ?>