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

Source for file Tag.php

Documentation is available at Tag.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_Id3
  18.  * @copyright   Copyright (c) 2009 Sven Strittmatter
  19.  */
  20.  
  21. /**
  22.  * @see MediaSearch_Id3_Exception
  23.  */
  24. require_once('MediaSearch/Id3/Exception.php');
  25. /**
  26.  * @see MediaSearch_File_Util
  27.  */
  28. require_once('MediaSearch/File/Util.php');
  29.  
  30. /**
  31.  * Gives read access to the ID3 tags of a mp3 file.
  32.  *
  33.  * @category   MediaSearch
  34.  * @package    MediaSearch_Id3
  35.  * @uses       MediaSearch_Id3_Exception
  36.  * @copyright  Copyright (c) 2009 Sven Strittmatter
  37.  */
  38.     /**
  39.      * ID3 Version 1.0
  40.      */
  41.     const VERSION_1   ID3_V1_0;
  42.     /**
  43.      * ID3 Version 1.1
  44.      */
  45.     const VERSION_1_1 ID3_V1_1;
  46.     /**
  47.      * ID3 Version 2.0
  48.      */
  49.     const VERSION_2   ID3_V2;
  50.  
  51.     /**
  52.      * The version of the ID3 tags the file has.
  53.      * A mp3 can have 1.x and 2.x tags on same time!
  54.      *
  55.      * @access private
  56.      * @var int 
  57.      */
  58.     private $version;
  59.     /**
  60.      * File path of the mp3 file.
  61.      *
  62.      * @access private
  63.      * @var string 
  64.      */
  65.     private $filePath;
  66.     /**
  67.      * The tag data from the file.
  68.      *
  69.      * @access private
  70.      * @var array 
  71.      */
  72.     private $tagData;
  73.  
  74.     /**
  75.      * Gather the tag version from the file.
  76.      *
  77.      * @access public
  78.      * @throws MediaSearch_Id3_Exception If the given file info object isnt a mp3.
  79.      * @param SplFileInfo $fileInfo 
  80.      */
  81.     public function __construct(SplFileInfo $fileInfo{
  82.         $this->filePath $fileInfo->getPathname();
  83.  
  84.         if (!self::isMp3($this->filePath)) {
  85.             $message  'The given file object (path: ' $this->filePath;
  86.             $message .= ') doesnt refer to an mp3 file!';
  87.             throw new MediaSearch_Id3_Exception($message,
  88.                                 MediaSearch_Id3_Exception::UNSUPPORTED_FILE_TYPE);
  89.         }
  90.  
  91.         $this->version  id3_get_version($this->filePath);
  92.     }
  93.  
  94.     /**
  95.      * Checks the given file name string if it has the file name extension 'mp3'.
  96.      *
  97.      * @static
  98.      * @access public
  99.      * @param string $fileName 
  100.      * @return bool 
  101.      */
  102.     public static function isMp3($fileName{
  103.         return ('mp3' === MediaSearch_File_Util::getExtension($fileName));
  104.     }
  105.  
  106.     /**
  107.      * Returns the version of ID3 tags this file has.
  108.      * Attend that a file can have version 1.x and 2.x tags together!
  109.      *
  110.      * @access public
  111.      * @return int 
  112.      */
  113.     public function getVersion({
  114.         return $this->version;
  115.     }
  116.  
  117.     /**
  118.      * Tells you if this file is of a version.
  119.      *
  120.      * Example:
  121.      * <code>
  122.      *  // ...
  123.      *  $tag = new MediaSearch_Id3_Tag($fileInfo);
  124.      *  // ...
  125.      *  if ($tag->isVersion(MediaSearch_Id3_Tag::VERSION_2)) {
  126.      *  }
  127.      * </code>
  128.      *
  129.      * @access public
  130.      * @param int $int 
  131.      * @return bool 
  132.      */
  133.     public function isVersion($int{
  134.         return ($this->getVersion($int);
  135.     }
  136.  
  137.     /**
  138.      * Returns a ID3 tag by its name. If the ta gis not setted or does not exists
  139.      * this method returns null.
  140.      *
  141.      * @access public
  142.      * @param string $name 
  143.      * @return string 
  144.      */
  145.     public function get($name{
  146.         if (null === $this->tagData{
  147.             $this->tagData id3_get_tag($this->filePath$this->version);
  148.         }
  149.  
  150.         if (!array_key_exists($name$this->tagData)) {
  151.             return null;
  152.         }
  153.  
  154.         return $this->tagData[$name];
  155.     }
  156.  
  157.     /**
  158.      * @see get()
  159.      *
  160.      * @access public
  161.      * @param string $name 
  162.      * @return string 
  163.      */
  164.     public function __get($name{
  165.         return $this->get($name);
  166.     }
  167.  
  168.     /**
  169.      * Throws permanent a BadEthodCallException because this object
  170.      * is imutable.
  171.      * 
  172.      * @access public
  173.      * @throws BadEthodCallException
  174.      * @param atring $name 
  175.      */
  176.     public function __set($name{
  177.         $message 'Setting of tags is currently not supported!';
  178.         throw new BadEthodCallException($message);
  179.     }
  180. }

Documentation generated on Mon, 17 Aug 2009 14:54:11 +0200 by phpDocumentor 1.4.2