1<?php 2 3 4namespace ComboStrap; 5 6 7class Mime 8{ 9 10 public const JSON = "application/json"; 11 public const HTML = "text/html"; 12 public const XHTML = self::HTML; 13 const PLAIN_TEXT = "text/plain"; 14 const HEADER_CONTENT_TYPE = "Content-Type"; 15 public const SVG = "image/svg+xml"; 16 public const JAVASCRIPT = "text/javascript"; 17 const PNG = "image/png"; 18 const GIF = "image/gif"; 19 const JPEG = "image/jpeg"; 20 const BMP = "image/bmp"; 21 const WEBP = "image/webp"; 22 /** 23 * @var array|null 24 */ 25 private static $knownTypes; 26 27 /** 28 * @var string 29 */ 30 private $mime; 31 32 /** 33 * Mime constructor. 34 */ 35 public function __construct(string $mime) 36 { 37 if (trim($mime) === "") { 38 LogUtility::msg("The mime should not be an empty string"); 39 } 40 $this->mime = $mime; 41 } 42 43 public static function create(string $mime): Mime 44 { 45 return new Mime($mime); 46 } 47 48 public function __toString() 49 { 50 return $this->mime; 51 } 52 53 public function isKnown(): bool 54 { 55 56 if (self::$knownTypes === null) { 57 self::$knownTypes = getMimeTypes(); 58 } 59 return array_search($this->mime, self::$knownTypes) !== false; 60 61 } 62 63 public function isTextBased(): bool 64 { 65 if ($this->getFirstPart() === "text") { 66 return true; 67 } 68 if (in_array($this->mime, [self::SVG, self::JSON])) { 69 return true; 70 } 71 return false; 72 } 73 74 private function getFirstPart() 75 { 76 return explode("/", $this->mime)[0]; 77 } 78 79 public function isImage(): bool 80 { 81 return substr($this->mime, 0, 5) === 'image'; 82 } 83 84 public function toString(): string 85 { 86 return $this->__toString(); 87 } 88 89 90} 91