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

nette / schema / 22834792529

09 Mar 2026 01:36AM UTC coverage: 96.579%. Remained the same
22834792529

push

github

dg
added CLAUDE.md

480 of 497 relevant lines covered (96.58%)

0.97 hits per line

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

88.14
/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
                });
1✔
94
        }
95

96

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

106

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

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

120

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

127
                return $value;
1✔
128
        }
129

130

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

141

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

154

155
        /** @deprecated use Nette\Schema\Validators::validateType() */
156
        private function doValidate(mixed $value, string $expected, Context $context): bool
157
        {
158
                $isOk = $context->createChecker();
×
159
                Helpers::validateType($value, $expected, $context);
×
160
                return $isOk();
×
161
        }
162

163

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

175

176
        /** @deprecated use doTransform() */
177
        private function doFinalize(mixed $value, Context $context): mixed
178
        {
179
                return $this->doTransform($value, $context);
×
180
        }
181
}
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