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

nette / schema / 11189580668

05 Oct 2024 02:14AM UTC coverage: 95.749% (-0.8%) from 96.552%
11189580668

push

github

dg
Structure::merge() refactoring

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

11 existing lines in 2 files now uncovered.

473 of 494 relevant lines covered (95.75%)

0.96 hits per line

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

94.95
/src/Schema/Elements/Type.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\Schema\Context;
13
use Nette\Schema\DynamicParameter;
14
use Nette\Schema\Helpers;
15
use Nette\Schema\MergeMode;
16
use Nette\Schema\Schema;
17

18

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

23
        private string $type;
24
        private ?Schema $itemsValue = null;
25
        private ?Schema $itemsKey = null;
26

27
        /** @var array{?float, ?float} */
28
        private array $range = [null, null];
29
        private ?string $pattern = null;
30
        private bool $merge = true;
31
        private MergeMode $mergeMode = MergeMode::AppendKeys;
32

33

34
        public function __construct(string $type)
1✔
35
        {
36
                $defaults = ['list' => [], 'array' => []];
1✔
37
                $this->type = $type;
1✔
38
                $this->default = strpos($type, '[]') ? [] : $defaults[$type] ?? null;
1✔
39
        }
1✔
40

41

42
        public function nullable(): self
43
        {
44
                $this->type = 'null|' . $this->type;
1✔
45
                return $this;
1✔
46
        }
47

48

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

55

56
        public function mergeMode(MergeMode $mode): self
57
        {
UNCOV
58
                $this->mergeMode = $mode;
×
UNCOV
59
                return $this;
×
60
        }
61

62

63
        public function dynamic(): self
64
        {
65
                $this->type = DynamicParameter::class . '|' . $this->type;
1✔
66
                return $this;
1✔
67
        }
68

69

70
        public function min(?float $min): self
1✔
71
        {
72
                $this->range[0] = $min;
1✔
73
                return $this;
1✔
74
        }
75

76

77
        public function max(?float $max): self
1✔
78
        {
79
                $this->range[1] = $max;
1✔
80
                return $this;
1✔
81
        }
82

83

84
        /**
85
         * @internal  use arrayOf() or listOf()
86
         */
87
        public function items(string|Schema $valueType = 'mixed', string|Schema|null $keyType = null): self
1✔
88
        {
89
                $this->itemsValue = $valueType instanceof Schema
1✔
90
                        ? $valueType
1✔
91
                        : new self($valueType);
1✔
92
                $this->itemsKey = $keyType instanceof Schema || $keyType === null
1✔
93
                        ? $keyType
1✔
UNCOV
94
                        : new self($keyType);
×
95
                return $this;
1✔
96
        }
97

98

99
        public function pattern(?string $pattern): self
1✔
100
        {
101
                $this->pattern = $pattern;
1✔
102
                return $this;
1✔
103
        }
104

105

106
        /********************* processing ****************d*g**/
107

108

109
        public function normalize(mixed $value, Context $context): mixed
1✔
110
        {
111
                if ($prevent = (is_array($value) && isset($value[Helpers::PreventMerging]))) {
1✔
112
                        unset($value[Helpers::PreventMerging]);
1✔
113
                }
114

115
                $value = $this->doNormalize($value, $context);
1✔
116
                if (is_array($value) && $this->itemsValue) {
1✔
117
                        $res = [];
1✔
118
                        foreach ($value as $key => $val) {
1✔
119
                                $context->path[] = $key;
1✔
120
                                $context->isKey = true;
1✔
121
                                $key = $this->itemsKey
1✔
122
                                        ? $this->itemsKey->normalize($key, $context)
1✔
123
                                        : $key;
1✔
124
                                $context->isKey = false;
1✔
125
                                $res[$key] = $this->itemsValue->normalize($val, $context);
1✔
126
                                array_pop($context->path);
1✔
127
                        }
128

129
                        $value = $res;
1✔
130
                }
131

132
                if ($prevent && is_array($value)) {
1✔
133
                        $value[Helpers::PreventMerging] = true;
1✔
134
                }
135

136
                return $value;
1✔
137
        }
138

139

140
        public function merge(mixed $value, mixed $base): mixed
1✔
141
        {
142
                if ($this->mergeMode === MergeMode::Replace || (is_array($value) && isset($value[Helpers::PreventMerging]))) {
1✔
UNCOV
143
                        unset($value[Helpers::PreventMerging]);
×
UNCOV
144
                        return $value;
×
145
                }
146

147
                if (is_array($value) && is_array($base) && $this->itemsValue) {
1✔
148
                        $index = $this->mergeMode === MergeMode::AppendKeys ? 0 : null;
1✔
149
                        foreach ($value as $key => $val) {
1✔
150
                                if ($key === $index) {
1✔
151
                                        $base[] = $val;
1✔
152
                                        $index++;
1✔
153
                                } else {
154
                                        $base[$key] = array_key_exists($key, $base)
1✔
155
                                                ? $this->itemsValue->merge($val, $base[$key])
1✔
156
                                                : $val;
1✔
157
                                }
158
                        }
159

160
                        return $base;
1✔
161
                }
162

163
                return Helpers::merge($value, $base);
1✔
164
        }
165

166

167
        public function complete(mixed $value, Context $context): mixed
1✔
168
        {
169
                $merge = $this->merge;
1✔
170
                if (is_array($value) && isset($value[Helpers::PreventMerging])) {
1✔
171
                        unset($value[Helpers::PreventMerging]);
1✔
172
                        $merge = false;
1✔
173
                }
174

175
                if ($value === null && is_array($this->default)) {
1✔
176
                        $value = []; // is unable to distinguish null from array in NEON
1✔
177
                }
178

179
                $this->doDeprecation($context);
1✔
180

181
                $isOk = $context->createChecker();
1✔
182
                Helpers::validateType($value, $this->type, $context);
1✔
183
                $isOk() && Helpers::validateRange($value, $this->range, $context, $this->type);
1✔
184
                $isOk() && $value !== null && $this->pattern !== null && Helpers::validatePattern($value, $this->pattern, $context);
1✔
185
                $isOk() && is_array($value) && $this->validateItems($value, $context);
1✔
186
                $isOk() && $merge && $value = Helpers::merge($value, $this->default);
1✔
187
                $isOk() && $value = $this->doTransform($value, $context);
1✔
188
                if (!$isOk()) {
1✔
189
                        return null;
1✔
190
                }
191

192
                if ($value instanceof DynamicParameter) {
1✔
193
                        $expected = $this->type . ($this->range === [null, null] ? '' : ':' . implode('..', $this->range));
1✔
194
                        $context->dynamics[] = [$value, str_replace(DynamicParameter::class . '|', '', $expected), $context->path];
1✔
195
                }
196
                return $value;
1✔
197
        }
198

199

200
        private function validateItems(array &$value, Context $context): void
1✔
201
        {
202
                if (!$this->itemsValue) {
1✔
203
                        return;
1✔
204
                }
205

206
                $res = [];
1✔
207
                foreach ($value as $key => $val) {
1✔
208
                        $context->path[] = $key;
1✔
209
                        $context->isKey = true;
1✔
210
                        $key = $this->itemsKey ? $this->itemsKey->complete($key, $context) : $key;
1✔
211
                        $context->isKey = false;
1✔
212
                        $res[$key] = $this->itemsValue->complete($val, $context);
1✔
213
                        array_pop($context->path);
1✔
214
                }
215
                $value = $res;
1✔
216
        }
1✔
217
}
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