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

nette / schema / 22292073757

23 Feb 2026 03:39AM UTC coverage: 96.579%. Remained the same
22292073757

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

45

46
        public function mergeDefaults(bool $state = true): self
1✔
47
        {
48
                $this->merge = $state;
1✔
49
                return $this;
1✔
50
        }
51

52

53
        public function dynamic(): self
54
        {
55
                $this->type = DynamicParameter::class . '|' . $this->type;
1✔
56
                return $this;
1✔
57
        }
58

59

60
        public function min(?float $min): self
1✔
61
        {
62
                $this->range[0] = $min;
1✔
63
                return $this;
1✔
64
        }
65

66

67
        public function max(?float $max): self
1✔
68
        {
69
                $this->range[1] = $max;
1✔
70
                return $this;
1✔
71
        }
72

73

74
        /**
75
         * @internal  use arrayOf() or listOf()
76
         */
77
        public function items(string|Schema $valueType = 'mixed', string|Schema|null $keyType = null): self
1✔
78
        {
79
                $this->itemsValue = $valueType instanceof Schema
1✔
80
                        ? $valueType
1✔
81
                        : new self($valueType);
1✔
82
                $this->itemsKey = $keyType instanceof Schema || $keyType === null
1✔
83
                        ? $keyType
1✔
84
                        : new self($keyType);
×
85
                return $this;
1✔
86
        }
87

88

89
        public function pattern(?string $pattern): self
1✔
90
        {
91
                $this->pattern = $pattern;
1✔
92
                return $this;
1✔
93
        }
94

95

96
        /********************* processing ****************d*g**/
97

98

99
        public function normalize(mixed $value, Context $context): mixed
1✔
100
        {
101
                if ($prevent = (is_array($value) && isset($value[Helpers::PreventMerging]))) {
1✔
102
                        unset($value[Helpers::PreventMerging]);
1✔
103
                }
104

105
                $value = $this->doNormalize($value, $context);
1✔
106
                if (is_array($value) && $this->itemsValue) {
1✔
107
                        $res = [];
1✔
108
                        foreach ($value as $key => $val) {
1✔
109
                                $context->path[] = $key;
1✔
110
                                $context->isKey = true;
1✔
111
                                $key = $this->itemsKey
1✔
112
                                        ? $this->itemsKey->normalize($key, $context)
1✔
113
                                        : $key;
1✔
114
                                $context->isKey = false;
1✔
115
                                $res[$key] = $this->itemsValue->normalize($val, $context);
1✔
116
                                array_pop($context->path);
1✔
117
                        }
118

119
                        $value = $res;
1✔
120
                }
121

122
                if ($prevent && is_array($value)) {
1✔
123
                        $value[Helpers::PreventMerging] = true;
1✔
124
                }
125

126
                return $value;
1✔
127
        }
128

129

130
        public function merge(mixed $value, mixed $base): mixed
1✔
131
        {
132
                if (is_array($value) && isset($value[Helpers::PreventMerging])) {
1✔
133
                        unset($value[Helpers::PreventMerging]);
×
134
                        return $value;
×
135
                }
136

137
                if (is_array($value) && is_array($base) && $this->itemsValue) {
1✔
138
                        $index = 0;
1✔
139
                        foreach ($value as $key => $val) {
1✔
140
                                if ($key === $index) {
1✔
141
                                        $base[] = $val;
1✔
142
                                        $index++;
1✔
143
                                } else {
144
                                        $base[$key] = array_key_exists($key, $base)
1✔
145
                                                ? $this->itemsValue->merge($val, $base[$key])
1✔
146
                                                : $val;
1✔
147
                                }
148
                        }
149

150
                        return $base;
1✔
151
                }
152

153
                return Helpers::merge($value, $base);
1✔
154
        }
155

156

157
        public function complete(mixed $value, Context $context): mixed
1✔
158
        {
159
                $merge = $this->merge;
1✔
160
                if (is_array($value) && isset($value[Helpers::PreventMerging])) {
1✔
161
                        unset($value[Helpers::PreventMerging]);
1✔
162
                        $merge = false;
1✔
163
                }
164

165
                if ($value === null && is_array($this->default)) {
1✔
166
                        $value = []; // is unable to distinguish null from array in NEON
1✔
167
                }
168

169
                $this->doDeprecation($context);
1✔
170

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

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

189

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

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