Adds the foundation for viewpoint to be able to modify there description more easily

This commit is contained in:
Vassyli
2017-09-13 18:24:37 +02:00
parent 867843dddd
commit 533378d006
3 changed files with 88 additions and 0 deletions
+37
View File
@@ -9,6 +9,7 @@ use Doctrine\ORM\Mapping\Table;
use LotGD\Core\Action;
use LotGD\Core\Tools\Model\Creator;
use LotGD\Core\Tools\Model\SceneBasics;
use LotGD\Core\Tools\SceneDescription;
/**
* A Viewpoint is the current Scene a character is experiencing with
@@ -32,6 +33,9 @@ class Viewpoint implements CreateableInterface
/** @ManyToOne(targetEntity="Scene") */
private $scene;
/** @var SceneDescription */
private $_description;
/** @var array */
private static $fillable = [
"owner"
@@ -55,6 +59,39 @@ class Viewpoint implements CreateableInterface
$this->owner = $owner;
}
/**
* Sets the description of this viewpoint.
* @param string $description
*/
public function setDescription(string $description): void
{
$this->description = $description;
$this->_description = new SceneDescription($description);
}
/**
* Returns the current description as a string
* @return string
*/
public function getDescription(): string
{
return $this->description;
}
/**
* Adds a paragraph to the existing description
* @param string $paragraph
*/
public function addDescriptionParagraph(string $paragraph)
{
if ($this->_description === null) {
$this->_description = new SceneDescription($this->description);
}
$this->_description->addParagraph($paragraph);
$this->description = (string)$this->_description;
}
/**
* Copies the static data from a scene to this Viewpoint entity.
* @param \LotGD\Core\Models\Scene $scene
+39
View File
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace LotGD\Core\Tools;
class SceneDescription
{
private $description = [];
public function __construct(string $description)
{
$this->description = $this->splitIntoParagraphs($description);
}
public function __toString(): string
{
return $this->getDescriptionBack();
}
public function addParagraph(string $paragraph): void
{
$paragraph = $this->splitIntoParagraphs($paragraph);
$this->description = array_merge($this->description, $paragraph);
}
public function getDescriptionBack(): string
{
return implode("\n\n", $this->description);
}
private function splitIntoParagraphs(string $input): array
{
$input = str_replace("\r\n", "\n", $input);
$input = str_replace("\r", "\n", $input);
return explode("\n\n", $input);
}
}
+12
View File
@@ -185,4 +185,16 @@ class ViewpointTest extends CoreModelTestCase
$output->removeActionsWithSceneId(2);
$this->assertEquals($actionGroupsWithout2, $output->getActionGroups());
}
public function testChangingSceneDescription()
{
$em = $this->getEntityManager();
$testCharacter = $em->getRepository(Character::class)->find(2);
$characterScene = $testCharacter->getViewpoint();
$this->assertSame("This is the village.", $characterScene->getDescription());
$characterScene->addDescriptionParagraph("You enjoy being here.");
$this->assertSame("This is the village.\n\nYou enjoy being here.", $characterScene->getDescription());
}
}