misuzu/src/Forum/ForumCategoryInfo.php

125 lines
3.8 KiB
PHP

<?php
namespace Misuzu\Forum;
use Carbon\CarbonImmutable;
use Index\Colour\Colour;
use Index\Db\DbResult;
class ForumCategoryInfo {
// should the types just be replaced with flags indicating what's allowed?
public const TYPE_DISCUSSION = 0;
public const TYPE_LISTING = 1;
public const TYPE_LINK = 2;
public const TYPE_ALIASES = [
'discussion' => self::TYPE_DISCUSSION,
'listing' => self::TYPE_LISTING,
'link' => self::TYPE_LINK,
];
public const MAY_HAVE_CHILDREN = [
self::TYPE_DISCUSSION,
self::TYPE_LISTING,
];
public const MAY_HAVE_TOPICS = [
self::TYPE_DISCUSSION,
];
public function __construct(
public private(set) string $id,
public private(set) int $order,
public private(set) ?string $parentId,
public private(set) string $name,
public private(set) int $type,
public private(set) ?string $description,
public private(set) ?string $icon,
public private(set) ?int $colourRaw,
public private(set) ?string $linkTarget,
public private(set) ?int $linkClicks,
public private(set) int $createdTime,
public private(set) bool $archived,
public private(set) bool $hidden,
public private(set) int $topicsCount,
public private(set) int $postsCount,
) {}
public static function fromResult(DbResult $result): ForumCategoryInfo {
return new ForumCategoryInfo(
id: $result->getString(0),
order: $result->getInteger(1),
parentId: $result->getStringOrNull(2),
name: $result->getString(3),
type: $result->getInteger(4),
description: $result->getStringOrNull(5),
icon: $result->getStringOrNull(6),
colourRaw: $result->getIntegerOrNull(7),
linkTarget: $result->getStringOrNull(8),
linkClicks: $result->getIntegerOrNull(9),
createdTime: $result->getInteger(10),
archived: $result->getBoolean(11),
hidden: $result->getBoolean(12),
topicsCount: $result->getInteger(13),
postsCount: $result->getInteger(14),
);
}
public bool $hasParent {
get => $this->parentId !== null && $this->parentId !== '0';
}
public function isDirectChildOf(ForumCategoryInfo|string $parentInfo): bool {
if($parentInfo instanceof ForumCategoryInfo)
$parentInfo = $parentInfo->id;
return $this->hasParent && $this->parentId === $parentInfo;
}
public bool $isDiscussion {
get => $this->type === self::TYPE_DISCUSSION;
}
public bool $isListing {
get => $this->type === self::TYPE_LISTING;
}
public bool $isLink {
get => $this->type === self::TYPE_LINK;
}
public bool $mayHaveChildren {
get => in_array($this->type, self::MAY_HAVE_CHILDREN);
}
public bool $mayHaveTopics {
get => in_array($this->type, self::MAY_HAVE_TOPICS);
}
public string $iconForDisplay {
get {
if(!empty($this->icon))
return $this->icon;
if($this->archived)
return 'fas fa-archive fa-fw';
return match($this->type) {
self::TYPE_LISTING => 'fas fa-folder fa-fw',
self::TYPE_LINK => 'fas fa-link fa-fw',
default => 'fas fa-comments fa-fw',
};
}
}
public bool $hasColour {
get => $this->colourRaw !== null && ($this->colourRaw & 0x40000000) === 0;
}
public Colour $colour {
get => $this->colourRaw === null ? Colour::none() : Colour::fromMisuzu($this->colourRaw);
}
public CarbonImmutable $createdAt {
get => CarbonImmutable::createFromTimestampUTC($this->createdTime);
}
}