Skip to content

Commit 1f40c8e

Browse files
committed
added user api function genDiff
1 parent 0df2fb6 commit 1f40c8e

10 files changed

+112
-77
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
},
1616
"files": [
1717
"vendor/docopt/docopt/src/docopt.php",
18-
"src/Gendiff.php"
18+
"src/Gendiff.php",
19+
"src/Differ.php"
1920
]
2021
},
2122
"authors": [

src/CommandFactory.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,23 @@
55
class CommandFactory implements CommandFactoryInterface
66
{
77
private string $docopt;
8-
private const COMMAND_LIST = ["parse", "difference", "show"];
98

109
public function __construct(string $docopt = '')
1110
{
1211
$this->docopt = $docopt;
1312
}
1413

15-
public function getCommandTypeList(): array
16-
{
17-
return CommandFactory::COMMAND_LIST;
18-
}
19-
20-
public function getCommand(string $commandType): CommandInterface | CommandLineParserInterface
14+
public function getCommand(string $commandType): ?CommandInterface
2115
{
2216
switch ($commandType) {
23-
case self::COMMAND_LIST[0]:
17+
case "parse":
2418
return new CommandLineParser($this->docopt);
25-
case self::COMMAND_LIST[1]:
19+
case "difference":
2620
return new FilesDiffCommand();
27-
case self::COMMAND_LIST[2]:
21+
case "show":
2822
return new DisplayCommand();
2923
default:
30-
throw new \Exception("Unknown command");
24+
return null;
3125
}
3226
}
3327
}

src/CommandFactoryInterface.php

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

55
interface CommandFactoryInterface
66
{
7-
public function getCommandTypeList(): array;
8-
public function getCommand(string $commandType): CommandInterface | CommandLineParserInterface;
7+
public function getCommand(string $commandType): ?CommandInterface;
98
}

src/CommandInterface.php

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

55
interface CommandInterface
66
{
7-
public function execute(array $data): ?array;
7+
public function execute(CommandInterface $data = null): CommandInterface;
88
}

src/CommandLineParser.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,33 @@
44

55
use Docopt;
66

7-
class CommandLineParser implements CommandLineParserInterface
7+
class CommandLineParser implements CommandInterface
88
{
99
private string $docopt;
10+
private array $args;
1011

11-
public function __construct(string $docopt)
12+
public function __construct(string $docopt = "")
1213
{
1314
$this->docopt = $docopt;
1415
}
1516

16-
public function execute(): ?array
17+
public function execute(CommandInterface $command = null): CommandInterface
1718
{
18-
return (new Docopt())->handle($this->docopt, array('version' => '1.0.6'))
19-
->args;
19+
$this->args = (new Docopt())->handle($this->docopt, array('version' => '1.0.6'))
20+
->args;
21+
return $this;
22+
}
23+
24+
public function setFileNames(array $fileNames): CommandInterface
25+
{
26+
$this->args["FILE1"] = $fileNames["FILE1"];
27+
$this->args["FILE2"] = $fileNames["FILE2"];
28+
29+
return $this;
30+
}
31+
32+
public function getFileNames()
33+
{
34+
return $this->args;
2035
}
2136
}

src/CommandLineParserInterface.php

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/ConsoleApp.php

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
class ConsoleApp
66
{
77
private string $docopt;
8-
private array $commandTypeList;
9-
private $currentCommand;
10-
private array $cliData;
11-
private array $filesContent;
8+
private CommandInterface $currentCommand;
9+
private CommandInterface $nextCommand;
1210
private CommandFactoryInterface $commandFactory;
1311

1412
public function __construct()
@@ -31,28 +29,23 @@ public function __construct()
3129
DOCOPT;
3230

3331
$this->commandFactory = new CommandFactory($this->docopt);
34-
$this->commandTypeList = $this->commandFactory->getCommandTypeList();
3532
}
3633

3734
public function run(): void
3835
{
39-
foreach ($this->commandTypeList as $commandType) {
40-
$this->currentCommand = $this->commandFactory->getCommand($commandType);
41-
switch ($commandType) {
42-
case "parse":
43-
$this->cliData = $this->currentCommand->execute();
44-
break;
45-
case "difference":
46-
$this->currentCommand->setFileReader(new FileReader());
47-
$this->filesContent = $this->currentCommand->execute($this->cliData);
48-
break;
49-
case "show":
50-
$this->currentCommand->execute($this->filesContent);
51-
$this->currentCommand->showDiffsToConsole();
52-
break;
53-
default:
54-
throw new \Exception('unknown command type');
55-
}
56-
}
36+
$this->currentCommand = $this->commandFactory->getCommand("parse");
37+
$this->nextCommand = $this->currentCommand
38+
->execute();
39+
40+
$this->currentCommand = $this->commandFactory->getCommand("difference");
41+
$this->nextCommand = $this->currentCommand
42+
->getFileNames($this->nextCommand)
43+
->setFileReader(new FileReader())
44+
->execute();
45+
46+
$this->currentCommand = $this->commandFactory->getCommand("show");
47+
$this->currentCommand
48+
->execute($this->nextCommand)
49+
->showDiffsToConsole();
5750
}
5851
}

src/Differ.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Differ\Differ;
4+
5+
use Differ\CommandLineParser;
6+
use Differ\FilesDiffCommand;
7+
use Differ\FileReader;
8+
use Differ\DisplayCommand;
9+
10+
function genDiff(string $pathToFile1, string $pathToFile2)
11+
{
12+
$parserCommand = new CommandLineParser();
13+
$fileNames = [
14+
"FILE1" => $pathToFile1,
15+
"FILE2" => $pathToFile2
16+
];
17+
$nextCommand = $parserCommand->setFileNames($fileNames);
18+
19+
$currentCommand = new FilesDiffCommand();
20+
$nextCommand = $currentCommand->getFileNames($nextCommand)
21+
->setFileReader(new FileReader())
22+
->execute();
23+
24+
$currentCommand = new DisplayCommand();
25+
return $currentCommand->execute($nextCommand)
26+
->getDiffs();
27+
}

src/DisplayCommand.php

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,46 +18,42 @@ private function constructContent($accum, $item)
1818
return $accum .= "\n " . $item;
1919
}
2020

21-
public function execute(array $data): ?array
21+
public function execute(CommandInterface $command = null): CommandInterface
2222
{
23+
$file1Content = $command->getFilesContent()['FILE1'];
24+
$file2Content = $command->getFilesContent()['FILE2'];
2325

2426
$this->filesContent[] = "file1.json content:\n";
2527
$this->filesContent[] = array_reduce(
26-
$data['file1'],
28+
$file1Content,
2729
[$this, 'constructContent'],
2830
"{"
2931
) . "\r}\n";
3032

3133
$this->filesContent[] = "file2.json content:\n";
3234
$this->filesContent[] = array_reduce(
33-
$data['file2'],
35+
$file2Content,
3436
[$this, 'constructContent'],
3537
"{"
3638
) . "\n}\n";
3739

38-
$file1Array = $data['file1'];
39-
$file1Keys = array_keys($file1Array);
40-
$file2Array = $data['file2'];
40+
$file1Keys = array_keys($file1Content);
4141
$this->filesDiffContent = array_map(
42-
function ($file1Key) use ($file1Array, $file2Array) {
43-
if (array_key_exists($file1Key, $file2Array)) {
44-
if (!strcmp($file1Array[$file1Key], $file2Array[$file1Key])) {
45-
return " " . $file1Array[$file1Key] . "\n";
42+
function ($file1Key) use ($file1Content, $file2Content) {
43+
if (array_key_exists($file1Key, $file2Content)) {
44+
if (!strcmp($file1Content[$file1Key], $file2Content[$file1Key])) {
45+
return " " . $file1Content[$file1Key] . "\n";
4646
} else {
47-
return " - " . $file1Array[$file1Key] . "\n" .
48-
" + " . $file2Array[$file1Key] . "\n";
47+
return " - " . $file1Content[$file1Key] . "\n" .
48+
" + " . $file2Content[$file1Key] . "\n";
4949
}
5050
} else {
51-
return " - " . $file1Array[$file1Key] . "\n";
51+
return " - " . $file1Content[$file1Key] . "\n";
5252
}
5353
},
5454
$file1Keys
5555
);
56-
return
57-
[
58-
"content" => $this->filesContent,
59-
"diff" => $this->filesDiffContent
60-
];
56+
return $this;
6157
}
6258

6359
public function showContentToConsole()
@@ -69,4 +65,9 @@ public function showDiffsToConsole()
6965
{
7066
echo "{\n" . implode("", $this->filesDiffContent) . "}\n";
7167
}
68+
69+
public function getDiffs()
70+
{
71+
return "{\n" . implode("", $this->filesDiffContent) . "}\n";
72+
}
7273
}

src/FilesDiffCommand.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
class FilesDiffCommand implements CommandInterface
66
{
77
private FileReaderInterface $fileReader;
8+
private array $filesData;
89
private array $file1Content;
910
private array $file2Content;
1011
private array $filesDiffContent;
@@ -15,22 +16,34 @@ public function __construct()
1516
$this->file2Content = [];
1617
}
1718

18-
public function setFileReader(FileReaderInterface $fileReader)
19+
public function setFileReader(FileReaderInterface $fileReader): CommandInterface
1920
{
2021
$this->fileReader = $fileReader;
22+
23+
return $this;
24+
}
25+
26+
public function getFileNames(CommandInterface $command): CommandInterface
27+
{
28+
$this->filesData = $command->getFileNames();
29+
30+
return $this;
2131
}
2232

23-
public function execute(array $cliData): ?array
33+
public function execute(CommandInterface $command = null): CommandInterface
2434
{
25-
if (isset($cliData['FILE1']) && isset($cliData['FILE2'])) {
26-
$this->file1Content = $this->fileReader->readFile($cliData['FILE1']);
27-
$this->file2Content = $this->fileReader->readFile($cliData['FILE2']);
28-
}
35+
$this->file1Content = $this->fileReader->readFile($this->filesData['FILE1']);
36+
$this->file2Content = $this->fileReader->readFile($this->filesData['FILE2']);
37+
38+
return $this;
39+
}
2940

41+
public function getFilesContent()
42+
{
3043
return
3144
[
32-
'file1' => $this->file1Content,
33-
'file2' => $this->file2Content
45+
"FILE1" => $this->file1Content,
46+
"FILE2" => $this->file2Content
3447
];
3548
}
3649
}

0 commit comments

Comments
 (0)