Skip to content

Commit

Permalink
modified logic in files, added test for plain formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
RasmuS2024 committed Oct 23, 2024
1 parent e467c7b commit 9be29df
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 77 deletions.
1 change: 1 addition & 0 deletions bin/gendiff
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ $args = Docopt::handle($doc, array('version'=>'Generate diff version 0.2 (21.10.
if (array_key_exists("<firstFile>", $args) & array_key_exists('<secondFile>', $args)) {
$result = genDiff($args['<firstFile>'], $args['<secondFile>'], $args['--format']);
print_r($result);
print_r(PHP_EOL);
}
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
"Hexlet\\Code\\": "src/"
},
"files": [
"src/Parsers.php"
"src/Parsers.php",
"src/Formatters.php",
"formatters/Stylish.php",
"formatters/Plain.php"
]
},

Expand Down
57 changes: 57 additions & 0 deletions formatters/Plain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace GenDiff\Formatters\Plain;

use function GenDiff\Formatters\Stylish\array_flatten;

function iter($value1, $level = '', $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'])) {
$type = $value['type'];
$tkey = $value['key'];
$level = ($level === '') ? $tkey : "{$level}.{$tkey}";
$arrayResult = iter($value['value'], $level, $tkey);
$arrayResult = array_flatten($arrayResult);
$arrayResult = implode('', $arrayResult);
if (array_key_exists('new_value', $value)) {
$newVal = $value['new_value'];
$newVal = (is_string($newVal)) ? "'{$newVal}'" : json_encode($newVal);
}
$result = match ($type) {
' ' => '',
'+' => "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);
}

70 changes: 70 additions & 0 deletions formatters/Stylish.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace GenDiff\Formatters\Stylish;

function getLevelSpaces(int $level)
{
return str_repeat(' ', $level * 4 - 2);
}

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$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));
}
15 changes: 15 additions & 0 deletions src/Formatters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace GenDiff\Formatters;

use function GenDiff\Formatters\Stylish\stylish;
use function GenDiff\Formatters\Plain\plain;

function formatSelect(array $diffSource, string $style): string
{
$result = match ($style) {
'stylish' => stylish($diffSource),
'plain' => plain($diffSource),
};
return $result;
}
82 changes: 6 additions & 76 deletions src/Parsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)) {
Expand Down Expand Up @@ -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));
}
9 changes: 9 additions & 0 deletions tests/DiffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
11 changes: 11 additions & 0 deletions tests/fixtures/2/Plainresult.txt
Original file line number Diff line number Diff line change
@@ -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]

0 comments on commit 9be29df

Please sign in to comment.