diff --git a/src/Schema/Elements/Type.php b/src/Schema/Elements/Type.php index adf0815..ad96f20 100644 --- a/src/Schema/Elements/Type.php +++ b/src/Schema/Elements/Type.php @@ -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 } @@ -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); diff --git a/tests/Schema/Expect.array.phpt b/tests/Schema/Expect.array.phpt index 9008a39..4d70c45 100644 --- a/tests/Schema/Expect.array.phpt +++ b/tests/Schema/Expect.array.phpt @@ -3,6 +3,7 @@ declare(strict_types=1); use Nette\Schema\Expect; +use Nette\Schema\Helpers; use Nette\Schema\Processor; use Tester\Assert; @@ -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'], ]) ); });