1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Defr\PhpMimeType;
13:
14: class MimeTypeInfo
15: {
16: 17: 18:
19: protected $file;
20:
21: 22: 23:
24: protected $mimeType;
25:
26: 27: 28: 29: 30: 31: 32: 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: 47:
48: public function __toString()
49: {
50: return $this->mimeType;
51: }
52:
53: 54: 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: 67:
68: public function getFile()
69: {
70: return $this->file;
71: }
72:
73: 74: 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: 93:
94: public function getMimeType()
95: {
96: return $this->mimeType;
97: }
98: }
99: