PocketMine-MP 5.15.1 git-08c6e63aac45a450fe3b437616ee8aa2500e04e2
SignText.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\block\utils;
25
28use function array_fill;
29use function array_pad;
30use function array_slice;
31use function count;
32use function explode;
33use function is_int;
34use function str_contains;
35
37 public const LINE_COUNT = 4;
38
40 private array $lines;
41 private Color $baseColor;
42 private bool $glowing;
43
51 public function __construct(?array $lines = null, ?Color $baseColor = null, bool $glowing = false){
52 $this->lines = array_fill(0, self::LINE_COUNT, "");
53 if($lines !== null){
54 if(count($lines) > self::LINE_COUNT){
55 throw new \InvalidArgumentException("Expected at most 4 lines, got " . count($lines));
56 }
57 foreach($lines as $k => $line){
58 $this->checkLineIndex($k);
59 Utils::checkUTF8($line);
60 if(str_contains($line, "\n")){
61 throw new \InvalidArgumentException("Line must not contain newlines");
62 }
63 //TODO: add length checks
64 $this->lines[$k] = $line;
65 }
66 }
67 $this->baseColor = $baseColor ?? new Color(0, 0, 0);
68 $this->glowing = $glowing;
69 }
70
77 public static function fromBlob(string $blob, ?Color $baseColor = null, bool $glowing = false) : SignText{
78 return new self(array_slice(array_pad(explode("\n", $blob), self::LINE_COUNT, ""), 0, self::LINE_COUNT), $baseColor, $glowing);
79 }
80
86 public function getLines() : array{
87 return $this->lines;
88 }
89
90 private function checkLineIndex(int|string $index) : void{
91 if(!is_int($index)){
92 throw new \InvalidArgumentException("Index must be an integer");
93 }
94 if($index < 0 || $index >= self::LINE_COUNT){
95 throw new \InvalidArgumentException("Line index is out of bounds");
96 }
97 }
98
104 public function getLine(int $index) : string{
105 $this->checkLineIndex($index);
106 return $this->lines[$index];
107 }
108
112 public function getBaseColor() : Color{
113 return $this->baseColor;
114 }
115
120 public function isGlowing() : bool{
121 return $this->glowing;
122 }
123}
__construct(?array $lines=null, ?Color $baseColor=null, bool $glowing=false)
Definition: SignText.php:51
static fromBlob(string $blob, ?Color $baseColor=null, bool $glowing=false)
Definition: SignText.php:77