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

nette / schema / 27922308304

22 Jun 2026 12:20AM UTC coverage: 97.912% (+0.01%) from 97.899%
27922308304

push

github

dg
used #Deprecated

469 of 479 relevant lines covered (97.91%)

0.98 hits per line

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

88.33
/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 function count, is_string;
14

15

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

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

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

31

32
        public function default(mixed $value): self
1✔
33
        {
34
                $this->default = $value;
1✔
35
                return $this;
1✔
36
        }
37

38

39
        public function required(bool $state = true): self
1✔
40
        {
41
                $this->required = $state;
1✔
42
                return $this;
1✔
43
        }
44

45

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

56

57
        /**
58
         * Casts the validated value to a built-in type or instantiates the given class.
59
         */
60
        public function castTo(string $type): self
1✔
61
        {
62
                return $this->transform(Helpers::getCastStrategy($type));
1✔
63
        }
64

65

66
        /**
67
         * Adds a post-validation transformation callback. The handler may also report errors via Context.
68
         * @param  callable(mixed, Context): mixed  $handler
69
         */
70
        public function transform(callable $handler): self
1✔
71
        {
72
                $this->transforms[] = $handler(...);
1✔
73
                return $this;
1✔
74
        }
75

76

77
        /**
78
         * Adds a custom validation assertion; optionally describe it for error messages.
79
         * @param  callable(mixed): bool  $handler
80
         */
81
        public function assert(callable $handler, ?string $description = null): self
1✔
82
        {
83
                $expected = $description ?? (is_string($handler) ? "$handler()" : '#' . count($this->transforms));
1✔
84
                return $this->transform(function ($value, Context $context) use ($handler, $description, $expected) {
1✔
85
                        if ($handler($value)) {
1✔
86
                                return $value;
1✔
87
                        }
88
                        $context->addError(
1✔
89
                                'Failed assertion ' . ($description ? "'%assertion%'" : '%assertion%') . ' for %label% %path% with value %value%.',
1✔
90
                                Nette\Schema\Message::FailedAssertion,
1✔
91
                                ['value' => $value, 'assertion' => $expected],
1✔
92
                        );
93
                        return null;
1✔
94
                });
1✔
95
        }
96

97

98
        /**
99
         * Marks the item as deprecated; emits a warning with the given message when the item is used.
100
         */
101
        public function deprecated(string $message = 'The item %path% is deprecated.'): self
1✔
102
        {
103
                $this->deprecated = $message;
1✔
104
                return $this;
1✔
105
        }
106

107

108
        public function completeDefault(Context $context): mixed
1✔
109
        {
110
                if ($this->required) {
1✔
111
                        $context->addError(
1✔
112
                                'The mandatory item %path% is missing.',
1✔
113
                                Nette\Schema\Message::MissingItem,
1✔
114
                        );
115
                        return null;
1✔
116
                }
117

118
                return $this->default;
1✔
119
        }
120

121

122
        public function doNormalize(mixed $value, Context $context): mixed
1✔
123
        {
124
                if ($this->before) {
1✔
125
                        $value = ($this->before)($value);
1✔
126
                }
127

128
                return $value;
1✔
129
        }
130

131

132
        private function doDeprecation(Context $context): void
1✔
133
        {
134
                if ($this->deprecated !== null) {
1✔
135
                        $context->addWarning(
1✔
136
                                $this->deprecated,
1✔
137
                                Nette\Schema\Message::Deprecated,
1✔
138
                        );
139
                }
140
        }
1✔
141

142

143
        private function doTransform(mixed $value, Context $context): mixed
1✔
144
        {
145
                $isOk = $context->createChecker();
1✔
146
                foreach ($this->transforms as $handler) {
1✔
147
                        $value = $handler($value, $context);
1✔
148
                        if (!$isOk()) {
1✔
149
                                return null;
1✔
150
                        }
151
                }
152
                return $value;
1✔
153
        }
154

155

156
        #[\Deprecated('use Nette\Schema\Validators::validateType()')]
157
        private function doValidate(mixed $value, string $expected, Context $context): bool
158
        {
159
                $isOk = $context->createChecker();
×
160
                Helpers::validateType($value, $expected, $context);
×
161
                return $isOk();
×
162
        }
163

164

165
        /** @param array{?float, ?float} $range */
166
        #[\Deprecated('use Nette\Schema\Validators::validateRange()')]
167
        private static function doValidateRange(mixed $value, array $range, Context $context, string $types = ''): bool
168
        {
169
                $isOk = $context->createChecker();
×
170
                Helpers::validateRange($value, $range, $context, $types);
×
171
                return $isOk();
×
172
        }
173

174

175
        #[\Deprecated('use doTransform()')]
176
        private function doFinalize(mixed $value, Context $context): mixed
177
        {
178
                return $this->doTransform($value, $context);
×
179
        }
180
}
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