diff --git a/bin/gendiff b/bin/gendiff index 0c03799..75c84b7 100755 --- a/bin/gendiff +++ b/bin/gendiff @@ -30,4 +30,5 @@ $args = Docopt::handle($doc, array('version'=>'Generate diff version 0.2 (21.10. if (array_key_exists("", $args) & array_key_exists('', $args)) { $result = genDiff($args[''], $args[''], $args['--format']); print_r($result); + print_r(PHP_EOL); } diff --git a/composer.json b/composer.json index 43d6a59..b3e2516 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,10 @@ "Hexlet\\Code\\": "src/" }, "files": [ - "src/Parsers.php" + "src/Parsers.php", + "src/Formatters.php", + "formatters/Stylish.php", + "formatters/Plain.php" ] }, diff --git a/formatters/Plain.php b/formatters/Plain.php new file mode 100644 index 0000000..a37acfc --- /dev/null +++ b/formatters/Plain.php @@ -0,0 +1,57 @@ + '', + '+' => "Property '{$level}' was added with value: [complex value]\n", + '-' => "Property '{$level}' was removed\n", + '_' => "Property '{$level}' was updated. From [complex value] to {$newVal}\n", + }; + return "{$result}{$arrayResult}"; + } else { + $type = $value['type']; + $tkey = $value['key']; + $val = $value['value']; + $val = (is_string($val)) ? "'{$val}'" : json_encode($val); + if (array_key_exists('new_value', $value)) { + $newVal = $value['new_value']; + $newVal = (is_string($newVal)) ? "'{$newVal}'" : json_encode($newVal); + } + $result = match ($type) { + ' ' => '', + '+' => "Property '{$level}.{$tkey}' was added with value: {$val}\n", + '-' => "Property '{$level}.{$tkey}' was removed\n", + '_' => "Property '{$level}.{$tkey}' was updated. From {$val} to {$newVal}\n", + }; + } + return $result; + } + }, array_keys($value1), $value1); + return $output; +} + +function plain(array $tree): string +{ + $result = iter($tree, ''); + $result = implode('', array_flatten($result)); + return substr($result, 0, -1); +} + diff --git a/formatters/Stylish.php b/formatters/Stylish.php new file mode 100644 index 0000000..f3c0a88 --- /dev/null +++ b/formatters/Stylish.php @@ -0,0 +1,70 @@ + $value) { + if ($depth >= 0 && is_array($value)) { + $value = array_flatten($value, $depth > 1 ? $depth - 1 : 0 - $depth); + $result = array_merge($result, $value); + } else { + $result[] = $value; + } + } + return $result; +} + +function iter(mixed$value1, $level = 1, $key1 = null): array +{ + $output = array_map(function ($key, $value) use ($level) { + if (is_array($value) && array_key_exists('value', $value)) { + if (is_array($value['value'])) { + $spaces = getLevelSpaces($level); + $type = $value['type']; + $tkey = $value['key']; + $level++; + $temp = iter($value['value'], $level, $tkey); + $temp = array_flatten($temp); + $temp = implode('', $temp); + if ($type === "_") { + $newVal = $value['new_value']; + $newVal = (is_string($newVal)) ? $newVal : json_encode($newVal); + return "{$spaces}- {$tkey}: {\n{$temp}{$spaces} }\n{$spaces}+ {$tkey}: {$newVal}\n"; + } else { + return "{$spaces}{$type} {$tkey}: {\n{$temp}{$spaces} }\n"; + } + } else { + $spaces = getLevelSpaces($level); + $type = $value['type']; + $tkey = $value['key']; + $val = $value['value']; + $val = (is_string($val)) ? "$val" : json_encode($val); + $val = ($val === '') ? '' : " {$val}"; + if ($type === "_") { + $newVal = $value['new_value']; + $newVal = (is_string($newVal)) ? $newVal : json_encode($newVal); + $newVal = ($newVal === '') ? '' : " {$newVal}"; + return "{$spaces}- {$tkey}:{$val}\n{$spaces}+ {$tkey}:{$newVal}\n"; + } else { + return "{$spaces}{$type} {$tkey}:{$val}\n"; + } + } + } + }, array_keys($value1), $value1); + return $output; +} + +function stylish(array $tree) +{ + $result[] = "{\n"; + $result[] = iter($tree, 1); + $result[] = "}"; + return implode('', array_flatten($result)); +} diff --git a/src/Formatters.php b/src/Formatters.php new file mode 100644 index 0000000..c00aed4 --- /dev/null +++ b/src/Formatters.php @@ -0,0 +1,15 @@ + stylish($diffSource), + 'plain' => plain($diffSource), + }; + return $result; +} diff --git a/src/Parsers.php b/src/Parsers.php index 13afabd..933e93d 100644 --- a/src/Parsers.php +++ b/src/Parsers.php @@ -4,6 +4,8 @@ use Symfony\Component\Yaml\Yaml; +use function GenDiff\Formatters\formatSelect; + function getSortedKeys(array $json1, array $json2): array { $keys1 = array_keys($json1); @@ -29,17 +31,12 @@ function genDiff(mixed $file1Path, mixed $file2Path, string $style = 'stylish'): { $data1 = fileReader($file1Path); $data2 = fileReader($file2Path); - $tt = filesDiffer($data1, $data2); - $st = ''; - if ($style === 'stylish') { - $st = stylish($tt); - } - //print_r($st); - return $st; - //json_encode(filesDiffer($data1, $data2)); + $dataDiff = filesDiffer($data1, $data2); + $result = formatSelect($dataDiff, $style); + return $result; } -function arrayKeysInsert(array $array) +function arrayKeysInsert(array $array): array { $value1 = array_map(function ($keyIn, $valueIn) { if (!is_array($valueIn)) { @@ -89,70 +86,3 @@ function filesDiffer(mixed $data1, mixed $data2): mixed }, $sortedKeys); return $data; } - -function array_flatten($tree, $depth = 0) -{ - $result = []; - foreach ($tree as $key => $value) { - if ($depth >= 0 && is_array($value)) { - $value = array_flatten($value, $depth > 1 ? $depth - 1 : 0 - $depth); - $result = array_merge($result, $value); - } else { - $result[] = $value; - } - } - return $result; -} - -function iter(mixed $key1, $value1, $level = 1) -{ - $output = array_map(function ($key, $value) use ($level) { - if (is_array($value) && array_key_exists('value', $value)) { - if (is_array($value['value'])) { - $spaces = getLevelSpaces($level); - $type = $value['type']; - $tkey = $value['key']; - $level++; - $temp = iter($tkey, $value['value'], $level); - $temp = array_flatten($temp); - $temp = implode('', $temp); - if ($type === "_") { - $newVal = $value['new_value']; - $newVal = (is_string($newVal)) ? $newVal : json_encode($newVal); - return "{$spaces}- {$tkey}: {\n{$temp}{$spaces} }\n{$spaces}+ {$tkey}: {$newVal}\n"; - } else { - return "{$spaces}{$type} {$tkey}: {\n{$temp}{$spaces} }\n"; - } - } else { - $spaces = getLevelSpaces($level); - $type = $value['type']; - $tkey = $value['key']; - $val = $value['value']; - $val = (is_string($val)) ? "$val" : json_encode($val); - $val = ($val === '') ? '' : " {$val}"; - if ($type === "_") { - $newVal = $value['new_value']; - $newVal = (is_string($newVal)) ? $newVal : json_encode($newVal); - $newVal = ($newVal === '') ? '' : " {$newVal}"; - return "{$spaces}- {$tkey}:{$val}\n{$spaces}+ {$tkey}:{$newVal}\n"; - } else { - return "{$spaces}{$type} {$tkey}:{$val}\n"; - } - } - } - }, array_keys($value1), $value1); - return $output; -} - -function getLevelSpaces(int $level) -{ - return str_repeat(' ', $level * 4 - 2); -} - -function stylish(array $tree) -{ - $result[] = "{\n"; - $result[] = iter('', $tree, 1); - $result[] = "}"; - return implode('', array_flatten($result)); -} diff --git a/tests/DiffTest.php b/tests/DiffTest.php index 8af3e1f..787c103 100644 --- a/tests/DiffTest.php +++ b/tests/DiffTest.php @@ -52,4 +52,13 @@ public function testFilesDiffYaml2(): void $diffStringFromFiles = genDiff($pathToFile1, $pathToFile2); $this->assertStringEqualsFile($pathToFile3, $diffStringFromFiles); } + + public function testFilesDiffJsonToPlainFormat(): void + { + $pathToFile1 = $this->getFixtureFullPath('2/file1.json'); + $pathToFile2 = $this->getFixtureFullPath('2/file2.json'); + $pathToFile3 = $this->getFixtureFullPath('2/Plainresult.txt'); + $diffStringFromFiles = genDiff($pathToFile1, $pathToFile2, 'plain'); + $this->assertStringEqualsFile($pathToFile3, $diffStringFromFiles); + } } diff --git a/tests/fixtures/2/Plainresult.txt b/tests/fixtures/2/Plainresult.txt new file mode 100644 index 0000000..bf58a17 --- /dev/null +++ b/tests/fixtures/2/Plainresult.txt @@ -0,0 +1,11 @@ +Property 'common.follow' was added with value: false +Property 'common.setting2' was removed +Property 'common.setting3' was updated. From true to null +Property 'common.setting4' was added with value: 'blah blah' +Property 'common.setting5' was added with value: [complex value] +Property 'common.setting6.doge.wow' was updated. From '' to 'so much' +Property 'common.setting6.ops' was added with value: 'vops' +Property 'group1.baz' was updated. From 'bas' to 'bars' +Property 'group1.nest' was updated. From [complex value] to 'str' +Property 'group2' was removed +Property 'group3' was added with value: [complex value] \ No newline at end of file