• 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

97.73
/src/Schema/Elements/Structure.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;
11
use Nette\Schema\Context;
12
use Nette\Schema\Helpers;
13
use Nette\Schema\Schema;
14
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, strval;
15

16

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

21
        /** @var Schema[] */
22
        private array $items;
23

24
        /** for array|list */
25
        private ?Schema $otherItems = null;
26

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

31

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

41

42
        /**
43
         * Not supported for structures; always throws.
44
         */
45
        public function default(mixed $value): self
1✔
46
        {
47
                throw new Nette\InvalidStateException('Structure cannot have default value.');
1✔
48
        }
49

50

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

57

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

64

65
        /**
66
         * Allows extra keys not defined in the shape, validating their values against the given type.
67
         */
68
        public function otherItems(string|Schema $type = 'mixed'): self
1✔
69
        {
70
                $this->otherItems = $type instanceof Schema ? $type : new Type($type);
1✔
71
                return $this;
1✔
72
        }
73

74

75
        /**
76
         * When enabled, properties whose value equals the default are omitted from the output.
77
         */
78
        public function skipDefaults(bool $state = true): self
1✔
79
        {
80
                $this->skipDefaults = $state;
1✔
81
                return $this;
1✔
82
        }
83

84

85
        /**
86
         * Creates a new structure by merging this shape with additional properties.
87
         * @param  Schema[]|self  $shape
88
         */
89
        public function extend(array|self $shape): self
1✔
90
        {
91
                $shape = $shape instanceof self ? $shape->items : $shape;
1✔
92
                return new self(array_merge($this->items, $shape));
1✔
93
        }
94

95

96
        /** @return Schema[] */
97
        public function getShape(): array
98
        {
99
                return $this->items;
1✔
100
        }
101

102

103
        /********************* processing ****************d*g**/
104

105

106
        public function normalize(mixed $value, Context $context): mixed
1✔
107
        {
108
                $value = $this->doNormalize($value, $context);
1✔
109
                if (is_object($value)) {
1✔
110
                        $value = (array) $value;
1✔
111
                }
112

113
                if (is_array($value)) {
1✔
114
                        foreach ($value as $key => $val) {
1✔
115
                                $itemSchema = $this->items[$key] ?? $this->otherItems;
1✔
116
                                if ($itemSchema) {
1✔
117
                                        $context->path[] = $key;
1✔
118
                                        $value[$key] = $itemSchema->normalize($val, $context);
1✔
119
                                        array_pop($context->path);
1✔
120
                                }
121
                        }
122
                }
123

124
                return $value;
1✔
125
        }
126

127

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

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

148
                        return $base;
1✔
149
                }
150

151
                return $value ?? $base;
1✔
152
        }
153

154

155
        public function complete(mixed $value, Context $context): mixed
1✔
156
        {
157
                if ($value === null) {
1✔
158
                        $value = []; // is unable to distinguish null from array in NEON
1✔
159
                }
160

161
                $this->doDeprecation($context);
1✔
162

163
                $isOk = $context->createChecker();
1✔
164
                Helpers::validateType($value, 'array', $context);
1✔
165
                $isOk() && Helpers::validateRange($value, $this->range, $context);
1✔
166
                $isOk() && $this->validateItems($value, $context);
1✔
167
                $isOk() && $value = $this->doTransform($value, $context);
1✔
168
                return $isOk() ? $value : null;
1✔
169
        }
170

171

172
        /** @param  array<mixed>  $value */
173
        private function validateItems(array &$value, Context $context): void
1✔
174
        {
175
                $items = $this->items;
1✔
176
                if ($extraKeys = array_keys(array_diff_key($value, $items))) {
1✔
177
                        if ($this->otherItems) {
1✔
178
                                $items += array_fill_keys($extraKeys, $this->otherItems);
1✔
179
                        } else {
180
                                $keys = array_map(strval(...), array_keys($items));
1✔
181
                                foreach ($extraKeys as $key) {
1✔
182
                                        $hint = Nette\Utils\Helpers::getSuggestion($keys, (string) $key);
1✔
183
                                        $context->addError(
1✔
184
                                                'Unexpected item %path%' . ($hint ? ", did you mean '%hint%'?" : '.'),
1✔
185
                                                Nette\Schema\Message::UnexpectedItem,
1✔
186
                                                ['hint' => $hint],
1✔
187
                                        )->path[] = $key;
1✔
188
                                }
189
                        }
190
                }
191

192
                foreach ($items as $itemKey => $itemVal) {
1✔
193
                        $context->path[] = $itemKey;
1✔
194
                        if (array_key_exists($itemKey, $value)) {
1✔
195
                                $value[$itemKey] = $itemVal->complete($value[$itemKey], $context);
1✔
196
                        } else {
197
                                $default = $itemVal->completeDefault($context); // checks required item
1✔
198
                                if (!$context->skipDefaults && !$this->skipDefaults) {
1✔
199
                                        $value[$itemKey] = $default;
1✔
200
                                }
201
                        }
202

203
                        array_pop($context->path);
1✔
204
                }
205
        }
1✔
206

207

208
        public function completeDefault(Context $context): mixed
1✔
209
        {
210
                return $this->required
1✔
211
                        ? $this->complete([], $context)
1✔
212
                        : null;
1✔
213
        }
214
}
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