File tree 4 files changed +40
-6
lines changed
src/PhpSpreadsheet/Reader
PhpSpreadsheetTests/Reader 4 files changed +40
-6
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
10
10
### Fixed
11
11
12
12
- Avoid potentially unsupported PSR-16 cache keys - [ #354 ] ( https://github.com/PHPOffice/PhpSpreadsheet/issues/354 )
13
+ - Check for MIME type to know if CSV reader can read a file - [ #167 ] ( https://github.com/PHPOffice/PhpSpreadsheet/issues/167 )
13
14
14
15
## [ 1.1.0] - 2018-01-28
15
16
Original file line number Diff line number Diff line change @@ -195,11 +195,11 @@ protected function inferSeparator()
195
195
}
196
196
197
197
$ meanSquareDeviations [$ delimiter ] = array_reduce (
198
- $ series ,
199
- function ($ sum , $ value ) use ($ median ) {
200
- return $ sum + pow ($ value - $ median , 2 );
201
- }
202
- ) / count ($ series );
198
+ $ series ,
199
+ function ($ sum , $ value ) use ($ median ) {
200
+ return $ sum + pow ($ value - $ median , 2 );
201
+ }
202
+ ) / count ($ series );
203
203
}
204
204
205
205
// ... and pick the delimiter with the smallest mean square deviation (in case of ties, the order in potentialDelimiters is respected)
@@ -476,6 +476,13 @@ public function canRead($pFilename)
476
476
477
477
fclose ($ this ->fileHandle );
478
478
479
- return true ;
479
+ $ type = mime_content_type ($ pFilename );
480
+ $ supportedTypes = [
481
+ 'text/csv ' ,
482
+ 'text/plain ' ,
483
+ 'inode/x-empty ' ,
484
+ ];
485
+
486
+ return in_array ($ type , $ supportedTypes , true );
480
487
}
481
488
}
Original file line number Diff line number Diff line change @@ -63,4 +63,30 @@ public function providerDelimiterDetection()
63
63
],
64
64
];
65
65
}
66
+
67
+ /**
68
+ * @dataProvider providerCanLoad
69
+ *
70
+ * @param bool $expected
71
+ * @param string $filename
72
+ */
73
+ public function testCanLoad ($ expected , $ filename )
74
+ {
75
+ $ reader = new Csv ();
76
+ self ::assertSame ($ expected , $ reader ->canRead ($ filename ));
77
+ }
78
+
79
+ public function providerCanLoad ()
80
+ {
81
+ return [
82
+ [false , 'data/Reader/Ods/data.ods ' ],
83
+ [false , 'data/Reader/Xml/WithoutStyle.xml ' ],
84
+ [true , 'data/Reader/CSV/enclosure.csv ' ],
85
+ [true , 'data/Reader/CSV/semicolon_separated.csv ' ],
86
+ [true , 'data/Reader/HTML/csv_with_angle_bracket.csv ' ],
87
+ [true , 'data/Reader/CSV/empty.csv ' ],
88
+ [true , '../samples/Reader/sampleData/example1.csv ' ],
89
+ [true , '../samples/Reader/sampleData/example2.csv ' ],
90
+ ];
91
+ }
66
92
}
You can’t perform that action at this time.
0 commit comments