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

nette / schema / 22292226210

23 Feb 2026 03:49AM UTC coverage: 97.899%. Remained the same
22292226210

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

47

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

58

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

65

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

72

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

79

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

86

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

101

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

108

109
        /********************* processing ****************d*g**/
110

111

112
        public function normalize(mixed $value, Context $context): mixed
1✔
113
        {
114
                $value = $this->doNormalize($value, $context);
1✔
115
                if (is_array($value) && $this->itemsValue) {
1✔
116
                        $res = [];
1✔
117
                        foreach ($value as $key => $val) {
1✔
118
                                $context->path[] = $key;
1✔
119
                                $context->isKey = true;
1✔
120
                                $key = $this->itemsKey
1✔
121
                                        ? $this->itemsKey->normalize($key, $context)
1✔
122
                                        : $key;
1✔
123
                                $context->isKey = false;
1✔
124
                                $res[$key] = $this->itemsValue->normalize($val, $context);
1✔
125
                                array_pop($context->path);
1✔
126
                        }
127

128
                        $value = $res;
1✔
129
                }
130

131
                return $value;
1✔
132
        }
133

134

135
        public function merge(mixed $value, mixed $base): mixed
1✔
136
        {
137
                if ($this->mergeMode === MergeMode::Replace) {
1✔
138
                        return $value;
1✔
139
                }
140

141
                if (is_array($value) && is_array($base)) {
1✔
142
                        $index = $this->mergeMode === MergeMode::OverwriteKeys ? null : 0;
1✔
143
                        foreach ($value as $key => $val) {
1✔
144
                                if ($key === $index) {
1✔
145
                                        $base[] = $val;
1✔
146
                                        $index++;
1✔
147
                                } else {
148
                                        $base[$key] = array_key_exists($key, $base) && $this->itemsValue
1✔
149
                                                ? $this->itemsValue->merge($val, $base[$key])
1✔
150
                                                : $val;
1✔
151
                                }
152
                        }
153

154
                        return $base;
1✔
155
                }
156

157
                return $value === null && is_array($base) ? $base : $value;
1✔
158
        }
159

160

161
        public function complete(mixed $value, Context $context): mixed
1✔
162
        {
163
                if ($value === null && is_array($this->default)) {
1✔
164
                        $value = []; // is unable to distinguish null from array in NEON
1✔
165
                }
166

167
                $this->doDeprecation($context);
1✔
168

169
                $isOk = $context->createChecker();
1✔
170
                Helpers::validateType($value, $this->type, $context);
1✔
171
                $isOk() && Helpers::validateRange($value, $this->range, $context, $this->type);
1✔
172
                $isOk() && $value !== null && $this->pattern !== null && Helpers::validatePattern($value, $this->pattern, $context);
1✔
173
                $isOk() && is_array($value) && $this->validateItems($value, $context);
1✔
174
                $isOk() && $this->merge && $value = Helpers::merge($value, $this->default);
1✔
175
                $isOk() && $value = $this->doTransform($value, $context);
1✔
176
                if (!$isOk()) {
1✔
177
                        return null;
1✔
178
                }
179

180
                if ($value instanceof DynamicParameter) {
1✔
181
                        $expected = $this->type . ($this->range === [null, null] ? '' : ':' . implode('..', $this->range));
1✔
182
                        $context->dynamics[] = [$value, str_replace(DynamicParameter::class . '|', '', $expected), $context->path];
1✔
183
                }
184
                return $value;
1✔
185
        }
186

187

188
        /** @param  array<mixed>  $value */
189
        private function validateItems(array &$value, Context $context): void
1✔
190
        {
191
                if (!$this->itemsValue) {
1✔
192
                        return;
1✔
193
                }
194

195
                $res = [];
1✔
196
                foreach ($value as $key => $val) {
1✔
197
                        $context->path[] = $key;
1✔
198
                        $context->isKey = true;
1✔
199
                        $key = $this->itemsKey ? $this->itemsKey->complete($key, $context) : $key;
1✔
200
                        $context->isKey = false;
1✔
201
                        $res[$key ?? ''] = $this->itemsValue->complete($val, $context);
1✔
202
                        array_pop($context->path);
1✔
203
                }
204
                $value = $res;
1✔
205
        }
1✔
206
}
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