dmBridge PHP API
DMMediaType.php
00001 <?php
00002 #
00003 # dmBridge: a data access framework for CONTENTdm(R)
00004 #
00005 # Copyright © 2009, 2010 Board of Regents of the Nevada System of Higher
00006 # Education, on behalf of the University of Nevada, Las Vegas
00007 #
00008 
00015 class DMMediaType {
00016 
00018    private static $dxml;
00019 
00021    private $type, $subtype;
00022 
00026    public static function getKnownExtensions() {
00027       if (!self::$dxml) {
00028          self::$dxml = new DOMDocument("1.0", "utf-8");
00029          self::$dxml->load(
00030                dirname(__FILE__) . "/../../includes/file_types.xml");
00031       }
00032       $dxp = new DOMXPath(self::$dxml);
00033       $result = $dxp->query("//file/@extension");
00034       $extensions = array();
00035       foreach ($result as $node) {
00036          $extensions[] = $node->value;
00037       }
00038       return $extensions;
00039    }
00040 
00046    public static function getTypeForExtension($ext) {
00047       if (!self::$dxml) {
00048          self::$dxml = new DOMDocument("1.0", "utf-8");
00049          self::$dxml->load(
00050                dirname(__FILE__) . "/../../includes/file_types.xml");
00051       }
00052       $dxp = new DOMXPath(self::$dxml);
00053       $result = $dxp->query(sprintf("//file[@extension = '%s']", $ext));
00054       if ($result->length > 0) {
00055          return new DMMediaType(
00056             $result->item(0)->getElementsByTagName("type")->item(0)->nodeValue,
00057             $result->item(0)->getElementsByTagName("subtype")->item(0)->nodeValue);
00058       }
00059       return null;
00060    }
00061 
00066    public static function getTypeForString($str) {
00067       $tmp = explode("/", $str);
00068       if (count($tmp) == 2) {
00069          return new DMMediaType($tmp[0], $tmp[1]);
00070       }
00071       return null;
00072    }
00073 
00078    public function __construct($type, $subtype) {
00079       $this->setType($type);
00080       $this->setSubtype($subtype);
00081    }
00082 
00086    public function __toString() {
00087       return $this->getType() . "/" . $this->getSubtype();
00088    }
00089 
00094    public function equals($object) {
00095       if (!$object instanceof DMMediaType) {
00096          return false;
00097       }
00098       return ($this->getType() == $object->getType()
00099             && $this->getSubtype() == $object->getSubtype());
00100    }
00101 
00105    public function getSubtype() {
00106       return $this->subtype;
00107    }
00108 
00112    public function setSubtype($subtype) {
00113       $this->subtype = $subtype;
00114    }
00115 
00119    public function getType() {
00120       return $this->type;
00121    }
00122 
00126    public function setType($type) {
00127       $this->type = $type;
00128    }
00129 
00130 }
 All Data Structures Functions Variables