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