Skip to content

Commit 3cec90d

Browse files
author
MarkBaker
committed
Minor refactoring, and additional unit tests (including exceptions) for Cell's setValueExplicit() method
1 parent 66e63eb commit 3cec90d

File tree

4 files changed

+62
-15
lines changed

4 files changed

+62
-15
lines changed

src/PhpSpreadsheet/Cell/Cell.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
namespace PhpOffice\PhpSpreadsheet\Cell;
44

5-
use DateTime;
65
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
76
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
87
use PhpOffice\PhpSpreadsheet\Collection\Cells;
98
use PhpOffice\PhpSpreadsheet\Exception;
109
use PhpOffice\PhpSpreadsheet\RichText\RichText;
11-
use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDate;
1210
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\CellStyleAssessor;
1311
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
1412
use PhpOffice\PhpSpreadsheet\Style\Style;
@@ -213,7 +211,8 @@ protected function formulaAttributes(bool $isArrayFormula, ?string $arrayFormula
213211
}
214212

215213
/**
216-
* Set the value for a cell, with the explicit data type passed to the method (bypassing any use of the value binder).
214+
* Set the value for a cell,
215+
* with the explicit data type passed to the method (bypassing any use of the value binders).
217216
*
218217
* @param mixed $value Value
219218
* @param string $dataType Explicit data type, see DataType::TYPE_*
@@ -250,6 +249,7 @@ public function setValueExplicit($value, $dataType, bool $isArrayFormula = false
250249
$dataType = DataType::TYPE_STRING;
251250
if (in_array($value, Calculation::$excelConstants, true)) {
252251
$value = array_search($value, Calculation::$excelConstants, true);
252+
$dataType = $value === null ? DataType::TYPE_NULL : DataType::TYPE_BOOL;
253253
}
254254
$value = (string) $value;
255255
$this->formulaAttributes = [];
@@ -264,18 +264,7 @@ public function setValueExplicit($value, $dataType, bool $isArrayFormula = false
264264

265265
break;
266266
case DataType::TYPE_ISO_DATE:
267-
if (!is_string($value)) {
268-
throw new Exception('Non-string supplied for datatype Date');
269-
}
270-
$date = new DateTime($value);
271-
$newValue = SharedDate::PHPToExcel($date);
272-
if ($newValue === false) {
273-
throw new Exception("Invalid string $value supplied for datatype Date");
274-
}
275-
if (preg_match('/^\\d\\d:\\d\\d:\\d\\d/', $value) == 1) {
276-
$newValue = fmod($newValue, 1.0);
277-
}
278-
$this->value = $newValue;
267+
$this->value = DataType::checkIsoDate($value);
279268
$dataType = DataType::TYPE_NUMERIC;
280269

281270
break;

src/PhpSpreadsheet/Cell/DataType.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace PhpOffice\PhpSpreadsheet\Cell;
44

5+
use DateTime;
6+
use PhpOffice\PhpSpreadsheet\Exception;
57
use PhpOffice\PhpSpreadsheet\RichText\RichText;
8+
use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDate;
69
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
710

811
class DataType
@@ -83,4 +86,33 @@ public static function checkErrorCode($value)
8386

8487
return $value;
8588
}
89+
90+
/**
91+
* @param mixed $value
92+
*
93+
* @return float|int
94+
*/
95+
public static function checkIsoDate($value)
96+
{
97+
if (!is_string($value)) {
98+
throw new Exception('Non-string supplied for datatype Date');
99+
}
100+
101+
try {
102+
$date = new DateTime($value);
103+
$newValue = SharedDate::PHPToExcel($date);
104+
} catch (\Exception $e) {
105+
throw new Exception("Invalid string $value supplied for datatype Date");
106+
}
107+
108+
if ($newValue === false) {
109+
throw new Exception("Invalid string $value supplied for datatype Date");
110+
}
111+
112+
if (preg_match('/^\\d\\d:\\d\\d:\\d\\d/', $value) == 1) {
113+
$newValue = fmod($newValue, 1.0);
114+
}
115+
116+
return $newValue;
117+
}
86118
}

tests/data/Cell/SetValueExplicit.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@
3838
true,
3939
DataType::TYPE_NUMERIC,
4040
],
41+
[
42+
'=SUM(A1:A10)',
43+
'=SUM(A1:A10)',
44+
DataType::TYPE_FORMULA,
45+
],
46+
[
47+
44596.5627893519,
48+
'2022-02-04 13:30:25',
49+
DataType::TYPE_ISO_DATE,
50+
],
4151
[
4252
'#DIV/0!',
4353
'#DIV/0!',

tests/data/Cell/SetValueExplicitException.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,20 @@
77
'XYZ',
88
DataType::TYPE_NUMERIC,
99
],
10+
[
11+
44596,
12+
DataType::TYPE_ISO_DATE,
13+
],
14+
[
15+
false,
16+
DataType::TYPE_ISO_DATE,
17+
],
18+
[
19+
'ABCD-EF-GH II:JJ:KK',
20+
DataType::TYPE_ISO_DATE,
21+
],
22+
[
23+
1234,
24+
'INVALID DATATYPE',
25+
],
1026
];

0 commit comments

Comments
 (0)