@@ -52,63 +52,55 @@ public function isValid(Cell $cell): bool
52
52
return $ returnValue ;
53
53
}
54
54
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
56
58
{
57
- $ calculation = null ;
58
- $ operator = $ dataValidation ->getOperator ();
59
- $ formula1 = $ dataValidation ->getFormula1 ();
60
- if (!is_numeric ($ formula1 )) {
59
+ if (!is_numeric ($ formula )) {
61
60
$ calculation = Calculation::getInstance ($ cell ->getWorksheet ()->getParent ());
62
61
63
62
try {
64
63
$ result = $ calculation
65
- ->calculateFormula ("= $ formula1 " , $ cell ->getCoordinate (), $ cell );
64
+ ->calculateFormula ("= $ formula " , $ cell ->getCoordinate (), $ cell );
66
65
while (is_array ($ result )) {
67
66
$ result = array_pop ($ result );
68
67
}
69
- $ formula1 = $ result ;
68
+ $ formula = $ result ;
70
69
} catch (Exception ) {
71
70
// do nothing
72
71
}
73
72
}
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 ());
79
73
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
+ );
109
91
}
110
92
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
+ };
112
104
}
113
105
114
106
/**
0 commit comments