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

nette / schema / 22834792529

09 Mar 2026 01:36AM UTC coverage: 96.579%. Remained the same
22834792529

push

github

dg
added CLAUDE.md

480 of 497 relevant lines covered (96.58%)

0.97 hits per line

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

96.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\Schema;
14
use function array_key_exists, array_pop, implode, is_array, str_replace, strpos;
15

16

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

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

25
        /** @var array{?float, ?float} */
26
        private array $range = [null, null];
27
        private ?string $pattern = null;
28
        private bool $merge = true;
29

30

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

38

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

48

49
        /**
50
         * Controls whether the default value is merged with the input array (enabled by default).
51
         */
52
        public function mergeDefaults(bool $state = true): self
1✔
53
        {
54
                $this->merge = $state;
1✔
55
                return $this;
1✔
56
        }
57

58

59
        /**
60
         * Allows the value to be a DynamicParameter, which is recorded for deferred validation.
61
         */
62
        public function dynamic(): self
63
        {
64
                $this->type = DynamicParameter::class . '|' . $this->type;
1✔
65
                return $this;
1✔
66
        }
67

68

69
        public function min(?float $min): self
1✔
70
        {
71
                $this->range[0] = $min;
1✔
72
                return $this;
1✔
73
        }
74

75

76
        public function max(?float $max): self
1✔
77
        {
78
                $this->range[1] = $max;
1✔
79
                return $this;
1✔
80
        }
81

82

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

97

98
        /**
99
         * Sets a regex pattern the string value must match entirely (anchored to start and end).
100
         */
101
        public function pattern(?string $pattern): self
1✔
102
        {
103
                $this->pattern = $pattern;
1✔
104
                return $this;
1✔
105
        }
106

107

108
        /********************* processing ****************d*g**/
109

110

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

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

131
                        $value = $res;
1✔
132
                }
133

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

138
                return $value;
1✔
139
        }
140

141

142
        public function merge(mixed $value, mixed $base): mixed
1✔
143
        {
144
                if (is_array($value) && isset($value[Helpers::PreventMerging])) {
1✔
145
                        unset($value[Helpers::PreventMerging]);
×
146
                        return $value;
×
147
                }
148

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

162
                        return $base;
1✔
163
                }
164

165
                return Helpers::merge($value, $base);
1✔
166
        }
167

168

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

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

181
                $this->doDeprecation($context);
1✔
182

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

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

201

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

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