浏览代码

Add insert and update for BanList

Visman 5 年之前
父节点
当前提交
efdc802b77
共有 2 个文件被更改,包括 81 次插入0 次删除
  1. 41 0
      app/Models/BanList/Insert.php
  2. 40 0
      app/Models/BanList/Update.php

+ 41 - 0
app/Models/BanList/Insert.php

@@ -0,0 +1,41 @@
+<?php
+
+namespace ForkBB\Models\BanList;
+
+use ForkBB\Models\Method;
+use InvalidArgumentException;
+
+class Insert extends Method
+{
+    /**
+     * Добавляет новый бан
+     *
+     * @param array $ban
+     *
+     * @return BanList\Model
+     */
+    public function insert(array $ban)
+    {
+        if (isset($ban['id'])
+            || ! isset($ban['username'])
+            || ! isset($ban['ip'])
+            || ! isset($ban['email'])
+            || ! isset($ban['message'])
+            || ! isset($ban['expire'])
+        ) {
+            throw new InvalidArgumentException('Expected an array with a ban description');
+        }
+
+        if ('' == $ban['username'] && '' == $ban['ip'] && '' == $ban['email']) {
+            throw new InvalidArgumentException('Empty ban');
+        }
+
+        $ban['creator'] = $this->c->user->id;
+
+        $sql = 'INSERT INTO ::bans (username, ip, email, message, expire, ban_creator)
+                VALUES (?s:username, ?s:ip, ?s:email, ?s:message, ?i:expire, ?i:creator)';
+        $this->c->DB->exec($sql, $ban);
+
+        return $this->model;
+    }
+}

+ 40 - 0
app/Models/BanList/Update.php

@@ -0,0 +1,40 @@
+<?php
+
+namespace ForkBB\Models\BanList;
+
+use ForkBB\Models\Method;
+use InvalidArgumentException;
+
+class Update extends Method
+{
+    /**
+     * Обновляет бан
+     *
+     * @param array $ban
+     *
+     * @return BanList\Model
+     */
+    public function update(array $ban)
+    {
+        if (empty($ban['id'])
+            || ! isset($ban['username'])
+            || ! isset($ban['ip'])
+            || ! isset($ban['email'])
+            || ! isset($ban['message'])
+            || ! isset($ban['expire'])
+        ) {
+            throw new InvalidArgumentException('Expected an array with a ban description');
+        }
+
+        if ('' == $ban['username'] && '' == $ban['ip'] && '' == $ban['email']) {
+            throw new InvalidArgumentException('Empty ban');
+        }
+
+        $sql = 'UPDATE ::bans
+                SET username=?s:username, ip=?s:ip, email=?s:email, message=?s:message, expire=?i:expire
+                WHERE id=?i:id';
+        $this->c->DB->exec($sql, $ban);
+
+        return $this->model;
+    }
+}