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 const CSS = "text/css"; 23 /** 24 * @var array|null 25 */ 26 private static $knownTypes; 27 28 /** 29 * @var string 30 */ 31 private $mime; 32 33 /** 34 * Mime constructor. 35 */ 36 public function __construct(string $mime) 37 { 38 if (trim($mime) === "") { 39 LogUtility::msg("The mime should not be an empty string"); 40 } 41 $this->mime = $mime; 42 } 43 44 public static function create(string $mime): Mime 45 { 46 return new Mime($mime); 47 } 48 49 public function __toString() 50 { 51 return $this->mime; 52 } 53 54 public function isKnown(): bool 55 { 56 57 if (self::$knownTypes === null) { 58 self::$knownTypes = getMimeTypes(); 59 } 60 return array_search($this->mime, self::$knownTypes) !== false; 61 62 } 63 64 public function isTextBased(): bool 65 { 66 if ($this->getFirstPart() === "text") { 67 return true; 68 } 69 if (in_array($this->mime, [self::SVG, self::JSON])) { 70 return true; 71 } 72 return false; 73 } 74 75 private function getFirstPart() 76 { 77 return explode("/", $this->mime)[0]; 78 } 79 80 public function isImage(): bool 81 { 82 return substr($this->mime, 0, 5) === 'image'; 83 } 84 85 public function toString(): string 86 { 87 return $this->__toString(); 88 } 89 90 91} 92