Add CommunityTag & parse ASGL tags
parent
a8f0464149
commit
bfa64118fa
@ -0,0 +1,124 @@
|
|||||||
|
<?php
|
||||||
|
require_once 'utils.php';
|
||||||
|
|
||||||
|
class TagType {
|
||||||
|
private function __construct() {}
|
||||||
|
const USER_TAG = 0;
|
||||||
|
const RESERVED_TAG = 1;
|
||||||
|
const WARNING_TAG = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
class CommunityTag implements JsonSerializable {
|
||||||
|
public function __construct(string $text, int $tag_type = TagType::USER_TAG) {
|
||||||
|
$this->text = $text;
|
||||||
|
$this->type = $tag_type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly int $type;
|
||||||
|
|
||||||
|
public readonly string $text;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a lowercase representation of the tag for purposes of de-duping.
|
||||||
|
*/
|
||||||
|
public function __toString(): string {
|
||||||
|
return strtolower($this->text);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function jsonSerialize(): mixed {
|
||||||
|
if ($this->type != TagType::USER_TAG) {
|
||||||
|
throw new LogicException("Should not serialize derived tags.");
|
||||||
|
}
|
||||||
|
return $this->text;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function preprocess_tag(?string $tag) {
|
||||||
|
$tag = trim($tag);
|
||||||
|
|
||||||
|
if (strlen($tag) == 0) {
|
||||||
|
return $tag;
|
||||||
|
}
|
||||||
|
|
||||||
|
$tag = html_sanitize(html_entity_decode($tag));
|
||||||
|
|
||||||
|
if ($tag[0] == '#') {
|
||||||
|
return substr($tag, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $tag;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string[] $tag_array
|
||||||
|
* @return \CommunityTag[]
|
||||||
|
*/
|
||||||
|
private static function from_tag_array(array $tag_array) {
|
||||||
|
$tags = array_map(function(?string $tag) {
|
||||||
|
return CommunityTag::preprocess_tag($tag);
|
||||||
|
}, $tag_array);
|
||||||
|
|
||||||
|
$tags = array_filter(
|
||||||
|
$tags, function(?string $tag) {
|
||||||
|
return strlen($tag) != 0;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return array_map(function(string $tag) {
|
||||||
|
return new CommunityTag($tag);
|
||||||
|
}, $tags);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the user tags given, without any reserved tags.
|
||||||
|
* @param string[] $tags
|
||||||
|
* @return \CommunityTag[]
|
||||||
|
*/
|
||||||
|
public static function from_user_tags(array $tags): array {
|
||||||
|
$tags_user = array_filter(
|
||||||
|
$tags,
|
||||||
|
function($tag) {
|
||||||
|
return !CommunityTag::is_reserved_tag($tag);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return CommunityTag::from_tag_array($tags_user);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string[] $details_array Array of string tags.
|
||||||
|
* @return \CommunityTag[]
|
||||||
|
*/
|
||||||
|
public static function from_details_array(array $details_array): array {
|
||||||
|
return CommunityTag::from_user_tags($details_array);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \CommunityTag[] $tags
|
||||||
|
* @return \CommunityTag[]
|
||||||
|
*/
|
||||||
|
public static function dedupe_tags(array $tags) {
|
||||||
|
return array_unique($tags);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get_tag_type(): string {
|
||||||
|
return match($this->type) {
|
||||||
|
TagType::USER_TAG => 'user',
|
||||||
|
TagType::RESERVED_TAG => 'reserved',
|
||||||
|
TagType::WARNING_TAG => 'warning'
|
||||||
|
};
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @var string[] RESERVED_TAGS
|
||||||
|
* Array of derived tags unavailable for manual tagging.
|
||||||
|
*/
|
||||||
|
private const RESERVED_TAGS = ["official"];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether the given manual tag can be accepted.
|
||||||
|
*/
|
||||||
|
public static function is_reserved_tag(string $tag): bool {
|
||||||
|
return in_array(strtolower($tag), CommunityTag::RESERVED_TAGS);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
?>
|
Loading…
Reference in New Issue