PageRenderTime 14ms CodeModel.GetById 1ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Zend/Media/Vorbis/Header/Comment.php

http://php-reader.googlecode.com/
PHP | 199 lines | 77 code | 12 blank | 110 comment | 14 complexity | 28728afa81288dbf8abdb82bf80b67a2 MD5 | raw file
  1<?php
  2/**
  3 * Zend Framework
  4 *
  5 * LICENSE
  6 *
  7 * This source file is subject to the new BSD license that is bundled with this package in the file LICENSE.txt. It is
  8 * also available through the world-wide-web at this URL: http://framework.zend.com/license/new-bsd. If you did not
  9 * receive a copy of the license and are unable to obtain it through the world-wide-web, please send an email to
 10 * license@zend.com so we can send you a copy immediately.
 11 *
 12 * @category   Zend
 13 * @package    Zend_Media
 14 * @subpackage Vorbis
 15 * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 16 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 17 * @version    $Id: Comment.php 251 2011-06-13 15:41:51Z svollbehr $
 18 */
 19
 20/**#@+ @ignore */
 21require_once 'Zend/Media/Vorbis/Header.php';
 22/**#@-*/
 23
 24/**
 25 * The Vorbis text comment header is the second (of three) header packets that begin a Vorbis bitstream. It is meant
 26 * for short text comments, not arbitrary metadata; arbitrary metadata belongs in a separate logical bitstream (usually
 27 * an XML stream type) that provides greater structure and machine parseability.
 28 *
 29 * The comment field is meant to be used much like someone jotting a quick note on the bottom of a CDR. It should be a
 30 * little information to remember the disc by and explain it to others; a short, to-the-point text note that need not
 31 * only be a couple words, but isn't going to be more than a short paragraph. The essentials, in other words, whatever
 32 * they turn out to be, eg:
 33 *
 34 *   Honest Bob and the Factory-to-Dealer-Incentives, \I'm Still Around", opening for Moxy Fruvous, 1997.
 35 *
 36 * The following web pages will guide you with applicaple field names and values:
 37 *
 38 * o Recommended set of 15 field names
 39 *   http://xiph.org/vorbis/doc/v-comment.html
 40 *
 41 * o Proposed update to the minimal list of 15 standard field names
 42 *   http://wiki.xiph.org/Field_names
 43 *
 44 * o Other proposals for additional field names
 45 *   http://age.hobba.nl/audio/mirroredpages/ogg-tagging.html
 46 *   http://reallylongword.org/vorbiscomment/
 47 *
 48 * @category   Zend
 49 * @package    Zend_Media
 50 * @subpackage Vorbis
 51 * @author     Sven Vollbehr <sven@vollbehr.eu>
 52 * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 53 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 54 * @version    $Id: Comment.php 251 2011-06-13 15:41:51Z svollbehr $
 55 */
 56final class Zend_Media_Vorbis_Header_Comment extends Zend_Media_Vorbis_Header
 57{
 58    /** @var string */
 59    private $_vendor;
 60
 61    /** @var Array */
 62    private $_comments;
 63
 64    /** @var integer */
 65    private $_framingFlag = 1;
 66
 67    /**
 68     * Constructs the class with given parameters and reads object related data from the bitstream.
 69     *
 70     * The following options are currently recognized:
 71     *  o vorbisContext -- Indicates whether to expect comments to be in the context of a vorbis bitstream or not. This
 72     *    option can be used to parse vorbis comments in another formats, eg FLAC, that do not use for example the
 73     *    framing flags. Defaults to true.
 74     *
 75     * @param Zend_Io_Reader $reader The reader object.
 76     * @param Array          $options Array of options.
 77     */
 78    public function __construct($reader, $options = array())
 79    {
 80        if (!isset($options['vorbisContext']) || $options['vorbisContext']) {
 81            parent::__construct($reader);
 82        } else {
 83            $this->_reader = $reader;
 84        }
 85        $this->_vendor = $this->_reader->read($this->_reader->readUInt32LE());
 86        $userCommentListLength = $this->_reader->readUInt32LE();
 87        for ($i = 0; $i < $userCommentListLength; $i++) {
 88            list ($name, $value) = preg_split('/=/', $this->_reader->read($this->_reader->readUInt32LE()), 2);
 89            if (!isset($this->_comments[strtoupper($name)])) {
 90                $this->_comments[strtoupper($name)] = array();
 91            }
 92            $this->_comments[strtoupper($name)][] = $value;
 93        }
 94        if (!isset($options['vorbisContext']) || $options['vorbisContext']) {
 95            $this->_framingFlag = $this->_reader->readUInt8() & 0x1;
 96            if ($this->_framingFlag == 0) {
 97                require_once 'Zend/Media/Vorbis/Exception.php';
 98                throw new Zend_Media_Vorbis_Exception('Undecodable Vorbis stream');
 99            }
100            $this->_reader->skip($this->_packetSize - $this->_reader->getOffset() + 30 /* header */);
101        }
102    }
103
104    /**
105     * Returns the vendor string.
106     *
107     * @return string
108     */
109    public function getVendor()
110    {
111        return $this->_vendor;
112    }
113
114    /**
115     * Returns an array of comments having the field names as keys and an array of values as a value.
116     *
117     * @return Array
118     */
119    public function getComments()
120    {
121        return $this->_comments;
122    }
123
124    /**
125     * Returns an array of comments having the field names as keys and an array of values as a value. The array is
126     * restricted to field names that matches the given criteria. Unlike the getX() methods, which return the first
127     * value, this method returns an array of field values.
128     *
129     * @return Array
130     */
131    public function getCommentsByName($name)
132    {
133        if (!empty($this->_comments[strtoupper($name)])) {
134           return $this->_comments[strtoupper($name)];
135        }
136        return array();
137    }
138
139    /**
140     * Magic function so that $obj->X() or $obj->getX() will work, where X is the name of the comment field. The method
141     * will attempt to return the first field by the given name from the comment. If there is no field with given name,
142     * an exception is thrown.
143     *
144     * @param string $name The field name.
145     * @return mixed
146     */
147    public function __call($name, $arguments)
148    {
149        if (preg_match('/^(?:get)([A-Z].*)$/', $name, $matches)) {
150            $name = $matches[1];
151        }
152        if (!empty($this->_comments[strtoupper($name)])) {
153           return $this->_comments[strtoupper($name)][0];
154        }
155        require_once 'Zend/Media/Vorbis/Exception.php';
156        throw new Zend_Media_Vorbis_Exception('Unknown field: ' . strtoupper($name));
157    }
158
159    /**
160     * Magic function so that $obj->value will work. The method will attempt to return the first field by the given
161     * name from the comment. If there is no field with given name, functionality of the parent method is executed.
162     *
163     * @param string $name The field name.
164     * @return mixed
165     */
166    public function __get($name)
167    {
168        if (method_exists($this, 'get' . ucfirst($name))) {
169            return call_user_func(array($this, 'get' . ucfirst($name)));
170        }
171        if (!empty($this->_comments[strtoupper($name)])) {
172            return $this->_comments[strtoupper($name)][0];
173        }
174        parent::__get($name);
175    }
176
177    /**
178     * Magic function so that isset($obj->value) will work. This method checks whether the comment contains a field by
179     * the given name.
180     *
181     * @param string $name The field name.
182     * @return boolean
183     */
184    public function __isset($name)
185    {
186        return count($this->_comments[strtoupper($name)]) > 0;
187    }
188
189    /**
190     * Magic function so that unset($obj->value) will work. This method removes all the comments matching the field
191     * name.
192     *
193     * @param string $name The field name.
194     */
195    public function __unset($name)
196    {
197        unset($this->_comments[strtoupper($name)]);
198    }
199}