You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
3.6 KiB
PHP

<?php
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
class NewsCategory {
private $category;
const NONE_ALL = -1;
const BUSINESS = 1;
const ENTERTAINMENT = 2;
const GENERAL = 4;
const HEALTH = 8;
const SCIENCE = 16;
const SPORTS = 32;
const TECHNOLOGY = 64;
const SOCIAL = 128;
const CATEGORIES = [
self::BUSINESS => "business",
self::ENTERTAINMENT => "entertainment",
self::GENERAL => "general",
self::HEALTH => "health",
self::SCIENCE => "science",
self::SPORTS => "sports",
self::TECHNOLOGY => "technology",
self::SOCIAL => "social"
];
public function __construct(int $category) {
$this->category = $category;
}
/**
* Get the category as an int corresponding to one of the constants.
* @return int
*/
public function get(): int {
return $this->category;
}
/**
* Get a string representation of the category.
* @return string
*/
public function toString(): string {
switch ($this->category) {
case self::BUSINESS:
return "business";
case self::ENTERTAINMENT:
return "entertainment";
case self::GENERAL:
return "general";
case self::HEALTH:
return "health";
case self::SCIENCE:
return "science";
case self::SPORTS:
return "sports";
case self::TECHNOLOGY:
return "technology";
case self::SOCIAL:
return "social";
default:
return "";
}
}
public static function fromString(string $category): NewsCategory {
$cat = self::NONE_ALL;
switch (strtolower($category)) {
case "business":
$cat = self::BUSINESS;
break;
case "entertainment":
$cat = self::ENTERTAINMENT;
break;
case "general":
$cat = self::GENERAL;
break;
case "health":
$cat = self::HEALTH;
break;
case "science":
$cat = self::SCIENCE;
break;
case "sports":
$cat = self::SPORTS;
break;
case "technology":
case "tech":
$cat = self::TECHNOLOGY;
break;
case "social":
$cat = self::SOCIAL;
break;
}
return new NewsCategory($cat);
}
/**
* Get a suitable FontAwesome 5 icon for the category.
* @return string CSS classes, such as "fas fa-icon".
*/
public function getIcon(): string {
switch ($this->category) {
case self::BUSINESS:
return "fas fa-briefcase";
case self::ENTERTAINMENT:
return "fas fa-tv";
case self::GENERAL:
return "fas fa-info-circle";
case self::HEALTH:
return "fas fa-heartbeat";
case self::SCIENCE:
return "fas fa-atom";
case self::SPORTS:
return "fas fa-futbol";
case self::TECHNOLOGY:
return "fas fa-laptop";
case self::SOCIAL:
return "fas fa-share-alt";
default:
return "fas fa-newspaper";
}
}
}