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

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