Skip to content

Commit 3b6b970

Browse files
committed
Type: PREVENT_MERGING prevents merging with defaults [Closes #14, Closes nette/application#257, nette/di#229]
1 parent 0bfe37c commit 3b6b970

3 files changed

Lines changed: 58 additions & 2 deletions

File tree

src/Schema/Elements/Structure.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,15 @@ public function otherItems($type = 'mixed'): self
7777

7878
public function normalize($value, Context $context)
7979
{
80+
if ($prevent = (is_array($value) && isset($value[Helpers::PREVENT_MERGING]))) {
81+
unset($value[Helpers::PREVENT_MERGING]);
82+
}
83+
8084
$value = $this->doNormalize($value, $context);
8185
if (is_object($value)) {
8286
$value = (array) $value;
8387
}
88+
8489
if (is_array($value)) {
8590
foreach ($value as $key => $val) {
8691
$itemSchema = $this->items[$key] ?? $this->otherItems;
@@ -90,6 +95,9 @@ public function normalize($value, Context $context)
9095
array_pop($context->path);
9196
}
9297
}
98+
if ($prevent) {
99+
$value[Helpers::PREVENT_MERGING] = true;
100+
}
93101
}
94102
return $value;
95103
}

src/Schema/Elements/Type.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ public function pattern(?string $pattern): self
103103

104104
public function normalize($value, Context $context)
105105
{
106+
if ($prevent = (is_array($value) && isset($value[Helpers::PREVENT_MERGING]))) {
107+
unset($value[Helpers::PREVENT_MERGING]);
108+
}
109+
106110
$value = $this->doNormalize($value, $context);
107111
if (is_array($value) && $this->items) {
108112
foreach ($value as $key => $val) {
@@ -111,6 +115,9 @@ public function normalize($value, Context $context)
111115
array_pop($context->path);
112116
}
113117
}
118+
if ($prevent && is_array($value)) {
119+
$value[Helpers::PREVENT_MERGING] = true;
120+
}
114121
return $value;
115122
}
116123

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

143150
public function complete($value, Context $context)
144151
{
152+
$merge = $this->merge;
153+
if (is_array($value) && isset($value[Helpers::PREVENT_MERGING])) {
154+
unset($value[Helpers::PREVENT_MERGING]);
155+
$merge = false;
156+
}
157+
145158
if ($value === null && is_array($this->default)) {
146159
$value = []; // is unable to distinguish null from array in NEON
147160
}
@@ -180,7 +193,7 @@ public function complete($value, Context $context)
180193
}
181194
}
182195

183-
if ($this->merge) {
196+
if ($merge) {
184197
$value = Helpers::merge($value, $this->default);
185198
}
186199
return $this->doFinalize($value, $context);

tests/Schema/Expect.array.phpt

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare(strict_types=1);
44

55
use Nette\Schema\Expect;
6+
use Nette\Schema\Helpers;
67
use Nette\Schema\Processor;
78
use Tester\Assert;
89

@@ -90,7 +91,41 @@ test('merging', function () {
9091
(new Processor)->process($schema, [
9192
'key1' => 'newval',
9293
'key3' => 'newval',
93-
'newval3', 'arr' => ['newitem'],
94+
'newval3',
95+
'arr' => ['newitem'],
96+
])
97+
);
98+
99+
Assert::same(
100+
[
101+
'key1' => 'newval',
102+
'key3' => 'newval',
103+
'newval3',
104+
'arr' => ['newitem'],
105+
],
106+
(new Processor)->process($schema, [
107+
Helpers::PREVENT_MERGING => true,
108+
'key1' => 'newval',
109+
'key3' => 'newval',
110+
'newval3',
111+
'arr' => ['newitem'],
112+
])
113+
);
114+
115+
Assert::same(
116+
[
117+
'key1' => 'newval',
118+
'key2' => 'val2',
119+
'val3',
120+
'arr' => ['newitem'],
121+
'key3' => 'newval',
122+
'newval3',
123+
],
124+
(new Processor)->process($schema, [
125+
'key1' => 'newval',
126+
'key3' => 'newval',
127+
'newval3',
128+
'arr' => [Helpers::PREVENT_MERGING => true, 'newitem'],
94129
])
95130
);
96131
});

0 commit comments

Comments
 (0)