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

nette / schema / 11205893850

06 Oct 2024 11:00PM UTC coverage: 96.559% (-0.9%) from 97.421%
11205893850

push

github

dg
Expect::array(shape) returns Structure

4 of 4 new or added lines in 1 file covered. (100.0%)

7 existing lines in 2 files now uncovered.

477 of 494 relevant lines covered (96.56%)

0.97 hits per line

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

95.6
/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

17

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

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

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

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

32

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

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

71

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

78

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
        public function getShape(): array
87
        {
88
                return $this->items;
1✔
89
        }
90

91

92
        /********************* processing ****************d*g**/
93

94

95
        public function normalize(mixed $value, Context $context): mixed
1✔
96
        {
97
                if ($prevent = (is_array($value) && isset($value[Helpers::PreventMerging]))) {
1✔
UNCOV
98
                        unset($value[Helpers::PreventMerging]);
×
99
                }
100

101
                $value = $this->doNormalize($value, $context);
1✔
102
                if (is_object($value)) {
1✔
103
                        $value = (array) $value;
1✔
104
                }
105

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

116
                        if ($prevent) {
1✔
UNCOV
117
                                $value[Helpers::PreventMerging] = true;
×
118
                        }
119
                }
120

121
                return $value;
1✔
122
        }
123

124

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

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

145
                        return $base;
1✔
146
                }
147

148
                return $value ?? $base;
1✔
149
        }
150

151

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

158
                $this->doDeprecation($context);
1✔
159

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

168

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

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

199
                        array_pop($context->path);
1✔
200
                }
201
        }
1✔
202

203

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