Source for file TypeValidator.php

Documentation is available at TypeValidator.php

  1. <?php
  2.  
  3. ///////////////////////////////////////////////////////////////////////////////
  4. /**
  5.  * This file contains the static TypeValidator class and the global function
  6.  * is_date().
  7.  *
  8.  * System requirements:
  9.  * <ul>
  10.  * <li>PHP 5</li>
  11.  * </ul>
  12.  *
  13.  * This library is free software: you can redistribute it and/or modify
  14.  * it under the terms of the GNU Lesser General Public License as published by
  15.  * the Free Software Foundation, either version 3 of the License, or
  16.  * (at your option) any later version.
  17.  * The Connector library is distributed in the hope that it will be useful,
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20.  * {@link http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License}
  21.  * for more details.
  22.  *
  23.  * @author Per Egil Roksvaag
  24.  * @copyright 2009 Per Egil Roksvaag
  25.  * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  26.  * @package connector
  27.  * @version 2.0.0
  28.  */
  29.  
  30. ///////////////////////////////////////////////////////////////////////////////
  31. /**
  32.  * Use the static TypeValidator class to validate an associated array of
  33.  * parameters against a type definition map.
  34.  * @package connector
  35.  */
  36.  
  37. {
  38.     ///////////////////////////////////////////////////////////////////////////
  39.     /**
  40.      * @var array Valid type identifiers
  41.      */
  42.  
  43.     static public $isType = array("bool""int""float""date""string""array""object");
  44.  
  45.     ///////////////////////////////////////////////////////////////////////////
  46.     /**
  47.      * @var array Valid boolean values
  48.      */
  49.  
  50.     static public $isBool = array(01-1"0""1""-1""true""TRUE""false""FALSE");
  51.  
  52.     ///////////////////////////////////////////////////////////////////////////
  53.     /**
  54.      * Validate an associated array of parameters against a type definition map.
  55.      *
  56.      * Normally, you don't need to call this function directly. Ist's called
  57.      * from every select(), insert(), update() and delete() function. See the
  58.      * {@link IConnector} interface.
  59.      *
  60.      * A <b>map</b> is an array of type definitions. A <b>type definition</b>
  61.      * is an array with two or three elements. Format:
  62.      *
  63.      * <pre>
  64.      * $map[] = array(name, type[, default]);
  65.      * name:    The key of an element in the associated array to validate.
  66.      * type:    A type identifier, see {@link TypeValidator::$isType}.
  67.      * default: An optional default value. Can be of any type, also null.
  68.      * </pre>
  69.      *
  70.      * If the type of <var>$param[name]</var> is wrong and can't be converted
  71.      * to the required type, an exception is thrown.
  72.      * If <var>$param[name]</var> doesn't exist or is null, the optional default
  73.      * value is used or - if no default value is given - an exception is thrown.
  74.      * Exsample:
  75.      *
  76.      * @param array $param An associated array of parameters to validate.
  77.      * @param array $map An array of type definitions.
  78.      * @throws Exception when $param doesn't match the type definition $map.
  79.      * @return array A valid (and possibly modified) associated array of parameters.
  80.      */
  81.  
  82.     static public function check($param$map)
  83.     {
  84.         if(is_array($param== false)
  85.         {
  86.             throw new Exception("TypeValidator - Wrong param format: the param must be an array.");
  87.         }
  88.         if(is_array($map== false)
  89.         {
  90.             throw new Exception("TypeValidator - Wrong map format: the map must be an array.");
  91.         }
  92.         foreach($map as $field)
  93.         {
  94.             self::checkField($field);
  95.             self::checkParam($param$field);
  96.         }
  97.         return $param;
  98.     }
  99.  
  100.     ///////////////////////////////////////////////////////////////////////////
  101.     /**
  102.      * Checks if a type definition is valid. If not, an exception is thrown.
  103.      *
  104.      * @param array $field The definition of a single field.
  105.      * @throws Exception when an invalid type definition is given.
  106.      * @return void 
  107.      */
  108.  
  109.     static protected function checkField($field)
  110.     {
  111.         if(!is_array($field))
  112.         {
  113.             throw new Exception("TypeValidator - Wrong field format: the field must be an array.");
  114.         }
  115.         if(count($field2)
  116.         {
  117.             throw new Exception("TypeValidator - Wrong field format: name and type must be definded.");
  118.         }
  119.         else
  120.         {
  121.             list($name$type$field;
  122.         }
  123.         if(!is_int($name&& !is_string($name))
  124.         {
  125.             throw new Exception("TypeValidator - Wrong field format: name must be alphanumeric.");
  126.         }
  127.         if(!in_array($typeself::$isType))
  128.         {
  129.             throw new Exception("TypeValidator - Wrong field format: unknown type.");
  130.         }
  131.     }
  132.  
  133.     ///////////////////////////////////////////////////////////////////////////
  134.     /**
  135.      * Check if an associated array of parameters match a type definition.
  136.      * If not, an exception is thrown.
  137.      *
  138.      * Valid parameters may be are converted or
  139.      * substituted to match the field definitien.
  140.      *
  141.      * @param array &$param An associated array of parameters to validate.
  142.      * @param array $field A type definition array.
  143.      * @throws Exception when $param doesn't match the type definition.
  144.      * @return void 
  145.      */
  146.  
  147.     static protected function checkParam(&$param$field)
  148.     {
  149.         list($name$type$field;
  150.  
  151.         if(array_key_exists($name$param))
  152.         {
  153.             if(self::checkType($param[$name]$type))
  154.             {
  155.                 self::cast($param[$name]$type);
  156.             }
  157.             else if(is_null($param[$name]&& count($field2)
  158.             {
  159.                 $param[$name$field[2];
  160.             }
  161.             else
  162.             {
  163.                 $found gettype($param[$name]);
  164.                 throw new Exception("TypeValidator - Wrong type in field {$name}, {$found} found.");
  165.             }
  166.         }
  167.         else
  168.         {
  169.             if(count($field2)
  170.             {
  171.                 $param[$name$field[2];
  172.             }
  173.             else
  174.             {
  175.                 throw new Exception("TypeValidator - Mandatory field {$name} is missing");
  176.             }
  177.         }
  178.     }
  179.  
  180.     ///////////////////////////////////////////////////////////////////////////
  181.     /**
  182.      * Check the type of a variable. A value of null allways returns false.
  183.      *
  184.      * @param mixed $value The value to validate.
  185.      * @param string $type A type identifier, see {@link TypeValidator::$isType}.
  186.      * @return bool true if the value has the correct type, false otherwise.
  187.      */
  188.  
  189.     static protected function checkType($value$type)
  190.     {
  191.         if(is_null($value)) return false;
  192.  
  193.         switch($type)
  194.         {
  195.             case "bool":    return is_bool($value)    || in_array($valueself::$isBooltrue);
  196.             case "int":        return is_int($value)    || is_numeric($value);
  197.             case "float":    return is_float($value)    || is_numeric($value);
  198.             case "date":    return is_date($value)    || (strtotime($value0);
  199.             case "string":    return is_string($value);
  200.             case "array":    return is_array($value);
  201.             case "object":    return is_object($value);
  202.         }
  203.     }
  204.  
  205.     ///////////////////////////////////////////////////////////////////////////
  206.     /**
  207.      * Convert the type of a variable if necessary.
  208.      *
  209.      * @param mixed &$value The variable to cast.
  210.      * @param string $type A type identifier, see {@link TypeValidator::$isType}.
  211.      * @return void 
  212.      */
  213.  
  214.     static protected function cast(&$value$type)
  215.     {
  216.         if($type == "date" && !function_exists("date_create"))
  217.         {
  218.             return;
  219.         }
  220.         switch($type)
  221.         {
  222.             case "bool":    is_bool($value)        || $value ($value == true)break;
  223.             case "int":        is_int($value)        || $value = (int)$valuebreak;
  224.             case "float":    is_float($value)    || $value = (float)$valuebreak;
  225.             case "date":    is_date($value)        || $value date_create($value)break;
  226.             case "string":    is_string($value)    || $value = (string)$valuebreak;
  227.             case "array":    is_array($value)    || $value = (array)$valuebreak;
  228.             case "object":    is_object($value)    || $value = (object)$valuebreak;
  229.         }
  230.     }
  231. }
  232.  
  233. ///////////////////////////////////////////////////////////////////////////////
  234. /**
  235.  * Shortcut type validation for DateTime.
  236.  *
  237.  * @param mixed $value The variable to validate.
  238.  * @return bool true if $value is a DateTime variable, false otherwise.
  239.  */
  240.  
  241. function is_date($value)
  242. {
  243.     return class_exists("DateTime"false&& ($value instanceof DateTime);
  244. }
  245.  
  246. ///////////////////////////////////////////////////////////////////////////////
  247.  
  248. ?>

Documentation generated on Wed, 03 Jun 2009 12:41:56 +0200 by phpDocumentor 1.4.1