From 55be48ebba8d459f7d279a9e94d2594db0417c35 Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Fri, 28 Dec 2012 13:27:14 +0100 Subject: [PATCH] Adds support for object decorators in DatabaseObjectList --- .../lib/data/DatabaseObjectList.class.php | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) 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) { -- 2.20.1