Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
249 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "31vi5/DocumentExporterKata", | ||
"description": "Document Exporter Kata", | ||
"require": {}, | ||
"autoload": { | ||
"psr-4": { | ||
"Dockata\\": "src/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Dockata\Exporter; | ||
|
||
class Exporter | ||
{ | ||
|
||
public function export(array $p, array $config): string | ||
{ | ||
if ($config['format'] === 'html') { | ||
|
||
$htmlExport = '<html><head></head><body>'; | ||
|
||
foreach ($p as $para) { | ||
$htmlExport .= '<p style="color=' . (isset($para['color']) ? $para['color'] : 'white') . | ||
'">' . | ||
$para['text'] . '</p>'; | ||
} | ||
|
||
$htmlExport .= '</body></html>'; | ||
|
||
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; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Dockata\Template; | ||
|
||
class BasicDocument | ||
{ | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $p; | ||
|
||
|
||
public function __construct(array $p) | ||
{ | ||
$p = $this->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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Dockata\Template; | ||
|
||
class DocumentWithAlternativeFooter extends BasicDocument | ||
{ | ||
|
||
protected function addFooter(array $p): array | ||
{ | ||
$p[] = [ | ||
'text' => ' alternative footer ', | ||
]; | ||
|
||
return $p; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Dockata\Template; | ||
|
||
class DocumentWithoutHeaderAndFooter extends BasicDocument | ||
{ | ||
|
||
protected function addHeader(array $p): array | ||
{ | ||
return $p; | ||
} | ||
|
||
|
||
protected function addFooter(array $p): array | ||
{ | ||
return $p; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
require_once __DIR__ . '/../vendor/autoload.php'; | ||
|
||
use Dockata\Exporter\Exporter; | ||
use Dockata\Template\BasicDocument; | ||
use Dockata\Template\DocumentWithAlternativeFooter; | ||
use Dockata\Template\DocumentWithoutHeaderAndFooter; | ||
|
||
$exporter = new Exporter(); | ||
|
||
$document = new BasicDocument( | ||
[ | ||
// thanks http://officeipsum.com | ||
[ | ||
'text' => '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, | ||
] | ||
); | ||
|
||
|