dmBridge PHP API
DMFile.php
00001 <?php
00002 #
00003 # dmBridge: a data access framework for CONTENTdm(R)
00004 #
00005 # Copyright © 2009, 2010, 2011 Board of Regents of the Nevada System of Higher
00006 # Education, on behalf of the University of Nevada, Las Vegas
00007 #
00008 
00013 class DMFile {
00014 
00018    private $pathname;
00019 
00026    public static function getUnitsForByteSize($byte_size,
00027          $standard = "SI") {
00028       switch ($standard) {
00029       case "JEDEC":
00030          if ($byte_size < 1024) {
00031             return "bytes";
00032          } else if ($byte_size < pow(1024, 2)) {
00033             return "kilobytes";
00034          } else if ($byte_size < pow(1024, 3)) {
00035             return "megabytes";
00036          } else if ($byte_size < pow(1024, 4)) {
00037             return "gigabytes";
00038          }
00039          break;
00040       case "IEC":
00041          if ($byte_size < 1024) {
00042             return "bytes";
00043          } else if ($byte_size < pow(1024, 2)) {
00044             return "kibibytes";
00045          } else if ($byte_size < pow(1024, 3)) {
00046             return "mebibytes";
00047          } else if ($byte_size < pow(1024, 4)) {
00048             return "gibibytes";
00049          } else if ($byte_size < pow(1024, 5)) {
00050             return "tebibytes";
00051          } else if ($byte_size < pow(1024, 6)) {
00052             return "pebibytes";
00053          } else if ($byte_size < pow(1024, 7)) {
00054             return "exbibytes";
00055          } else if ($byte_size < pow(1024, 8)) {
00056             return "zebibytes";
00057          } else if ($byte_size < pow(1024, 9)) {
00058             return "yobibytes";
00059          }
00060          break;
00061       case "SI":
00062          if ($byte_size < 1000) {
00063             return "bytes";
00064          } else if ($byte_size < pow(1024, 2)) {
00065             return "kilobytes";
00066          } else if ($byte_size < pow(1024, 3)) {
00067             return "megabytes";
00068          } else if ($byte_size < pow(1024, 4)) {
00069             return "gigabytes";
00070          } else if ($byte_size < pow(1024, 5)) {
00071             return "terabytes";
00072          } else if ($byte_size < pow(1024, 6)) {
00073             return "petabytes";
00074          } else if ($byte_size < pow(1024, 7)) {
00075             return "exabytes";
00076          } else if ($byte_size < pow(1024, 8)) {
00077             return "zettabytes";
00078          } else if ($byte_size < pow(1024, 9)) {
00079             return "yottabytes";
00080          }
00081          break;
00082       }
00083       return null;
00084    }
00085    
00095    public static function getUnitSymbolForByteSize($byte_size,
00096          $standard = "SI") {
00097       switch ($standard) {
00098       case "JEDEC":
00099          if ($byte_size < 1024) {
00100             return "b";
00101          } else if ($byte_size < pow(1024, 2)) {
00102             return "K";
00103          } else if ($byte_size < pow(1024, 3)) {
00104             return "M";
00105          } else if ($byte_size < pow(1024, 4)) {
00106             return "G";
00107          }
00108          break;
00109       case "IEC":
00110          if ($byte_size < 1024) {
00111             return "b";
00112          } else if ($byte_size < pow(1024, 2)) {
00113             return "Ki";
00114          } else if ($byte_size < pow(1024, 3)) {
00115             return "Mi";
00116          } else if ($byte_size < pow(1024, 4)) {
00117             return "Gi";
00118          } else if ($byte_size < pow(1024, 5)) {
00119             return "Ti";
00120          } else if ($byte_size < pow(1024, 6)) {
00121             return "Pi";
00122          } else if ($byte_size < pow(1024, 7)) {
00123             return "Ei";
00124          } else if ($byte_size < pow(1024, 8)) {
00125             return "Zi";
00126          } else if ($byte_size < pow(1024, 9)) {
00127             return "Yi";
00128          }
00129          break;
00130       case "SI":
00131          if ($byte_size < 1000) {
00132             return "b";
00133          } else if ($byte_size < pow(1024, 2)) {
00134             return "k";
00135          } else if ($byte_size < pow(1024, 3)) {
00136             return "M";
00137          } else if ($byte_size < pow(1024, 4)) {
00138             return "G";
00139          } else if ($byte_size < pow(1024, 5)) {
00140             return "T";
00141          } else if ($byte_size < pow(1024, 6)) {
00142             return "P";
00143          } else if ($byte_size < pow(1024, 7)) {
00144             return "E";
00145          } else if ($byte_size < pow(1024, 8)) {
00146             return "Z";
00147          } else if ($byte_size < pow(1024, 9)) {
00148             return "Y";
00149          }
00150          break;
00151       }
00152       return null;
00153    }
00154 
00158    public function __construct($pathname) {
00159       $this->setPathname($pathname);
00160    }
00161 
00165    public function __toString() {
00166       return $this->getLastPathComponent();
00167    }
00168 
00173    public function equals($obj) {
00174       if (!$obj instanceof DMFile) {
00175          return false;
00176       }
00177       return ($obj->getPathname() == $this->getPathname());
00178    }
00179 
00183    public function getExtension() {
00184       $tmp = explode(".", $this->getLastPathComponent());
00185       if (count($tmp) > 1) {
00186          return $tmp[count($tmp) - 1];
00187       }
00188       return null;
00189    }
00190 
00195    public function getHumanReadableSize($precision = 2) {
00196       $bytes = $this->getSize();
00197       if ($bytes < 1000) {
00198          return $bytes . ' b';
00199       } else if ($bytes < pow(1000, 2)) {
00200          return round($bytes / 1000, $precision) . " k";
00201       } else if ($bytes < pow(1000, 3)) {
00202          return round($bytes / pow(1000, 2), $precision) . " M";
00203       } elseif ($bytes < pow(1000, 4)) {
00204          return round($bytes / pow(1000, 3), $precision) . " G";
00205       } elseif ($bytes >= pow(1000, 5)) {
00206          return round($bytes / pow(1000, 4), $precision) . " T";
00207       } elseif ($bytes >= pow(1000, 6)) {
00208          return round($bytes / pow(1000, 5), $precision) . " P";
00209       } elseif ($bytes >= pow(1000, 7)) {
00210          return round($bytes / pow(1000, 6), $precision) . " E";
00211       } elseif ($bytes >= pow(1000, 8)) {
00212          return round($bytes / pow(1000, 7), $precision) . " Z";
00213       } elseif ($bytes >= pow(1000, 9)) {
00214          return round($bytes / pow(1000, 8), $precision) . " Y";
00215       }
00216    }
00217 
00221    public function getLastPathComponent() {
00222       return basename($this->getPathname());
00223    }
00224 
00231    public function getMediaType() {
00232       return DMMediaType::getTypeForExtension($this->getExtension());
00233    }
00234 
00240    public function getNameWithoutExtension() {
00241       $basename = basename($this->getPathname());
00242       $tmp = explode(".", $basename);
00243       if (count($tmp) == 1) {
00244          return $basename;
00245       }
00246       array_pop($tmp);
00247       return implode(".", $tmp);
00248    }
00249 
00253    public function getPathname() {
00254       return $this->pathname;
00255    }
00256 
00260    public function setPathname($pathname) {
00261       $this->pathname = $pathname;
00262    }
00263 
00268    public function getSize() {
00269       if (!file_exists($this->getPathname())) {
00270          throw new DMFileNotFoundException($this->getPathname());
00271       }
00272       return filesize($this->getPathname());
00273    }
00274 
00275 }
 All Data Structures Functions Variables