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

nette / schema / 21791083743

08 Feb 2026 02:48AM UTC coverage: 97.877% (+0.009%) from 97.868%
21791083743

push

github

dg
used attribute Deprecated

461 of 471 relevant lines covered (97.88%)

0.98 hits per line

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

97.7
/src/Schema/Elements/Structure.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;
13
use Nette\Schema\Context;
14
use Nette\Schema\Helpers;
15
use Nette\Schema\Schema;
16
use function array_diff_key, array_fill_keys, array_key_exists, array_keys, array_map, array_merge, array_pop, array_values, is_array, is_object;
17

18

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

23
        /** @var Schema[] */
24
        private array $items;
25

26
        /** for array|list */
27
        private ?Schema $otherItems = null;
28

29
        /** @var array{?int, ?int} */
30
        private array $range = [null, null];
31
        private bool $skipDefaults = false;
32

33

34
        /** @param Schema[]  $shape */
35
        public function __construct(array $shape)
1✔
36
        {
37
                (function (Schema ...$items) {})(...array_values($shape));
1✔
38
                $this->items = $shape;
1✔
39
                $this->castTo('object');
1✔
40
                $this->required = true;
1✔
41
        }
1✔
42

43

44
        public function default(mixed $value): self
1✔
45
        {
46
                throw new Nette\InvalidStateException('Structure cannot have default value.');
1✔
47
        }
48

49

50
        public function min(?int $min): self
1✔
51
        {
52
                $this->range[0] = $min;
1✔
53
                return $this;
1✔
54
        }
55

56

57
        public function max(?int $max): self
1✔
58
        {
59
                $this->range[1] = $max;
1✔
60
                return $this;
1✔
61
        }
62

63

64
        public function otherItems(string|Schema $type = 'mixed'): self
1✔
65
        {
66
                $this->otherItems = $type instanceof Schema ? $type : new Type($type);
1✔
67
                return $this;
1✔
68
        }
69

70

71
        public function skipDefaults(bool $state = true): self
1✔
72
        {
73
                $this->skipDefaults = $state;
1✔
74
                return $this;
1✔
75
        }
76

77

78
        /** @param Schema[]|self  $shape */
79
        public function extend(array|self $shape): self
1✔
80
        {
81
                $shape = $shape instanceof self ? $shape->items : $shape;
1✔
82
                return new self(array_merge($this->items, $shape));
1✔
83
        }
84

85

86
        /** @return Schema[] */
87
        public function getShape(): array
88
        {
89
                return $this->items;
1✔
90
        }
91

92

93
        /********************* processing ****************d*g**/
94

95

96
        public function normalize(mixed $value, Context $context): mixed
1✔
97
        {
98
                $value = $this->doNormalize($value, $context);
1✔
99
                if (is_object($value)) {
1✔
100
                        $value = (array) $value;
1✔
101
                }
102

103
                if (is_array($value)) {
1✔
104
                        foreach ($value as $key => $val) {
1✔
105
                                $itemSchema = $this->items[$key] ?? $this->otherItems;
1✔
106
                                if ($itemSchema) {
1✔
107
                                        $context->path[] = $key;
1✔
108
                                        $value[$key] = $itemSchema->normalize($val, $context);
1✔
109
                                        array_pop($context->path);
1✔
110
                                }
111
                        }
112
                }
113

114
                return $value;
1✔
115
        }
116

117

118
        public function merge(mixed $value, mixed $base): mixed
1✔
119
        {
120
                if (is_array($value) && isset($value[Helpers::PreventMerging])) {
1✔
121
                        unset($value[Helpers::PreventMerging]);
×
122
                        $base = null;
×
123
                }
124

125
                if (is_array($value) && is_array($base)) {
1✔
126
                        $index = $this->otherItems === null ? null : 0;
1✔
127
                        foreach ($value as $key => $val) {
1✔
128
                                if ($key === $index) {
1✔
129
                                        $base[] = $val;
1✔
130
                                        $index++;
1✔
131
                                } else {
132
                                        $base[$key] = array_key_exists($key, $base) && ($itemSchema = $this->items[$key] ?? $this->otherItems)
1✔
133
                                                ? $itemSchema->merge($val, $base[$key])
1✔
134
                                                : $val;
1✔
135
                                }
136
                        }
137

138
                        return $base;
1✔
139
                }
140

141
                return $value ?? $base;
1✔
142
        }
143

144

145
        public function complete(mixed $value, Context $context): mixed
1✔
146
        {
147
                if ($value === null) {
1✔
148
                        $value = []; // is unable to distinguish null from array in NEON
1✔
149
                }
150

151
                $this->doDeprecation($context);
1✔
152

153
                $isOk = $context->createChecker();
1✔
154
                Helpers::validateType($value, 'array', $context);
1✔
155
                $isOk() && Helpers::validateRange($value, $this->range, $context);
1✔
156
                $isOk() && $this->validateItems($value, $context);
1✔
157
                $isOk() && $value = $this->doTransform($value, $context);
1✔
158
                return $isOk() ? $value : null;
1✔
159
        }
160

161

162
        /** @param  array<mixed>  $value */
163
        private function validateItems(array &$value, Context $context): void
1✔
164
        {
165
                $items = $this->items;
1✔
166
                if ($extraKeys = array_keys(array_diff_key($value, $items))) {
1✔
167
                        if ($this->otherItems) {
1✔
168
                                $items += array_fill_keys($extraKeys, $this->otherItems);
1✔
169
                        } else {
170
                                $keys = array_map(strval(...), array_keys($items));
1✔
171
                                foreach ($extraKeys as $key) {
1✔
172
                                        $hint = Nette\Utils\Helpers::getSuggestion($keys, (string) $key);
1✔
173
                                        $context->addError(
1✔
174
                                                'Unexpected item %path%' . ($hint ? ", did you mean '%hint%'?" : '.'),
1✔
175
                                                Nette\Schema\Message::UnexpectedItem,
1✔
176
                                                ['hint' => $hint],
1✔
177
                                        )->path[] = $key;
1✔
178
                                }
179
                        }
180
                }
181

182
                foreach ($items as $itemKey => $itemVal) {
1✔
183
                        $context->path[] = $itemKey;
1✔
184
                        if (array_key_exists($itemKey, $value)) {
1✔
185
                                $value[$itemKey] = $itemVal->complete($value[$itemKey], $context);
1✔
186
                        } else {
187
                                $default = $itemVal->completeDefault($context); // checks required item
1✔
188
                                if (!$context->skipDefaults && !$this->skipDefaults) {
1✔
189
                                        $value[$itemKey] = $default;
1✔
190
                                }
191
                        }
192

193
                        array_pop($context->path);
1✔
194
                }
195
        }
1✔
196

197

198
        public function completeDefault(Context $context): mixed
1✔
199
        {
200
                return $this->required
1✔
201
                        ? $this->complete([], $context)
1✔
202
                        : null;
1✔
203
        }
204
}
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