Skip to content

Commit 9fc8279

Browse files
committed
update execute in filesDiffCommand
1 parent af2f5d6 commit 9fc8279

File tree

1 file changed

+64
-19
lines changed

1 file changed

+64
-19
lines changed

src/FilesDiffCommand.php

Lines changed: 64 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,21 @@
55
class FilesDiffCommand implements CommandInterface
66
{
77
private FileReaderInterface $fileReader;
8-
private array $filesData;
9-
private array $file1Content;
10-
private array $file2Content;
11-
private array $filesDiffContent;
8+
private array $filesPaths;
9+
private array $filesDataItems;
10+
private array $filesDiffs;
11+
private string $filesDiffsString;
12+
private array $filesContent;
13+
private string $filesContentString;
14+
15+
private function constructContent($accum, $item)
16+
{
17+
return $accum .= "\n " . $item;
18+
}
1219

1320
public function __construct()
1421
{
15-
$this->file1Content = [];
16-
$this->file2Content = [];
22+
$this->filesDataItems = [];
1723
}
1824

1925
public function setFileReader(FileReaderInterface $fileReader): CommandInterface
@@ -23,27 +29,66 @@ public function setFileReader(FileReaderInterface $fileReader): CommandInterface
2329
return $this;
2430
}
2531

26-
public function setFileNames(CommandInterface $command): CommandInterface
32+
public function execute(CommandInterface $command = null): CommandInterface
2733
{
28-
$this->filesData = $command->getFileNames();
34+
if (!is_null($command)) {
35+
$this->filesPaths = [
36+
$command->getFileNames()['FILE1'],
37+
$command->getFileNames()['FILE2']
38+
];
2939

30-
return $this;
31-
}
40+
foreach ($this->filesPaths as $filePath) {
41+
$this->filesDataItems[] = $this->fileReader->readFile($filePath);
42+
}
3243

33-
public function execute(CommandInterface $command = null): CommandInterface
34-
{
35-
$this->file1Content = $this->fileReader->readFile($this->filesData['FILE1']);
36-
$this->file2Content = $this->fileReader->readFile($this->filesData['FILE2']);
44+
$file1Content = $this->filesDataItems[0];
45+
$file2Content = $this->filesDataItems[1];
46+
47+
$this->filesContent[] = "file1.json content:\n";
48+
$this->filesContent[] = array_reduce(
49+
$file1Content,
50+
[$this, 'constructContent'],
51+
"{"
52+
) . "\n}\n";
53+
54+
$this->filesContent[] = "file2.json content:\n";
55+
$this->filesContent[] = array_reduce(
56+
$file2Content,
57+
[$this, 'constructContent'],
58+
"{"
59+
) . "\n}\n";
60+
61+
$this->filesContentString = implode("", $this->filesContent);
62+
63+
$file1Keys = array_keys($file1Content);
64+
$this->filesDiffs = array_map(
65+
function ($file1Key) use ($file1Content, $file2Content) {
66+
if (array_key_exists($file1Key, $file2Content)) {
67+
if (!strcmp($file1Content[$file1Key], $file2Content[$file1Key])) {
68+
return " " . $file1Content[$file1Key] . "\n";
69+
} else {
70+
return " - " . $file1Content[$file1Key] . "\n" .
71+
" + " . $file2Content[$file1Key] . "\n";
72+
}
73+
} else {
74+
return " - " . $file1Content[$file1Key] . "\n";
75+
}
76+
},
77+
$file1Keys
78+
);
79+
$this->filesDiffsString = "{\n" . implode("", $this->filesDiffs) . "}\n";
80+
}
3781

3882
return $this;
3983
}
4084

4185
public function getFilesContent()
4286
{
43-
return
44-
[
45-
"FILE1" => $this->file1Content,
46-
"FILE2" => $this->file2Content
47-
];
87+
return $this->filesContentString;
88+
}
89+
90+
public function getFilesDiffs()
91+
{
92+
return $this->filesDiffsString;
4893
}
4994
}

0 commit comments

Comments
 (0)