This repository has been archived on 2024-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
sakura/app/Rank.php

155 lines
3.4 KiB
PHP
Raw Normal View History

<?php
2016-02-03 22:22:56 +00:00
/**
* Holds the rank object class.
* @package Sakura
*/
namespace Sakura;
/**
2016-02-02 21:04:15 +00:00
* Serves Rank data.
* @package Sakura
2016-02-02 21:04:15 +00:00
* @author Julian van de Groep <me@flash.moe>
*/
class Rank
{
2016-02-02 21:04:15 +00:00
/**
* ID of the rank.
* @var int
*/
2016-01-17 01:58:31 +00:00
public $id = 0;
2016-02-02 21:04:15 +00:00
/**
* Name of the rank.
* @var string
*/
2016-01-17 01:58:31 +00:00
public $name = 'Rank';
2016-02-02 21:04:15 +00:00
/**
* Global hierarchy of the rank.
* @var int
*/
2016-01-17 01:58:31 +00:00
public $hierarchy = 0;
2016-02-02 21:04:15 +00:00
/**
* Text that should be append to the name to make it address multiple.
* @var string
*/
2016-01-17 01:58:31 +00:00
public $multiple = '';
2016-02-02 21:04:15 +00:00
/**
* The rank's username colour.
* @var string
*/
2016-01-17 01:58:31 +00:00
public $colour = 'inherit';
2016-02-02 21:04:15 +00:00
/**
* Description of the rank.
* @var string
*/
2016-01-17 01:58:31 +00:00
public $description = '';
2016-02-02 21:04:15 +00:00
/**
* User title of the rank.
* @var string
*/
2016-01-17 01:58:31 +00:00
public $title = '';
2016-02-02 21:04:15 +00:00
/**
* Indicates if this rank should be hidden.
* @var bool
*/
2016-12-04 16:33:52 +00:00
public $hidden = true;
2016-02-02 21:04:15 +00:00
/**
* Instance cache container.
* @var array
*/
2016-07-30 13:48:09 +00:00
protected static $rankCache = [];
2016-02-05 11:20:33 +00:00
2016-02-02 21:04:15 +00:00
/**
* Cached constructor.
2016-08-05 02:35:37 +00:00
* @param int $rid
* @param bool $forceRefresh
* @return Rank
2016-02-02 21:04:15 +00:00
*/
2016-12-04 16:33:52 +00:00
public static function construct(int $rid, bool $forceRefresh = false): Rank
{
2015-12-29 21:52:19 +00:00
// Check if a rank object isn't present in cache
2016-07-30 13:48:09 +00:00
if ($forceRefresh || !array_key_exists($rid, self::$rankCache)) {
2015-12-29 21:52:19 +00:00
// If not create a new object and cache it
2016-07-30 13:48:09 +00:00
self::$rankCache[$rid] = new Rank($rid);
2015-12-29 21:52:19 +00:00
}
// Return the cached object
2016-07-30 13:48:09 +00:00
return self::$rankCache[$rid];
2015-12-29 21:52:19 +00:00
}
2016-02-02 21:04:15 +00:00
/**
* Constructor.
2016-08-05 02:35:37 +00:00
* @param int $rankId
2016-02-02 21:04:15 +00:00
*/
2016-12-04 16:33:52 +00:00
private function __construct(int $rankId)
{
// Get the rank database row
2016-02-25 16:06:29 +00:00
$rankRow = DB::table('ranks')
->where('rank_id', $rankId)
2016-08-06 14:09:01 +00:00
->first();
// Check if the rank actually exists
2016-01-17 01:58:31 +00:00
if ($rankRow) {
2016-02-18 23:28:44 +00:00
$this->id = $rankRow->rank_id;
$this->name = $rankRow->rank_name;
$this->hierarchy = $rankRow->rank_hierarchy;
$this->multiple = $rankRow->rank_multiple;
$this->hidden = (bool) $rankRow->rank_hidden;
$this->colour = $rankRow->rank_colour;
$this->description = $rankRow->rank_description;
$this->title = $rankRow->rank_title;
}
}
2016-02-02 21:04:15 +00:00
/**
* Get the name of the rank.
2016-08-05 02:35:37 +00:00
* @param bool $multi
* @return string
2016-02-02 21:04:15 +00:00
*/
2016-12-04 16:33:52 +00:00
public function name(bool $multi = false): string
{
2016-01-17 01:58:31 +00:00
return $this->name . ($multi ? $this->multiple : null);
}
2016-02-05 11:20:33 +00:00
/**
* Returns all users that are part of this rank.
2016-08-05 02:35:37 +00:00
* @param bool $justIds
* @return array
2016-02-05 11:20:33 +00:00
*/
2016-12-04 16:33:52 +00:00
public function users(bool $justIds = false): array
2016-02-05 11:20:33 +00:00
{
// Fetch all users part of this rank
$get = DB::table('user_ranks')
->where('rank_id', $this->id)
2016-03-24 18:28:32 +00:00
->orderBy('user_id')
->get(['user_id']);
// Filter the user ids into one array
$userIds = array_column($get, 'user_id');
2016-02-05 11:20:33 +00:00
// Just return that if we were asked for just the ids
if ($justIds) {
return $userIds;
}
// Create the storage array
$users = [];
// Create User objects and store
foreach ($userIds as $id) {
$users[$id] = User::construct($id);
}
// Return the array
return $users;
}
}