• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In
Build has been canceled!

nette / schema / 20847969596

09 Jan 2026 09:51AM UTC coverage: 97.868%. Remained the same
20847969596

push

github

dg
used attribute Deprecated

459 of 469 relevant lines covered (97.87%)

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

45

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

51

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

58

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

65

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

72

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

79

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

87

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

94

95
        /********************* processing ****************d*g**/
96

97

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

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

116
                return $value;
1✔
117
        }
118

119

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

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

140
                        return $base;
1✔
141
                }
142

143
                return $value ?? $base;
1✔
144
        }
145

146

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

153
                $this->doDeprecation($context);
1✔
154

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

163

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

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

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

198

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