Skip to content

Commit

Permalink
changed add old and new interface
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-xz committed Feb 29, 2024
1 parent 5ac8bb2 commit 53bafe6
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 103 deletions.
21 changes: 11 additions & 10 deletions src/Differ.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,36 @@

function addNewLine(mixed $value): array
{
return ['status' => 'added', 'difference' => $value ];
return ['type' => 'added', 'key' => key($value), 'difference' => $value ];
}

function addDeletedLine(mixed $value): array
{
return ['status' => 'deleted', 'difference' => $value ];
return ['type' => 'deleted', 'key' => key($value), 'difference' => $value ];
}

function addSameLine(mixed $value): array
{
return ['status' => 'same', 'difference' => $value ];
return ['type' => 'same', 'key' => key($value), 'difference' => $value ];
}

function addChangedLine(mixed $value): array
{
return ['status' => 'changed', 'difference' => $value ];
return ['type' => 'changed', 'key' => key($value), 'difference' => $value ];
}

function addOldAndNew(mixed $old, mixed $new): array
function addOldAndNew(string $commonKey, mixed $old, mixed $new): array
{
$commomKey = key($old);
return ['status' => 'old and new',
'difference' => [$commomKey => [addDeletedLine($old), addNewLine($new)]]];
return ['type' => 'old and new',
'key' => $commonKey,
'difference' => [addDeletedLine([$commonKey => $old]), addNewLine([$commonKey => $new])]];
}

function getNode(mixed $value): array
{
return [
'status' => $value['status'],
'type' => $value['type'],
'key' => $value['key'],
'difference' => $value['difference']
];
}
Expand All @@ -57,7 +58,7 @@ function compare(array $dataOne, array $dataTwo): array
} elseif (is_array($dataOne[$key]) && is_array($dataTwo[$key])) {
return addChangedLine([$key => compare($dataOne[$key], $dataTwo[$key])]);
} else {
return addOldAndNew([$key => $dataOne[$key]], [$key => $dataTwo[$key]]);
return addOldAndNew($key, $dataOne[$key], $dataTwo[$key]);
}
}, $sortedKeys);
}
Expand Down
13 changes: 6 additions & 7 deletions src/Formatters/Plain.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@ function printValuePlain(mixed $value): string
function format(array $comparedArray, array $tempForKeys = []): string
{
$differencies = array_map(function ($node) use ($tempForKeys) {
['status' => $status, 'difference' => $difference] = getNode($node);
$key = key($difference);
['type' => $type, 'key' => $key, 'difference' => $difference] = getNode($node);
$value = current($difference);
$newKeys = array_merge($tempForKeys, [$key]);
$keyToPrint = implode('.', $newKeys);
switch ($status) {
switch ($type) {
case 'old and new':
$oldAndNewValues = array_map(function ($node) {
['status' => $status, 'difference' => $difference] = getNode($node);
['type' => $type, 'difference' => $difference] = getNode($node);
$valueToPrint = printValuePlain(current($difference));
return [$status => $valueToPrint];
}, $value);
return [$type => $valueToPrint];
}, $difference);
$bothValues = array_merge(...$oldAndNewValues);
return "Property '{$keyToPrint}' was updated. From {$bothValues['deleted']} to {$bothValues['added']}";
case 'changed':
Expand All @@ -39,7 +38,7 @@ function format(array $comparedArray, array $tempForKeys = []): string
case 'deleted':
return "Property '{$keyToPrint}' was removed";
default:
throw new \Exception("Unknown status of value: \"{$status}\"!");
throw new \Exception("Unknown status of value: \"{$type}\"!");
}
}, $comparedArray);
$withoutEmpties = array_filter($differencies, fn ($array) => $array);
Expand Down
11 changes: 5 additions & 6 deletions src/Formatters/Stylish.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,15 @@ function format(array $comparedData, int $depth = 0): string
{
$iter = function ($comparedData) use (&$iter, $depth) {
$result = array_map(function ($data) use ($iter, $depth) {
['status' => $status, 'difference' => $difference] = getNode($data);
$key = key($difference);
['type' => $type, 'key' => $key, 'difference' => $difference] = getNode($data);
$value = current($difference);
$symbol = SYMBOLS[$status];
$symbol = SYMBOLS[$type];
$keyWithSymbol = "{$symbol} {$key}";
$nextDepth = $depth + 1;
$offsetWithoutSymbol = 0;
if ($status === 'old and new') {
return $iter($value);
} elseif ($status === 'changed') {
if ($type === 'old and new') {
return $iter($difference);
} elseif ($type === 'changed') {
$valueString = format($value, $nextDepth);
} else {
$valueString = stringify($value, $nextDepth, $offsetWithoutSymbol);
Expand Down
2 changes: 1 addition & 1 deletion tests/FormattersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public function testUnknownReportFormatException(): void
public function testUnknownStatusOfValue(): void
{
$this->expectExceptionMessage("Unknown status of value: \"*\"!");
format([['status' => '*', 'symbol' => ' ', 'difference' => ['key' => 'value']]]);
format([['type' => '*', 'key' => 'key', 'difference' => ['key' => 'value']]]);
}
}
176 changes: 97 additions & 79 deletions tests/fixtures/ResultOfJson.json
Original file line number Diff line number Diff line change
@@ -1,97 +1,109 @@
[
{
"status": "changed",
"type": "changed",
"key": "common",
"difference": {
"common": [
{
"status": "added",
"type": "added",
"key": "follow",
"difference": {
"follow": false
}
},
{
"status": "same",
"type": "same",
"key": "setting1",
"difference": {
"setting1": "Value 1"
}
},
{
"status": "deleted",
"type": "deleted",
"key": "setting2",
"difference": {
"setting2": 200
}
},
{
"status": "old and new",
"difference": {
"setting3": [
{
"status": "deleted",
"difference": {
"setting3": true
}
},
{
"status": "added",
"difference": {
"setting3": null
}
"type": "old and new",
"key": "setting3",
"difference": [
{
"type": "deleted",
"key": "setting3",
"difference": {
"setting3": true
}
]
}
},
{
"type": "added",
"key": "setting3",
"difference": {
"setting3": null
}
}
]
},
{
"status": "added",
"type": "added",
"key": "setting4",
"difference": {
"setting4": "blah blah"
}
},
{
"status": "added",
"type": "added",
"key": "setting5",
"difference": {
"setting5": {
"key5": "value5"
}
}
},
{
"status": "changed",
"type": "changed",
"key": "setting6",
"difference": {
"setting6": [
{
"status": "changed",
"type": "changed",
"key": "doge",
"difference": {
"doge": [
{
"status": "old and new",
"difference": {
"wow": [
{
"status": "deleted",
"difference": {
"wow": ""
}
},
{
"status": "added",
"difference": {
"wow": "so much"
}
"type": "old and new",
"key": "wow",
"difference": [
{
"type": "deleted",
"key": "wow",
"difference": {
"wow": ""
}
},
{
"type": "added",
"key": "wow",
"difference": {
"wow": "so much"
}
]
}
}
]
}
]
}
},
{
"status": "same",
"type": "same",
"key": "key",
"difference": {
"key": "value"
}
},
{
"status": "added",
"type": "added",
"key": "ops",
"difference": {
"ops": "vops"
}
Expand All @@ -103,60 +115,65 @@
}
},
{
"status": "changed",
"type": "changed",
"key": "group1",
"difference": {
"group1": [
{
"status": "old and new",
"difference": {
"baz": [
{
"status": "deleted",
"difference": {
"baz": "bas"
}
},
{
"status": "added",
"difference": {
"baz": "bars"
}
"type": "old and new",
"key": "baz",
"difference": [
{
"type": "deleted",
"key": "baz",
"difference": {
"baz": "bas"
}
]
}
},
{
"type": "added",
"key": "baz",
"difference": {
"baz": "bars"
}
}
]
},
{
"status": "same",
"type": "same",
"key": "foo",
"difference": {
"foo": "bar"
}
},
{
"status": "old and new",
"difference": {
"nest": [
{
"status": "deleted",
"difference": {
"nest": {
"key": "value"
}
}
},
{
"status": "added",
"difference": {
"nest": "str"
"type": "old and new",
"key": "nest",
"difference": [
{
"type": "deleted",
"key": "nest",
"difference": {
"nest": {
"key": "value"
}
}
]
}
},
{
"type": "added",
"key": "nest",
"difference": {
"nest": "str"
}
}
]
}
]
}
},
{
"status": "deleted",
"type": "deleted",
"key": "group2",
"difference": {
"group2": {
"abc": 12345,
Expand All @@ -167,7 +184,8 @@
}
},
{
"status": "added",
"type": "added",
"key": "group3",
"difference": {
"group3": {
"deep": {
Expand Down

0 comments on commit 53bafe6

Please sign in to comment.