Skip to content

Commit 27a166e

Browse files
committed
feat: add support for json scalar types
1 parent b748b92 commit 27a166e

File tree

2 files changed

+123
-3
lines changed

2 files changed

+123
-3
lines changed

src/JsonDiff.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ class JsonDiff
5858
*/
5959
private $calculateMinimalDiffOfListArrayAction;
6060

61-
public function __construct(array $original, array $new, string $startingPath = '')
61+
/**
62+
* @param array|bool|float|int|string|null $original
63+
* @param array|bool|float|int|string|null $new
64+
*/
65+
public function __construct($original, $new, string $startingPath = '')
6266
{
6367
$this->keysAdded = collect();
6468
$this->keysRemoved = collect();
@@ -75,8 +79,21 @@ public function __construct(array $original, array $new, string $startingPath =
7579
$this->calculateMinimalDiffOfListArrayAction = $this->serviceContainer
7680
->make(CalculateMinimalDiffOfListArrayAction::class);
7781

78-
// Arrays are the same, no differences
79-
if (empty($original) && empty($new)) {
82+
// No differences
83+
if ($original === $new) {
84+
return;
85+
}
86+
87+
// If either is not an array, just a value change occurred
88+
if (! is_array($original) || ! is_array($new)) {
89+
$this->valuesChanged->push(
90+
new ValueChange(
91+
$startingPath,
92+
$original,
93+
$new
94+
)
95+
);
96+
8097
return;
8198
}
8299

@@ -183,26 +200,41 @@ public function addRemovedKey(string $path, $name, $value): self
183200
return $this;
184201
}
185202

203+
/**
204+
* @return Collection<KeyAdded>
205+
*/
186206
public function getKeysAdded(): Collection
187207
{
188208
return $this->keysAdded;
189209
}
190210

211+
/**
212+
* @return Collection<KeyRemoved>
213+
*/
191214
public function getKeysRemoved(): Collection
192215
{
193216
return $this->keysRemoved;
194217
}
195218

219+
/**
220+
* @return Collection<ValueAdded>
221+
*/
196222
public function getValuesAdded(): Collection
197223
{
198224
return $this->valuesAdded;
199225
}
200226

227+
/**
228+
* @return Collection<ValueRemoved>
229+
*/
201230
public function getValuesRemoved(): Collection
202231
{
203232
return $this->valuesRemoved;
204233
}
205234

235+
/**
236+
* @return Collection<ValueChange>
237+
*/
206238
public function getValuesChanged(): Collection
207239
{
208240
return $this->valuesChanged;

tests/Feature/JsonDiffTest.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,4 +523,92 @@ public function test_remove_nested_element_between_jsons(): void
523523
$jsonDiff = new JsonDiff($original, $new);
524524
// dd($jsonDiff->getKeysRemoved());
525525
}
526+
527+
public function test_json_diff_with_scalar_types(): void
528+
{
529+
$dataSets = [
530+
'string' => [
531+
'original' => 'Original String',
532+
'new' => 'New String',
533+
'changes' => 1,
534+
],
535+
'number' => [
536+
'original' => 1,
537+
'new' => 2,
538+
'changes' => 1,
539+
],
540+
'boolean' => [
541+
'original' => true,
542+
'new' => false,
543+
'changes' => 1,
544+
],
545+
'null' => [
546+
'original' => null,
547+
'new' => null,
548+
'changes' => 0,
549+
],
550+
];
551+
552+
// Compare same data types
553+
foreach ($dataSets as $dataSet) {
554+
$jsonDiff = new JsonDiff($dataSet['original'], $dataSet['new']);
555+
$this->assertCount($dataSet['changes'], $jsonDiff->getValuesChanged());
556+
$this->assertSame($dataSet['changes'], $jsonDiff->getNumberOfChanges());
557+
$this->assertCount(0, $jsonDiff->getKeysAdded());
558+
$this->assertCount(0, $jsonDiff->getKeysRemoved());
559+
$this->assertCount(0, $jsonDiff->getValuesAdded());
560+
$this->assertCount(0, $jsonDiff->getValuesRemoved());
561+
562+
if (! $dataSet['changes']) {
563+
continue;
564+
}
565+
566+
/** @var ValueChange $valueChange */
567+
$valueChange = $jsonDiff->getValuesChanged()->first();
568+
$this->assertSame(
569+
$dataSet['original'],
570+
$valueChange->getOldValue()
571+
);
572+
$this->assertSame(
573+
$dataSet['new'],
574+
$valueChange->getNewValue()
575+
);
576+
$this->assertSame(
577+
'',
578+
$valueChange->getPath()
579+
);
580+
}
581+
582+
// Compare different data types
583+
foreach ($dataSets as $dataType => $dataSet) {
584+
foreach ($dataSets as $subDataType => $subDataSet) {
585+
if ($dataType === $subDataType) {
586+
continue;
587+
}
588+
589+
$jsonDiff = new JsonDiff($dataSet['original'], $subDataSet['original']);
590+
$this->assertCount(1, $jsonDiff->getValuesChanged());
591+
$this->assertSame(1, $jsonDiff->getNumberOfChanges());
592+
$this->assertCount(0, $jsonDiff->getKeysAdded());
593+
$this->assertCount(0, $jsonDiff->getKeysRemoved());
594+
$this->assertCount(0, $jsonDiff->getValuesAdded());
595+
$this->assertCount(0, $jsonDiff->getValuesRemoved());
596+
597+
/** @var ValueChange $valueChange */
598+
$valueChange = $jsonDiff->getValuesChanged()->first();
599+
$this->assertSame(
600+
$dataSet['original'],
601+
$valueChange->getOldValue()
602+
);
603+
$this->assertSame(
604+
$subDataSet['original'],
605+
$valueChange->getNewValue()
606+
);
607+
$this->assertSame(
608+
'',
609+
$valueChange->getPath()
610+
);
611+
}
612+
}
613+
}
526614
}

0 commit comments

Comments
 (0)