Improve usability of Sign API (#1202)

* Improve usability of Sign API

* PHP

* Throw exceptions if out of range

* Fix phpdocs

* Formatting, additional checks in the SignChangeEvent.

* Blame php storm

* require line count to always be 4

* Adjust exception message
This commit is contained in:
Tim (robske_110) 2017-07-19 16:22:04 +02:00 committed by Dylan K. Taylor
parent cca9cf2c86
commit 42fb1d1fef
2 changed files with 43 additions and 0 deletions

View File

@ -69,14 +69,30 @@ class SignChangeEvent extends BlockEvent implements Cancellable{
* @return string * @return string
*/ */
public function getLine($index){ public function getLine($index){
if($index < 0 or $index > 3){
throw new \InvalidArgumentException("Index must be in the range 0-3!");
}
return $this->lines[$index]; return $this->lines[$index];
} }
/**
* @param string[] $lines
*/
public function setLines(array $lines){
if(count($lines) !== 4){
throw new \InvalidArgumentException("Array size must be 4!");
}
$this->lines = $lines;
}
/** /**
* @param int $index 0-3 * @param int $index 0-3
* @param string $line * @param string $line
*/ */
public function setLine($index, $line){ public function setLine($index, $line){
if($index < 0 or $index > 3){
throw new \InvalidArgumentException("Index must be in the range 0-3!");
}
$this->lines[$index] = $line; $this->lines[$index] = $line;
} }
} }

View File

@ -65,6 +65,33 @@ class Sign extends Spawnable{
return true; return true;
} }
/**
* @param int $index 0-3
* @param string $line
* @param bool $update
*/
public function setLine(int $index, string $line, bool $update = true){
if($index < 0 or $index > 3){
throw new \InvalidArgumentException("Index must be in the range 0-3!");
}
$this->namedtag["Text" . ($index + 1)] = $line;
if($update){
$this->onChanged();
}
}
/**
* @param int $index 0-3
*
* @return string
*/
public function getLine(int $index) : string{
if($index < 0 or $index > 3){
throw new \InvalidArgumentException("Index must be in the range 0-3!");
}
return (string) $this->namedtag["Text" . ($index + 1)];
}
public function getText(){ public function getText(){
return [ return [
$this->namedtag["Text1"], $this->namedtag["Text1"],