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

nette / schema / 29631259250

18 Jul 2026 04:51AM UTC coverage: 98.361% (+1.6%) from 96.735%
29631259250

push

github

dg
DOCS

540 of 549 relevant lines covered (98.36%)

0.98 hits per line

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

89.71
/src/Schema/Elements/Base.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\MergeMode;
14
use function count, is_string;
15

16

17
/**
18
 * @internal
19
 */
20
trait Base
21
{
22
        private bool $required = false;
23
        private mixed $default = null;
24

25
        /** @var ?\Closure(mixed): mixed */
26
        private ?\Closure $before = null;
27

28
        /** @var list<\Closure(mixed, Context): mixed> */
29
        private array $transforms = [];
30
        private ?string $deprecated = null;
31
        private ?MergeMode $mergeMode = null;
32

33
        /** @var ?\Closure(mixed, mixed): mixed */
34
        private ?\Closure $mergeWith = null;
35

36

37
        public function default(mixed $value): self
1✔
38
        {
39
                $this->default = $value;
1✔
40
                return $this;
1✔
41
        }
42

43

44
        public function required(bool $state = true): self
1✔
45
        {
46
                $this->required = $state;
1✔
47
                return $this;
1✔
48
        }
49

50

51
        /**
52
         * Sets a pre-normalization callback applied to the raw input value before any validation.
53
         * @param  callable(mixed): mixed  $handler
54
         */
55
        public function before(callable $handler): self
1✔
56
        {
57
                $this->before = $handler(...);
1✔
58
                return $this;
1✔
59
        }
60

61

62
        /**
63
         * Sets how array values are combined when merging multiple configuration layers.
64
         */
65
        public function mergeMode(MergeMode $mode): self
1✔
66
        {
67
                $this->mergeMode = $mode;
1✔
68
                return $this;
1✔
69
        }
70

71

72
        /**
73
         * Sets a custom strategy combining two layers. Must be a pure combiner; canonicalize layer shape in before() instead.
74
         * Either side may be null, a layer can legally be null.
75
         * @param  callable(mixed, mixed): mixed  $fn
76
         */
77
        public function mergeWith(callable $fn): self
1✔
78
        {
79
                $this->mergeWith = $fn(...);
1✔
80
                return $this;
1✔
81
        }
82

83

84
        /**
85
         * Casts the validated value to a built-in type or instantiates the given class.
86
         */
87
        public function castTo(string $type): self
1✔
88
        {
89
                return $this->transform(Helpers::getCastStrategy($type));
1✔
90
        }
91

92

93
        /**
94
         * Adds a post-validation transformation callback. The handler may also report errors via Context.
95
         * @param  callable(mixed, Context): mixed  $handler
96
         */
97
        public function transform(callable $handler): self
1✔
98
        {
99
                $this->transforms[] = $handler(...);
1✔
100
                return $this;
1✔
101
        }
102

103

104
        /**
105
         * Adds a custom validation assertion; optionally describe it for error messages.
106
         * @param  callable(mixed): bool  $handler
107
         */
108
        public function assert(callable $handler, ?string $description = null): self
1✔
109
        {
110
                $expected = $description ?? (is_string($handler) ? "$handler()" : '#' . count($this->transforms));
1✔
111
                return $this->transform(function ($value, Context $context) use ($handler, $description, $expected) {
1✔
112
                        if ($handler($value)) {
1✔
113
                                return $value;
1✔
114
                        }
115
                        $context->addError(
1✔
116
                                'Failed assertion ' . ($description ? "'%assertion%'" : '%assertion%') . ' for %label% %path% with value %value%.',
1✔
117
                                Nette\Schema\Message::FailedAssertion,
1✔
118
                                ['value' => $value, 'assertion' => $expected],
1✔
119
                        );
120
                        return null;
1✔
121
                });
1✔
122
        }
123

124

125
        /**
126
         * Marks the item as deprecated; emits a warning with the given message when the item is used.
127
         */
128
        public function deprecated(string $message = 'The item %path% is deprecated.'): self
1✔
129
        {
130
                $this->deprecated = $message;
1✔
131
                return $this;
1✔
132
        }
133

134

135
        public function completeDefault(Context $context): mixed
1✔
136
        {
137
                if ($this->required && !$context->isPartial) {
1✔
138
                        $context->addError(
1✔
139
                                'The mandatory item %path% is missing.',
1✔
140
                                Nette\Schema\Message::MissingItem,
1✔
141
                        );
142
                        return null;
1✔
143
                }
144

145
                return $this->default;
1✔
146
        }
147

148

149
        public function doNormalize(mixed $value, Context $context): mixed
1✔
150
        {
151
                if ($this->before) {
1✔
152
                        $value = ($this->before)($value);
1✔
153
                }
154

155
                return $value;
1✔
156
        }
157

158

159
        private function doDeprecation(Context $context): void
1✔
160
        {
161
                if ($this->deprecated !== null && !$context->isPartial) {
1✔
162
                        $context->addWarning(
1✔
163
                                $this->deprecated,
1✔
164
                                Nette\Schema\Message::Deprecated,
1✔
165
                        );
166
                }
167
        }
1✔
168

169

170
        private function doTransform(mixed $value, Context $context): mixed
1✔
171
        {
172
                if ($context->isPartial) {
1✔
173
                        return $value;
1✔
174
                }
175

176
                $isOk = $context->createChecker();
1✔
177
                foreach ($this->transforms as $handler) {
1✔
178
                        $value = $handler($value, $context);
1✔
179
                        if (!$isOk()) {
1✔
180
                                return null;
1✔
181
                        }
182
                }
183
                return $value;
1✔
184
        }
185

186

187
        #[\Deprecated('use Nette\Schema\Validators::validateType()')]
188
        private function doValidate(mixed $value, string $expected, Context $context): bool
189
        {
190
                $isOk = $context->createChecker();
×
191
                Helpers::validateType($value, $expected, $context);
×
192
                return $isOk();
×
193
        }
194

195

196
        /** @param array{?float, ?float} $range */
197
        #[\Deprecated('use Nette\Schema\Validators::validateRange()')]
198
        private static function doValidateRange(mixed $value, array $range, Context $context, string $types = ''): bool
199
        {
200
                $isOk = $context->createChecker();
×
201
                Helpers::validateRange($value, $range, $context, $types);
×
202
                return $isOk();
×
203
        }
204

205

206
        #[\Deprecated('use doTransform()')]
207
        private function doFinalize(mixed $value, Context $context): mixed
208
        {
209
                return $this->doTransform($value, $context);
×
210
        }
211
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc