lines = array_fill(0, self::LINE_COUNT, ""); if($lines !== null){ if(count($lines) > self::LINE_COUNT){ throw new \InvalidArgumentException("Expected at most 4 lines, got " . count($lines)); } foreach($lines as $k => $line){ $this->checkLineIndex($k); Utils::checkUTF8($line); if(strpos($line, "\n") !== false){ throw new \InvalidArgumentException("Line must not contain newlines"); } //TODO: add length checks $this->lines[$k] = $line; } } } /** * Parses sign lines from the given string blob. * TODO: add a strict mode for this * * @throws \InvalidArgumentException if the text is not valid UTF-8 */ public static function fromBlob(string $blob) : SignText{ return new self(array_slice(array_pad(explode("\n", $blob), self::LINE_COUNT, ""), 0, self::LINE_COUNT)); } /** * Returns an array of lines currently on the sign. * * @return string[] */ public function getLines() : array{ return $this->lines; } /** * @param int|string $index */ private function checkLineIndex($index) : void{ if(!is_int($index)){ throw new \InvalidArgumentException("Index must be an integer"); } if($index < 0 || $index >= self::LINE_COUNT){ throw new \InvalidArgumentException("Line index is out of bounds"); } } /** * Returns the sign line at the given offset. * * @throws \InvalidArgumentException */ public function getLine(int $index) : string{ $this->checkLineIndex($index); return $this->lines[$index]; } }