1 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace PhpOffice\PhpSpreadsheet\Cell;
|
|
|
4 |
|
|
|
5 |
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
|
|
|
6 |
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
|
|
|
7 |
use PhpOffice\PhpSpreadsheet\Exception;
|
|
|
8 |
|
|
|
9 |
/**
|
|
|
10 |
* Validate a cell value according to its validation rules.
|
|
|
11 |
*/
|
|
|
12 |
class DataValidator
|
|
|
13 |
{
|
|
|
14 |
/**
|
|
|
15 |
* Does this cell contain valid value?
|
|
|
16 |
*
|
|
|
17 |
* @param Cell $cell Cell to check the value
|
|
|
18 |
*
|
|
|
19 |
* @return bool
|
|
|
20 |
*/
|
|
|
21 |
public function isValid(Cell $cell)
|
|
|
22 |
{
|
|
|
23 |
if (!$cell->hasDataValidation() || $cell->getDataValidation()->getType() === DataValidation::TYPE_NONE) {
|
|
|
24 |
return true;
|
|
|
25 |
}
|
|
|
26 |
|
|
|
27 |
$cellValue = $cell->getValue();
|
|
|
28 |
$dataValidation = $cell->getDataValidation();
|
|
|
29 |
|
|
|
30 |
if (!$dataValidation->getAllowBlank() && ($cellValue === null || $cellValue === '')) {
|
|
|
31 |
return false;
|
|
|
32 |
}
|
|
|
33 |
|
|
|
34 |
$returnValue = false;
|
|
|
35 |
$type = $dataValidation->getType();
|
|
|
36 |
if ($type === DataValidation::TYPE_LIST) {
|
|
|
37 |
$returnValue = $this->isValueInList($cell);
|
|
|
38 |
} elseif ($type === DataValidation::TYPE_WHOLE) {
|
|
|
39 |
if (!is_numeric($cellValue) || fmod((float) $cellValue, 1) != 0) {
|
|
|
40 |
$returnValue = false;
|
|
|
41 |
} else {
|
|
|
42 |
$returnValue = $this->numericOperator($dataValidation, (int) $cellValue);
|
|
|
43 |
}
|
|
|
44 |
} elseif ($type === DataValidation::TYPE_DECIMAL || $type === DataValidation::TYPE_DATE || $type === DataValidation::TYPE_TIME) {
|
|
|
45 |
if (!is_numeric($cellValue)) {
|
|
|
46 |
$returnValue = false;
|
|
|
47 |
} else {
|
|
|
48 |
$returnValue = $this->numericOperator($dataValidation, (float) $cellValue);
|
|
|
49 |
}
|
|
|
50 |
} elseif ($type === DataValidation::TYPE_TEXTLENGTH) {
|
|
|
51 |
$returnValue = $this->numericOperator($dataValidation, mb_strlen((string) $cellValue));
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
return $returnValue;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
/** @param float|int $cellValue */
|
|
|
58 |
private function numericOperator(DataValidation $dataValidation, $cellValue): bool
|
|
|
59 |
{
|
|
|
60 |
$operator = $dataValidation->getOperator();
|
|
|
61 |
$formula1 = $dataValidation->getFormula1();
|
|
|
62 |
$formula2 = $dataValidation->getFormula2();
|
|
|
63 |
$returnValue = false;
|
|
|
64 |
if ($operator === DataValidation::OPERATOR_BETWEEN) {
|
|
|
65 |
$returnValue = $cellValue >= $formula1 && $cellValue <= $formula2;
|
|
|
66 |
} elseif ($operator === DataValidation::OPERATOR_NOTBETWEEN) {
|
|
|
67 |
$returnValue = $cellValue < $formula1 || $cellValue > $formula2;
|
|
|
68 |
} elseif ($operator === DataValidation::OPERATOR_EQUAL) {
|
|
|
69 |
$returnValue = $cellValue == $formula1;
|
|
|
70 |
} elseif ($operator === DataValidation::OPERATOR_NOTEQUAL) {
|
|
|
71 |
$returnValue = $cellValue != $formula1;
|
|
|
72 |
} elseif ($operator === DataValidation::OPERATOR_LESSTHAN) {
|
|
|
73 |
$returnValue = $cellValue < $formula1;
|
|
|
74 |
} elseif ($operator === DataValidation::OPERATOR_LESSTHANOREQUAL) {
|
|
|
75 |
$returnValue = $cellValue <= $formula1;
|
|
|
76 |
} elseif ($operator === DataValidation::OPERATOR_GREATERTHAN) {
|
|
|
77 |
$returnValue = $cellValue > $formula1;
|
|
|
78 |
} elseif ($operator === DataValidation::OPERATOR_GREATERTHANOREQUAL) {
|
|
|
79 |
$returnValue = $cellValue >= $formula1;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
return $returnValue;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* Does this cell contain valid value, based on list?
|
|
|
87 |
*
|
|
|
88 |
* @param Cell $cell Cell to check the value
|
|
|
89 |
*
|
|
|
90 |
* @return bool
|
|
|
91 |
*/
|
|
|
92 |
private function isValueInList(Cell $cell)
|
|
|
93 |
{
|
|
|
94 |
$cellValue = $cell->getValue();
|
|
|
95 |
$dataValidation = $cell->getDataValidation();
|
|
|
96 |
|
|
|
97 |
$formula1 = $dataValidation->getFormula1();
|
|
|
98 |
if (!empty($formula1)) {
|
|
|
99 |
// inline values list
|
|
|
100 |
if ($formula1[0] === '"') {
|
|
|
101 |
return in_array(strtolower($cellValue), explode(',', strtolower(trim($formula1, '"'))), true);
|
|
|
102 |
} elseif (strpos($formula1, ':') > 0) {
|
|
|
103 |
// values list cells
|
|
|
104 |
$matchFormula = '=MATCH(' . $cell->getCoordinate() . ', ' . $formula1 . ', 0)';
|
|
|
105 |
$calculation = Calculation::getInstance($cell->getWorksheet()->getParent());
|
|
|
106 |
|
|
|
107 |
try {
|
|
|
108 |
$result = $calculation->calculateFormula($matchFormula, $cell->getCoordinate(), $cell);
|
|
|
109 |
while (is_array($result)) {
|
|
|
110 |
$result = array_pop($result);
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
return $result !== ExcelError::NA();
|
|
|
114 |
} catch (Exception $ex) {
|
|
|
115 |
return false;
|
|
|
116 |
}
|
|
|
117 |
}
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
return true;
|
|
|
121 |
}
|
|
|
122 |
}
|