Skip to content

Commit

Permalink
Added kata code
Browse files Browse the repository at this point in the history
  • Loading branch information
31vi5 committed Sep 12, 2018
1 parent bca0576 commit 4982247
Show file tree
Hide file tree
Showing 8 changed files with 249 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
/vendor
12 changes: 10 additions & 2 deletions 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.
10 changes: 10 additions & 0 deletions composer.json
@@ -0,0 +1,10 @@
{
"name": "31vi5/DocumentExporterKata",
"description": "Document Exporter Kata",
"require": {},
"autoload": {
"psr-4": {
"Dockata\\": "src/"
}
}
}
52 changes: 52 additions & 0 deletions src/Exporter/Exporter.php
@@ -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;
}
}

}
68 changes: 68 additions & 0 deletions src/Template/BasicDocument.php
@@ -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;
}

}
19 changes: 19 additions & 0 deletions src/Template/DocumentWithAlternativeFooter.php
@@ -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;
}

}
21 changes: 21 additions & 0 deletions src/Template/DocumentWithoutHeaderAndFooter.php
@@ -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;
}

}
68 changes: 68 additions & 0 deletions src/index.php
@@ -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,
]
);


0 comments on commit 4982247

Please sign in to comment.