| 1441 |
ariadna |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace PhpOffice\PhpSpreadsheet\Cell;
|
|
|
4 |
|
|
|
5 |
use PhpOffice\PhpSpreadsheet\RichText\RichText;
|
|
|
6 |
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
|
|
|
7 |
use Stringable;
|
|
|
8 |
|
|
|
9 |
class DataType
|
|
|
10 |
{
|
|
|
11 |
// Data types
|
|
|
12 |
const TYPE_STRING2 = 'str';
|
|
|
13 |
const TYPE_STRING = 's';
|
|
|
14 |
const TYPE_FORMULA = 'f';
|
|
|
15 |
const TYPE_NUMERIC = 'n';
|
|
|
16 |
const TYPE_BOOL = 'b';
|
|
|
17 |
const TYPE_NULL = 'null';
|
|
|
18 |
const TYPE_INLINE = 'inlineStr';
|
|
|
19 |
const TYPE_ERROR = 'e';
|
|
|
20 |
const TYPE_ISO_DATE = 'd';
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* List of error codes.
|
|
|
24 |
*
|
|
|
25 |
* @var array<string, int>
|
|
|
26 |
*/
|
|
|
27 |
private static array $errorCodes = [
|
|
|
28 |
'#NULL!' => 0,
|
|
|
29 |
'#DIV/0!' => 1,
|
|
|
30 |
'#VALUE!' => 2,
|
|
|
31 |
'#REF!' => 3,
|
|
|
32 |
'#NAME?' => 4,
|
|
|
33 |
'#NUM!' => 5,
|
|
|
34 |
'#N/A' => 6,
|
|
|
35 |
'#CALC!' => 7,
|
|
|
36 |
];
|
|
|
37 |
|
|
|
38 |
public const MAX_STRING_LENGTH = 32767;
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Get list of error codes.
|
|
|
42 |
*
|
|
|
43 |
* @return array<string, int>
|
|
|
44 |
*/
|
|
|
45 |
public static function getErrorCodes(): array
|
|
|
46 |
{
|
|
|
47 |
return self::$errorCodes;
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Check a string that it satisfies Excel requirements.
|
|
|
52 |
*
|
|
|
53 |
* @param null|RichText|string $textValue Value to sanitize to an Excel string
|
|
|
54 |
*
|
|
|
55 |
* @return RichText|string Sanitized value
|
|
|
56 |
*/
|
|
|
57 |
public static function checkString(null|RichText|string $textValue): RichText|string
|
|
|
58 |
{
|
|
|
59 |
if ($textValue instanceof RichText) {
|
|
|
60 |
// TODO: Sanitize Rich-Text string (max. character count is 32,767)
|
|
|
61 |
return $textValue;
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
// string must never be longer than 32,767 characters, truncate if necessary
|
|
|
65 |
$textValue = StringHelper::substring((string) $textValue, 0, self::MAX_STRING_LENGTH);
|
|
|
66 |
|
|
|
67 |
// we require that newline is represented as "\n" in core, not as "\r\n" or "\r"
|
|
|
68 |
$textValue = str_replace(["\r\n", "\r"], "\n", $textValue);
|
|
|
69 |
|
|
|
70 |
return $textValue;
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* Check a value that it is a valid error code.
|
|
|
75 |
*
|
|
|
76 |
* @param mixed $value Value to sanitize to an Excel error code
|
|
|
77 |
*
|
|
|
78 |
* @return string Sanitized value
|
|
|
79 |
*/
|
|
|
80 |
public static function checkErrorCode(mixed $value): string
|
|
|
81 |
{
|
|
|
82 |
$value = (is_scalar($value) || $value instanceof Stringable) ? ((string) $value) : '#NULL!';
|
|
|
83 |
|
|
|
84 |
if (!isset(self::$errorCodes[$value])) {
|
|
|
85 |
$value = '#NULL!';
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
return $value;
|
|
|
89 |
}
|
|
|
90 |
}
|