浏览代码

Add Models\PM\PBlock

Visman 3 年之前
父节点
当前提交
40ea64c25a
共有 1 个文件被更改,包括 106 次插入0 次删除
  1. 106 0
      app/Models/PM/PBlock.php

+ 106 - 0
app/Models/PM/PBlock.php

@@ -0,0 +1,106 @@
+<?php
+/**
+ * This file is part of the ForkBB <https://github.com/forkbb>.
+ *
+ * @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
+ * @license   The MIT License (MIT)
+ */
+
+declare(strict_types=1);
+
+namespace ForkBB\Models\PM;
+
+use ForkBB\Core\Container;
+use ForkBB\Models\Model;
+use ForkBB\Models\PM\Cnst;
+use ForkBB\Models\User\Model as User;
+use InvalidArgumentException;
+use RuntimeException;
+
+class PBlock extends Model
+{
+    /**
+     * @var array
+     */
+    protected $repository;
+
+    /**
+     * Текущий пользователь установленный в методе init()
+     * @var User
+     */
+    protected $user;
+
+    public function __construct(Container $container)
+    {
+        parent::__construct($container);
+
+        $this->init($container->user);
+    }
+
+    public function init(User $user): void
+    {
+        $this->repository = [];
+        $this->user       = $user;
+
+        $vars = [
+            ':id' => $user->id,
+        ];
+        $query = 'SELECT pb.bl_first_id, pb.bl_second_id
+                    FROM ::pm_block AS pb
+                   WHERE pb.bl_first_id=?i:id OR pb.bl_second_id=?i:id';
+
+        $stmt = $this->c->DB->query($query, $vars);
+
+        while ($row = $stmt->fetch()) {
+            if ($row['bl_first_id'] === $user->id) {
+                $this->repository[$user->id][$row['bl_second_id']] = $row['bl_second_id'];
+            } elseif ($row['bl_second_id'] !== $user->id) {
+                $this->repository[$row['bl_second_id']] = true;
+            }
+        }
+    }
+
+    /**
+     * Проверяет: $user в блоке у $this->user
+     */
+    public function isBlock(User $user): bool
+    {
+        return isset($this->repository[$this->user->id][$user->id]);
+    }
+
+    /**
+     * Проверяет: $this->user в блоке у $user
+     */
+    public function inBlock(User $user): bool
+    {
+        return isset($this->repository[$user->id]);
+    }
+
+    /**
+     * Установка блока: $this->user -> $user
+     */
+    public function add(User $user): bool
+    {
+        $vars = [
+            ':first_id'  => $this->user->id,
+            ':second_id' => $user->id,
+        ];
+        $query = 'INSERT INTO ::pm_block (bl_first_id, bl_second_id) VALUES (:first_id, :second_id)';
+
+        return false !== $this->c->DB->exec($query, $vars);
+    }
+
+    /**
+     * Снятие блока: $this->user -> $user
+     */
+    public function remove(User $user): bool
+    {
+        $vars = [
+            ':first_id'  => $this->user->id,
+            ':second_id' => $user->id,
+        ];
+        $query = 'DELETE FROM ::pm_block WHERE bl_first_id=:first_id AND bl_second_id=:second_id';
+
+        return false !== $this->c->DB->exec($query, $vars);
+    }
+}