From: Matthias Schmidt Date: Fri, 28 Dec 2012 12:27:14 +0000 (+0100) Subject: Adds support for object decorators in DatabaseObjectList X-Git-Tag: 2.0.0_Beta_1~656^2~4^2 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=55be48ebba8d459f7d279a9e94d2594db0417c35;p=GitHub%2FWoltLab%2FWCF.git Adds support for object decorators in DatabaseObjectList --- diff --git a/wcfsetup/install/files/lib/data/DatabaseObjectList.class.php b/wcfsetup/install/files/lib/data/DatabaseObjectList.class.php index adb014e440..745700b0ba 100644 --- a/wcfsetup/install/files/lib/data/DatabaseObjectList.class.php +++ b/wcfsetup/install/files/lib/data/DatabaseObjectList.class.php @@ -3,6 +3,7 @@ namespace wcf\data; use wcf\system\database\util\PreparedStatementConditionBuilder; use wcf\system\exception\SystemException; use wcf\system\WCF; +use wcf\util\ClassUtil; use wcf\util\StringUtil; /** @@ -22,6 +23,12 @@ abstract class DatabaseObjectList implements \Countable, ITraversableObject { */ public $className = ''; + /** + * class name of the object decorator; if left empty, no decorator is used + * @var string + */ + public $decoratorClassName = ''; + /** * object class name * @var string @@ -113,6 +120,19 @@ abstract class DatabaseObjectList implements \Countable, ITraversableObject { } } + if (!empty($this->decoratorClassName)) { + // validate decorator class name + if (!ClassUtil::isInstanceOf($this->decoratorClassName, 'wcf\data\DatabaseObjectDecorator')) { + throw new SystemException("'".$this->decoratorClassName."' should extend 'wcf\data\DatabaseObjectDecorator'"); + } + + $objectClassName = $this->objectClassName ?: $this->className; + $baseClassName = call_user_func(array($this->decoratorClassName, 'getBaseClass')); + if ($objectClassName != $baseClassName && !ClassUtil::isInstanceOf($baseClassName, $objectClassName)) { + throw new SystemException("'".$this->decoratorClassName."' can't decorate objects of class '".$objectClassName."'"); + } + } + $this->conditionBuilder = new PreparedStatementConditionBuilder(); } @@ -179,6 +199,14 @@ abstract class DatabaseObjectList implements \Countable, ITraversableObject { $this->objects = $statement->fetchObjects(($this->objectClassName ?: $this->className)); } + // decorate objects + if (!empty($this->decoratorClassName)) { + foreach ($this->objects as &$object) { + $object = new $this->decoratorClassName($object); + } + unset($object); + } + // use table index as array index $objects = array(); foreach ($this->objects as $object) {