File tree Expand file tree Collapse file tree 2 files changed +69
-0
lines changed Expand file tree Collapse file tree 2 files changed +69
-0
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Http \Message \MultipartStream ;
4
+
5
+ /**
6
+ * Let you add your own mimetypes. The mimetype lookup will fallback on the ApacheMimeTypeHelper.
7
+ *
8
+ * @author Tobias Nyholm <tobias.nyholm@gmail.com>
9
+ */
10
+ class CustomMimetypeHelper extends ApacheMimetypeHelper
11
+ {
12
+ /**
13
+ * @var array
14
+ */
15
+ private $ mimetypes = [];
16
+
17
+ /**
18
+ * @param array $mimetypes should be of type extension => mimetype
19
+ */
20
+ public function __construct (array $ mimetypes = [])
21
+ {
22
+ $ this ->mimetypes = $ mimetypes ;
23
+ }
24
+
25
+ /**
26
+ * @param string $extension
27
+ * @param string $mimetype
28
+ *
29
+ * @return $this
30
+ */
31
+ public function addMimetype ($ extension , $ mimetype )
32
+ {
33
+ $ this ->mimetypes [$ extension ] = $ mimetype ;
34
+
35
+ return $ this ;
36
+ }
37
+
38
+ /**
39
+ * {@inheritdoc}
40
+ *
41
+ * Check if we have any defined mimetypes and of not fallback to ApacheMimetypeHelper
42
+ */
43
+ public function getMimetypeFromExtension ($ extension )
44
+ {
45
+ $ extension = strtolower ($ extension );
46
+
47
+ return isset ($ this ->mimetypes [$ extension ])
48
+ ? $ this ->mimetypes [$ extension ]
49
+ : parent ::getMimetypeFromExtension ($ extension );
50
+ }
51
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace tests \Http \Message \MultipartStream ;
4
+
5
+ use Http \Message \MultipartStream \CustomMimetypeHelper ;
6
+
7
+ class CustomMimetypeHelperTest extends \PHPUnit_Framework_TestCase
8
+ {
9
+ public function testGetMimetypeFromExtension ()
10
+ {
11
+ $ helper = new CustomMimetypeHelper (['foo ' =>'foo/bar ' ]);
12
+ $ this ->assertEquals ('foo/bar ' , $ helper ->getMimetypeFromExtension ('foo ' ));
13
+
14
+ $ this ->assertEquals ('application/x-rar-compressed ' , $ helper ->getMimetypeFromExtension ('rar ' ));
15
+ $ helper ->addMimetype ('rar ' , 'test/test ' );
16
+ $ this ->assertEquals ('test/test ' , $ helper ->getMimetypeFromExtension ('rar ' ));
17
+ }
18
+ }
You can’t perform that action at this time.
0 commit comments