Overview

Namespaces

  • Defr
    • PhpMimeType

Classes

  • Defr\PhpMimeType\FileAsResponse
  • Defr\PhpMimeType\MimeType
  • Defr\PhpMimeType\MimeTypeInfo

Exceptions

  • Defr\PhpMimeType\MimeTypeException
  • Overview
  • Namespace
  • Class
 1: <?php
 2: 
 3: /*
 4:  * This file is part of the library "PhpMimeType".
 5:  *
 6:  * (c) Dennis Fridrich <fridrich.dennis@gmail.com>
 7:  *
 8:  * For the full copyright and license information,
 9:  * please view LICENSE.
10:  */
11: 
12: namespace Defr\PhpMimeType;
13: 
14: class MimeTypeInfo
15: {
16:     /**
17:      * @var string|\SplFileInfo|\SplFileObject
18:      */
19:     protected $file;
20: 
21:     /**
22:      * @var string
23:      */
24:     protected $mimeType;
25: 
26:     /**
27:      * MimeTypeInfo constructor.
28:      *
29:      * @param \SplFileInfo|\SplFileObject|string $file
30:      * @param string                             $mimeType
31:      *
32:      * @throws MimeTypeException
33:      */
34:     public function __construct($file, $mimeType)
35:     {
36:         if ((is_object($file) && !in_array(get_class($file), ['SplFileInfo', 'SplFileObject'], true)) && is_string($file)) {
37:             throw new MimeTypeException(
38:                 sprintf('Object %s can not be passed to %s', is_object($file) ? get_class($file) : $file, __CLASS__)
39:             );
40:         }
41:         $this->file = $file;
42:         $this->mimeType = $mimeType;
43:     }
44: 
45:     /**
46:      * @return string
47:      */
48:     public function __toString()
49:     {
50:         return $this->mimeType;
51:     }
52: 
53:     /**
54:      * @return \SplFileInfo|\SplFileObject|string
55:      */
56:     public function getFileName()
57:     {
58:         if ($this->file instanceof \SplFileInfo || $this->file instanceof \SplFileObject) {
59:             return $this->file->getFilename();
60:         }
61: 
62:         return $this->file;
63:     }
64: 
65:     /**
66:      * @return \SplFileInfo|\SplFileObject|string
67:      */
68:     public function getFile()
69:     {
70:         return $this->file;
71:     }
72: 
73:     /**
74:      * @return null|\SplFileInfo|\SplFileObject|string
75:      */
76:     public function getSplFileObject()
77:     {
78:         if ($this->file instanceof \SplFileObject) {
79:             return $this->file;
80:         }
81:         if ($this->file instanceof \SplFileInfo) {
82:             return $this->file->openFile();
83:         }
84:         if (is_string($this->file) && is_file($this->file)) {
85:             return new \SplFileObject($this->file);
86:         }
87: 
88:         return null;
89:     }
90: 
91:     /**
92:      * @return string
93:      */
94:     public function getMimeType()
95:     {
96:         return $this->mimeType;
97:     }
98: }
99: 
API documentation generated by ApiGen