From 4b82ee4b89c708bf1713ac403728076df15b7ec7 Mon Sep 17 00:00:00 2001 From: Vassyli Date: Wed, 27 Sep 2017 13:17:26 +0200 Subject: [PATCH] ViewpointDescription now ignores empty lines --- src/Models/Viewpoint.php | 8 ++++++++ src/Tools/SceneDescription.php | 9 ++++++++- tests/Models/ViewpointTest.php | 13 +++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/Models/Viewpoint.php b/src/Models/Viewpoint.php index f5a301a..3a43def 100644 --- a/src/Models/Viewpoint.php +++ b/src/Models/Viewpoint.php @@ -71,6 +71,14 @@ class Viewpoint implements CreateableInterface $this->_description = new SceneDescription($description); } + /** + * Clears the description + */ + public function clearDescription(): void + { + $this->description = ""; + } + /** * Returns the current description as a string * @return string diff --git a/src/Tools/SceneDescription.php b/src/Tools/SceneDescription.php index 74df985..be9a242 100644 --- a/src/Tools/SceneDescription.php +++ b/src/Tools/SceneDescription.php @@ -61,6 +61,13 @@ class SceneDescription $input = str_replace("\r\n", "\n", $input); $input = str_replace("\r", "\n", $input); - return explode("\n\n", $input); + $parts = explode("\n\n", $input); + foreach ($parts as $key => $part) { + if (strlen($part) === 0) { + unset($parts[$key]); + } + } + + return $parts; } } \ No newline at end of file diff --git a/tests/Models/ViewpointTest.php b/tests/Models/ViewpointTest.php index aba779d..1400876 100644 --- a/tests/Models/ViewpointTest.php +++ b/tests/Models/ViewpointTest.php @@ -197,4 +197,17 @@ class ViewpointTest extends CoreModelTestCase $characterScene->addDescriptionParagraph("You enjoy being here."); $this->assertSame("This is the village.\n\nYou enjoy being here.", $characterScene->getDescription()); } + + public function testClearingSceneDescription() + { + $em = $this->getEntityManager(); + $testCharacter = $em->getRepository(Character::class)->find(2); + $characterScene = $testCharacter->getViewpoint(); + + $characterScene->clearDescription(); + $this->assertSame("", $characterScene->getDescription()); + + $characterScene->addDescriptionParagraph("You enjoy being here."); + $this->assertSame("You enjoy being here.", $characterScene->getDescription()); + } }