Ver Fonte

Update post model

Visman há 5 anos atrás
pai
commit
a83848a57f
2 ficheiros alterados com 19 adições e 4 exclusões
  1. 16 0
      app/Models/Post/Load.php
  2. 3 4
      app/Models/Post/Manager.php

+ 16 - 0
app/Models/Post/Load.php

@@ -4,6 +4,7 @@ namespace ForkBB\Models\Post;
 
 use ForkBB\Models\Action;
 use ForkBB\Models\Post\Model as Post;
+use InvalidArgumentException;
 
 class Load extends Action
 {
@@ -68,10 +69,19 @@ class Load extends Action
      * @param int $id
      * @param int $tid
      *
+     * @throws InvalidArgumentException
+     *
      * @return null|Post
      */
     public function loadFromTopic(int $id, int $tid): ?Post
     {
+        if ($id < 1) {
+            throw new InvalidArgumentException('Expected a positive post id');
+        }
+        if ($tid < 1) {
+            throw new InvalidArgumentException('Expected a positive topic id');
+        }
+
         $vars = [
             ':pid' => $id,
             ':tid' => $tid,
@@ -104,10 +114,16 @@ class Load extends Action
      *
      * @param int $id
      *
+     * @throws InvalidArgumentException
+     *
      * @return null|Post
      */
     public function load(int $id): ?Post
     {
+        if ($id < 1) {
+            throw new InvalidArgumentException('Expected a positive post id');
+        }
+
         if ($this->c->user->isGuest) {
             $vars = [
                 ':pid' => $id,

+ 3 - 4
app/Models/Post/Manager.php

@@ -4,7 +4,6 @@ namespace ForkBB\Models\Post;
 
 use ForkBB\Models\ManagerModel;
 use ForkBB\Models\Post\Model as Post;
-use RuntimeException;
 
 class Manager extends ManagerModel
 {
@@ -30,10 +29,10 @@ class Manager extends ManagerModel
      */
     public function load(int $id, int $tid = null): ?Post
     {
-        $post = $this->get($id);
+        if ($this->isset($id)) {
+            $post = $this->get($id);
 
-        if ($post instanceof Post) {
-            if (null !== $tid && $post->topic_id !== $tid) {
+            if ($post instanceof Post && null !== $tid && $post->topic_id !== $tid) {
                 return null;
             }
         } else {