Adds support for object decorators in DatabaseObjectList
authorMatthias Schmidt <gravatronics@live.com>
Fri, 28 Dec 2012 12:27:14 +0000 (13:27 +0100)
committerMatthias Schmidt <gravatronics@live.com>
Fri, 28 Dec 2012 12:27:14 +0000 (13:27 +0100)
wcfsetup/install/files/lib/data/DatabaseObjectList.class.php

index adb014e440c78052a6099364aedd194ee058be21..745700b0ba27558428a419b323163e0cf514ccd5 100644 (file)
@@ -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) {