Skip to content

Commit 6632117

Browse files
committed
Tweaks to DataValidator
1 parent 6938fcb commit 6632117

File tree

1 file changed

+34
-42
lines changed

1 file changed

+34
-42
lines changed

src/PhpSpreadsheet/Cell/DataValidator.php

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -52,63 +52,55 @@ public function isValid(Cell $cell): bool
5252
return $returnValue;
5353
}
5454

55-
private function numericOperator(DataValidation $dataValidation, int|float $cellValue, Cell $cell): bool
55+
private const TWO_FORMULAS = [DataValidation::OPERATOR_BETWEEN, DataValidation::OPERATOR_NOTBETWEEN];
56+
57+
private static function evaluateNumericFormula(mixed $formula, Cell $cell): mixed
5658
{
57-
$calculation = null;
58-
$operator = $dataValidation->getOperator();
59-
$formula1 = $dataValidation->getFormula1();
60-
if (!is_numeric($formula1)) {
59+
if (!is_numeric($formula)) {
6160
$calculation = Calculation::getInstance($cell->getWorksheet()->getParent());
6261

6362
try {
6463
$result = $calculation
65-
->calculateFormula("=$formula1", $cell->getCoordinate(), $cell);
64+
->calculateFormula("=$formula", $cell->getCoordinate(), $cell);
6665
while (is_array($result)) {
6766
$result = array_pop($result);
6867
}
69-
$formula1 = $result;
68+
$formula = $result;
7069
} catch (Exception) {
7170
// do nothing
7271
}
7372
}
74-
$formula2 = 0;
75-
if ($operator === DataValidation::OPERATOR_BETWEEN || $operator === DataValidation::OPERATOR_NOTBETWEEN) {
76-
$formula2 = $dataValidation->getFormula2();
77-
if (!is_numeric($formula2)) {
78-
$calculation ??= Calculation::getInstance($cell->getWorksheet()->getParent());
7973

80-
try {
81-
$result = $calculation
82-
->calculateFormula("=$formula2", $cell->getCoordinate(), $cell);
83-
while (is_array($result)) {
84-
$result = array_pop($result);
85-
}
86-
$formula2 = $result;
87-
} catch (Exception) {
88-
// do nothing
89-
}
90-
}
91-
}
92-
$returnValue = false;
93-
if ($operator === DataValidation::OPERATOR_BETWEEN) {
94-
$returnValue = $cellValue >= $formula1 && $cellValue <= $formula2;
95-
} elseif ($operator === DataValidation::OPERATOR_NOTBETWEEN) {
96-
$returnValue = $cellValue < $formula1 || $cellValue > $formula2;
97-
} elseif ($operator === DataValidation::OPERATOR_EQUAL) {
98-
$returnValue = $cellValue == $formula1;
99-
} elseif ($operator === DataValidation::OPERATOR_NOTEQUAL) {
100-
$returnValue = $cellValue != $formula1;
101-
} elseif ($operator === DataValidation::OPERATOR_LESSTHAN) {
102-
$returnValue = $cellValue < $formula1;
103-
} elseif ($operator === DataValidation::OPERATOR_LESSTHANOREQUAL) {
104-
$returnValue = $cellValue <= $formula1;
105-
} elseif ($operator === DataValidation::OPERATOR_GREATERTHAN) {
106-
$returnValue = $cellValue > $formula1;
107-
} elseif ($operator === DataValidation::OPERATOR_GREATERTHANOREQUAL) {
108-
$returnValue = $cellValue >= $formula1;
74+
return $formula;
75+
}
76+
77+
private function numericOperator(DataValidation $dataValidation, int|float $cellValue, Cell $cell): bool
78+
{
79+
$operator = $dataValidation->getOperator();
80+
$formula1 = self::evaluateNumericFormula(
81+
$dataValidation->getFormula1(),
82+
$cell
83+
);
84+
85+
$formula2 = 0;
86+
if (in_array($operator, self::TWO_FORMULAS, true)) {
87+
$formula2 = self::evaluateNumericFormula(
88+
$dataValidation->getFormula2(),
89+
$cell
90+
);
10991
}
11092

111-
return $returnValue;
93+
return match ($operator) {
94+
DataValidation::OPERATOR_BETWEEN => $cellValue >= $formula1 && $cellValue <= $formula2,
95+
DataValidation::OPERATOR_NOTBETWEEN => $cellValue < $formula1 || $cellValue > $formula2,
96+
DataValidation::OPERATOR_EQUAL => $cellValue == $formula1,
97+
DataValidation::OPERATOR_NOTEQUAL => $cellValue != $formula1,
98+
DataValidation::OPERATOR_LESSTHAN => $cellValue < $formula1,
99+
DataValidation::OPERATOR_LESSTHANOREQUAL => $cellValue <= $formula1,
100+
DataValidation::OPERATOR_GREATERTHAN => $cellValue > $formula1,
101+
DataValidation::OPERATOR_GREATERTHANOREQUAL => $cellValue >= $formula1,
102+
default => false,
103+
};
112104
}
113105

114106
/**

0 commit comments

Comments
 (0)