Browse Source

Add MIME type groups and skeleton of test-set

remotes/origin/master
J. King 4 years ago
parent
commit
cf7998a51e
  1. 54
      lib/MimeType.php
  2. 6
      tests/cases/MimeTypeTest.php
  3. 3
      tests/cases/mime-groups.json

54
lib/MimeType.php

@ -25,6 +25,16 @@ namespace MensBeam\Mime;
* @property-read string $subtype The subtype of the MIME type i.e. the part after the slash
* @property-read string $essence The full MIME type without paramters e.g. `"text/html"`
* @property-read array $params The associative array of parameters included with the type. Keys are lowercase; values are presented in their original case, unescaped
* @property-read bool $isArchive Whether the MIME type is an archive type
* @property-read bool $isAudioVideo Whether the MIME type is an audio or video type
* @property-read bool $isFont Whether the MIME type is a font type
* @property-read bool $isHtml Whether the MIME type is HTML
* @property-read bool $isImage Whether the MIME type is an image type
* @property-read bool $isJavascript Whether the MIME type is a JavaScript type
* @property-read bool $isJson Whether the MIME type is a JSON type
* @property-read bool $isScriptable Whether the MIME type is a type which can be scripted (namely via JavaScript)
* @property-read bool $isXml Whether the MIME type is an XML type
* @property-read bool $isZipBased Whether the MIME type is a ZIP-based type
*/
class MimeType {
protected const TYPE_PATTERN = <<<'PATTERN'
@ -61,6 +71,16 @@ PATTERN;
protected $subtype = "";
protected $params = [];
private $essence;
private $isArchive;
private $isAudioVideo;
private $isFont;
private $isHtml;
private $isImage;
private $isJavascript;
private $isJson;
private $isScriptable;
private $isXml;
private $isZipBased;
protected function __construct(string $type = "", string $subtype = "", array $params = []) {
$this->type = $type;
@ -69,14 +89,36 @@ PATTERN;
}
public function __get(string $name) {
if ($name === "essence") {
return $this->type."/".$this->subtype;
}
switch ($name) {
case "essence":
return $this->essence();
case "isArchive":
return in_array($this->essence(), ["application/zip", "application/x-gzip", "application/x-rar-compressed"]);
case "isAudioVideo":
return $this->type === "audio" || $this->type === "video" || $this->essence() === "application/ogg";
case "isFont":
return $this->type === "font" || preg_match("<^application/(?:font-(?:cff|off|sfnt|ttf|woff)|vnd\.ms-(?:fontobject|opentype))$>", $this->essence());
case "isHtml":
return $this->essence() === "text/html";
case "isImage":
return $this->type === "image";
case "isJavascript":
return (bool) preg_match("<^(?:(?:text|application)/(?:(?:x-)?(?:ecma|java)script)|text/(?:livescript|jscript|javascript1\.[1-5]))$>", $this->essence());
case "isJson":
return substr($this->subtype, -5) === "+json" || preg_match("<^(?:text|application)/json$>", $this->essence());
case "isScriptable":
return $this->essence() === "application/pdf" || $this->__get("isHtml") || $this->__get("isXml");
case "isXml":
return substr($this->subtype, -4) === "+xml" || preg_match("<^(?:text|application)/xml$>", $this->essence());
case "isZipBased":
return substr($this->subtype, -4) === "+zip" || $this->essence() === "application/zip";
default:
return $this->$name ?? null;
}
}
public function __toString(): string {
$out = $this->__get("essence");
$out = $this->essence();
if (is_array($this->params) && sizeof($this->params)) {
foreach ($this->params as $name => $value) {
$out .= ";$name=".(preg_match(self::TOKEN_PATTERN, $value) ? $value : '"'.str_replace(["\\", '"'], ["\\\\", "\\\""], $value).'"');
@ -85,6 +127,10 @@ PATTERN;
return $out;
}
protected function essence(): string {
return $this->type."/".$this->subtype;
}
/** Parses a UTF-8 string and returns a MimeType instance, or null on failure
*
* If parsing an HTTP header, the MimeType::parseBytes method should be used instead

6
tests/cases/MimeTypeTest.php

@ -41,7 +41,7 @@ class MimeTypeTest extends \PHPUnit\Framework\TestCase {
}
public function testDecodeAByteString(): void {
// set up the text with the Intl extension
// set up the test with the Intl extension
$input = "";
$exp = "";
for ($a = 0; $a <= 0xFF; $a++) {
@ -53,7 +53,7 @@ class MimeTypeTest extends \PHPUnit\Framework\TestCase {
}
public function testEncodeAValidString(): void {
// set up the text with the Intl extension
// set up the test with the Intl extension
$input = "";
$exp = "";
for ($a = 0; $a <= 0xFF; $a++) {
@ -65,9 +65,7 @@ class MimeTypeTest extends \PHPUnit\Framework\TestCase {
}
public function testEncodeAnInvalidString(): void {
// set up the text with the Intl extension
$input = "!\u{1F4A9}!";
// perform the test
$this->assertNull(Mime::encode($input));
}

3
tests/cases/mime-groups.json

@ -0,0 +1,3 @@
{
"text/plain": {"image": false, "audioVideo": false, "font": false, "ZIP-based": false, "archive": false, "XML": false, "scriptable": false, "JavaScript": false, "JSON": false}
}
Loading…
Cancel
Save