Skip to content

Commit

Permalink
Add PHPDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
Spirit55555 committed May 8, 2021
1 parent acf156e commit 7e40312
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/MinecraftColors.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

namespace Spirit55555\Minecraft;

/**
* Convert Minecraft color codes to HTML/CSS. Can also remove the color codes.
*/
class MinecraftColors {
const REGEX = '/(?:§|&)([0-9a-fklmnor])/i';
const REGEX_HEX = '/(?:§|&)(#[0-9a-z]{6})/i';
Expand All @@ -33,6 +36,11 @@ class MinecraftColors {
const EMPTY_TAGS = '/<[^\/>]*>([\s]?)*<\/[^>]*>/';
const LINE_BREAK = '<br />';

/**
* Color codes mapped to HEX colors.
*
* @var array
*/
static private $colors = array(
'0' => '000000', //Black
'1' => '0000AA', //Dark Blue
Expand All @@ -52,6 +60,12 @@ class MinecraftColors {
'f' => 'FFFFFF' //White
);

/**
* Formatting codes mapped to CSS style.
* Some codes intentionally have no CSS.
*
* @var array
*/
static private $formatting = array(
'k' => '', //Obfuscated
'l' => 'font-weight: bold;', //Bold
Expand All @@ -61,6 +75,11 @@ class MinecraftColors {
'r' => '' //Reset
);

/**
* Colors and formatting codes mapped to CSS classes.
*
* @var array
*/
static private $css_classnames = array(
'0' => 'black',
'1' => 'dark-blue',
Expand All @@ -85,6 +104,12 @@ class MinecraftColors {
'o' => 'italic'
);

/**
* Encode text in UTF-8.
*
* @param mixed $text
* @return string
*/
static private function UFT8Encode(string $text): string {
//Encode the text in UTF-8, but only if it's not already.
if (mb_detect_encoding($text) != 'UTF-8')
Expand All @@ -93,13 +118,27 @@ static private function UFT8Encode(string $text): string {
return $text;
}

/**
* Clean a string from all colors and formatting codes.
*
* @param string $text
* @return string
*/
static public function clean(string $text): string {
$text = self::UFT8Encode($text);
$text = htmlspecialchars($text);

return preg_replace(self::REGEX_ALL, '', $text);
}

/**
* Convert a string to a MOTD format.
*
* @param string $text
* @param string $sign The text to prepend all color codes.
* @param bool $hex_colors Should HEX colors be converted as well? If not, they will be removed.
* @return string
*/
static public function convertToMOTD(string $text, string $sign = '\u00A7', bool $hex_colors = false): string {
$text = self::UFT8Encode($text);
$text = str_replace("&", "&amp;", $text);
Expand Down Expand Up @@ -127,6 +166,15 @@ function($matches) use ($sign) {
return $text;
}

/**
* Convert a string to HTML.
*
* @param string $text
* @param bool $line_break_element Should new lines be converted to br tags?
* @param bool $css_classes Should CSS classes be used instead of inline styes?
* @param string $css_prefix The prefix for CSS classes.
* @return string
*/
static public function convertToHTML(string $text, bool $line_break_element = false, bool $css_classes = false, string $css_prefix = 'minecraft-formatted--'): string {
$text = self::UFT8Encode($text);
$text = htmlspecialchars($text);
Expand Down
26 changes: 26 additions & 0 deletions src/MinecraftJSONColors.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,19 @@
namespace Spirit55555\Minecraft;

/**
* Convert Minecraft JSON text to legacy format.
*
* Based on http://wiki.vg/Chat
*/
class MinecraftJSONColors {
static private $color_char;
static private $hex_colors;

/**
* Color names types mapped to legacy codes.
*
* @var array
*/
static private $colors = array(
'black' => '0',
'dark_blue' => '1',
Expand All @@ -46,6 +53,11 @@ class MinecraftJSONColors {
'white' => 'f'
);

/**
* Formatting names mapped to legacy codes.
*
* @var array
*/
static private $formatting = array(
'obfuscated' => 'k',
'bold' => 'l',
Expand All @@ -55,6 +67,14 @@ class MinecraftJSONColors {
'reset' => 'r'
);

/**
* Convert Minecraft JSON text to legacy format.
*
* @param string|array $json JSON as a string or an array.
* @param string $color_char The text to prepend all color codes.
* @param bool $hex_colors Should HEX colors be converted as well? If not, they will be skipped.
* @return string
*/
public static function convertToLegacy($json, string $color_char = '§', bool $hex_colors = false): string {
self::$color_char = $color_char;
self::$hex_colors = $hex_colors;
Expand Down Expand Up @@ -92,6 +112,12 @@ public static function convertToLegacy($json, string $color_char = '§', bool $h
return $legacy;
}

/**
* Parse an array to a legacy string with color codes.
*
* @param array $json
* @return string
*/
private static function parseElement(array $json): string {
$legacy = '';

Expand Down

0 comments on commit 7e40312

Please sign in to comment.