You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.1 KiB

13 years ago
<?php
/**
* ReflectionHelper class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\util;
13 years ago
use yii\base\Exception;
13 years ago
/**
* ReflectionHelper
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ReflectionHelper
{
/**
* Prepares parameters so that they can be bound to the specified method.
13 years ago
* This method converts the input parameters into an array that can later be
* passed to `call_user_func_array()` when calling the specified method.
* The conversion is based on the matching of method parameter names
13 years ago
* and the input array keys. For example,
*
* ~~~
* class Foo {
* function bar($a, $b) { ... }
* }
13 years ago
* $object = new Foo;
13 years ago
* $params = array('b' => 2, 'c' => 3, 'a' => 1);
13 years ago
* var_export(ReflectionHelper::extractMethodParams($object, 'bar', $params));
* // output: array('a' => 1, 'b' => 2);
13 years ago
* ~~~
*
13 years ago
* @param object|string $object the object or class name that owns the specified method
* @param string $method the method name
13 years ago
* @param array $params the parameters in terms of name-value pairs
13 years ago
* @return array parameters that are needed by the method only and
* can be passed to the method via `call_user_func_array()`.
* @throws Exception if any required method parameter is not found in the given parameters
13 years ago
*/
13 years ago
public static function extractMethodParams($object, $method, $params)
13 years ago
{
13 years ago
$m = new \ReflectionMethod($object, $method);
13 years ago
$ps = array();
13 years ago
foreach ($m->getParameters() as $param) {
13 years ago
$name = $param->getName();
if (array_key_exists($name, $params)) {
13 years ago
$ps[$name] = $params[$name];
13 years ago
} elseif ($param->isDefaultValueAvailable()) {
$ps[$name] = $param->getDefaultValue();
} else {
13 years ago
throw new Exception(\Yii::t('yii', 'Missing required parameter "{name}".', array('{name' => $name)));
13 years ago
}
}
return $ps;
}
13 years ago
/**
* Initializes an object with the given parameters.
* Only the public non-static properties of the object will be initialized, and their names must
* match the given parameter names. For example,
*
* ~~~
* class Foo {
* public $a;
* protected $b;
* }
* $object = new Foo;
* $params = array('b' => 2, 'c' => 3, 'a' => 1);
* $remaining = ReflectionHelper::bindObjectParams($object, $params);
* var_export($object); // output: $object->a = 1; $object->b = null;
* var_export($remaining); // output: array('b' => 2, 'c' => 3);
* ~~~
*
* @param object $object the object whose properties are to be initialized
* @param array $params the input parameters to be used to initialize the object
* @return array the remaining unused input parameters
*/
public static function initObjectWithParams($object, $params)
{
if (empty($params)) {
return array();
}
$class = new \ReflectionClass(get_class($object));
foreach ($params as $name => $value) {
if ($class->hasProperty($name)) {
$property = $class->getProperty($name);
if ($property->isPublic() && !$property->isStatic()) {
$object->$name = $value;
unset($params[$name]);
}
}
}
return $params;
}
13 years ago
}