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 17, 2020
1 parent d4c4d95 commit 9ee8a61
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/Schema/Elements/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,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 +186,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 9ee8a61

Please sign in to comment.