From f5442b9a13410f1cdc52d0e47c1ef093049ae34b Mon Sep 17 00:00:00 2001 From: Eden Meireles Date: Wed, 13 Sep 2017 17:53:25 -0300 Subject: [PATCH] =?UTF-8?q?Implementando=20quebra=20de=20linhas=20a=20cada?= =?UTF-8?q?=20N=20caracteres=20/=20Testes=20unit=C3=A1rios?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/FlowChartImage.php | 56 +++++++++++++++++++++++++++++------- tests/FlowChartImageTest.php | 15 +++++++++- 2 files changed, 60 insertions(+), 11 deletions(-) diff --git a/src/FlowChartImage.php b/src/FlowChartImage.php index 371b4cf..b807074 100644 --- a/src/FlowChartImage.php +++ b/src/FlowChartImage.php @@ -14,6 +14,8 @@ class FlowChartImage private $boxProperties; private $backgroundColor; + const MAX_CHARS_PER_LINE = 10; + public function generate() { $this->jsonData = json_decode(utf8_encode($this->content)); @@ -110,18 +112,52 @@ public function drawActionBox($top, $left, $text, $type, $nodeId) $size = 5; if (strlen($text) > 10) { - $size = 3; - } - if (strlen($text) > 15) { - $size = 1; + $size = 4; } $fw = imagefontwidth($size); - $l = strlen($text); - $tw = $l * $fw; - $iw = 100; - $xpos = ($iw - $tw) / 2; - imagestring($this->imageHandle, $size, $left + $xpos, $top + 40, utf8_decode($text), $black); + + $chunkedText = self::chunkText($text); + + foreach($chunkedText as $key => $line) { + + $l = strlen($line); + $tw = $l * $fw; + $iw = 100; + $xpos = ($iw - $tw) / 2; + + $topIniPos = 50 - (7 * count($chunkedText)); + + imagestring($this->imageHandle, $size, $left + $xpos, $top + ($topIniPos + (13 * ($key + 1))), utf8_decode($line), $black); + + } + } + + public static function chunkText($text) { + + $length = strlen($text); + + if($length <= self::MAX_CHARS_PER_LINE) { + return [$text]; + } + + $chunkedText = []; + $line = ""; + $words = explode(" ", $text); + + foreach($words as $word) { + + if(strlen($line) + strlen($word) > self::MAX_CHARS_PER_LINE && !empty($line)) { + $chunkedText[] = trim($line); + $line = ""; + } + + $line .= $word . " "; + } + + $chunkedText[] = trim($line); + + return $chunkedText; } public function drawConnector($originTop, $originLeft, $destinTop, $destinLeft, $text, $positionOrigin, @@ -333,7 +369,7 @@ private function drawEdges() list($topDestin, $leftDestin) = $positionDestin; $this->drawConnector($topOrigin, $leftOrigin, $topDestin, $leftDestin, $edge->data->label, - $edge->data->positionSource, $edge->data->positionTarget); + $edge->data->positionSource, $edge->data->positionTarget); } } diff --git a/tests/FlowChartImageTest.php b/tests/FlowChartImageTest.php index 3036aca..d8d12f7 100644 --- a/tests/FlowChartImageTest.php +++ b/tests/FlowChartImageTest.php @@ -7,7 +7,7 @@ class FlowChartImageTest extends TestCase { public function testToPngShouldGenerateCorrectly() { - $expected = 'e51fb07e78eebe558a22048c18d97909'; + $expected = 'e8d065ac24600682639ef29238195c2c'; $image = new FlowChartImage(); $image->setContent($this->simpleFlowchart()); $image->generate(); @@ -102,5 +102,18 @@ protected function simpleFlowchart() ]; return json_encode($flowchart); } + + public function testChunkTextMustTurnStringInArrayOfChunkedStrings() + { + $input = "123456789asdf 0123456789 abcdefgh lorem ipsu"; + $expected = [ + '123456789asdf', + '0123456789', + 'abcdefgh', + 'lorem ipsu' + ]; + + $this->assertEquals($expected, FlowChartImage::chunkText($input)); + } }