| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 | <?phpnamespace PhpOffice\PhpSpreadsheet\Calculation;class Logical{    /**     * TRUE.     *     * Returns the boolean TRUE.     *     * Excel Function:     *        =TRUE()     *     * @category Logical Functions     *     * @return bool True     */    public static function true()    {        return true;    }    /**     * FALSE.     *     * Returns the boolean FALSE.     *     * Excel Function:     *        =FALSE()     *     * @category Logical Functions     *     * @return bool False     */    public static function false()    {        return false;    }    private static function countTrueValues(array $args)    {        $returnValue = 0;        foreach ($args as $arg) {            // Is it a boolean value?            if (is_bool($arg)) {                $returnValue += $arg;            } elseif ((is_numeric($arg)) && (!is_string($arg))) {                $returnValue += ((int) $arg != 0);            } elseif (is_string($arg)) {                $arg = strtoupper($arg);                if (($arg == 'TRUE') || ($arg == Calculation::getTRUE())) {                    $arg = true;                } elseif (($arg == 'FALSE') || ($arg == Calculation::getFALSE())) {                    $arg = false;                } else {                    return Functions::VALUE();                }                $returnValue += ($arg != 0);            }        }        return $returnValue;    }    /**     * LOGICAL_AND.     *     * Returns boolean TRUE if all its arguments are TRUE; returns FALSE if one or more argument is FALSE.     *     * Excel Function:     *        =AND(logical1[,logical2[, ...]])     *     *        The arguments must evaluate to logical values such as TRUE or FALSE, or the arguments must be arrays     *            or references that contain logical values.     *     *        Boolean arguments are treated as True or False as appropriate     *        Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False     *        If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string holds     *            the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value     *     * @category Logical Functions     *     * @param mixed ...$args Data values     *     * @return bool|string the logical AND of the arguments     */    public static function logicalAnd(...$args)    {        $args = Functions::flattenArray($args);        if (count($args) == 0) {            return Functions::VALUE();        }        $args = array_filter($args, function ($value) {            return $value !== null || (is_string($value) && trim($value) == '');        });        $argCount = count($args);        $returnValue = self::countTrueValues($args);        if (is_string($returnValue)) {            return $returnValue;        }        return ($returnValue > 0) && ($returnValue == $argCount);    }    /**     * LOGICAL_OR.     *     * Returns boolean TRUE if any argument is TRUE; returns FALSE if all arguments are FALSE.     *     * Excel Function:     *        =OR(logical1[,logical2[, ...]])     *     *        The arguments must evaluate to logical values such as TRUE or FALSE, or the arguments must be arrays     *            or references that contain logical values.     *     *        Boolean arguments are treated as True or False as appropriate     *        Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False     *        If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string holds     *            the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value     *     * @category Logical Functions     *     * @param mixed $args Data values     *     * @return bool|string the logical OR of the arguments     */    public static function logicalOr(...$args)    {        $args = Functions::flattenArray($args);        if (count($args) == 0) {            return Functions::VALUE();        }        $args = array_filter($args, function ($value) {            return $value !== null || (is_string($value) && trim($value) == '');        });        $returnValue = self::countTrueValues($args);        if (is_string($returnValue)) {            return $returnValue;        }        return $returnValue > 0;    }    /**     * LOGICAL_XOR.     *     * Returns the Exclusive Or logical operation for one or more supplied conditions.     * i.e. the Xor function returns TRUE if an odd number of the supplied conditions evaluate to TRUE, and FALSE otherwise.     *     * Excel Function:     *        =XOR(logical1[,logical2[, ...]])     *     *        The arguments must evaluate to logical values such as TRUE or FALSE, or the arguments must be arrays     *            or references that contain logical values.     *     *        Boolean arguments are treated as True or False as appropriate     *        Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False     *        If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string holds     *            the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value     *     * @category Logical Functions     *     * @param mixed $args Data values     *     * @return bool|string the logical XOR of the arguments     */    public static function logicalXor(...$args)    {        $args = Functions::flattenArray($args);        if (count($args) == 0) {            return Functions::VALUE();        }        $args = array_filter($args, function ($value) {            return $value !== null || (is_string($value) && trim($value) == '');        });        $returnValue = self::countTrueValues($args);        if (is_string($returnValue)) {            return $returnValue;        }        return $returnValue % 2 == 1;    }    /**     * NOT.     *     * Returns the boolean inverse of the argument.     *     * Excel Function:     *        =NOT(logical)     *     *        The argument must evaluate to a logical value such as TRUE or FALSE     *     *        Boolean arguments are treated as True or False as appropriate     *        Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False     *        If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string holds     *            the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value     *     * @category Logical Functions     *     * @param mixed $logical A value or expression that can be evaluated to TRUE or FALSE     *     * @return bool|string the boolean inverse of the argument     */    public static function NOT($logical = false)    {        $logical = Functions::flattenSingleValue($logical);        if (is_string($logical)) {            $logical = strtoupper($logical);            if (($logical == 'TRUE') || ($logical == Calculation::getTRUE())) {                return false;            } elseif (($logical == 'FALSE') || ($logical == Calculation::getFALSE())) {                return true;            }            return Functions::VALUE();        }        return !$logical;    }    /**     * STATEMENT_IF.     *     * Returns one value if a condition you specify evaluates to TRUE and another value if it evaluates to FALSE.     *     * Excel Function:     *        =IF(condition[,returnIfTrue[,returnIfFalse]])     *     *        Condition is any value or expression that can be evaluated to TRUE or FALSE.     *            For example, A10=100 is a logical expression; if the value in cell A10 is equal to 100,     *            the expression evaluates to TRUE. Otherwise, the expression evaluates to FALSE.     *            This argument can use any comparison calculation operator.     *        ReturnIfTrue is the value that is returned if condition evaluates to TRUE.     *            For example, if this argument is the text string "Within budget" and the condition argument evaluates to TRUE,     *            then the IF function returns the text "Within budget"     *            If condition is TRUE and ReturnIfTrue is blank, this argument returns 0 (zero). To display the word TRUE, use     *            the logical value TRUE for this argument.     *            ReturnIfTrue can be another formula.     *        ReturnIfFalse is the value that is returned if condition evaluates to FALSE.     *            For example, if this argument is the text string "Over budget" and the condition argument evaluates to FALSE,     *            then the IF function returns the text "Over budget".     *            If condition is FALSE and ReturnIfFalse is omitted, then the logical value FALSE is returned.     *            If condition is FALSE and ReturnIfFalse is blank, then the value 0 (zero) is returned.     *            ReturnIfFalse can be another formula.     *     * @category Logical Functions     *     * @param mixed $condition Condition to evaluate     * @param mixed $returnIfTrue Value to return when condition is true     * @param mixed $returnIfFalse Optional value to return when condition is false     *     * @return mixed The value of returnIfTrue or returnIfFalse determined by condition     */    public static function statementIf($condition = true, $returnIfTrue = 0, $returnIfFalse = false)    {        $condition = ($condition === null) ? true : (bool) Functions::flattenSingleValue($condition);        $returnIfTrue = ($returnIfTrue === null) ? 0 : Functions::flattenSingleValue($returnIfTrue);        $returnIfFalse = ($returnIfFalse === null) ? false : Functions::flattenSingleValue($returnIfFalse);        return ($condition) ? $returnIfTrue : $returnIfFalse;    }    /**     * IFERROR.     *     * Excel Function:     *        =IFERROR(testValue,errorpart)     *     * @category Logical Functions     *     * @param mixed $testValue Value to check, is also the value returned when no error     * @param mixed $errorpart Value to return when testValue is an error condition     *     * @return mixed The value of errorpart or testValue determined by error condition     */    public static function IFERROR($testValue = '', $errorpart = '')    {        $testValue = ($testValue === null) ? '' : Functions::flattenSingleValue($testValue);        $errorpart = ($errorpart === null) ? '' : Functions::flattenSingleValue($errorpart);        return self::statementIf(Functions::isError($testValue), $errorpart, $testValue);    }}
 |