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

Source for file Database.php

Documentation is available at Database.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2011 PHPExcel
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20.  *
  21.  * @category    PHPExcel
  22.  * @package        PHPExcel_Calculation
  23.  * @copyright    Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license        http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version        1.7.6, 2011-02-27
  26.  */
  27.  
  28.  
  29. /** PHPExcel root directory */
  30. if (!defined('PHPEXCEL_ROOT')) {
  31.     /**
  32.      * @ignore
  33.      */
  34.     define('PHPEXCEL_ROOT'dirname(__FILE__'/../../');
  35.     require(PHPEXCEL_ROOT 'PHPExcel/Autoloader.php');
  36. }
  37.  
  38.  
  39. /**
  40.  * PHPExcel_Calculation_Database
  41.  *
  42.  * @category    PHPExcel
  43.  * @package        PHPExcel_Calculation
  44.  * @copyright    Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  45.  */
  46.  
  47.  
  48.     private static function __fieldExtract($database,$field{
  49.         $field strtoupper(PHPExcel_Calculation_Functions::flattenSingleValue($field));
  50.         $fieldNames array_map('strtoupper',array_shift($database));
  51.  
  52.         if (is_numeric($field)) {
  53.             $keys array_keys($fieldNames);
  54.             return $keys[$field-1];
  55.         }
  56.         $key array_search($field,$fieldNames);
  57.         return ($key$key null;
  58.     }
  59.  
  60.     private static function __filter($database,$criteria{
  61.         $fieldNames array_shift($database);
  62.         $criteriaNames array_shift($criteria);
  63.  
  64.         //    Convert the criteria into a set of AND/OR conditions with [:placeholders]
  65.         $testConditions $testValues array();
  66.         $testConditionsCount 0;
  67.         foreach($criteriaNames as $key => $criteriaName{
  68.             $testCondition array();
  69.             $testConditionCount 0;
  70.             foreach($criteria as $row => $criterion{
  71.                 if ($criterion[$key''{
  72.                     $testCondition['[:'.$criteriaName.']'.PHPExcel_Calculation_Functions::_ifCondition($criterion[$key]);
  73.                     $testConditionCount++;
  74.                 }
  75.             }
  76.             if ($testConditionCount 1{
  77.                 $testConditions['OR('.implode(',',$testCondition).')';
  78.                 $testConditionsCount++;
  79.             elseif($testConditionCount == 1{
  80.                 $testConditions[$testCondition[0];
  81.                 $testConditionsCount++;
  82.             }
  83.         }
  84.         if ($testConditionsCount 1{
  85.             $testConditionSet 'AND('.implode(',',$testConditions).')';
  86.         elseif($testConditionsCount == 1{
  87.             $testConditionSet $testConditions[0];
  88.         }
  89.  
  90.         //    Loop through each row of the database
  91.         foreach($database as $dataRow => $dataValues{
  92.             //    Substitute actual values from the database row for our [:placeholders]
  93.             $testConditionList $testConditionSet;
  94.             foreach($criteriaNames as $key => $criteriaName{
  95.                 $k array_search($criteriaName,$fieldNames);
  96.                 if (isset($dataValues[$k])) {
  97.                     $dataValue $dataValues[$k];
  98.                     $dataValue (is_string($dataValue)) PHPExcel_Calculation::_wrapResult(strtoupper($dataValue)) $dataValue;
  99.                     $testConditionList str_replace('[:'.$criteriaName.']',$dataValue,$testConditionList);
  100.                 }
  101.             }
  102.             //    evaluate the criteria against the row data
  103.             $result PHPExcel_Calculation::getInstance()->_calculateFormulaValue('='.$testConditionList);
  104.             //    If the row failed to meet the criteria, remove it from the database
  105.             if (!$result{
  106.                 unset($database[$dataRow]);
  107.             }
  108.         }
  109.  
  110.         return $database;
  111.     }
  112.  
  113.  
  114.     /**
  115.      *    DAVERAGE
  116.      *
  117.      */
  118.     public static function DAVERAGE($database,$field,$criteria{
  119.         $field self::__fieldExtract($database,$field);
  120.         if (is_null($field)) {
  121.             return NULL;
  122.         }
  123.  
  124.         //    reduce the database to a set of rows that match all the criteria
  125.         $database self::__filter($database,$criteria);
  126.         //    extract an array of values for the requested column
  127.         $colData array();
  128.         foreach($database as $row{
  129.             $colData[$row[$field];
  130.         }
  131.  
  132.         // Return
  133.         return PHPExcel_Calculation_Statistical::AVERAGE($colData);
  134.     }    //    function DAVERAGE()
  135.  
  136.     /**
  137.      *    DCOUNT
  138.      *
  139.      */
  140.     public static function DCOUNT($database,$field,$criteria{
  141.         $field self::__fieldExtract($database,$field);
  142.         if (is_null($field)) {
  143.             return NULL;
  144.         }
  145.  
  146.         //    reduce the database to a set of rows that match all the criteria
  147.         $database self::__filter($database,$criteria);
  148.         //    extract an array of values for the requested column
  149.         $colData array();
  150.         foreach($database as $row{
  151.             $colData[$row[$field];
  152.         }
  153.  
  154.         // Return
  155.         return PHPExcel_Calculation_Statistical::COUNT($colData);
  156.     }    //    function DCOUNT()
  157.  
  158.     /**
  159.      *    DCOUNTA
  160.      *
  161.      */
  162.     public static function DCOUNTA($database,$field,$criteria{
  163.         $field self::__fieldExtract($database,$field);
  164.         if (is_null($field)) {
  165.             return NULL;
  166.         }
  167.  
  168.         //    reduce the database to a set of rows that match all the criteria
  169.         $database self::__filter($database,$criteria);
  170.         //    extract an array of values for the requested column
  171.         $colData array();
  172.         foreach($database as $row{
  173.             $colData[$row[$field];
  174.         }
  175.  
  176.         // Return
  177.         return PHPExcel_Calculation_Statistical::COUNTA($colData);
  178.     }    //    function DCOUNTA()
  179.  
  180.     /**
  181.      *    DGET
  182.      *
  183.      */
  184.     public static function DGET($database,$field,$criteria{
  185.         $field self::__fieldExtract($database,$field);
  186.         if (is_null($field)) {
  187.             return NULL;
  188.         }
  189.  
  190.         //    reduce the database to a set of rows that match all the criteria
  191.         $database self::__filter($database,$criteria);
  192.         //    extract an array of values for the requested column
  193.         $colData array();
  194.         foreach($database as $row{
  195.             $colData[$row[$field];
  196.         }
  197.  
  198.         // Return
  199.         if (count($colData1{
  200.             return PHPExcel_Calculation_Functions::NaN();
  201.         }
  202.  
  203.         return $colData[0];
  204.     }    //    function DGET()
  205.  
  206.     /**
  207.      *    DMAX
  208.      *
  209.      */
  210.     public static function DMAX($database,$field,$criteria{
  211.         $field self::__fieldExtract($database,$field);
  212.         if (is_null($field)) {
  213.             return NULL;
  214.         }
  215.  
  216.         //    reduce the database to a set of rows that match all the criteria
  217.         $database self::__filter($database,$criteria);
  218.         //    extract an array of values for the requested column
  219.         $colData array();
  220.         foreach($database as $row{
  221.             $colData[$row[$field];
  222.         }
  223.  
  224.         // Return
  225.         return PHPExcel_Calculation_Statistical::MAX($colData);
  226.     }    //    function DMAX()
  227.  
  228.     /**
  229.      *    DMIN
  230.      *
  231.      */
  232.     public static function DMIN($database,$field,$criteria{
  233.         $field self::__fieldExtract($database,$field);
  234.         if (is_null($field)) {
  235.             return NULL;
  236.         }
  237.  
  238.         //    reduce the database to a set of rows that match all the criteria
  239.         $database self::__filter($database,$criteria);
  240.         //    extract an array of values for the requested column
  241.         $colData array();
  242.         foreach($database as $row{
  243.             $colData[$row[$field];
  244.         }
  245.  
  246.         // Return
  247.         return PHPExcel_Calculation_Statistical::MIN($colData);
  248.     }    //    function DMIN()
  249.  
  250.     /**
  251.      *    DPRODUCT
  252.      *
  253.      */
  254.     public static function DPRODUCT($database,$field,$criteria{
  255.         $field self::__fieldExtract($database,$field);
  256.         if (is_null($field)) {
  257.             return NULL;
  258.         }
  259.  
  260.         //    reduce the database to a set of rows that match all the criteria
  261.         $database self::__filter($database,$criteria);
  262.         //    extract an array of values for the requested column
  263.         $colData array();
  264.         foreach($database as $row{
  265.             $colData[$row[$field];
  266.         }
  267.  
  268.         // Return
  269.         return PHPExcel_Calculation_MathTrig::PRODUCT($colData);
  270.     }    //    function DPRODUCT()
  271.  
  272.     /**
  273.      *    DSTDEV
  274.      *
  275.      */
  276.     public static function DSTDEV($database,$field,$criteria{
  277.         $field self::__fieldExtract($database,$field);
  278.         if (is_null($field)) {
  279.             return NULL;
  280.         }
  281.  
  282.         //    reduce the database to a set of rows that match all the criteria
  283.         $database self::__filter($database,$criteria);
  284.         //    extract an array of values for the requested column
  285.         $colData array();
  286.         foreach($database as $row{
  287.             $colData[$row[$field];
  288.         }
  289.  
  290.         // Return
  291.         return PHPExcel_Calculation_Statistical::STDEV($colData);
  292.     }    //    function DSTDEV()
  293.  
  294.     /**
  295.      *    DSTDEVP
  296.      *
  297.      */
  298.     public static function DSTDEVP($database,$field,$criteria{
  299.         $field self::__fieldExtract($database,$field);
  300.         if (is_null($field)) {
  301.             return NULL;
  302.         }
  303.  
  304.         //    reduce the database to a set of rows that match all the criteria
  305.         $database self::__filter($database,$criteria);
  306.         //    extract an array of values for the requested column
  307.         $colData array();
  308.         foreach($database as $row{
  309.             $colData[$row[$field];
  310.         }
  311.  
  312.         // Return
  313.         return PHPExcel_Calculation_Statistical::STDEVP($colData);
  314.     }    //    function DSTDEVP()
  315.  
  316.     /**
  317.      *    DSUM
  318.      *
  319.      */
  320.     public static function DSUM($database,$field,$criteria{
  321.         $field self::__fieldExtract($database,$field);
  322.         if (is_null($field)) {
  323.             return NULL;
  324.         }
  325.  
  326.         //    reduce the database to a set of rows that match all the criteria
  327.         $database self::__filter($database,$criteria);
  328.         //    extract an array of values for the requested column
  329.         $colData array();
  330.         foreach($database as $row{
  331.             $colData[$row[$field];
  332.         }
  333.  
  334.         // Return
  335.         return PHPExcel_Calculation_MathTrig::SUM($colData);
  336.     }    //    function DSUM()
  337.  
  338.     /**
  339.      *    DVAR
  340.      *
  341.      */
  342.     public static function DVAR($database,$field,$criteria{
  343.         $field self::__fieldExtract($database,$field);
  344.         if (is_null($field)) {
  345.             return NULL;
  346.         }
  347.  
  348.         //    reduce the database to a set of rows that match all the criteria
  349.         $database self::__filter($database,$criteria);
  350.         //    extract an array of values for the requested column
  351.         $colData array();
  352.         foreach($database as $row{
  353.             $colData[$row[$field];
  354.         }
  355.  
  356.         // Return
  357.         return PHPExcel_Calculation_Statistical::VARFunc($colData);
  358.     }    //    function DVAR()
  359.  
  360.     /**
  361.      *    DVARP
  362.      *
  363.      */
  364.     public static function DVARP($database,$field,$criteria{
  365.         $field self::__fieldExtract($database,$field);
  366.         if (is_null($field)) {
  367.             return NULL;
  368.         }
  369.  
  370.         //    reduce the database to a set of rows that match all the criteria
  371.         $database self::__filter($database,$criteria);
  372.         //    extract an array of values for the requested column
  373.         $colData array();
  374.         foreach($database as $row{
  375.             $colData[$row[$field];
  376.         }
  377.  
  378.         // Return
  379.         return PHPExcel_Calculation_Statistical::VARP($colData);
  380.     }    //    function DVARP()
  381.  
  382.  
  383. }    //    class PHPExcel_Calculation_Database

Documentation generated on Sun, 27 Feb 2011 16:29:19 -0800 by phpDocumentor 1.4.3