Alpha Framework alpha--util--helpers
[ class tree: alpha--util--helpers ] [ index: alpha--util--helpers ] [ all elements ]

Source for file AlphaValidator.inc

Documentation is available at AlphaValidator.inc

  1. <?php
  2.  
  3. /**
  4.  * Generic validation class used throughout the Alpha Framework
  5.  * 
  6.  * @package alpha::util::helpers
  7.  * @since 1.0
  8.  * @author John Collins <dev@alphaframework.org>
  9.  * @version $Id: AlphaValidator.inc 1341 2011-03-17 15:02:02Z johnc $
  10.  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  11.  * @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
  12.  *  All rights reserved.
  13.  * 
  14.  *  <pre>
  15.  *  Redistribution and use in source and binary forms, with or
  16.  *  without modification, are permitted provided that the
  17.  *  following conditions are met:
  18.  * 
  19.  *  * Redistributions of source code must retain the above
  20.  *    copyright notice, this list of conditions and the
  21.  *    following disclaimer.
  22.  *  * Redistributions in binary form must reproduce the above
  23.  *    copyright notice, this list of conditions and the
  24.  *    following disclaimer in the documentation and/or other
  25.  *    materials provided with the distribution.
  26.  *  * Neither the name of the Alpha Framework nor the names
  27.  *    of its contributors may be used to endorse or promote
  28.  *    products derived from this software without specific
  29.  *    prior written permission.
  30.  *   
  31.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32.  *  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33.  *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  34.  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  35.  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  36.  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37.  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38.  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39.  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  40.  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  41.  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  42.  *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  43.  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44.  *  </pre>
  45.  *  
  46.  */
  47. class AlphaValidator {
  48.     /**
  49.      * Allows any kind of input, including blanks
  50.      *
  51.      * @var string 
  52.      * @since 1.0
  53.      */
  54.     const ALLOW_ALL '/.*/i';
  55.     
  56.     /**
  57.      * Required double value
  58.      *
  59.      * @var string 
  60.      * @since 1.0
  61.      */
  62.     const REQUIRED_DOUBLE '/^-{0,1}[0-9\.]+$/';
  63.     
  64.     /**
  65.      * Required integer value
  66.      *
  67.      * @var string 
  68.      * @since 1.0
  69.      */
  70.     const REQUIRED_INTEGER '/^-{0,1}[0-9]*$/';
  71.     
  72.     /**
  73.      * Required text value, accepts a maximum of 65536 characters
  74.      *
  75.      * @var string 
  76.      * @since 1.0
  77.      */
  78.     const REQUIRED_TEXT '/^[a-z\d\.,\'][a-z\d\.,\' ]{0,65535}$/i';
  79.     
  80.     /**
  81.      * Required string value, accepts a maximum of 255 characters
  82.      *
  83.      * @var string 
  84.      * @since 1.0
  85.      */
  86.     const REQUIRED_STRING '/^[a-z\d\.,\'][a-z\d\.,\' ]{0,255}$/i';
  87.     
  88.     /**
  89.      * Required alphabet string
  90.      *
  91.      * @var string 
  92.      * @since 1.0
  93.      */
  94.     const REQUIRED_ALPHA '/^[a-zA-Z]+$/';
  95.     
  96.     /**
  97.      * Required uppercase alphabet string
  98.      *
  99.      * @var string 
  100.      * @since 1.0
  101.      */
  102.     const REQUIRED_ALPHA_UPPER '/^[A-Z]+$/';
  103.     
  104.     /**
  105.      * Required alpha-numeric string
  106.      *
  107.      * @var string 
  108.      * @since 1.0
  109.      */
  110.     const REQUIRED_ALPHA_NUMBEIC '/^[a-zA-Z0-9]+$/';
  111.     
  112.     /**
  113.      * Required HTTP URL value
  114.      *
  115.      * @var string 
  116.      * @since 1.0
  117.      */
  118.     const REQUIRED_HTTP_URL '/^(http:\/\/.*)$/i';
  119.     
  120.     /**
  121.      * Optional HTTP URL value
  122.      *
  123.      * @var string 
  124.      * @since 1.0
  125.      */
  126.     const OPTIONAL_HTTP_URL '/^(http:\/\/.*\/.*)$|^([.*]{0})$/i';
  127.     
  128.     /**
  129.      * Required IP address value
  130.      *
  131.      * @var string 
  132.      * @since 1.0
  133.      */
  134.     const REQUIRED_IP '/^(((([1-9])|([1-9][\d])|(1[\d]{2})|(2[0-4][\d])|(25[0-4]))(\.(([\d])|([1-9][\d])|(1[\d]{2})|(2[0-4][\d])|(25[0-4]))){3})|(0(\.0){3}))$/';
  135.     
  136.     /**
  137.      * Required email address value
  138.      *
  139.      * @var string 
  140.      * @since 1.0
  141.      */
  142.     const REQUIRED_EMAIL '/^[-_.a-zA-Z0-9]+@((([a-zA-Z0-9]|[-_.a-zA-Z0-9]*[a-zA-Z0-9])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i';
  143.     
  144.     /**
  145.      * Required username (allows a-z A-Z 0-9 and -_. characters)
  146.      * 
  147.      * @var string 
  148.      * @since 1.0
  149.      */
  150.     const REQUIRED_USERNAME '/^[-_\.a-zA-Z0-9]*$/i';
  151.     
  152.     /**
  153.      * Required sequence value
  154.      *
  155.      * @var string 
  156.      * @since 1.0
  157.      */
  158.     const REQUIRED_SEQUENCE '/^[A-Z]*-[0-9]*$/';
  159.     
  160.     /**
  161.      * Validate that the provided value is a valid integer
  162.      *
  163.      * @param $value 
  164.      * @return boolean 
  165.      * @since 1.0
  166.      */
  167.     public static function isInteger($value{
  168.         if (preg_match(self::REQUIRED_INTEGER$value))
  169.             return (is_numeric($valueintval($value== $value false);
  170.         else
  171.             return false;
  172.     }
  173.  
  174.     /**
  175.      * Validate that the provided value is a valid double
  176.      *
  177.      * @param $value 
  178.      * @return boolean 
  179.      * @since 1.0
  180.      */
  181.     public static function isDouble($value{
  182.         if (preg_match(self::REQUIRED_DOUBLE$value))
  183.             return is_double($value);
  184.         else
  185.             return false;
  186.     }
  187.     
  188.     /**
  189.      * Validate that the provided value is a valid boolean (will accept 1 or 0 as valid booleans)
  190.      *
  191.      * @param $value 
  192.      * @return boolean 
  193.      * @since 1.0
  194.      */
  195.     public static function isBoolean($value{
  196.         $acceptable array(truefalse"true""false"10"1""0");
  197.         
  198.         if(!in_array($value$acceptabletrue))
  199.             return false;
  200.         else
  201.             return true;
  202.     }
  203.  
  204.     /**
  205.      * Validate that the provided value is a valid alphabetic string (strictly a-zA-Z)
  206.      *
  207.      * @param $value 
  208.      * @return boolean 
  209.      * @since 1.0
  210.      */
  211.     public static function isAlpha($value{
  212.         if(preg_match(self::REQUIRED_ALPHA$value))
  213.             return true;
  214.         else
  215.             return false;
  216.     }
  217.  
  218.     /**
  219.      * Validate that the provided value is a valid alpha-numeric string (strictly a-zA-Z0-9)
  220.      *
  221.      * @param $value 
  222.      * @return boolean 
  223.      * @since 1.0
  224.      */
  225.     public static function isAlphaNum($value{
  226.         if(preg_match(self::REQUIRED_ALPHA_NUMBEIC$value))
  227.             return true;
  228.         else
  229.             return false;
  230.     }
  231.     
  232.     /**
  233.      * Validate that the provided value is a valid Sequence string ([A-Z]-[0-9])
  234.      *
  235.      * @param $value 
  236.      * @return boolean 
  237.      * @since 1.0
  238.      */
  239.     public static function isSequence($value{
  240.         if(preg_match(self::REQUIRED_SEQUENCE$value))
  241.             return true;
  242.         else
  243.             return false;
  244.     }
  245.  
  246.     /**
  247.      * Validate that the provided value is a valid URL
  248.      *
  249.      * @param $value 
  250.      * @return boolean 
  251.      * @since 1.0
  252.      */
  253.     public static function isURL($url{
  254.         if(preg_match(self::REQUIRED_HTTP_URL$url)) {
  255.             if(!filter_var($urlFILTER_VALIDATE_URLFILTER_FLAG_HOST_REQUIRED))
  256.                 return false;
  257.             else
  258.                 return true;
  259.         }else{
  260.             return false;
  261.         }
  262.     }
  263.  
  264.     /**
  265.      * Validate that the provided value is a valid IP address
  266.      *
  267.      * @param $value 
  268.      * @return boolean 
  269.      * @since 1.0
  270.      */
  271.     public static function isIP($ip{
  272.         if(preg_match(self::REQUIRED_IP$ip)) {
  273.             if(!filter_var($ipFILTER_VALIDATE_IPFILTER_FLAG_IPV4))
  274.                 return false;
  275.             else
  276.                 return true;
  277.         }else{
  278.             return false;
  279.         }
  280.     }
  281.  
  282.     /**
  283.      * Validate that the provided value is a valid email address
  284.      *
  285.      * @param $value 
  286.      * @return boolean 
  287.      * @since 1.0
  288.      */
  289.     public function isEmail($email{
  290.         if(preg_match(self::REQUIRED_EMAIL$email)) {
  291.             if(!filter_var($emailFILTER_VALIDATE_EMAIL))
  292.                 return false;
  293.             else
  294.                 return true;
  295.         }else{
  296.             return false;
  297.         }
  298.     }
  299. }
  300.  
  301. ?>

Documentation generated on Thu, 17 Mar 2011 16:43:41 +0000 by phpDocumentor 1.4.3