<?php
/**
 * This file contains the QGoogleMimeType class.
 *
 * @author David Maliska
 */

/**
 * This class build array of mime types suppuoted on Google Drive
 *
 * @property string $fileName contains name of file
 * @property array $mimeTypes contains array mime types 
 */
class QGoogleMimeType {
    ///////////////////////////
    // Private Member Variables
    ///////////////////////////
    
    /** @var string Name of file */
    protected $fileName;
    
    /** @var array Array of mime types */
    protected $mimeTypes;
    
    //////////
    // Methods
    //////////
    /**
     * Create the QGoogleMimeType object.
     * 
     * @param string  $filename        The name of the file to ge mime type.
     *
     * @return void
     */
    public function __construct($filename = null) {
        $this->fileName = $filename;
        
        //Create mimetypes array 
        $this->mimeTypes = $this->buildMimeArray();
    }
    
    /**
     * Retrieve mime type of file
     *
     * @return string|null
     */
    public function getType() { 
        if ($this->fileName) {
            //Get base name of the filename
            $baseName = basename($this->fileName); 

            //Break file into parts seperated by "." 
            $fileNameArray = explode('.', $baseName); 

            //Take the last part of the file to get the file extension 
            $extension = $fileNameArray[count($fileNameArray)-1];    

            //Find mime type 
            if (isset($this->mimeTypes[$extension])) { 
                return $this->mimeTypes[$extension]; 
            //If the extension wasn't found return octet-stream          
            } else { 
                return 'application/octet-stream'; 
            } 
        } else {
            return null;
        }
    } 

    /**
     * Retrieve array of mime types
     *
     * @return array
     */
    protected function buildMimeArray() { 
        return array( 
            "xls" =>'application/vnd.ms-excel',
            "xlsx" =>'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
            "xml" =>'text/xml',
            "ods"=>'application/vnd.oasis.opendocument.spreadsheet',
            "csv"=>'text/plain',
            "tmpl"=>'text/plain',
            "pdf"=> 'application/pdf',
            "php"=>'application/x-httpd-php',
            "jpg"=>'image/jpeg',
            "png"=>'image/png',
            "gif"=>'image/gif',
            "bmp"=>'image/bmp',
            "txt"=>'text/plain',
            "doc"=>'application/msword',
            "js"=>'text/js',
            "swf"=>'application/x-shockwave-flash',
            "mp3"=>'audio/mpeg',
            "zip"=>'application/zip',
            "rar"=>'application/rar',
            "tar"=>'application/tar',
            "arj"=>'application/arj',
            "cab"=>'application/cab',
            "html"=>'text/html',
            "htm"=>'text/html',
            "folder"=>'application/vnd.google-apps.folder',
            "xlsx"=>'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
            "xltx"=>'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
            "potx"=>'application/vnd.openxmlformats-officedocument.presentationml.template',
            "ppsx"=>'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
            "pptx"=>'application/vnd.openxmlformats-officedocument.presentationml.presentation',
            "sldx"=>'application/vnd.openxmlformats-officedocument.presentationml.slide',
            "docx"=>'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
            "dotx"=>'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
            "xlam"=>'application/vnd.ms-excel.addin.macroEnabled.12',
            "xlsb"=>'application/vnd.ms-excel.sheet.binary.macroEnabled.12'
        ); 
    } 
    
    /////////////////////////
    // Public Properties: GET
    /////////////////////////
    /**
    * PHP Magic __get method implementation.
    * @param string $strName Name of the property to be fetched.
    *
    * @return array|string
    */
    public function __get($strName) {
        switch ($strName) {
            case 'MimeTypes': return $this->mimeTypes;
            case 'FileName': return $this->fileName;
                
            default:
                return null;
        }
    }
}

?>

