From 4982247bc43d812bfdcd9aaa4493347e5d811d9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Dobi=C3=A1=C5=A1?= Date: Wed, 12 Sep 2018 11:02:45 +0200 Subject: [PATCH] Added kata code --- .gitignore | 1 + README.md | 12 +++- composer.json | 10 +++ src/Exporter/Exporter.php | 52 ++++++++++++++ src/Template/BasicDocument.php | 68 +++++++++++++++++++ .../DocumentWithAlternativeFooter.php | 19 ++++++ .../DocumentWithoutHeaderAndFooter.php | 21 ++++++ src/index.php | 68 +++++++++++++++++++ 8 files changed, 249 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 src/Exporter/Exporter.php create mode 100644 src/Template/BasicDocument.php create mode 100644 src/Template/DocumentWithAlternativeFooter.php create mode 100644 src/Template/DocumentWithoutHeaderAndFooter.php create mode 100644 src/index.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..61ead86 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/vendor diff --git a/README.md b/README.md index cde4653..8994b6a 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,10 @@ -# DocumentExporterKata -Document exporter kata +# Document Exporter Kata + +The goal of this kata is to refactor the existing code. + +Also since we want to produce nice content, we've decided that we'd like to make sure that no one using our exporter creates documents containing bad words and phrases. +For example we'd like the word `bad` to be replaced with `good`, `problem` with `opportunity` etc. + +And not only that, we'd like to add smileys after each paragraph, to make it even more positive. + +Your job is to add those features. We might even think about more modifications in the future so please be ready for those. diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..c7df5c4 --- /dev/null +++ b/composer.json @@ -0,0 +1,10 @@ +{ + "name": "31vi5/DocumentExporterKata", + "description": "Document Exporter Kata", + "require": {}, + "autoload": { + "psr-4": { + "Dockata\\": "src/" + } + } +} diff --git a/src/Exporter/Exporter.php b/src/Exporter/Exporter.php new file mode 100644 index 0000000..49828b5 --- /dev/null +++ b/src/Exporter/Exporter.php @@ -0,0 +1,52 @@ +'; + + foreach ($p as $para) { + $htmlExport .= '

' . + $para['text'] . '

'; + } + + $htmlExport .= ''; + + return self::output($htmlExport, $config['print']); + + } elseif ($config['format'] === 'json') { + + $jsonExport = '['; + foreach ($p as $para) { + $jsonExport .= '{"text":"' . $para['text'] . '","color": ' . + (isset($para['color']) ? $para['color'] : 'white') . '},'; + } + $jsonExport = rtrim($jsonExport, ','); + $jsonExport .= ']'; + + return self::output($jsonExport, $config['print']); + } + } + + + private static function output(string $htmlExport, $print): string + { + if ($print) { + echo $htmlExport; + + return ''; + } else { + return $htmlExport; + } + } + +} diff --git a/src/Template/BasicDocument.php b/src/Template/BasicDocument.php new file mode 100644 index 0000000..490cbcb --- /dev/null +++ b/src/Template/BasicDocument.php @@ -0,0 +1,68 @@ +addHeader($p); + $p = $this->addFooter($p); + $this->p = $p; + } + + + public function getP(): array + { + return $this->p; + } + + + public function getRawParagraphs(): array + { + $p = $this->p; + unset($p[0]); + unset($p[count($p) - 1]); + + return $p; + } + + + protected function addHeader(array $p): array + { + $p2 = [ + [ + 'text' => ' BIG HEADER ', + ], + ]; + foreach ($p as $para) { + $p2[] = $para; + } + + return $p2; + } + + + protected function addFooter(array $p): array + { + $p2 = []; + foreach ($p as $para) { + $p2[] = $para; + } + $p2[] = [ + 'text' => ' small footer ', + ]; + + return $p2; + } + +} diff --git a/src/Template/DocumentWithAlternativeFooter.php b/src/Template/DocumentWithAlternativeFooter.php new file mode 100644 index 0000000..532f053 --- /dev/null +++ b/src/Template/DocumentWithAlternativeFooter.php @@ -0,0 +1,19 @@ + ' alternative footer ', + ]; + + return $p; + } + +} diff --git a/src/Template/DocumentWithoutHeaderAndFooter.php b/src/Template/DocumentWithoutHeaderAndFooter.php new file mode 100644 index 0000000..c797bd1 --- /dev/null +++ b/src/Template/DocumentWithoutHeaderAndFooter.php @@ -0,0 +1,21 @@ + 'Bad commitment to the cause it just needs more cowbell nor pipeline digitalize. Low-hanging fruit programmatically we need distributors to evangelize the new line to local markets, so message the initiative win-win-win nor organic growth.', + 'color' => 'blue', + ], + [ + 'text' => 'Parallel path quarterly sales are at an all-time low, that\'s a huge problem. Goalposts on this journey. Come up with something buzzworthy put in in a deck for our standup today nor back of the net. Not enough bandwidth we are running out of runway.', + 'color' => 'red', + ], + [ + 'text' => 'Reach out we need to dialog around your choice of work attire customer centric, yet synergestic actionables cannibalize. Going forward nail jelly to the hothouse wall, yet it\'s a simple lift and shift job, quick win.', + 'color' => 'yellow', + ], + ] +); + +$exporter->export( + $document->getP(), + [ + 'format' => 'html', + 'print' => true, + ] +); + +echo ' + + +'; + +$document2 = new DocumentWithoutHeaderAndFooter($document->getRawParagraphs()); + +$exporter->export( + $document2->getP(), + [ + 'format' => 'json', + 'print' => true, + ] +); + +echo ' + + +'; + +$document3 = new DocumentWithAlternativeFooter($document->getRawParagraphs()); + +$exporter->export( + $document3->getP(), + [ + 'format' => 'json', + 'print' => true, + ] +); + +