-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
modified logic in files, added test for plain formatter
- Loading branch information
1 parent
e467c7b
commit 9be29df
Showing
8 changed files
with
173 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |