MediaSearch_File
[ class tree: MediaSearch_File ] [ index: MediaSearch_File ] [ all elements ]

Source for file File.php

Documentation is available at File.php

  1. <?php
  2. /**
  3.  * MediaSearch
  4.  *
  5.  * LICENSE
  6.  *
  7.  * "THE BEER-WARE LICENSE" (Revision 42):
  8.  * "Sven Strittmatter" <ausserirdisch@sven-space.de> wrote this file.
  9.  * As long as you retain this notice you can do whatever you want with
  10.  * this stuff. If we meet some day, and you think this stuff is worth it,
  11.  * you can buy me a beer in return.
  12.  *
  13.  * $Author$
  14.  * $Revision$
  15.  *
  16.  * @category    MediaSearch
  17.  * @package     MediaSearch_File
  18.  * @copyright   Copyright (c) 2009 Sven Strittmatter
  19.  */
  20.  
  21. /**
  22.  * @see MediaSearch_File_Abstract
  23.  */
  24. require_once('MediaSearch/File/Abstract.php');
  25.  
  26. /**
  27.  * Concrete implementation of a file. Objects of this type are leafes
  28.  * in a file tree. A file can have a type (see class constants). The
  29.  * type of a file is determined by its file name extension.
  30.  *
  31.  * @category   MediaSearch
  32.  * @package    MediaSearch_File
  33.  * @copyright  Copyright (c) 2009 Sven Strittmatter
  34.  */
  35.     const TRACE_LELVEL = 23;
  36.  
  37.     /**
  38.      * Unknown file type
  39.      */
  40.     const TYPE_UNKNOW     = 0;
  41.     /**
  42.      * File type movie: All files with the file name extensions:
  43.      * 'avi', 'mov', 'mpg', 'mpeg', 'mp4', 'mkv', 'wmv', 'rm',
  44.      * 'xvid', 'divx', 'mpa', 'm4v', 'ogm', 'sfv'.
  45.      */
  46.     const TYPE_MOVIE     = 1;
  47.     /**
  48.      * File type music: All files with the file name extensions:
  49.      * 'mp3', 'wav', 'ogg', 'm4a'.
  50.      */
  51.     const TYPE_MUSIC     = 2;
  52.     /**
  53.      * File type software: All files with the file name extensions:
  54.      * 'exe', 'dmg', 'pkg', 'dpkg', 'xpi'.
  55.      */
  56.     const TYPE_SOFTWARE = 3;
  57.     /**
  58.      * File type image: All files with the file name extensions:
  59.      * 'jpg', 'jpeg', 'gif', 'png', 'bmp', 'tif'.
  60.      */
  61.     const TYPE_IMAGE    = 4;
  62.     /**
  63.      * File type document: All files with the file name extensions:
  64.      * 'txt', 'pdf', 'odt', 'doc'.
  65.      */
  66.     const TYPE_DOCUMENT = 5;
  67.  
  68.     /**
  69.      * Maps the file types to the file name extensions.
  70.      *
  71.      * @access private
  72.      * @var array 
  73.      */
  74.     private $typeExtensions array(
  75.         self::TYPE_MOVIE => array(
  76.             'avi''mov''mpg''mpeg''mp4''mkv''wmv''rm',
  77.             'xvid''divx''mpa''m4v''ogm''sfv'),
  78.         self::TYPE_MUSIC    => array('mp3''wav''ogg''m4a'),
  79.         self::TYPE_SOFTWARE => array('exe''dmg''pkg''dpkg''xpi'),
  80.         self::TYPE_IMAGE    => array('jpg''jpeg''gif''png''bmp''tif'),
  81.         self::TYPE_DOCUMENT => array('txt''pdf''odt''doc')
  82.     );
  83.  
  84.     /**
  85.      * The type of the file. One of the calls constants named TYPE_*
  86.      *
  87.      * @access private
  88.      * @var int 
  89.      */
  90.     private $type;
  91.  
  92.     /**
  93.      * @see MediaSearch_File_Abstract
  94.      *
  95.      *  Additionaly discovers the type of the file.
  96.      */
  97.     public function __construct($fileInfo{
  98.         parent::__construct($fileInfo);
  99.         $this->discoverType();
  100.     }
  101.  
  102.     /**
  103.      * Discovers the file type depending on the file name extension.
  104.      *
  105.      * @access private
  106.      */
  107.     private function discoverType({        
  108.         $fileExtension MediaSearch_File_Util::getExtension($this->fileInfo->getFilename());
  109.         $fileExtension strtolower($fileExtension);
  110.  
  111.         if ($this->isType(self::TYPE_MOVIE$fileExtension)) {
  112.             $this->type self::TYPE_MOVIE;
  113.         else if ($this->isType(self::TYPE_MUSIC$fileExtension)) {
  114.             $this->type self::TYPE_MUSIC;
  115.         else if ($this->isType(self::TYPE_SOFTWARE$fileExtension)) {
  116.             $this->type self::TYPE_SOFTWARE;
  117.         else if ($this->isType(self::TYPE_IMAGE$fileExtension)) {
  118.             $this->type self::TYPE_IMAGE;
  119.         else if ($this->isType(self::TYPE_DOCUMENT$fileExtension)) {
  120.             $this->type self::TYPE_DOCUMENT;
  121.         else {
  122.             $this->type self::TYPE_UNKNOW;
  123.         }
  124.     }
  125.  
  126.     /**
  127.      * Returns the type according to the class constants named by TYPE_*.
  128.      *
  129.      * @accesss public
  130.      * @return int 
  131.      */
  132.     public function getType({
  133.         return $this->type;
  134.     }
  135.  
  136.     /**
  137.      * This mthod provides two functions:
  138.      * <ol>
  139.      *  <li>If only the type integer is given this method returns if the
  140.      *  actual object is of this type.</li>
  141.      * <li>If you provide a file name extension like 'jpg' as second parameter
  142.      * this method tells you if files with this extension are from the type you
  143.      * passed as first argument.</li>
  144.      * </ol>
  145.      *
  146.      * Valid integers as type identifier are all class constants named
  147.      * like TYPE_*.
  148.      *
  149.      * @access public
  150.      * @param int $type 
  151.      * @param string $extension An optional filename extension like 'jpg'.
  152.      * @return bool 
  153.      */
  154.     public function isType($type$extension null{
  155.         if (null === $extension{
  156.             return in_array($extension$this->typeExtensions[$type]);
  157.         else {
  158.             return ($this->getType(=== $type);
  159.         }
  160.     }
  161.  
  162.     /**
  163.      * Implements MediaSearch_Interface_Visitable.
  164.      *
  165.      * @access public
  166.      * @param MediaSearch_Interface_Visitor $visitor 
  167.      */
  168.     public function acceptVisitor(MediaSearch_File_Visitor $visitor{
  169.         $visitor->addScannedFile($this->fileInfo$this->getType());
  170.     }
  171. }

Documentation generated on Mon, 17 Aug 2009 14:52:32 +0200 by phpDocumentor 1.4.2