Completed
Pull Request — master (#14)
by Tobias
01:34
created

CustomMimetypeHelper::addMimetype()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
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 <[email protected]>
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
}
52