• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nette / schema / 11189863618

05 Oct 2024 02:59AM UTC coverage: 97.384% (+0.7%) from 96.667%
11189863618

push

github

dg
Type::merge() merges arrays only according to the schema (BC break)

2 of 2 new or added lines in 1 file covered. (100.0%)

4 existing lines in 3 files now uncovered.

484 of 497 relevant lines covered (97.38%)

0.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

99.02
/src/Schema/Elements/Type.php
1
<?php
2

3
/**
4
 * This file is part of the Nette Framework (https://nette.org)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7

8
declare(strict_types=1);
9

10
namespace Nette\Schema\Elements;
11

12
use Nette\Schema\Context;
13
use Nette\Schema\DynamicParameter;
14
use Nette\Schema\Helpers;
15
use Nette\Schema\MergeMode;
16
use Nette\Schema\Schema;
17

18

19
final class Type implements Schema
20
{
21
        use Base;
22

23
        private string $type;
24
        private ?Schema $itemsValue = null;
25
        private ?Schema $itemsKey = null;
26

27
        /** @var array{?float, ?float} */
28
        private array $range = [null, null];
29
        private ?string $pattern = null;
30
        private bool $merge = false;
31
        private MergeMode $mergeMode = MergeMode::AppendKeys;
32

33

34
        public function __construct(string $type)
1✔
35
        {
36
                $defaults = ['list' => [], 'array' => []];
1✔
37
                $this->type = $type;
1✔
38
                $this->default = strpos($type, '[]') ? [] : $defaults[$type] ?? null;
1✔
39
        }
1✔
40

41

42
        public function nullable(): self
43
        {
44
                $this->type = 'null|' . $this->type;
1✔
45
                return $this;
1✔
46
        }
47

48

49
        /** @deprecated mergeDefaults is disabled by default */
50
        public function mergeDefaults(bool $state = true): self
1✔
51
        {
52
                if ($state === true) {
1✔
53
                        trigger_error(__METHOD__ . '() is deprecated and will be removed in the next major version.', E_USER_DEPRECATED);
1✔
54
                }
55
                $this->merge = $state;
1✔
56
                return $this;
1✔
57
        }
58

59

60
        public function mergeMode(MergeMode $mode): self
1✔
61
        {
62
                $this->mergeMode = $mode;
1✔
63
                return $this;
1✔
64
        }
65

66

67
        public function dynamic(): self
68
        {
69
                $this->type = DynamicParameter::class . '|' . $this->type;
1✔
70
                return $this;
1✔
71
        }
72

73

74
        public function min(?float $min): self
1✔
75
        {
76
                $this->range[0] = $min;
1✔
77
                return $this;
1✔
78
        }
79

80

81
        public function max(?float $max): self
1✔
82
        {
83
                $this->range[1] = $max;
1✔
84
                return $this;
1✔
85
        }
86

87

88
        /**
89
         * @internal  use arrayOf() or listOf()
90
         */
91
        public function items(string|Schema $valueType = 'mixed', string|Schema|null $keyType = null): self
1✔
92
        {
93
                $this->itemsValue = $valueType instanceof Schema
1✔
94
                        ? $valueType
1✔
95
                        : new self($valueType);
1✔
96
                $this->itemsKey = $keyType instanceof Schema || $keyType === null
1✔
97
                        ? $keyType
1✔
UNCOV
98
                        : new self($keyType);
×
99
                return $this;
1✔
100
        }
101

102

103
        public function pattern(?string $pattern): self
1✔
104
        {
105
                $this->pattern = $pattern;
1✔
106
                return $this;
1✔
107
        }
108

109

110
        /********************* processing ****************d*g**/
111

112

113
        public function normalize(mixed $value, Context $context): mixed
1✔
114
        {
115
                if ($prevent = (is_array($value) && isset($value[Helpers::PreventMerging]))) {
1✔
116
                        unset($value[Helpers::PreventMerging]);
1✔
117
                }
118

119
                $value = $this->doNormalize($value, $context);
1✔
120
                if (is_array($value) && $this->itemsValue) {
1✔
121
                        $res = [];
1✔
122
                        foreach ($value as $key => $val) {
1✔
123
                                $context->path[] = $key;
1✔
124
                                $context->isKey = true;
1✔
125
                                $key = $this->itemsKey
1✔
126
                                        ? $this->itemsKey->normalize($key, $context)
1✔
127
                                        : $key;
1✔
128
                                $context->isKey = false;
1✔
129
                                $res[$key] = $this->itemsValue->normalize($val, $context);
1✔
130
                                array_pop($context->path);
1✔
131
                        }
132

133
                        $value = $res;
1✔
134
                }
135

136
                if ($prevent && is_array($value)) {
1✔
137
                        $value[Helpers::PreventMerging] = true;
1✔
138
                }
139

140
                return $value;
1✔
141
        }
142

143

144
        public function merge(mixed $value, mixed $base): mixed
1✔
145
        {
146
                if ($this->mergeMode === MergeMode::Replace || (is_array($value) && isset($value[Helpers::PreventMerging]))) {
1✔
147
                        unset($value[Helpers::PreventMerging]);
1✔
148
                        return $value;
1✔
149
                }
150

151
                if (is_array($value) && is_array($base)) {
1✔
152
                        $index = $this->mergeMode === MergeMode::OverwriteKeys ? null : 0;
1✔
153
                        foreach ($value as $key => $val) {
1✔
154
                                if ($key === $index) {
1✔
155
                                        $base[] = $val;
1✔
156
                                        $index++;
1✔
157
                                } else {
158
                                        $base[$key] = array_key_exists($key, $base) && $this->itemsValue
1✔
159
                                                ? $this->itemsValue->merge($val, $base[$key])
1✔
160
                                                : $val;
1✔
161
                                }
162
                        }
163

164
                        return $base;
1✔
165
                }
166

167
                return $value === null && is_array($base) ? $base : $value;
1✔
168
        }
169

170

171
        public function complete(mixed $value, Context $context): mixed
1✔
172
        {
173
                $merge = $this->merge;
1✔
174
                if (is_array($value) && isset($value[Helpers::PreventMerging])) {
1✔
175
                        unset($value[Helpers::PreventMerging]);
1✔
176
                        $merge = false;
1✔
177
                }
178

179
                if ($value === null && is_array($this->default)) {
1✔
180
                        $value = []; // is unable to distinguish null from array in NEON
1✔
181
                }
182

183
                $this->doDeprecation($context);
1✔
184

185
                $isOk = $context->createChecker();
1✔
186
                Helpers::validateType($value, $this->type, $context);
1✔
187
                $isOk() && Helpers::validateRange($value, $this->range, $context, $this->type);
1✔
188
                $isOk() && $value !== null && $this->pattern !== null && Helpers::validatePattern($value, $this->pattern, $context);
1✔
189
                $isOk() && is_array($value) && $this->validateItems($value, $context);
1✔
190
                $isOk() && $merge && $value = Helpers::merge($value, $this->default);
1✔
191
                $isOk() && $value = $this->doTransform($value, $context);
1✔
192
                if (!$isOk()) {
1✔
193
                        return null;
1✔
194
                }
195

196
                if ($value instanceof DynamicParameter) {
1✔
197
                        $expected = $this->type . ($this->range === [null, null] ? '' : ':' . implode('..', $this->range));
1✔
198
                        $context->dynamics[] = [$value, str_replace(DynamicParameter::class . '|', '', $expected), $context->path];
1✔
199
                }
200
                return $value;
1✔
201
        }
202

203

204
        private function validateItems(array &$value, Context $context): void
1✔
205
        {
206
                if (!$this->itemsValue) {
1✔
207
                        return;
1✔
208
                }
209

210
                $res = [];
1✔
211
                foreach ($value as $key => $val) {
1✔
212
                        $context->path[] = $key;
1✔
213
                        $context->isKey = true;
1✔
214
                        $key = $this->itemsKey ? $this->itemsKey->complete($key, $context) : $key;
1✔
215
                        $context->isKey = false;
1✔
216
                        $res[$key] = $this->itemsValue->complete($val, $context);
1✔
217
                        array_pop($context->path);
1✔
218
                }
219
                $value = $res;
1✔
220
        }
1✔
221
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc