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

Source for file Functions.php

Documentation is available at Functions.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | Copyright (c) 2002-2003 Brent Cook                                        |
  5. // | Copyright (c) 2002 The PHP Group
  6. // +----------------------------------------------------------------------+
  7. // | This library is free software; you can redistribute it and/or        |
  8. // | modify it under the terms of the GNU Lesser General Public           |
  9. // | License as published by the Free Software Foundation; either         |
  10. // | version 2.1 of the License, or (at your option) any later version.   |
  11. // |                                                                      |
  12. // | This library is distributed in the hope that it will be useful,      |
  13. // | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
  14. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
  15. // | Lesser General Public License for more details.                      |
  16. // |                                                                      |
  17. // | You should have received a copy of the GNU Lesser General Public     |
  18. // | License along with this library; if not, write to the Free Software  |
  19. // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA|
  20. // +----------------------------------------------------------------------+
  21. // | Authors: Jacob Lee <jacobswell4u@yahoo.com>                          |
  22. // | Authors: Brent Cook <busterb@mail.utexas.edu>                         |
  23. // +----------------------------------------------------------------------+
  24. //
  25. // $Id: Functions.php,v 1.1 2003/01/08 19:22:51 busterb Exp $
  26. //
  27. /**
  28.  * functions like those in SQL
  29.  */
  30.  
  31. class DBA_Functions
  32. {
  33.     // {{{ max($field, $rows)
  34.     function max($field$rows)
  35.     {
  36.         if (is_null($rows|| !sizeof($rows)) return null;
  37.         return max(DBA_Table::_getColumn($field$rows));
  38.     }
  39.     // }}}
  40.  
  41.     // {{{ min($field, $rows)
  42.     function min($field$rows)
  43.     {
  44.         if (is_null($rows|| !sizeof($rows)) return null;
  45.         return min(DBA_Table::_getColumn($field$rows));
  46.     }
  47.     // }}}
  48.  
  49.     // {{{ sum($field, $rows)
  50.     function sum($field$rows)
  51.     {
  52.         if (is_null($rows|| !sizeof($rows)) return null;
  53.         return array_sum(DBA_Table::_getColumn($field$rows));
  54.     }
  55.     // }}}
  56.  
  57.     // {{{ avg($field, $rows)
  58.     function avg($field$rows)
  59.     {
  60.         if (is_null($rows|| !sizeof($rows)) return null;
  61.         return array_sum(DBA_Table::_getColumn($field$rows)) sizeof($rows);
  62.     }
  63.     // }}}
  64.  
  65.     // {{{ count($field, $rows)
  66.     function count($rows)
  67.     {
  68.         if (is_null($rows|| !sizeof($rows)) return null;
  69.         return count($rows);
  70.     }
  71.     // }}}
  72.  
  73.     // {{{ time()
  74.     /**
  75.      * Internal function for returning the current time
  76.      *
  77.      * @access private
  78.      */
  79.     function time({
  80.         return time();
  81.     }
  82.     // }}}
  83.  
  84.     // {{{ in()
  85.     function in($val$val1$val2$val3 = null)
  86.     {
  87.         $isIn ($val == $val1 or $val == $val2? true : false;
  88.         if ($val3 == nullreturn $isIn;
  89.         return ($isIn or $val == $val3? true : false;
  90.     }
  91.     // }}}
  92.  
  93.     // {{{ between($num, $min, $max)
  94.     function between($num$min$max)
  95.     {
  96.         if ($min == $maxreturn ($num == $min? true : false;
  97.         if ($min $max{
  98.             $tmp $max;
  99.             $max $min;
  100.             $min &$tmp;
  101.         }
  102.         return ($num >= $min and $num <= $max? true : false;
  103.     }
  104.     // }}}
  105.  
  106.     // {{{ log($val, $base)
  107.     function log($val$base)
  108.     {
  109.         return log10($val)/log10($base);
  110.     }
  111.     // }}}
  112.  
  113.     // {{{ mod($val, $devider)
  114.     function mod($val$devider)
  115.     {
  116.         if ($devider == nullreturn null;
  117.         return $val $devider;
  118.     }
  119.     // }}}
  120.  
  121.     // {{{ sign($val)
  122.     function sign($val)
  123.     {
  124.         return ($val > 0? 1 : (($val == 0? 0 : -1);
  125.     }
  126.     // }}}
  127.  
  128.     // {{{ concat($str1, $str2)
  129.     function concat($str1$str2)
  130.     {
  131.         return $str1.$str2;
  132.     }
  133.     // }}}
  134.  
  135.     // {{{ initcap($str)
  136.     function initcap($str)
  137.     {
  138.         return ucfirst($str);
  139.     }
  140.     // }}}
  141.  
  142.     // {{{ lpad($input, $padLength, $padString)
  143.     function lpad($input$padLength$padString)
  144.     {
  145.         return str_pad($input$padLength$padStringSTR_PAD_LEFT);
  146.     }
  147.     // }}}
  148.  
  149.     // {{{ rpad($input, $padlength, $padString)
  150.     function rpad($input$padlength$padString)
  151.     {
  152.         return str_pad($input$padLength$padStringSTR_PAD_RIGHT);
  153.     }
  154.     // }}}
  155.  
  156.     // {{{ replace($str, $search, $replace = '')
  157.     function replace($str$search$replace '')
  158.     {
  159.         if ($search == nullreturn $str;
  160.         if ($replace == null$replace '';
  161.         return str_replace($search$replace$str);
  162.     }
  163.     // }}}
  164.  
  165.     // {{{ substring($str, $start, $length = null)
  166.     function substring($str$start$length = null)
  167.     {
  168.         if ($start > 0$start--;
  169.         return ($length == null)    substr($str$start
  170.                                     : substr($str$start$length);
  171.     }
  172.     // }}}
  173.  
  174.     // {{{ translate($str, $from, $to)
  175.     function translate($str$from$to)
  176.     {
  177.         if strlen($from!= strlen($to) )
  178.             return PEAR::raiseError("needs elements that have the same string length");
  179.         $fromWord explode(''$from);
  180.         $fromWord array_flip($fromWord);
  181.         $toWord explode(''$to);
  182.         $translate '';
  183.         for ($i = 0; $i strlen($str)$i++{
  184.             $translate .= $toWord[$fromWord[$str[$i]]];
  185.         }
  186.         return $translate;
  187.     }
  188.     // }}}
  189.  
  190.     // {{{ instr($str, $match, $start = 1, $count = 1)
  191.     function instr($str$match$start = 1$count = 1)
  192.     {
  193.         if ($start > 0{
  194.             $start--;
  195.             $pos strpos($str$match$start);
  196.             for ($i = 1; $i != $count$i++{
  197.                 $pos strpos($str$match$pos+1);
  198.                 if ($pos == falsereturn 0;
  199.             }
  200.             return $pos+1;
  201.         }elseif ($start < 0{
  202.             $pos strrpos($str$match);
  203.             for ($i = 1; $i != $count$i++{
  204.                 $pos strrpos(substr($str0$pos)$match);
  205.                 if ($pos == falsereturn 0;
  206.             }
  207.             return $pos+1;
  208.         }
  209.         return PEAR::raiseError("invalid start number");
  210.     }
  211.     // }}}
  212.  
  213.     // {{{ strlike($str,$search,$option=0)
  214.     function strlike($str,$search,$option=0)
  215.     {
  216.         /**
  217.         * if the value of $option is 
  218.         *     0 : CASE SENSITIVE
  219.         *     1 : CASE INSENSITIVE
  220.         *     2 : LIKE PREG_MATCH
  221.         */
  222.         if ($option<2{
  223.             $search quotemeta($search);
  224.             $search preg_replace("/^\\%/"".*"$search);
  225.             $search preg_replace("/\\%$/"".*"$search);
  226.             $search preg_replace("/_/""?"$search);
  227.         }
  228.         switch ($option{
  229.             case 0:
  230.                 return preg_match("/^$search$/s"$str);
  231.             case 1:
  232.                 return preg_match("/^$search$/si"$str);
  233.             case 2:
  234.                 return preg_match("/$search/is"$str);
  235.         }
  236.     }
  237.     // }}}
  238. }
  239. ?>

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