diff --git a/src/Differ.php b/src/Differ.php index c8d0cd0..303793e 100644 --- a/src/Differ.php +++ b/src/Differ.php @@ -10,29 +10,32 @@ function addNewLine(string $key, mixed $value): array { - return ['type' => 'added', 'key' => $key, 'difference' => [$key => $value]]; + return ['type' => 'added', 'key' => $key, 'value' => $value]; } function addDeletedLine(string $key, mixed $value): array { - return ['type' => 'deleted', 'key' => $key, 'difference' => [$key => $value]]; + return ['type' => 'deleted', 'key' => $key, 'value' => $value]; } function addSameLine(string $key, mixed $value): array { - return ['type' => 'same', 'key' => $key, 'difference' => [$key => $value]]; + return ['type' => 'same', 'key' => $key, 'value' => $value]; } function addChangedLine(string $key, mixed $value): array { - return ['type' => 'changed', 'key' => $key, 'difference' => $value ]; + return ['type' => 'changed', 'key' => $key, 'value' => $value ]; } function addOldAndNew(string $commonKey, mixed $old, mixed $new): array { return ['type' => 'old and new', 'key' => $commonKey, - 'difference' => [addDeletedLine($commonKey, $old), addNewLine($commonKey, $new)]]; + 'value' => [ + 'oldValue' => $old, + 'newValue' => $new + ]]; } function getNode(mixed $value): array @@ -40,7 +43,7 @@ function getNode(mixed $value): array return [ 'type' => $value['type'], 'key' => $value['key'], - 'difference' => $value['difference'] + 'value' => $value['value'] ]; } diff --git a/src/Formatters/Plain.php b/src/Formatters/Plain.php index eae0693..942cc1a 100644 --- a/src/Formatters/Plain.php +++ b/src/Formatters/Plain.php @@ -15,29 +15,25 @@ function printValuePlain(mixed $value): string function format(array $comparedArray, array $tempForKeys = []): string { $differencies = array_map(function ($node) use ($tempForKeys) { - ['type' => $type, 'key' => $key, 'difference' => $difference] = getNode($node); + ['type' => $type, 'key' => $key, 'value' => $value] = getNode($node); $newKeys = array_merge($tempForKeys, [$key]); $keyToPrint = implode('.', $newKeys); switch ($type) { case 'old and new': - $oldAndNewValues = array_map(function ($node) { - ['type' => $type, 'difference' => $difference] = getNode($node); - $valueToPrint = printValuePlain(current($difference)); - return [$type => $valueToPrint]; - }, $difference); - $bothValues = array_merge(...$oldAndNewValues); - return "Property '{$keyToPrint}' was updated. From {$bothValues['deleted']} to {$bothValues['added']}"; + $oldValue = printValuePlain($value['oldValue']); + $newValue = printValuePlain($value['newValue']); + return "Property '{$keyToPrint}' was updated. From {$oldValue} to {$newValue}"; case 'changed': - return format($difference, $newKeys); + return format($value, $newKeys); case 'same': break; case 'added': - $valueString = printValuePlain(current($difference)); + $valueString = printValuePlain($value); return "Property '{$keyToPrint}' was added with value: {$valueString}"; case 'deleted': return "Property '{$keyToPrint}' was removed"; default: - throw new \Exception("Unknown status of value: \"{$type}\"!"); + throw new \Exception("Unknown type of value: \"{$type}\"!"); } }, $comparedArray); $withoutEmpties = array_filter($differencies, fn ($array) => $array); diff --git a/src/Formatters/Stylish.php b/src/Formatters/Stylish.php index 674b611..f5e6b25 100644 --- a/src/Formatters/Stylish.php +++ b/src/Formatters/Stylish.php @@ -12,17 +12,26 @@ 'old and new' => '', ]; -function stringify(mixed $item, int $depth, int $offset = 2, string $separator = ' '): string +function createEmptySpace(int $depth, int $offset = 0, string $separator = ' ') +{ + $emptySpaceWithoutSymbol = str_repeat($separator, $depth); + if (!$offset) { + return $emptySpaceWithoutSymbol; + } + return substr($emptySpaceWithoutSymbol, $offset, null); +} + +function stringify(mixed $item, int $depth): string { if (!is_array($item)) { $itemString = is_string($item) ? $item : var_export($item, true); return $itemString === 'NULL' ? 'null' : $itemString; } - $emptySpace = str_repeat($separator, $depth); - $lines = array_map(function ($key, $value) use ($depth, $separator, $offset) { + $emptySpace = createEmptySpace($depth); + $lines = array_map(function ($key, $value) use ($depth) { $nextDepth = $depth + 1; - $emptySpace = substr(str_repeat($separator, $nextDepth), $offset, null); - $valueString = stringify($value, $nextDepth, $offset); + $emptySpace = createEmptySpace($nextDepth); + $valueString = stringify($value, $nextDepth); return "{$emptySpace}{$key}: {$valueString}"; }, array_keys($item), $item); $linesWithBrackets = ['{', ...$lines, "{$emptySpace}}"]; @@ -31,23 +40,25 @@ function stringify(mixed $item, int $depth, int $offset = 2, string $separator = function format(array $comparedData, int $depth = 0): string { - $iter = function ($comparedData) use (&$iter, $depth) { - $result = array_map(function ($data) use ($iter, $depth) { - ['type' => $type, 'key' => $key, 'difference' => $difference] = getNode($data); - $symbol = SYMBOLS[$type]; - $keyWithSymbol = "{$symbol} {$key}"; - $nextDepth = $depth + 1; - $offsetWithoutSymbol = 0; - if ($type === 'old and new') { - return $iter($difference); - } elseif ($type === 'changed') { - $valueString = format($difference, $nextDepth); - } else { - $valueString = stringify(current($difference), $nextDepth, $offsetWithoutSymbol); - } - return [$keyWithSymbol => $valueString]; - }, $comparedData); - return array_merge(...$result); - }; - return stringify($iter($comparedData), $depth); + $emptySpace = createEmptySpace($depth); + $lines = array_map(function ($data) use ($depth) { + ['type' => $type, 'key' => $key, 'value' => $value] = getNode($data); + $symbol = SYMBOLS[$type]; + $nextDepth = $depth + 1; + $offsetForSymbol = 2; + $emptySpace = createEmptySpace($nextDepth, $offsetForSymbol); + $keyForPrint = "{$emptySpace}{$symbol} {$key}"; + if ($type === 'old and new') { + $valueOld = stringify($value['oldValue'], $nextDepth); + $valueNew = stringify($value['newValue'], $nextDepth); + return "{$emptySpace}- {$key}: {$valueOld}\n{$emptySpace}+ {$key}: {$valueNew}"; + } elseif ($type === 'changed') { + $valueString = format($value, $nextDepth); + } else { + $valueString = stringify($value, $nextDepth); + } + return "{$keyForPrint}: {$valueString}"; + }, $comparedData); + $linesWithBrackets = ['{', ...$lines, "{$emptySpace}}"]; + return implode("\n", $linesWithBrackets); } diff --git a/tests/FormattersTest.php b/tests/FormattersTest.php index f3c5749..e0a0c5b 100644 --- a/tests/FormattersTest.php +++ b/tests/FormattersTest.php @@ -17,7 +17,7 @@ public function testUnknownReportFormatException(): void public function testUnknownStatusOfValue(): void { - $this->expectExceptionMessage("Unknown status of value: \"*\"!"); - format([['type' => '*', 'key' => 'key', 'difference' => ['key' => 'value']]]); + $this->expectExceptionMessage("Unknown type of value: \"*\"!"); + format([['type' => '*', 'key' => 'key', 'value' => 'value']]); } } diff --git a/tests/fixtures/ResultOfJson.json b/tests/fixtures/ResultOfJson.json index ca90446..102f40f 100644 --- a/tests/fixtures/ResultOfJson.json +++ b/tests/fixtures/ResultOfJson.json @@ -2,107 +2,69 @@ { "type": "changed", "key": "common", - "difference": [ + "value": [ { "type": "added", "key": "follow", - "difference": { - "follow": false - } + "value": false }, { "type": "same", "key": "setting1", - "difference": { - "setting1": "Value 1" - } + "value": "Value 1" }, { "type": "deleted", "key": "setting2", - "difference": { - "setting2": 200 - } + "value": 200 }, { "type": "old and new", "key": "setting3", - "difference": [ - { - "type": "deleted", - "key": "setting3", - "difference": { - "setting3": true - } - }, - { - "type": "added", - "key": "setting3", - "difference": { - "setting3": null - } - } - ] + "value": { + "oldValue": true, + "newValue": null + } }, { "type": "added", "key": "setting4", - "difference": { - "setting4": "blah blah" - } + "value": "blah blah" }, { "type": "added", "key": "setting5", - "difference": { - "setting5": { - "key5": "value5" - } + "value": { + "key5": "value5" } }, { "type": "changed", "key": "setting6", - "difference": [ + "value": [ { "type": "changed", "key": "doge", - "difference": [ + "value": [ { "type": "old and new", "key": "wow", - "difference": [ - { - "type": "deleted", - "key": "wow", - "difference": { - "wow": "" - } - }, - { - "type": "added", - "key": "wow", - "difference": { - "wow": "so much" - } - } - ] + "value": { + "oldValue": "", + "newValue": "so much" + } } ] }, { "type": "same", "key": "key", - "difference": { - "key": "value" - } + "value": "value" }, { "type": "added", "key": "ops", - "difference": { - "ops": "vops" - } + "value": "vops" } ] } @@ -111,82 +73,52 @@ { "type": "changed", "key": "group1", - "difference": [ + "value": [ { "type": "old and new", "key": "baz", - "difference": [ - { - "type": "deleted", - "key": "baz", - "difference": { - "baz": "bas" - } - }, - { - "type": "added", - "key": "baz", - "difference": { - "baz": "bars" - } - } - ] + "value": { + "oldValue": "bas", + "newValue": "bars" + } }, { "type": "same", "key": "foo", - "difference": { - "foo": "bar" - } + "value": "bar" }, { "type": "old and new", "key": "nest", - "difference": [ - { - "type": "deleted", - "key": "nest", - "difference": { - "nest": { - "key": "value" - } - } + "value": { + "oldValue": { + "key": "value" }, - { - "type": "added", - "key": "nest", - "difference": { - "nest": "str" - } - } - ] + "newValue": "str" + } } ] }, { "type": "deleted", "key": "group2", - "difference": { - "group2": { - "abc": 12345, - "deep": { - "id": 45 - } + "value": { + "abc": 12345, + "deep": { + "id": 45 } } }, { "type": "added", "key": "group3", - "difference": { - "group3": { - "deep": { - "id": { - "number": 45 - } - }, - "fee": 100500 - } + "value": { + "deep": { + "id": { + "number": 45 + } + }, + "fee": 100500 } } ] \ No newline at end of file