From ba4c4929a8297c097c43357e6b1fd660b781c3d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20Klabbers?= Date: Mon, 7 Oct 2019 14:16:10 +0200 Subject: [PATCH] cleaned up the Tag model and obsoleted/unused property --- src/Api/Serializer/TagSerializer.php | 1 - src/Tag.php | 79 ++++++++++------------------ 2 files changed, 28 insertions(+), 52 deletions(-) diff --git a/src/Api/Serializer/TagSerializer.php b/src/Api/Serializer/TagSerializer.php index 367c30ae..0a475a91 100644 --- a/src/Api/Serializer/TagSerializer.php +++ b/src/Api/Serializer/TagSerializer.php @@ -34,7 +34,6 @@ protected function getDefaultAttributes($tag) 'backgroundUrl' => $tag->background_path, 'backgroundMode' => $tag->background_mode, 'icon' => $tag->icon, - 'iconUrl' => $tag->icon_path, 'discussionCount' => (int) $tag->discussion_count, 'position' => $tag->position === null ? null : (int) $tag->position, 'defaultSort' => $tag->default_sort, diff --git a/src/Tag.php b/src/Tag.php index 6db45532..5e4c5e99 100644 --- a/src/Tag.php +++ b/src/Tag.php @@ -18,23 +18,38 @@ use Flarum\User\User; use Illuminate\Database\Eloquent\Builder; +/** + * @property int $id + * @property string $name + * @property string $slug + * @property string $description + * @property string $color + * @property string $background_path + * @property string $background_mode + * @property int $position + * @property int $parent_id + * @property string $default_sort + * @property bool $is_restricted + * @property bool $is_hidden + * @property int $discussion_count + * @property \Carbon\Carbon $last_posted_at + * @property int $last_posted_discussion_id + * @property int $last_posted_user_id + * @property string $icon + */ class Tag extends AbstractModel { use ScopeVisibilityTrait; - /** - * {@inheritdoc} - */ protected $table = 'tags'; - /** - * {@inheritdoc} - */ protected $dates = ['last_posted_at']; - /** - * {@inheritdoc} - */ + protected $casts = [ + 'is_hidden' => 'bool', + 'is_restricted' => 'bool' + ]; + public static function boot() { parent::boot(); @@ -51,6 +66,7 @@ public static function boot() * @param string $slug * @param string $description * @param string $color + * @param string $icon * @param bool $isHidden * @return static */ @@ -68,43 +84,26 @@ public static function build($name, $slug, $description, $color, $icon, $isHidde return $tag; } - /** - * @return \Illuminate\Database\Eloquent\Relations\BelongsTo - */ public function parent() { return $this->belongsTo(self::class); } - /** - * @return \Illuminate\Database\Eloquent\Relations\BelongsTo - */ public function lastPostedDiscussion() { return $this->belongsTo(Discussion::class, 'last_posted_discussion_id'); } - /** - * @return \Illuminate\Database\Eloquent\Relations\BelongsTo - */ public function lastPostedUser() { return $this->belongsTo(User::class, 'last_posted_user_id'); } - /** - * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany - */ public function discussions() { return $this->belongsToMany(Discussion::class); } - /** - * Refresh a tag's last discussion details. - * - * @return $this - */ public function refreshLastPostedDiscussion() { if ($lastPostedDiscussion = $this->discussions()->latest('last_posted_at')->first()) { @@ -114,12 +113,6 @@ public function refreshLastPostedDiscussion() return $this; } - /** - * Set the tag's last discussion details. - * - * @param Discussion $discussion - * @return $this - */ public function setLastPostedDiscussion(Discussion $discussion) { $this->last_posted_at = $discussion->last_posted_at; @@ -173,13 +166,7 @@ public function scopeWithStateFor(Builder $query, User $user) ]); } - /** - * @param User $user - * @param string $permission - * @param bool $condition - * @return array - */ - protected static function getIdsWherePermission(User $user, $permission, $condition = true) + protected static function getIdsWherePermission(User $user, string $permission, bool $condition = true): array { static $tags; @@ -209,22 +196,12 @@ protected static function getIdsWherePermission(User $user, $permission, $condit return $ids; } - /** - * @param User $user - * @param string $permission - * @return array - */ - public static function getIdsWhereCan(User $user, $permission) + public static function getIdsWhereCan(User $user, string $permission): array { return static::getIdsWherePermission($user, $permission, true); } - /** - * @param User $user - * @param string $permission - * @return array - */ - public static function getIdsWhereCannot(User $user, $permission) + public static function getIdsWhereCannot(User $user, string $permission): array { return static::getIdsWherePermission($user, $permission, false); }