Skip to content

Commit d2c5efa

Browse files
committed
display to console files differents
1 parent 464caab commit d2c5efa

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

src/DisplayCommand.php

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
class DisplayCommand implements CommandInterface
88
{
99
private array $filesDiffContent;
10+
private array $filesContent;
1011

1112
public function __construct()
1213
{
14+
$this->filesContent = [];
1315
$this->filesDiffContent = [];
1416
}
1517

@@ -21,23 +23,50 @@ private function constructContent($accum, $item)
2123
public function execute(object $data): object
2224
{
2325

24-
$this->filesDiffContent[] = "file1.json content:\n";
25-
$this->filesDiffContent[] = array_reduce(
26+
$this->filesContent[] = "file1.json content:\n";
27+
$this->filesContent[] = array_reduce(
2628
get_object_vars($data)['file1'],
2729
[$this, 'constructContent'],
2830
"{") . "\r}\n";
2931

30-
$this->filesDiffContent[] = "file2.json content:\n";
31-
$this->filesDiffContent[] = array_reduce(
32+
$this->filesContent[] = "file2.json content:\n";
33+
$this->filesContent[] = array_reduce(
3234
get_object_vars($data)['file2'],
3335
[$this, 'constructContent'],
3436
"{") . "\n}\n";
3537

38+
$file1Array = get_object_vars($data)['file1'];
39+
$file1Keys = array_keys($file1Array);
40+
$file2Array = get_object_vars($data)['file2'];
41+
$this->filesDiffContent = array_map(
42+
function($file1Key) use ($file1Array, $file2Array)
43+
{
44+
if (array_key_exists($file1Key, $file2Array)) {
45+
if (!strcmp($file1Array[$file1Key], $file2Array[$file1Key])) {
46+
return " " . $file1Array[$file1Key] . "\n";
47+
} else {
48+
return " - " . $file1Array[$file1Key] . "\n" .
49+
" + " . $file2Array[$file1Key] . "\n";
50+
}
51+
} else {
52+
return " - " . $file1Array[$file1Key] . "\n";
53+
}
54+
}, $file1Keys
55+
);
56+
return $this;
57+
}
58+
59+
public function showContentToConsole()
60+
{
61+
echo implode("", $this->filesContent);
62+
3663
return $this;
3764
}
3865

3966
public function showDiffsToConsole()
4067
{
41-
echo implode("", $this->filesDiffContent);
68+
echo "{\n" . implode("", $this->filesDiffContent) . "}\n";
69+
70+
return $this;
4271
}
4372
}

0 commit comments

Comments
 (0)