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);
+ }
}
$list->enableContentLoading($enableContentLoading);
$list->setObjectIDs([$articleID]);
$list->readObjects();
- $objects = $list->getObjects();
- if (isset($objects[$articleID])) return $objects[$articleID];
- return null;
+
+ return $list->getSingleObject();
}
/**
$list = new ViewableCommentList();
$list->setObjectIDs([$commentID]);
$list->readObjects();
- $objects = $list->getObjects();
- if (isset($objects[$commentID])) return $objects[$commentID];
- return null;
+
+ return $list->getSingleObject();
}
}
$list = new ViewableCommentResponseList();
$list->setObjectIDs([$responseID]);
$list->readObjects();
- $objects = $list->getObjects();
- if (isset($objects[$responseID])) return $objects[$responseID];
- return null;
+
+ return $list->getSingleObject();
}
}
$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();
}
/**
$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();
}
}
}