File tree Expand file tree Collapse file tree 2 files changed +35
-6
lines changed
tests/PhpSpreadsheetTests/Cell Expand file tree Collapse file tree 2 files changed +35
-6
lines changed Original file line number Diff line number Diff line change @@ -61,7 +61,15 @@ public static function dataTypeForValue(mixed $value): string
61
61
if ($ value instanceof RichText) {
62
62
return DataType::TYPE_INLINE ;
63
63
}
64
- if (is_string ($ value ) && strlen ($ value ) > 1 && $ value [0 ] === '= ' ) {
64
+ if ($ value instanceof Stringable) {
65
+ $ value = (string ) $ value ;
66
+ }
67
+ if (!is_string ($ value )) {
68
+ $ gettype = is_object ($ value ) ? get_class ($ value ) : gettype ($ value );
69
+
70
+ throw new SpreadsheetException ("unusable type $ gettype " );
71
+ }
72
+ if (strlen ($ value ) > 1 && $ value [0 ] === '= ' ) {
65
73
$ calculation = new Calculation ();
66
74
$ calculation ->disableBranchPruning ();
67
75
@@ -93,11 +101,9 @@ public static function dataTypeForValue(mixed $value): string
93
101
94
102
return DataType::TYPE_NUMERIC ;
95
103
}
96
- if (is_string ($ value )) {
97
- $ errorCodes = DataType::getErrorCodes ();
98
- if (isset ($ errorCodes [$ value ])) {
99
- return DataType::TYPE_ERROR ;
100
- }
104
+ $ errorCodes = DataType::getErrorCodes ();
105
+ if (isset ($ errorCodes [$ value ])) {
106
+ return DataType::TYPE_ERROR ;
101
107
}
102
108
103
109
return DataType::TYPE_STRING ;
Original file line number Diff line number Diff line change @@ -108,4 +108,27 @@ public function testCanOverrideStaticMethodWithoutOverridingBindValue(): void
108
108
self ::assertTrue ($ binder ::$ called );
109
109
$ spreadsheet ->disconnectWorksheets ();
110
110
}
111
+
112
+ public function testDataTypeForValueExceptions (): void
113
+ {
114
+ try {
115
+ self ::assertSame ('s ' , DefaultValueBinder::dataTypeForValue (new SpreadsheetException ()));
116
+ } catch (SpreadsheetException $ e ) {
117
+ self ::fail ('Should not have failed for stringable ' );
118
+ }
119
+
120
+ try {
121
+ DefaultValueBinder::dataTypeForValue ([]);
122
+ self ::fail ('Should have failed for array ' );
123
+ } catch (SpreadsheetException $ e ) {
124
+ self ::assertStringContainsString ('unusable type array ' , $ e ->getMessage ());
125
+ }
126
+
127
+ try {
128
+ DefaultValueBinder::dataTypeForValue (new DateTime ());
129
+ self ::fail ('Should have failed for DateTime ' );
130
+ } catch (SpreadsheetException $ e ) {
131
+ self ::assertStringContainsString ('unusable type DateTime ' , $ e ->getMessage ());
132
+ }
133
+ }
111
134
}
You can’t perform that action at this time.
0 commit comments