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 $name;
00022 
00024    private $subtype;
00025 
00027    private $type;
00028    
00032    public static function getKnownExtensions() {
00033       if (!self::$dxml) {
00034          self::$dxml = new DOMDocument("1.0", "utf-8");
00035          self::$dxml->load(
00036                dirname(__FILE__) . "/../../includes/MediaTypes.xml");
00037       }
00038       $dxp = new DOMXPath(self::$dxml);
00039       $result = $dxp->query("//extension");
00040       $extensions = array();
00041       foreach ($result as $node) {
00042          $extensions[] = $node->nodeValue;
00043       }
00044       return $extensions;
00045    }
00046 
00052    public static function getTypeForExtension($ext) {
00053       if (!self::$dxml) {
00054          self::$dxml = new DOMDocument("1.0", "utf-8");
00055          self::$dxml->load(
00056                dirname(__FILE__) . "/../../includes/MediaTypes.xml");
00057       }
00058       $dxp = new DOMXPath(self::$dxml);
00059       $result = $dxp->query(
00060          sprintf("//type[extensions/extension = '%s'][1]", $ext));
00061       if ($result->length > 0) {
00062          $type = new DMMediaType(
00063             $result->item(0)->getAttribute("type"),
00064             $result->item(0)->getAttribute("subtype"));
00065          $type->setName(
00066             $result->item(0)->getElementsByTagName("name")->item(0)->nodeValue);
00067          return $type;
00068       }
00069       return null;
00070    }
00071 
00076    public static function getTypeForString($str) {
00077       $tmp = explode("/", $str);
00078       if (count($tmp) == 2) {
00079          return new DMMediaType($tmp[0], $tmp[1]);
00080       }
00081       return null;
00082    }
00083 
00088    public function __construct($type, $subtype) {
00089       $this->setType($type);
00090       $this->setSubtype($subtype);
00091    }
00092 
00096    public function __toString() {
00097       return $this->getType() . "/" . $this->getSubtype();
00098    }
00099 
00104    public function equals($object) {
00105       if (!$object instanceof DMMediaType) {
00106          return false;
00107       }
00108       return ($this->getType() == $object->getType()
00109             && $this->getSubtype() == $object->getSubtype());
00110    }
00111 
00115    public function getName() {
00116       if (!$this->name) {
00117          if (!self::$dxml) {
00118             self::$dxml = new DOMDocument("1.0", "utf-8");
00119             self::$dxml->load(
00120                   dirname(__FILE__) . "/../../includes/MediaTypes.xml");
00121          }
00122          $dxp = new DOMXPath(self::$dxml);
00123          $result = $dxp->query(
00124             sprintf("//type[@type = '%s' and @subtype = '%s']/name",
00125                   $this->getType(),
00126                   $this->getSubtype()));
00127          if ($result->length > 0) {
00128             $this->name = $result->item(0)->nodeValue;
00129          }
00130       }
00131       return $this->name;
00132    }
00133 
00137    public function setName($name) {
00138       $this->name = (string) $name;
00139    }
00140 
00144    public function getSubtype() {
00145       return $this->subtype;
00146    }
00147 
00151    public function setSubtype($subtype) {
00152       $this->subtype = $subtype;
00153    }
00154 
00158    public function getType() {
00159       return $this->type;
00160    }
00161 
00165    public function setType($type) {
00166       $this->type = $type;
00167    }
00168 
00169 }
 All Data Structures Functions Variables