diff --git a/src/Zend/Media/Flac.php b/src/Zend/Media/Flac.php index 4ce6e3d..a51ed9d 100644 --- a/src/Zend/Media/Flac.php +++ b/src/Zend/Media/Flac.php @@ -185,8 +185,8 @@ final class Zend_Media_Flac * Returns an array of metadata blocks frames matching the given type or an empty array if no metadata blocks * matched the type. * - * Please note that one may also use the shorthand $obj->type to access the first metadata block with the given - * type. + * Please note that one may also use the shorthand $obj->type or $obj->getType(), where the type is the metadata + * block name, to access the first metadata block with the given type. * * @param string $type The metadata block type. * @return Array @@ -203,10 +203,35 @@ final class Zend_Media_Flac return $matches; } + /** + * Magic function so that $obj->X() or $obj->getX() will work, where X is the name of the metadata block. If there + * is no metadata block by the given name, an exception is thrown. + * + * @param string $name The metadata block name. + * @return mixed + */ + public function __call($name, $arguments) + { + if (preg_match('/^(?:get)([A-Z].*)$/', $name, $matches)) { + $name = lcfirst($matches[1]); + } + if (defined($constant = 'self::' . strtoupper(preg_replace('/(?<=[a-z])[A-Z]/', '_$0', $name)))) { + $metadataBlocks = $this->getMetadataBlocksByType(constant($constant)); + if (isset($metadataBlocks[0])) { + return $metadataBlocks[0]; + } + } + if (!empty($this->_comments[strtoupper($name)])) { + return $this->_comments[strtoupper($name)][0]; + } + require_once 'Zend/Media/Flac/Exception.php'; + throw new Zend_Media_Flac_Exception('Unknown metadata block: ' . strtoupper($name)); + } + /** * Magic function so that $obj->value will work. * - * @param string $name The field name. + * @param string $name The metadata block name. * @return mixed */ public function __get($name) diff --git a/src/Zend/Media/Flac/MetadataBlock/VorbisComment.php b/src/Zend/Media/Flac/MetadataBlock/VorbisComment.php index 84e8258..088584a 100644 --- a/src/Zend/Media/Flac/MetadataBlock/VorbisComment.php +++ b/src/Zend/Media/Flac/MetadataBlock/VorbisComment.php @@ -69,7 +69,7 @@ final class Zend_Media_Flac_MetadataBlock_VorbisComment extends Zend_Media_Flac_ public function __call($name, $arguments) { if (method_exists($this, $name)) { - return $this->$name($arguments); + return call_user_func(array($this, $name), $arguments); } try { return $this->_impl->$name($arguments); diff --git a/src/Zend/Media/Vorbis/Header/Comment.php b/src/Zend/Media/Vorbis/Header/Comment.php index 967e985..5ad0a9e 100644 --- a/src/Zend/Media/Vorbis/Header/Comment.php +++ b/src/Zend/Media/Vorbis/Header/Comment.php @@ -121,15 +121,53 @@ final class Zend_Media_Vorbis_Header_Comment extends Zend_Media_Vorbis_Header return $this->_comments; } + /** + * Returns an array of comments having the field names as keys and an array of values as a value. The array is + * restricted to field names that matches the given criteria. Unlike the getX() methods, which return the first + * value, this method returns an array of field values. + * + * @return Array + */ + public function getCommentsByName($name) + { + if (!empty($this->_comments[strtoupper($name)])) { + return $this->_comments[strtoupper($name)]; + } + return array(); + } + + /** + * Magic function so that $obj->X() or $obj->getX() will work, where X is the name of the comment field. The method + * will attempt to return the first field by the given name from the comment. If there is no field with given name, + * an exception is thrown. + * + * @param string $name The field name. + * @return mixed + */ + public function __call($name, $arguments) + { + if (preg_match('/^(?:get)([A-Z].*)$/', $name, $matches)) { + $name = $matches[1]; + } + if (!empty($this->_comments[strtoupper($name)])) { + return $this->_comments[strtoupper($name)][0]; + } + require_once 'Zend/Media/Vorbis/Exception.php'; + throw new Zend_Media_Vorbis_Exception('Unknown field: ' . strtoupper($name)); + } + /** * Magic function so that $obj->value will work. The method will attempt to return the first field by the given * name from the comment. If there is no field with given name, functionality of the parent method is executed. - * + * * @param string $name The field name. * @return mixed */ public function __get($name) { + if (method_exists($this, 'get' . ucfirst($name))) { + return call_user_func(array($this, 'get' . ucfirst($name))); + } if (!empty($this->_comments[strtoupper($name)])) { return $this->_comments[strtoupper($name)][0]; }