Use objects for internal structures created in TextFormat::toJSON() (#3747)

This commit is contained in:
Dylan T 2020-08-15 18:30:26 +01:00 committed by GitHub
parent 700e0afee0
commit 730ee74a65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 104 additions and 45 deletions

View File

@ -141,8 +141,8 @@ abstract class TextFormat{
if(!is_array($string)){ if(!is_array($string)){
$string = self::tokenize($string); $string = self::tokenize($string);
} }
$newString = []; $newString = new TextFormatJsonObject();
$pointer =& $newString; $pointer = $newString;
$color = "white"; $color = "white";
$bold = false; $bold = false;
$italic = false; $italic = false;
@ -152,165 +152,164 @@ abstract class TextFormat{
$index = 0; $index = 0;
foreach($string as $token){ foreach($string as $token){
if(isset($pointer["text"])){ if($pointer->text !== null){
if(!isset($newString["extra"])){ if($newString->extra === null){
$newString["extra"] = []; $newString->extra = [];
} }
$newString["extra"][$index] = []; $newString->extra[$index] = $pointer = new TextFormatJsonObject();
$pointer =& $newString["extra"][$index];
if($color !== "white"){ if($color !== "white"){
$pointer["color"] = $color; $pointer->color = $color;
} }
if($bold){ if($bold){
$pointer["bold"] = true; $pointer->bold = true;
} }
if($italic){ if($italic){
$pointer["italic"] = true; $pointer->italic = true;
} }
if($underlined){ if($underlined){
$pointer["underlined"] = true; $pointer->underlined = true;
} }
if($strikethrough){ if($strikethrough){
$pointer["strikethrough"] = true; $pointer->strikethrough = true;
} }
if($obfuscated){ if($obfuscated){
$pointer["obfuscated"] = true; $pointer->obfuscated = true;
} }
++$index; ++$index;
} }
switch($token){ switch($token){
case TextFormat::BOLD: case TextFormat::BOLD:
if(!$bold){ if(!$bold){
$pointer["bold"] = true; $pointer->bold = true;
$bold = true; $bold = true;
} }
break; break;
case TextFormat::OBFUSCATED: case TextFormat::OBFUSCATED:
if(!$obfuscated){ if(!$obfuscated){
$pointer["obfuscated"] = true; $pointer->obfuscated = true;
$obfuscated = true; $obfuscated = true;
} }
break; break;
case TextFormat::ITALIC: case TextFormat::ITALIC:
if(!$italic){ if(!$italic){
$pointer["italic"] = true; $pointer->italic = true;
$italic = true; $italic = true;
} }
break; break;
case TextFormat::UNDERLINE: case TextFormat::UNDERLINE:
if(!$underlined){ if(!$underlined){
$pointer["underlined"] = true; $pointer->underlined = true;
$underlined = true; $underlined = true;
} }
break; break;
case TextFormat::STRIKETHROUGH: case TextFormat::STRIKETHROUGH:
if(!$strikethrough){ if(!$strikethrough){
$pointer["strikethrough"] = true; $pointer->strikethrough = true;
$strikethrough = true; $strikethrough = true;
} }
break; break;
case TextFormat::RESET: case TextFormat::RESET:
if($color !== "white"){ if($color !== "white"){
$pointer["color"] = "white"; $pointer->color = "white";
$color = "white"; $color = "white";
} }
if($bold){ if($bold){
$pointer["bold"] = false; $pointer->bold = false;
$bold = false; $bold = false;
} }
if($italic){ if($italic){
$pointer["italic"] = false; $pointer->italic = false;
$italic = false; $italic = false;
} }
if($underlined){ if($underlined){
$pointer["underlined"] = false; $pointer->underlined = false;
$underlined = false; $underlined = false;
} }
if($strikethrough){ if($strikethrough){
$pointer["strikethrough"] = false; $pointer->strikethrough = false;
$strikethrough = false; $strikethrough = false;
} }
if($obfuscated){ if($obfuscated){
$pointer["obfuscated"] = false; $pointer->obfuscated = false;
$obfuscated = false; $obfuscated = false;
} }
break; break;
//Colors //Colors
case TextFormat::BLACK: case TextFormat::BLACK:
$pointer["color"] = "black"; $pointer->color = "black";
$color = "black"; $color = "black";
break; break;
case TextFormat::DARK_BLUE: case TextFormat::DARK_BLUE:
$pointer["color"] = "dark_blue"; $pointer->color = "dark_blue";
$color = "dark_blue"; $color = "dark_blue";
break; break;
case TextFormat::DARK_GREEN: case TextFormat::DARK_GREEN:
$pointer["color"] = "dark_green"; $pointer->color = "dark_green";
$color = "dark_green"; $color = "dark_green";
break; break;
case TextFormat::DARK_AQUA: case TextFormat::DARK_AQUA:
$pointer["color"] = "dark_aqua"; $pointer->color = "dark_aqua";
$color = "dark_aqua"; $color = "dark_aqua";
break; break;
case TextFormat::DARK_RED: case TextFormat::DARK_RED:
$pointer["color"] = "dark_red"; $pointer->color = "dark_red";
$color = "dark_red"; $color = "dark_red";
break; break;
case TextFormat::DARK_PURPLE: case TextFormat::DARK_PURPLE:
$pointer["color"] = "dark_purple"; $pointer->color = "dark_purple";
$color = "dark_purple"; $color = "dark_purple";
break; break;
case TextFormat::GOLD: case TextFormat::GOLD:
$pointer["color"] = "gold"; $pointer->color = "gold";
$color = "gold"; $color = "gold";
break; break;
case TextFormat::GRAY: case TextFormat::GRAY:
$pointer["color"] = "gray"; $pointer->color = "gray";
$color = "gray"; $color = "gray";
break; break;
case TextFormat::DARK_GRAY: case TextFormat::DARK_GRAY:
$pointer["color"] = "dark_gray"; $pointer->color = "dark_gray";
$color = "dark_gray"; $color = "dark_gray";
break; break;
case TextFormat::BLUE: case TextFormat::BLUE:
$pointer["color"] = "blue"; $pointer->color = "blue";
$color = "blue"; $color = "blue";
break; break;
case TextFormat::GREEN: case TextFormat::GREEN:
$pointer["color"] = "green"; $pointer->color = "green";
$color = "green"; $color = "green";
break; break;
case TextFormat::AQUA: case TextFormat::AQUA:
$pointer["color"] = "aqua"; $pointer->color = "aqua";
$color = "aqua"; $color = "aqua";
break; break;
case TextFormat::RED: case TextFormat::RED:
$pointer["color"] = "red"; $pointer->color = "red";
$color = "red"; $color = "red";
break; break;
case TextFormat::LIGHT_PURPLE: case TextFormat::LIGHT_PURPLE:
$pointer["color"] = "light_purple"; $pointer->color = "light_purple";
$color = "light_purple"; $color = "light_purple";
break; break;
case TextFormat::YELLOW: case TextFormat::YELLOW:
$pointer["color"] = "yellow"; $pointer->color = "yellow";
$color = "yellow"; $color = "yellow";
break; break;
case TextFormat::WHITE: case TextFormat::WHITE:
$pointer["color"] = "white"; $pointer->color = "white";
$color = "white"; $color = "white";
break; break;
default: default:
$pointer["text"] = $token; $pointer->text = $token;
break; break;
} }
} }
if(isset($newString["extra"])){ if($newString->extra !== null){
foreach($newString["extra"] as $k => $d){ foreach($newString->extra as $k => $d){
if(!isset($d["text"])){ if($d->text === null){
unset($newString["extra"][$k]); unset($newString->extra[$k]);
} }
} }
} }

View File

@ -0,0 +1,60 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\utils;
/**
* @internal
* @see TextFormat::toJSON()
*/
final class TextFormatJsonObject implements \JsonSerializable{
/** @var string|null */
public $text = null;
/** @var string|null */
public $color = null;
/** @var bool|null */
public $bold = null;
/** @var bool|null */
public $italic = null;
/** @var bool|null */
public $underlined = null;
/** @var bool|null */
public $strikethrough = null;
/** @var bool|null */
public $obfuscated = null;
/**
* @var TextFormatJsonObject[]|null
* @phpstan-var array<int, TextFormatJsonObject>|null
*/
public $extra = null;
public function jsonSerialize(){
$result = (array) $this;
foreach($result as $k => $v){
if($v === null){
unset($result[$k]);
}
}
return $result;
}
}