Skip to content

Commit f158753

Browse files
committed
update formatter
1 parent 16dea59 commit f158753

18 files changed

+649
-193
lines changed

fixtures/filesRecursiveJSONDiffs.json

Lines changed: 429 additions & 0 deletions
Large diffs are not rendered by default.

src/CommandFactory.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,49 +17,46 @@ class CommandFactory implements CommandFactoryInterface
1717
{
1818
private mixed $parser;
1919
private FileReaderInterface $fileReader;
20-
/*
20+
private CommandFactoryInterface $formatters;
21+
2122
private const array FORMAT_KEYS = [
2223
"stylish" => "stylish",
2324
"plain" => "plain",
2425
"json" => "json"
2526
];
26-
*/
27+
2728
public function __construct(
2829
mixed $parser,
29-
FileReaderInterface $fileReader
30+
FileReaderInterface $fileReader,
31+
CommandFactoryInterface $formatters
3032
) {
3133
$this->parser = $parser;
3234
$this->fileReader = $fileReader;
35+
$this->formatters = $formatters;
3336
}
34-
/*
37+
3538
public function getFormatKeys(): array
3639
{
3740
return self::FORMAT_KEYS;
3841
}
39-
*/
40-
public function getCommand(string $commandType): ?CommandInterface
42+
43+
public function createCommand(string $commandType): ?CommandInterface
4144
{
4245
switch ($commandType) {
4346
case "parse":
4447
$requestedCommand = new CommandLineParser($this->parser);
4548
break;
4649
case "difference":
47-
$requestedCommand = (new FilesDiffCommand())
48-
->setFileReader($this->fileReader);
50+
$requestedCommand = new FilesDiffCommand($this->fileReader);
4951
break;
50-
/*
5152
case self::FORMAT_KEYS["stylish"]:
52-
$requestedCommand = new StylishCommand();
53+
$requestedCommand = $this->formatters->createCommand(self::FORMAT_KEYS["stylish"]);
5354
break;
5455
case self::FORMAT_KEYS["plain"]:
55-
$requestedCommand = new PlainCommand();
56+
$requestedCommand = $this->formatters->createCommand(self::FORMAT_KEYS["plain"]);
5657
break;
5758
case self::FORMAT_KEYS["json"]:
58-
$requestedCommand = new JSONCommand();
59-
break;
60-
*/
61-
case "format":
62-
$requestedCommand = new Formatters();
59+
$requestedCommand = $this->formatters->createCommand(self::FORMAT_KEYS["json"]);
6360
break;
6461
case "show":
6562
$requestedCommand = new DisplayCommand();

src/CommandFactoryInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55
interface CommandFactoryInterface
66
{
7-
public function getCommand(string $commandType): ?CommandInterface;
7+
public function createCommand(string $commandType): ?CommandInterface;
88
}

src/ConsoleApp.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Differ;
44

5+
use Differ\CommandLineParser;
6+
57
class ConsoleApp
68
{
79
private CommandInterface $nextCommand;
@@ -15,19 +17,21 @@ public function __construct(
1517

1618
public function run(): void
1719
{
18-
$parseCommand = $this->commandFactory->getCommand("parse");
20+
$parseCommand = $this->commandFactory->createCommand("parse");
1921
$this->nextCommand = $parseCommand
2022
->execute();
2123

22-
$differenceCommand = $this->commandFactory->getCommand("difference");
24+
$differenceCommand = $this->commandFactory->createCommand("difference");
2325
$this->nextCommand = $differenceCommand
2426
->execute($this->nextCommand);
2527

26-
$formatCommand = $this->commandFactory->getCommand("format");
27-
$this->nextCommand = $formatCommand->selectFormat($parseCommand)
28-
->execute($this->nextCommand);
28+
$formatCommand = $this->commandFactory->createCommand(
29+
strtolower($parseCommand->getFormat()
30+
));
31+
$this->nextCommand = $formatCommand->execute($this->nextCommand);
32+
33+
$displayCommand = $this->commandFactory->createCommand("show");
2934

30-
$displayCommand = $this->commandFactory->getCommand("show");
3135
$displayCommand->execute($this->nextCommand);
3236
}
3337
}

src/Differ.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,32 @@
77
use Differ\FilesDiffCommand;
88
use Differ\FileReader;
99
use Differ\DisplayCommand;
10+
use Differ\Formatters;
1011

1112
function genDiff(string $pathToFile1, string $pathToFile2, string $format = 'stylish')
1213
{
1314
$commandFactory = new CommandFactory(
1415
new \Docopt(),
15-
new FileReader()
16+
new FileReader(),
17+
new Formatters()
1618
);
17-
$parseCommand = $commandFactory->getCommand('parse');
19+
$parseCommand = $commandFactory->createCommand('parse');
1820
$fileNames = [
1921
"FILE1" => $pathToFile1,
2022
"FILE2" => $pathToFile2
2123
];
2224
$nextCommand = $parseCommand->setFileNames($fileNames)
2325
->setFormat($format);
2426

25-
$differenceCommand = $commandFactory->getCommand('difference');
27+
$differenceCommand = $commandFactory->createCommand('difference');
2628
$nextCommand = $differenceCommand->execute($nextCommand);
2729

28-
$formatCommand = $commandFactory->getCommand('format');
29-
$nextCommand = $formatCommand->selectFormat($parseCommand)
30-
->execute($nextCommand);
30+
$formatCommand = $commandFactory->createCommand(
31+
strtolower($parseCommand->getFormat())
32+
);
33+
$formatter = $formatCommand->execute($nextCommand);
3134

32-
return $nextCommand->getFilesDiffs();
35+
$displayCommand = $commandFactory->createCommand("show");
36+
return $displayCommand->setFormatter($formatter)
37+
->getFilesDiffs();
3338
}

src/DisplayCommand.php

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,61 @@
44

55
class DisplayCommand implements CommandInterface
66
{
7-
// Property to store displaying mode for debug
87
private string $mode;
8+
private CommandInterface $formatCommand;
9+
private const string MODE_EXCEPTION_MESSAGE = "internal error: unknown mode for display\n";
910
public const AVAILABLE_MODES = [
10-
"differents",
11-
"content"
11+
"differents" => "differents",
12+
"content" => "content"
1213
];
1314

14-
public function __construct(string $mode = self::AVAILABLE_MODES[0])
15+
public function __construct(string $mode = self::AVAILABLE_MODES["differents"])
1516
{
1617
$this->mode = $mode;
1718
}
1819

20+
public function setFormatter(CommandInterface $formatter)
21+
{
22+
$this->formatCommand = $formatter;
23+
24+
return $this;
25+
}
26+
27+
public function getFilesContent(): string
28+
{
29+
return $this->formatCommand->getContentString();
30+
}
31+
32+
public function getFilesDiffs(): string
33+
{
34+
return $this->formatCommand->getDiffsString();
35+
}
36+
1937
public function execute(CommandInterface $command = null): CommandInterface
2038
{
21-
if (!is_null($command)) {
22-
switch ($this->mode) {
23-
case self::AVAILABLE_MODES[0]:
24-
print_r($command->getFilesDiffs());
25-
break;
26-
case self::AVAILABLE_MODES[1]:
27-
print_r($command->getFilesContent());
28-
break;
29-
default:
30-
throw new DifferException("internal error: unknown mode for display\n");
31-
}
39+
$this->formatCommand = $command;
40+
switch ($this->mode) {
41+
case self::AVAILABLE_MODES["differents"]:
42+
print_r($this->getFilesDiffs());
43+
break;
44+
case self::AVAILABLE_MODES["content"]:
45+
print_r($this->getFilesContent());
46+
break;
47+
default:
48+
throw new DifferException(self::MODE_EXCEPTION_MESSAGE);
3249
}
3350

3451
return $this;
3552
}
3653

3754
public function setMode(string $mode): CommandInterface
3855
{
39-
$this->mode = $mode;
56+
if (in_array($mode, array_keys(self::AVAILABLE_MODES))) {
57+
$this->mode = $mode;
4058

41-
return $this;
59+
return $this;
60+
} else {
61+
throw new DifferException(self::MODE_EXCEPTION_MESSAGE);
62+
}
4263
}
4364
}

src/FilesDiffCommand.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ class FilesDiffCommand implements CommandInterface
1414
"not changed", "changed", "added", "deleted", "empty", "new value"
1515
];
1616

17-
public function __construct()
17+
public function __construct(FileReaderInterface $reader)
1818
{
1919
$this->filesDataItems = [];
20+
21+
$this->fileReader = $reader;
2022
}
2123

2224
private function normalizeData($data)
@@ -188,13 +190,6 @@ function ($differenceDescriptor, $fileKey) {
188190
);
189191
}
190192

191-
public function setFileReader(FileReaderInterface $fileReader): CommandInterface
192-
{
193-
$this->fileReader = $fileReader;
194-
195-
return $this;
196-
}
197-
198193
public function execute(CommandInterface $command = null): CommandInterface
199194
{
200195
if (!is_null($command)) {

src/Formatters.php

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Differ\Formatters\PlainCommand;
77
use Differ\Formatters\JSONCommand;
88

9-
class Formatters implements CommandInterface
9+
class Formatters implements CommandFactoryInterface
1010
{
1111
private CommandInterface $formatCommand;
1212
private const array FORMAT_KEYS = [
@@ -15,7 +15,7 @@ class Formatters implements CommandInterface
1515
"json" => "json"
1616
];
1717

18-
private function createCommand(string $commandKey): CommandInterface
18+
public function createCommand(string $commandKey): CommandInterface
1919
{
2020
switch ($commandKey) {
2121
case self::FORMAT_KEYS["stylish"]:
@@ -25,40 +25,7 @@ private function createCommand(string $commandKey): CommandInterface
2525
case self::FORMAT_KEYS["json"]:
2626
return new JSONCommand();
2727
default:
28-
return throw new DifferException("input error: unknown output format\nUse gendiff -h\n");
28+
return throw new DifferException("input error: unknown output format\n");
2929
}
3030
}
31-
32-
public function selectFormat(CommandInterface $command = null): CommandInterface
33-
{
34-
$commandFactory = new CommandFactory(
35-
new \Docopt(),
36-
new FileReader()
37-
);
38-
$currentFormat = strtolower($command->getFormat());
39-
//$formatKeys = $commandFactory->getFormatKeys();
40-
41-
$this->formatCommand = $this->createCommand(
42-
$currentFormat
43-
);
44-
45-
return $this;
46-
}
47-
48-
public function execute(CommandInterface $command = null): CommandInterface
49-
{
50-
$this->formatCommand = $this->formatCommand->execute($command);
51-
52-
return $this;
53-
}
54-
55-
public function getFilesContent(): string
56-
{
57-
return $this->formatCommand->filesContentString;
58-
}
59-
60-
public function getFilesDiffs(): string
61-
{
62-
return $this->formatCommand->filesDiffsString;
63-
}
6431
}

src/Formatters/JSONCommand.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,14 @@ public function execute(CommandInterface $command = null): CommandInterface
4343

4444
return $this;
4545
}
46+
47+
public function getContentString()
48+
{
49+
return $this->filesContentString;
50+
}
51+
52+
public function getDiffsString()
53+
{
54+
return $this->filesDiffsString;
55+
}
4656
}

src/Formatters/PlainCommand.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,14 @@ public function execute(CommandInterface $command = null): CommandInterface
188188

189189
return $this;
190190
}
191+
192+
public function getContentString()
193+
{
194+
return $this->filesContentString;
195+
}
196+
197+
public function getDiffsString()
198+
{
199+
return $this->filesDiffsString;
200+
}
191201
}

0 commit comments

Comments
 (0)