Fix magic functions

git-svn-id: http://php-reader.googlecode.com/svn/trunk@251 51a70ab9-7547-0410-9469-37e369ee0574
This commit is contained in:
svollbehr
2011-06-13 15:41:51 +00:00
parent 8bdcf4209a
commit e411507e3d
3 changed files with 68 additions and 5 deletions

View File

@@ -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)

View File

@@ -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);

View File

@@ -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];
}