Skip to content

Commit

Permalink
Type: PREVENT_MERGING prevents merging with defaults [Closes #14, Closes
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Dec 20, 2020
1 parent 0bfe37c commit 3b6b970
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/Schema/Elements/Structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,15 @@ public function otherItems($type = 'mixed'): self

public function normalize($value, Context $context)
{
if ($prevent = (is_array($value) && isset($value[Helpers::PREVENT_MERGING]))) {
unset($value[Helpers::PREVENT_MERGING]);
}

$value = $this->doNormalize($value, $context);
if (is_object($value)) {
$value = (array) $value;
}

if (is_array($value)) {
foreach ($value as $key => $val) {
$itemSchema = $this->items[$key] ?? $this->otherItems;
Expand All @@ -90,6 +95,9 @@ public function normalize($value, Context $context)
array_pop($context->path);
}
}
if ($prevent) {
$value[Helpers::PREVENT_MERGING] = true;
}
}
return $value;
}
Expand Down
15 changes: 14 additions & 1 deletion src/Schema/Elements/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ public function pattern(?string $pattern): self

public function normalize($value, Context $context)
{
if ($prevent = (is_array($value) && isset($value[Helpers::PREVENT_MERGING]))) {
unset($value[Helpers::PREVENT_MERGING]);
}

$value = $this->doNormalize($value, $context);
if (is_array($value) && $this->items) {
foreach ($value as $key => $val) {
Expand All @@ -111,6 +115,9 @@ public function normalize($value, Context $context)
array_pop($context->path);
}
}
if ($prevent && is_array($value)) {
$value[Helpers::PREVENT_MERGING] = true;
}
return $value;
}

Expand Down Expand Up @@ -142,6 +149,12 @@ public function merge($value, $base)

public function complete($value, Context $context)
{
$merge = $this->merge;
if (is_array($value) && isset($value[Helpers::PREVENT_MERGING])) {
unset($value[Helpers::PREVENT_MERGING]);
$merge = false;
}

if ($value === null && is_array($this->default)) {
$value = []; // is unable to distinguish null from array in NEON
}
Expand Down Expand Up @@ -180,7 +193,7 @@ public function complete($value, Context $context)
}
}

if ($this->merge) {
if ($merge) {
$value = Helpers::merge($value, $this->default);
}
return $this->doFinalize($value, $context);
Expand Down
37 changes: 36 additions & 1 deletion tests/Schema/Expect.array.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Nette\Schema\Expect;
use Nette\Schema\Helpers;
use Nette\Schema\Processor;
use Tester\Assert;

Expand Down Expand Up @@ -90,7 +91,41 @@ test('merging', function () {
(new Processor)->process($schema, [
'key1' => 'newval',
'key3' => 'newval',
'newval3', 'arr' => ['newitem'],
'newval3',
'arr' => ['newitem'],
])
);

Assert::same(
[
'key1' => 'newval',
'key3' => 'newval',
'newval3',
'arr' => ['newitem'],
],
(new Processor)->process($schema, [
Helpers::PREVENT_MERGING => true,
'key1' => 'newval',
'key3' => 'newval',
'newval3',
'arr' => ['newitem'],
])
);

Assert::same(
[
'key1' => 'newval',
'key2' => 'val2',
'val3',
'arr' => ['newitem'],
'key3' => 'newval',
'newval3',
],
(new Processor)->process($schema, [
'key1' => 'newval',
'key3' => 'newval',
'newval3',
'arr' => [Helpers::PREVENT_MERGING => true, 'newitem'],
])
);
});
Expand Down

0 comments on commit 3b6b970

Please sign in to comment.