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

nette / schema / 22834966437

09 Mar 2026 01:44AM UTC coverage: 97.899%. Remained the same
22834966437

push

github

dg
used attribute Deprecated

466 of 476 relevant lines covered (97.9%)

0.98 hits per line

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

98.94
/src/Schema/Elements/Type.php
1
<?php declare(strict_types=1);
1✔
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
namespace Nette\Schema\Elements;
9

10
use Nette\Schema\Context;
11
use Nette\Schema\DynamicParameter;
12
use Nette\Schema\Helpers;
13
use Nette\Schema\MergeMode;
14
use Nette\Schema\Schema;
15
use function array_key_exists, array_pop, implode, is_array, str_replace, strpos;
16

17

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

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

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

32

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

40

41
        /**
42
         * Allows the value to be null in addition to the declared type.
43
         */
44
        public function nullable(): self
45
        {
46
                $this->type = 'null|' . $this->type;
1✔
47
                return $this;
1✔
48
        }
49

50

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

61

62
        /**
63
         * Sets how arrays are combined when merging multiple configurations.
64
         */
65
        public function mergeMode(MergeMode $mode): self
1✔
66
        {
67
                $this->mergeMode = $mode;
1✔
68
                return $this;
1✔
69
        }
70

71

72
        /**
73
         * Allows the value to be a DynamicParameter, which is recorded for deferred validation.
74
         */
75
        public function dynamic(): self
76
        {
77
                $this->type = DynamicParameter::class . '|' . $this->type;
1✔
78
                return $this;
1✔
79
        }
80

81

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

88

89
        public function max(?float $max): self
1✔
90
        {
91
                $this->range[1] = $max;
1✔
92
                return $this;
1✔
93
        }
94

95

96
        /**
97
         * @internal  use arrayOf() or listOf()
98
         */
99
        public function items(string|Schema $valueType = 'mixed', string|Schema|null $keyType = null): self
1✔
100
        {
101
                $this->itemsValue = $valueType instanceof Schema
1✔
102
                        ? $valueType
1✔
103
                        : new self($valueType);
1✔
104
                $this->itemsKey = $keyType instanceof Schema || $keyType === null
1✔
105
                        ? $keyType
1✔
106
                        : new self($keyType);
×
107
                return $this;
1✔
108
        }
109

110

111
        /**
112
         * Sets a regex pattern the string value must match entirely (anchored to start and end).
113
         */
114
        public function pattern(?string $pattern): self
1✔
115
        {
116
                $this->pattern = $pattern;
1✔
117
                return $this;
1✔
118
        }
119

120

121
        /********************* processing ****************d*g**/
122

123

124
        public function normalize(mixed $value, Context $context): mixed
1✔
125
        {
126
                $value = $this->doNormalize($value, $context);
1✔
127
                if (is_array($value) && $this->itemsValue) {
1✔
128
                        $res = [];
1✔
129
                        foreach ($value as $key => $val) {
1✔
130
                                $context->path[] = $key;
1✔
131
                                $context->isKey = true;
1✔
132
                                $key = $this->itemsKey
1✔
133
                                        ? $this->itemsKey->normalize($key, $context)
1✔
134
                                        : $key;
1✔
135
                                $context->isKey = false;
1✔
136
                                $res[$key] = $this->itemsValue->normalize($val, $context);
1✔
137
                                array_pop($context->path);
1✔
138
                        }
139

140
                        $value = $res;
1✔
141
                }
142

143
                return $value;
1✔
144
        }
145

146

147
        public function merge(mixed $value, mixed $base): mixed
1✔
148
        {
149
                if ($this->mergeMode === MergeMode::Replace) {
1✔
150
                        return $value;
1✔
151
                }
152

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

166
                        return $base;
1✔
167
                }
168

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

172

173
        public function complete(mixed $value, Context $context): mixed
1✔
174
        {
175
                if ($value === null && is_array($this->default)) {
1✔
176
                        $value = []; // is unable to distinguish null from array in NEON
1✔
177
                }
178

179
                $this->doDeprecation($context);
1✔
180

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

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

199

200
        /** @param  array<mixed>  $value */
201
        private function validateItems(array &$value, Context $context): void
1✔
202
        {
203
                if (!$this->itemsValue) {
1✔
204
                        return;
1✔
205
                }
206

207
                $res = [];
1✔
208
                foreach ($value as $key => $val) {
1✔
209
                        $context->path[] = $key;
1✔
210
                        $context->isKey = true;
1✔
211
                        $key = $this->itemsKey ? $this->itemsKey->complete($key, $context) : $key;
1✔
212
                        $context->isKey = false;
1✔
213
                        $res[$key ?? ''] = $this->itemsValue->complete($val, $context);
1✔
214
                        array_pop($context->path);
1✔
215
                }
216
                $value = $res;
1✔
217
        }
1✔
218
}
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