From 547ed6a2d5f2c3719ea355f41d431c73d813adab Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 17 Dec 2020 22:45:14 +0100 Subject: [PATCH] Type: PREVENT_MERGING prevents merging with defaults [Closes #14, Closes nette/application#257, nette/di#229] --- src/Schema/Elements/Structure.php | 3 +++ src/Schema/Elements/Type.php | 11 ++++++++- tests/Schema/Expect.array.phpt | 37 ++++++++++++++++++++++++++++++- 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/Schema/Elements/Structure.php b/src/Schema/Elements/Structure.php index f40cf01..10a7a3f 100644 --- a/src/Schema/Elements/Structure.php +++ b/src/Schema/Elements/Structure.php @@ -83,6 +83,9 @@ public function normalize($value, Context $context) } if (is_array($value)) { foreach ($value as $key => $val) { + if ($key === Helpers::PREVENT_MERGING) { + continue; + } $itemSchema = $this->items[$key] ?? $this->otherItems; if ($itemSchema) { $context->path[] = $key; diff --git a/src/Schema/Elements/Type.php b/src/Schema/Elements/Type.php index 0a6ffa9..b7b4df5 100644 --- a/src/Schema/Elements/Type.php +++ b/src/Schema/Elements/Type.php @@ -106,6 +106,9 @@ public function normalize($value, Context $context) $value = $this->doNormalize($value, $context); if (is_array($value) && $this->items) { foreach ($value as $key => $val) { + if ($key === Helpers::PREVENT_MERGING) { + continue; + } $context->path[] = $key; $value[$key] = $this->items->normalize($val, $context); array_pop($context->path); @@ -142,6 +145,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 +189,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 cf3ab45..b876abf 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'], ]) ); });