[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/phpexcel/PHPExcel/Calculation/ -> Logical.php (source)

   1  <?php
   2  
   3  /** PHPExcel root directory */
   4  if (!defined('PHPEXCEL_ROOT')) {
   5      /**
   6       * @ignore
   7       */
   8      define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
   9      require (PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
  10  }
  11  
  12  /**
  13   * PHPExcel_Calculation_Logical
  14   *
  15   * Copyright (c) 2006 - 2015 PHPExcel
  16   *
  17   * This library is free software; you can redistribute it and/or
  18   * modify it under the terms of the GNU Lesser General Public
  19   * License as published by the Free Software Foundation; either
  20   * version 2.1 of the License, or (at your option) any later version.
  21   *
  22   * This library is distributed in the hope that it will be useful,
  23   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  25   * Lesser General Public License for more details.
  26   *
  27   * You should have received a copy of the GNU Lesser General Public
  28   * License along with this library; if not, write to the Free Software
  29   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  30   *
  31   * @category    PHPExcel
  32   * @package        PHPExcel_Calculation
  33   * @copyright    Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  34   * @license        http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  35   * @version        ##VERSION##, ##DATE##
  36   */
  37  class PHPExcel_Calculation_Logical
  38  {
  39      /**
  40       * TRUE
  41       *
  42       * Returns the boolean TRUE.
  43       *
  44       * Excel Function:
  45       *        =TRUE()
  46       *
  47       * @access    public
  48       * @category Logical Functions
  49       * @return    boolean        True
  50       */
  51      public static function TRUE()
  52      {
  53          return true;
  54      }
  55  
  56  
  57      /**
  58       * FALSE
  59       *
  60       * Returns the boolean FALSE.
  61       *
  62       * Excel Function:
  63       *        =FALSE()
  64       *
  65       * @access    public
  66       * @category Logical Functions
  67       * @return    boolean        False
  68       */
  69      public static function FALSE()
  70      {
  71          return false;
  72      }
  73  
  74  
  75      /**
  76       * LOGICAL_AND
  77       *
  78       * Returns boolean TRUE if all its arguments are TRUE; returns FALSE if one or more argument is FALSE.
  79       *
  80       * Excel Function:
  81       *        =AND(logical1[,logical2[, ...]])
  82       *
  83       *        The arguments must evaluate to logical values such as TRUE or FALSE, or the arguments must be arrays
  84       *            or references that contain logical values.
  85       *
  86       *        Boolean arguments are treated as True or False as appropriate
  87       *        Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False
  88       *        If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string holds
  89       *            the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value
  90       *
  91       * @access    public
  92       * @category Logical Functions
  93       * @param    mixed        $arg,...        Data values
  94       * @return    boolean        The logical AND of the arguments.
  95       */
  96      public static function LOGICAL_AND()
  97      {
  98          // Return value
  99          $returnValue = true;
 100  
 101          // Loop through the arguments
 102          $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
 103          $argCount = -1;
 104          foreach ($aArgs as $argCount => $arg) {
 105              // Is it a boolean value?
 106              if (is_bool($arg)) {
 107                  $returnValue = $returnValue && $arg;
 108              } elseif ((is_numeric($arg)) && (!is_string($arg))) {
 109                  $returnValue = $returnValue && ($arg != 0);
 110              } elseif (is_string($arg)) {
 111                  $arg = strtoupper($arg);
 112                  if (($arg == 'TRUE') || ($arg == PHPExcel_Calculation::getTRUE())) {
 113                      $arg = true;
 114                  } elseif (($arg == 'FALSE') || ($arg == PHPExcel_Calculation::getFALSE())) {
 115                      $arg = false;
 116                  } else {
 117                      return PHPExcel_Calculation_Functions::VALUE();
 118                  }
 119                  $returnValue = $returnValue && ($arg != 0);
 120              }
 121          }
 122  
 123          // Return
 124          if ($argCount < 0) {
 125              return PHPExcel_Calculation_Functions::VALUE();
 126          }
 127          return $returnValue;
 128      }
 129  
 130  
 131      /**
 132       * LOGICAL_OR
 133       *
 134       * Returns boolean TRUE if any argument is TRUE; returns FALSE if all arguments are FALSE.
 135       *
 136       * Excel Function:
 137       *        =OR(logical1[,logical2[, ...]])
 138       *
 139       *        The arguments must evaluate to logical values such as TRUE or FALSE, or the arguments must be arrays
 140       *            or references that contain logical values.
 141       *
 142       *        Boolean arguments are treated as True or False as appropriate
 143       *        Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False
 144       *        If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string holds
 145       *            the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value
 146       *
 147       * @access    public
 148       * @category Logical Functions
 149       * @param    mixed        $arg,...        Data values
 150       * @return    boolean        The logical OR of the arguments.
 151       */
 152      public static function LOGICAL_OR()
 153      {
 154          // Return value
 155          $returnValue = false;
 156  
 157          // Loop through the arguments
 158          $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
 159          $argCount = -1;
 160          foreach ($aArgs as $argCount => $arg) {
 161              // Is it a boolean value?
 162              if (is_bool($arg)) {
 163                  $returnValue = $returnValue || $arg;
 164              } elseif ((is_numeric($arg)) && (!is_string($arg))) {
 165                  $returnValue = $returnValue || ($arg != 0);
 166              } elseif (is_string($arg)) {
 167                  $arg = strtoupper($arg);
 168                  if (($arg == 'TRUE') || ($arg == PHPExcel_Calculation::getTRUE())) {
 169                      $arg = true;
 170                  } elseif (($arg == 'FALSE') || ($arg == PHPExcel_Calculation::getFALSE())) {
 171                      $arg = false;
 172                  } else {
 173                      return PHPExcel_Calculation_Functions::VALUE();
 174                  }
 175                  $returnValue = $returnValue || ($arg != 0);
 176              }
 177          }
 178  
 179          // Return
 180          if ($argCount < 0) {
 181              return PHPExcel_Calculation_Functions::VALUE();
 182          }
 183          return $returnValue;
 184      }
 185  
 186  
 187      /**
 188       * NOT
 189       *
 190       * Returns the boolean inverse of the argument.
 191       *
 192       * Excel Function:
 193       *        =NOT(logical)
 194       *
 195       *        The argument must evaluate to a logical value such as TRUE or FALSE
 196       *
 197       *        Boolean arguments are treated as True or False as appropriate
 198       *        Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False
 199       *        If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string holds
 200       *            the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value
 201       *
 202       * @access    public
 203       * @category Logical Functions
 204       * @param    mixed        $logical    A value or expression that can be evaluated to TRUE or FALSE
 205       * @return    boolean        The boolean inverse of the argument.
 206       */
 207      public static function NOT($logical = false)
 208      {
 209          $logical = PHPExcel_Calculation_Functions::flattenSingleValue($logical);
 210          if (is_string($logical)) {
 211              $logical = strtoupper($logical);
 212              if (($logical == 'TRUE') || ($logical == PHPExcel_Calculation::getTRUE())) {
 213                  return false;
 214              } elseif (($logical == 'FALSE') || ($logical == PHPExcel_Calculation::getFALSE())) {
 215                  return true;
 216              } else {
 217                  return PHPExcel_Calculation_Functions::VALUE();
 218              }
 219          }
 220  
 221          return !$logical;
 222      }
 223  
 224      /**
 225       * STATEMENT_IF
 226       *
 227       * Returns one value if a condition you specify evaluates to TRUE and another value if it evaluates to FALSE.
 228       *
 229       * Excel Function:
 230       *        =IF(condition[,returnIfTrue[,returnIfFalse]])
 231       *
 232       *        Condition is any value or expression that can be evaluated to TRUE or FALSE.
 233       *            For example, A10=100 is a logical expression; if the value in cell A10 is equal to 100,
 234       *            the expression evaluates to TRUE. Otherwise, the expression evaluates to FALSE.
 235       *            This argument can use any comparison calculation operator.
 236       *        ReturnIfTrue is the value that is returned if condition evaluates to TRUE.
 237       *            For example, if this argument is the text string "Within budget" and the condition argument evaluates to TRUE,
 238       *            then the IF function returns the text "Within budget"
 239       *            If condition is TRUE and ReturnIfTrue is blank, this argument returns 0 (zero). To display the word TRUE, use
 240       *            the logical value TRUE for this argument.
 241       *            ReturnIfTrue can be another formula.
 242       *        ReturnIfFalse is the value that is returned if condition evaluates to FALSE.
 243       *            For example, if this argument is the text string "Over budget" and the condition argument evaluates to FALSE,
 244       *            then the IF function returns the text "Over budget".
 245       *            If condition is FALSE and ReturnIfFalse is omitted, then the logical value FALSE is returned.
 246       *            If condition is FALSE and ReturnIfFalse is blank, then the value 0 (zero) is returned.
 247       *            ReturnIfFalse can be another formula.
 248       *
 249       * @access    public
 250       * @category Logical Functions
 251       * @param    mixed    $condition        Condition to evaluate
 252       * @param    mixed    $returnIfTrue    Value to return when condition is true
 253       * @param    mixed    $returnIfFalse    Optional value to return when condition is false
 254       * @return    mixed    The value of returnIfTrue or returnIfFalse determined by condition
 255       */
 256      public static function STATEMENT_IF($condition = true, $returnIfTrue = 0, $returnIfFalse = false)
 257      {
 258          $condition     = (is_null($condition))     ? true :  (boolean) PHPExcel_Calculation_Functions::flattenSingleValue($condition);
 259          $returnIfTrue  = (is_null($returnIfTrue))  ? 0 :     PHPExcel_Calculation_Functions::flattenSingleValue($returnIfTrue);
 260          $returnIfFalse = (is_null($returnIfFalse)) ? false : PHPExcel_Calculation_Functions::flattenSingleValue($returnIfFalse);
 261  
 262          return ($condition) ? $returnIfTrue : $returnIfFalse;
 263      }
 264  
 265  
 266      /**
 267       * IFERROR
 268       *
 269       * Excel Function:
 270       *        =IFERROR(testValue,errorpart)
 271       *
 272       * @access    public
 273       * @category Logical Functions
 274       * @param    mixed    $testValue    Value to check, is also the value returned when no error
 275       * @param    mixed    $errorpart    Value to return when testValue is an error condition
 276       * @return    mixed    The value of errorpart or testValue determined by error condition
 277       */
 278      public static function IFERROR($testValue = '', $errorpart = '')
 279      {
 280          $testValue = (is_null($testValue)) ? '' : PHPExcel_Calculation_Functions::flattenSingleValue($testValue);
 281          $errorpart = (is_null($errorpart)) ? '' : PHPExcel_Calculation_Functions::flattenSingleValue($errorpart);
 282  
 283          return self::STATEMENT_IF(PHPExcel_Calculation_Functions::IS_ERROR($testValue), $errorpart, $testValue);
 284      }
 285  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1