Skip to content

Commit adb7ff1

Browse files
committed
added .yaml files support
1 parent 379209a commit adb7ff1

File tree

12 files changed

+114
-20
lines changed

12 files changed

+114
-20
lines changed

file1.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
host: hexlet.io
2+
timeout: 50
3+
proxy: 123.234.53.22
4+
follow: false

file2.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
timeout: 20
2+
verbose: true
3+
host: hexlet.io

fixtures/file1.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"host": "hexlet.io",
3+
"timeout": 50,
4+
"proxy": "123.234.53.22",
5+
"follow": false
6+
}

fixtures/filesContent.txt renamed to fixtures/filesJSONContent.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ file1.json content:
33
hexlet.io
44
50
55
123.234.53.22
6-
6+
false
77
}
88
file2.json content:
99
{
1010
20
11-
1
11+
true
1212
hexlet.io
1313
}

fixtures/filesYAMLContent.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
file1.yaml content:
2+
{
3+
hexlet.io
4+
50
5+
123.234.53.22
6+
false
7+
}
8+
file2.yaml content:
9+
{
10+
20
11+
true
12+
hexlet.io
13+
}

src/CommandFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class CommandFactory implements CommandFactoryInterface
66
{
77
private $parser;
88
private $fileReader;
9+
private $displayMode;
910

1011
public function __construct($parser, $fileReader)
1112
{

src/DisplayCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function execute(CommandInterface $command = null): CommandInterface
2727
print_r($command->getFilesContent());
2828
break;
2929
default:
30-
print_r("error: unknown mode");
30+
throw new \Exception("internal error: unknown mode for display\n");
3131
}
3232
}
3333

src/FileReader.php

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22

33
namespace Differ;
44

5+
use Symfony\Component\Yaml\Yaml;
6+
57
class FileReader implements FileReaderInterface
68
{
79
private const MAX_FILE_SIZE = 4096;
810
private string $fileFormat;
911

12+
private function normalizeFilename(string $filename)
13+
{
14+
return strtolower($filename);
15+
}
16+
1017
public function __construct(string $fileFormat = 'json')
1118
{
1219
$this->fileFormat = $fileFormat;
@@ -15,14 +22,31 @@ public function __construct(string $fileFormat = 'json')
1522
public function readFile(string $filename, bool $isArray = null): ?array
1623
{
1724
if (file_exists($filename)) {
18-
$handle = fopen($filename, "r");
19-
$result = json_decode(fread($handle, self::MAX_FILE_SIZE), $isArray);
20-
fclose($handle);
21-
$type = gettype($result);
22-
if ($type === 'object') {
23-
return get_object_vars($result);
24-
} elseif ($type === 'array') {
25-
return $result;
25+
$fileNameParts = explode(".", $this->normalizeFilename($filename));
26+
$fileFormat = end($fileNameParts);
27+
if ($fileFormat === 'json') {
28+
$handle = fopen($filename, "r");
29+
$result = json_decode(fread($handle, self::MAX_FILE_SIZE), $isArray);
30+
fclose($handle);
31+
$type = gettype($result);
32+
if ($type === 'object') {
33+
return get_object_vars($result);
34+
} elseif ($type === 'array') {
35+
return $result;
36+
}
37+
} else if ($fileFormat === 'yaml' || $fileFormat === 'yml') {
38+
$handle = fopen($filename, "r");
39+
$result = Yaml::parse(fread($handle, self::MAX_FILE_SIZE), Yaml::PARSE_OBJECT_FOR_MAP);
40+
fclose($handle);
41+
$type = gettype($result);
42+
if ($type === 'object') {
43+
return get_object_vars($result);
44+
} elseif ($type === 'array') {
45+
return $result;
46+
}
47+
} else {
48+
throw new \Exception("Unknown files format: \n" .
49+
"use .json, .yaml (.yml) enstead \n");
2650
}
2751
} else {
2852
return null;

src/FilesDiffCommand.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class FilesDiffCommand implements CommandInterface
1414

1515
private function constructContent($accum, $item): string
1616
{
17-
return $accum .= "\n " . $item;
17+
return $accum .= "\n " . $this->normalizeData($item);
1818
}
1919

2020
private function normalizeData($data): string
@@ -57,14 +57,17 @@ public function execute(CommandInterface $command = null): CommandInterface
5757
$file1Content = $this->filesDataItems[0];
5858
$file2Content = $this->filesDataItems[1];
5959

60-
$this->filesContent[] = "file1.json content:\n";
60+
$filename1Path = explode("/", $this->filesPaths[0]);
61+
$filename2Path = explode("/", $this->filesPaths[1]);
62+
63+
$this->filesContent[] = end($filename1Path) . " content:\n";
6164
$this->filesContent[] = array_reduce(
6265
$file1Content,
6366
[$this, 'constructContent'],
6467
"{"
6568
) . "\n}\n";
6669

67-
$this->filesContent[] = "file2.json content:\n";
70+
$this->filesContent[] = end($filename2Path) . " content:\n";
6871
$this->filesContent[] = array_reduce(
6972
$file2Content,
7073
[$this, 'constructContent'],

src/Gendiff.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,9 @@ function runGendiff(
99
)
1010
): void {
1111
$consoleApp = new ConsoleApp($commandFactory);
12-
$consoleApp->run();
12+
try {
13+
$consoleApp->run();
14+
} catch (\Exception $e) {
15+
print_r("{$e->getMessage()}");
16+
}
1317
}

0 commit comments

Comments
 (0)