DB_Table
[ class tree: DB_Table ] [ index: DB_Table ] [ all elements ]

Source for file Valid.php

Documentation is available at Valid.php

  1. <?php
  2.  
  3. /**
  4. * DB_Table class for constants and other globals.
  5. */
  6. require_once 'DB/Table.php';
  7.  
  8.  
  9. /**
  10. * validation ranges for integers
  11. */
  12. if (isset($GLOBALS['_DB_TABLE']['valid'])) {
  13.     $GLOBALS['_DB_TABLE']['valid'= array(
  14.         'smallint' => array(pow(-215)pow(+215- 1),
  15.         'integer' => array(pow(-231)pow(+231- 1),
  16.         'bigint' => array(pow(-263)pow(+263- 1)
  17.     );
  18. }
  19.  
  20.  
  21. /**
  22. * DB_Table_Valid validates values against DB_Table column types.
  23. * $Id: Valid.php,v 1.3 2004/07/10 15:07:55 pmjones Exp $
  24. @author Paul M. Jones <pmjones@ciaweb.net>
  25. *
  26. @package DB_Table
  27. */
  28.  
  29. class DB_Table_Valid {
  30.     
  31.     /**
  32.     * 
  33.     * Check if a value validates against the 'boolean' data type.
  34.     * 
  35.     * @static
  36.     * 
  37.     * @access public
  38.     * 
  39.     * @param mixed $value The value to validate.
  40.     * 
  41.     * @return boolean True if the value is valid for the data type, false
  42.     *  if not.
  43.     * 
  44.     */
  45.     
  46.     function isBoolean($value)
  47.     {
  48.         if ($value === true || $value === false{
  49.             return true;
  50.         elseif (is_numeric($value&& ($value == 0 || $value == 1)) {
  51.             return true;
  52.         else {
  53.             return false;
  54.         }
  55.     }
  56.     
  57.     
  58.     /**
  59.     * 
  60.     * Check if a value validates against the 'char' and 'varchar' data type.
  61.     * 
  62.     * @static
  63.     * 
  64.     * @access public
  65.     * 
  66.     * @param mixed $value The value to validate.
  67.     * 
  68.     * @return boolean True if the value is valid for the data type, false
  69.     *  if not.
  70.     * 
  71.     */
  72.     
  73.     function isChar($value$colsize)
  74.     {
  75.         $in_range (strlen($value<= $colsize);
  76.         return is_string($value&& $in_range;
  77.     }
  78.     
  79.     
  80.     /**
  81.     * 
  82.     * Check if a value validates against the 'smallint' data type.
  83.     * 
  84.     * @static
  85.     * 
  86.     * @access public
  87.     * 
  88.     * @param mixed $value The value to validate.
  89.     * 
  90.     * @return boolean True if the value is valid for the data type, false
  91.     *  if not.
  92.     * 
  93.     */
  94.     
  95.     function isSmallint($value)
  96.     {
  97.         return is_integer($value&&
  98.             ($value >= $GLOBALS['_DB_TABLE']['valid']['smallint'][0]&&
  99.             ($value <= $GLOBALS['_DB_TABLE']['valid']['smallint'][1]);
  100.     }
  101.     
  102.     
  103.     /**
  104.     * 
  105.     * Check if a value validates against the 'integer' data type.
  106.     * 
  107.     * @static
  108.     * 
  109.     * @access public
  110.     * 
  111.     * @param mixed $value The value to validate.
  112.     * 
  113.     * @return boolean True if the value is valid for the data type, false
  114.     *  if not.
  115.     * 
  116.     */
  117.     
  118.     function isInteger($value)
  119.     {
  120.         return is_integer($value&&
  121.             ($value >= $GLOBALS['_DB_TABLE']['valid']['integer'][0]&&
  122.             ($value <= $GLOBALS['_DB_TABLE']['valid']['integer'][1]);
  123.     }
  124.     
  125.     
  126.     /**
  127.     * 
  128.     * Check if a value validates against the 'bigint' data type.
  129.     * 
  130.     * @static
  131.     * 
  132.     * @access public
  133.     * 
  134.     * @param mixed $value The value to validate.
  135.     * 
  136.     * @return boolean True if the value is valid for the data type, false
  137.     *  if not.
  138.     * 
  139.     */
  140.     
  141.     function isBigint($value)
  142.     {
  143.         return is_integer($value&&
  144.             ($value >= $GLOBALS['_DB_TABLE']['valid']['bigint'][0]&&
  145.             ($value <= $GLOBALS['_DB_TABLE']['valid']['bigint'][1]);
  146.     }
  147.     
  148.     
  149.     /**
  150.     * 
  151.     * Check if a value validates against the 'decimal' data type.
  152.     * 
  153.     * For the column defined "DECIMAL(5,2)" standard SQL requires that
  154.     * the column be able to store any value with 5 digits and 2
  155.     * decimals. In this case, therefore, the range of values that can be
  156.     * stored in the column is from -999.99 to 999.99.  DB_Table attempts
  157.     * to enforce this behavior regardless of the RDBMS backend behavior.
  158.     * 
  159.     * @static
  160.     * 
  161.     * @access public
  162.     * 
  163.     * @param mixed $value The value to validate.
  164.     * 
  165.     * @param string $colsize The 'size' to use for validation (to make
  166.     *  sure of min/max and decimal places).
  167.     * 
  168.     * @param string $colscope The 'scope' to use for validation (to make
  169.     *  sure of min/max and decimal places).
  170.     * 
  171.     * @return boolean True if the value is valid for the data type, false
  172.     *  if not.
  173.     * 
  174.     */
  175.     
  176.     function isDecimal($value$colsize$colscope)
  177.     {
  178.         if (is_numeric($value)) {
  179.             return false;
  180.         }
  181.         
  182.         // maximum number of digits allowed to the left
  183.         // and right of the decimal point.
  184.         $right_max $colscope;
  185.         $left_max $colsize $colscope;
  186.         
  187.         // ignore negative signs in all validation
  188.         $value str_replace('-'''$value);
  189.         
  190.         // find the decimal point, then get the left
  191.         // and right portions.
  192.         $pos strpos($value'.');
  193.         if ($pos === false{
  194.             $left $value;
  195.             $right '';
  196.         else {
  197.             $left substr($value0$pos);
  198.             $right substr($value$pos+1);
  199.         }
  200.         
  201.         // how long are the left and right portions?
  202.         $left_len strlen($left);
  203.         $right_len strlen($right);
  204.         
  205.         // do the portions exceed their maxes?
  206.         if ($left_len $left_max ||
  207.             $right_len $right_max{
  208.             // one or the other exceeds the max lengths
  209.             return false;
  210.         else {
  211.             // both are within parameters
  212.             return true;
  213.         }
  214.     }
  215.     
  216.     
  217.     /**
  218.     * 
  219.     * Check if a value validates against the 'single' data type.
  220.     * 
  221.     * @static
  222.     * 
  223.     * @access public
  224.     * 
  225.     * @param mixed $value The value to validate.
  226.     * 
  227.     * @return boolean True if the value is valid for the data type, false
  228.     *  if not.
  229.     * 
  230.     */
  231.     
  232.     function isSingle($value)
  233.     {
  234.         return is_float($value);
  235.     }
  236.     
  237.     
  238.     /**
  239.     * 
  240.     * Check if a value validates against the 'double' data type.
  241.     * 
  242.     * @static
  243.     * 
  244.     * @access public
  245.     * 
  246.     * @param mixed $value The value to validate.
  247.     * 
  248.     * @return boolean True if the value is valid for the data type, false
  249.     *  if not.
  250.     * 
  251.     */
  252.     
  253.     function isDouble($value)
  254.     {
  255.         return is_float($value);
  256.     }
  257.     
  258.     
  259.     /**
  260.     * 
  261.     * Check if a value validates against the 'time' data type.
  262.     * 
  263.     * @static
  264.     * 
  265.     * @access public
  266.     * 
  267.     * @param mixed $value The value to validate.
  268.     * 
  269.     * @return boolean True if the value is valid for the data type, false
  270.     *  if not.
  271.     * 
  272.     */
  273.     
  274.     function isTime($value)
  275.     {
  276.         // hh:ii:ss
  277.         // 01234567
  278.         $h  substr($value02);
  279.         $s1 substr($value21);
  280.         $i  substr($value32);
  281.         $s2 substr($value51);
  282.         $s  substr($value62);
  283.         
  284.         // time check
  285.         if (strlen($value!= 8 ||
  286.             is_numeric($h|| $h < 0 || $h > 23  ||
  287.             $s1 != ':' ||
  288.             is_numeric($i|| $i < 0 || $i > 59 ||
  289.             $s2 != ':' ||
  290.             is_numeric($s|| $s < 0 || $s > 59{
  291.             
  292.             return false;
  293.             
  294.         else {
  295.         
  296.             return true;
  297.             
  298.         }
  299.     }
  300.     
  301.     
  302.     /**
  303.     * 
  304.     * Check if a value validates against the 'date' data type.
  305.     * 
  306.     * @static
  307.     * 
  308.     * @access public
  309.     * 
  310.     * @param mixed $value The value to validate.
  311.     * 
  312.     * @return boolean True if the value is valid for the data type, false
  313.     *  if not.
  314.     * 
  315.     */
  316.     
  317.     function isDate($value)
  318.     {
  319.         // yyyy-mm-dd
  320.         // 0123456789
  321.         $y  substr($value04);
  322.         $s1 substr($value41);
  323.         $m  substr($value52);
  324.         $s2 substr($value71);
  325.         $d  substr($value82);
  326.         
  327.         // date check
  328.         if (strlen($value!= 10 || $s1 != '-' || $s2 != '-' ||
  329.             checkdate($m$d$y)) {
  330.             
  331.             return false;
  332.             
  333.         else {
  334.         
  335.             return true;
  336.             
  337.         }
  338.     }
  339.     
  340.     
  341.     /**
  342.     * 
  343.     * Check if a value validates against the 'timestamp' data type.
  344.     * 
  345.     * @static
  346.     * 
  347.     * @access public
  348.     * 
  349.     * @param mixed $value The value to validate.
  350.     * 
  351.     * @return boolean True if the value is valid for the data type, false
  352.     *  if not.
  353.     * 
  354.     */
  355.     
  356.     function isTimestamp($value)
  357.     {
  358.         // yyyy-mm-dd hh:ii:ss
  359.         // 0123456789012345678
  360.         $date substr($value010);
  361.         $sep substr($value101);
  362.         $time substr($value118);
  363.         
  364.         if (strlen($value!= 19 || $sep != ' ' ||
  365.             DB_Table_Valid::isDate($date||
  366.             DB_Table_Valid::isTime($time)) {
  367.             
  368.             return false;
  369.             
  370.         else {
  371.         
  372.             return true;
  373.             
  374.         }
  375.     }
  376.     
  377.     
  378.     /**
  379.     * 
  380.     * Check if a value validates against the 'clob' data type.
  381.     * 
  382.     * @static
  383.     * 
  384.     * @access public
  385.     * 
  386.     * @param mixed $value The value to validate.
  387.     * 
  388.     * @return boolean True if the value is valid for the data type, false
  389.     *  if not.
  390.     * 
  391.     */
  392.     
  393.     function isClob($value)
  394.     {
  395.         return is_string($value);
  396.     }
  397. }
  398.  
  399. ?>

Documentation generated on Mon, 11 Mar 2019 13:52:55 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.