Add DatabaseObjectList::getSingleObject() (#3298)
authorMatthias Schmidt <gravatronics@live.com>
Sun, 17 May 2020 11:03:27 +0000 (13:03 +0200)
committerGitHub <noreply@github.com>
Sun, 17 May 2020 11:03:27 +0000 (13:03 +0200)
Close #3183

wcfsetup/install/files/lib/data/DatabaseObjectList.class.php
wcfsetup/install/files/lib/data/article/ViewableArticle.class.php
wcfsetup/install/files/lib/data/comment/ViewableComment.class.php
wcfsetup/install/files/lib/data/comment/response/ViewableCommentResponse.class.php
wcfsetup/install/files/lib/data/moderation/queue/ViewableModerationQueue.class.php
wcfsetup/install/files/lib/system/package/plugin/StylePackageInstallationPlugin.class.php

index 3078a0caf20f1e98a27b9cded6056374e3f9e332..a2a297fd2721af14de1a40f01237b7dfd352b1f2 100644 (file)
@@ -359,4 +359,22 @@ abstract class DatabaseObjectList implements \Countable, ITraversableObject {
                        return null;
                }
        }
+       
+       /**
+        * Returns the only object in this list or `null` if the list is empty.
+        * 
+        * @return      DatabaseObject|null
+        * @throws      \BadMethodCallException         if list contains more than one object
+        */
+       public function getSingleObject() {
+               if (count($this->objects) > 1) {
+                       throw new \BadMethodCallException("Cannot get a single object when the list contains " . count($this->objects) . " objects.");
+               }
+               
+               if (empty($this->objects)) {
+                       return null;
+               }
+               
+               return reset($this->objects);
+       }
 }
index 448bf072d830d3e14266bb565fa1bddc08cc098a..1984151c1ca9fab8464dedd3c1c8ab49452bb1da 100644 (file)
@@ -84,9 +84,8 @@ class ViewableArticle extends DatabaseObjectDecorator {
                $list->enableContentLoading($enableContentLoading);
                $list->setObjectIDs([$articleID]);
                $list->readObjects();
-               $objects = $list->getObjects();
-               if (isset($objects[$articleID])) return $objects[$articleID];
-               return null;
+               
+               return $list->getSingleObject();
        }
        
        /**
index c253d224c26b488c3d06da8b1b1c88132fe8f5c9..062c8575e6f522e1b352e89759a53926a0600b42 100644 (file)
@@ -58,8 +58,7 @@ class ViewableComment extends DatabaseObjectDecorator {
                $list = new ViewableCommentList();
                $list->setObjectIDs([$commentID]);
                $list->readObjects();
-               $objects = $list->getObjects();
-               if (isset($objects[$commentID])) return $objects[$commentID];
-               return null;
+               
+               return $list->getSingleObject();
        }
 }
index dd803ade89ef7f2d4683ff9abbcd135cead819d7..9e98895d7f73fa26e1d1118fba3cce30c8204a05 100644 (file)
@@ -58,8 +58,7 @@ class ViewableCommentResponse extends DatabaseObjectDecorator {
                $list = new ViewableCommentResponseList();
                $list->setObjectIDs([$responseID]);
                $list->readObjects();
-               $objects = $list->getObjects();
-               if (isset($objects[$responseID])) return $objects[$responseID];
-               return null;
+               
+               return $list->getSingleObject();
        }
 }
index 871cd590662ab7de9ca88adc3df1eb9127dbd373..b9618a0202dada6e1a93434728ecb1aa4240103f 100644 (file)
@@ -157,9 +157,8 @@ class ViewableModerationQueue extends DatabaseObjectDecorator implements ILinkab
                $queueList->getConditionBuilder()->add("moderation_queue.queueID = ?", [$queueID]);
                $queueList->sqlLimit = 1;
                $queueList->readObjects();
-               $queues = $queueList->getObjects();
                
-               return (isset($queues[$queueID]) ? $queues[$queueID] : null);
+               return $queueList->getSingleObject();
        }
        
        /**
index 6971bb702cc02cfa5b2a00e3c46302a86fc8a85b..6b2cd2bd962655e6490ffa11256e0cb11290c936 100644 (file)
@@ -69,11 +69,10 @@ class StylePackageInstallationPlugin extends AbstractPackageInstallationPlugin {
                        $styleList->sqlOrderBy = 'style.styleID ASC';
                        $styleList->sqlLimit = 1;
                        $styleList->readObjects();
-                       $styles = $styleList->getObjects();
+                       $style = $styleList->getSingleObject();
                        
-                       if (!empty($styles)) {
-                               $styleEditor = new StyleEditor(current($styles));
-                               $styleEditor->setAsDefault();
+                       if ($style !== null) {
+                               (new StyleEditor($style))->setAsDefault();
                        }
                }
        }