-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
490 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/vendor/ | ||
composer.lock | ||
tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "dev-lancer/minecraft-motd-parser", | ||
"type": "library", | ||
"description": "PHP library to query Minecraft servers", | ||
"keywords": ["minecraft", "status", "server", "query", "php", "ping", "bedrock", "raknet"], | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Jakub Gniecki", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"minimum-stability": "stable", | ||
"require": { | ||
"php": ">=7.4", | ||
"ext-mbstring": "*", | ||
"ext-json": "*", | ||
"ext-iconv": "*" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"DevLancer\\MinecraftMotdParser\\": "src/" | ||
} | ||
}, | ||
"require-dev": { | ||
"phpstan/phpstan": "^1.10" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php declare(strict_types=1); | ||
/** | ||
* @author Jakub Gniecki <[email protected]> | ||
* @copyright | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace DevLancer\MinecraftMotdParser; | ||
|
||
class ArrayParser | ||
{ | ||
private array $list = []; | ||
|
||
public function parse(array $list): array | ||
{ | ||
$this->list = []; | ||
$this->arrayParse([$list], new Container()); | ||
|
||
return $this->list; | ||
} | ||
|
||
private function arrayParse(array $list, Container $parent) | ||
{ | ||
foreach ($list as $data) { | ||
$container = clone $parent; | ||
|
||
if (isset($data['color'])) | ||
$container->setColor($data['color']); | ||
|
||
if (isset($data['bold'])) | ||
$container->setBold((bool) $data['bold']); | ||
|
||
if (isset($data['underlined'])) | ||
$container->setUnderlined((bool) $data['underlined']); | ||
|
||
if (isset($data['strikethrough'])) | ||
$container->setStrikethrough((bool) $data['strikethrough']); | ||
|
||
if (isset($data['italic'])) | ||
$container->setItalic((bool) $data['italic']); | ||
|
||
if (isset($data['text'])) { | ||
$text = $data['text']; | ||
$newLine = strpos($text, "\n"); | ||
$container->setText(($newLine === false)? $text : substr($text, 0, $newLine)); | ||
} else { | ||
$container->setText(null); | ||
} | ||
|
||
$this->list[] = $container; | ||
if (isset($newLine) && $newLine !== false) { | ||
$container = new Container(); | ||
$container->setText("\n"); | ||
$this->list[] = $container; | ||
|
||
if (strlen(substr($text, $newLine+1)) > 0) | ||
$this->arrayParse([['text' => substr($text, $newLine+1)]], new Container()); | ||
|
||
$container = new Container(); | ||
} | ||
|
||
if (isset($data['extra'])) | ||
$this->arrayParse($data['extra'], $container); | ||
} | ||
} | ||
/** | ||
* @return array | ||
*/ | ||
public function getList(): array | ||
{ | ||
return $this->list; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
<?php declare(strict_types=1); | ||
/** | ||
* @author Jakub Gniecki <[email protected]> | ||
* @copyright | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace DevLancer\MinecraftMotdParser; | ||
|
||
class Container implements \JsonSerializable | ||
{ | ||
private ?string $text = null; | ||
private ?string $color = null; | ||
private bool $obfuscated = false; | ||
private bool $bold = false; | ||
private bool $strikethrough = false; | ||
private bool $underlined = false; | ||
private bool $italic = false; | ||
private bool $reset = false; | ||
|
||
/** | ||
* @return string|null | ||
*/ | ||
public function getText(): ?string | ||
{ | ||
return $this->text; | ||
} | ||
|
||
/** | ||
* @param string|null $text | ||
*/ | ||
public function setText(?string $text): void | ||
{ | ||
$this->text = $text; | ||
} | ||
|
||
/** | ||
* @return string|null | ||
*/ | ||
public function getColor(): ?string | ||
{ | ||
return $this->color; | ||
} | ||
|
||
/** | ||
* @param string|null $color | ||
*/ | ||
public function setColor(?string $color): void | ||
{ | ||
$this->color = $color; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function isObfuscated(): bool | ||
{ | ||
return $this->obfuscated; | ||
} | ||
|
||
/** | ||
* @param bool $obfuscated | ||
*/ | ||
public function setObfuscated(bool $obfuscated): void | ||
{ | ||
$this->obfuscated = $obfuscated; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function isBold(): bool | ||
{ | ||
return $this->bold; | ||
} | ||
|
||
/** | ||
* @param bool $bold | ||
*/ | ||
public function setBold(bool $bold): void | ||
{ | ||
$this->bold = $bold; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function isUnderlined(): bool | ||
{ | ||
return $this->underlined; | ||
} | ||
|
||
/** | ||
* @param bool $underlined | ||
*/ | ||
public function setUnderlined(bool $underlined): void | ||
{ | ||
$this->underlined = $underlined; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function isStrikethrough(): bool | ||
{ | ||
return $this->strikethrough; | ||
} | ||
|
||
/** | ||
* @param bool $strikethrough | ||
*/ | ||
public function setStrikethrough(bool $strikethrough): void | ||
{ | ||
$this->strikethrough = $strikethrough; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function isItalic(): bool | ||
{ | ||
return $this->italic; | ||
} | ||
|
||
/** | ||
* @param bool $italic | ||
*/ | ||
public function setItalic(bool $italic): void | ||
{ | ||
$this->italic = $italic; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function isReset(): bool | ||
{ | ||
return $this->reset; | ||
} | ||
|
||
/** | ||
* @param bool $reset | ||
*/ | ||
public function setReset(bool $reset): void | ||
{ | ||
$this->reset = $reset; | ||
} | ||
|
||
public function jsonSerialize(): array | ||
{ | ||
return [ | ||
'text' => $this->getText(), | ||
'color' => $this->getColor(), | ||
'bold' => $this->isBold(), | ||
'italic' => $this->isItalic(), | ||
'obfuscated' => $this->isObfuscated(), | ||
'underlined' => $this->isUnderlined(), | ||
'strikethrough' => $this->isStrikethrough(), | ||
'reset' => $this->isReset(), | ||
]; | ||
} | ||
|
||
public function __toString() | ||
{ | ||
$result = $this->getText(); | ||
if (!$result) | ||
$result = ""; | ||
|
||
if ($this->isBold()) | ||
$result = sprintf("<b>%s</b>", $result); | ||
|
||
if ($this->isUnderlined()) | ||
$result = sprintf("<u>%s</u>", $result); | ||
|
||
if ($this->isItalic()) | ||
$result = sprintf("<i>%s</i>", $result); | ||
|
||
if ($this->isStrikethrough()) | ||
$result = sprintf("<s>%s</s>", $result); | ||
|
||
if ($this->getColor()) | ||
$result = sprintf("<span style='color: %s;'>%s</span>", Format::getColorHex($this->getColor()), $result); | ||
|
||
if ($this->getText() == "\n") | ||
$result .= "<br />"; | ||
return $result; | ||
} | ||
} |
Oops, something went wrong.