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

nette / schema / 20849065652

09 Jan 2026 10:32AM UTC coverage: 96.531% (+0.007%) from 96.524%
20849065652

push

github

dg
added CLAUDE.md

473 of 490 relevant lines covered (96.53%)

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
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 function count, is_string;
16

17

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

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

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

33

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

40

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

47

48
        /** @param  callable(mixed): mixed  $handler */
49
        public function before(callable $handler): self
1✔
50
        {
51
                $this->before = $handler(...);
1✔
52
                return $this;
1✔
53
        }
54

55

56
        public function castTo(string $type): self
1✔
57
        {
58
                return $this->transform(Helpers::getCastStrategy($type));
1✔
59
        }
60

61

62
        /** @param  callable(mixed, Context): mixed  $handler */
63
        public function transform(callable $handler): self
1✔
64
        {
65
                $this->transforms[] = $handler(...);
1✔
66
                return $this;
1✔
67
        }
68

69

70
        /** @param  callable(mixed): bool  $handler */
71
        public function assert(callable $handler, ?string $description = null): self
1✔
72
        {
73
                $expected = $description ?? (is_string($handler) ? "$handler()" : '#' . count($this->transforms));
1✔
74
                $handler = $handler(...);
1✔
75
                return $this->transform(function ($value, Context $context) use ($handler, $description, $expected) {
1✔
76
                        if ($handler($value)) {
1✔
77
                                return $value;
1✔
78
                        }
79
                        $context->addError(
1✔
80
                                'Failed assertion ' . ($description ? "'%assertion%'" : '%assertion%') . ' for %label% %path% with value %value%.',
1✔
81
                                Nette\Schema\Message::FailedAssertion,
1✔
82
                                ['value' => $value, 'assertion' => $expected],
1✔
83
                        );
84
                });
1✔
85
        }
86

87

88
        /** Marks as deprecated */
89
        public function deprecated(string $message = 'The item %path% is deprecated.'): self
1✔
90
        {
91
                $this->deprecated = $message;
1✔
92
                return $this;
1✔
93
        }
94

95

96
        public function completeDefault(Context $context): mixed
1✔
97
        {
98
                if ($this->required) {
1✔
99
                        $context->addError(
1✔
100
                                'The mandatory item %path% is missing.',
1✔
101
                                Nette\Schema\Message::MissingItem,
1✔
102
                        );
103
                        return null;
1✔
104
                }
105

106
                return $this->default;
1✔
107
        }
108

109

110
        public function doNormalize(mixed $value, Context $context): mixed
1✔
111
        {
112
                if ($this->before) {
1✔
113
                        $value = ($this->before)($value);
1✔
114
                }
115

116
                return $value;
1✔
117
        }
118

119

120
        private function doDeprecation(Context $context): void
1✔
121
        {
122
                if ($this->deprecated !== null) {
1✔
123
                        $context->addWarning(
1✔
124
                                $this->deprecated,
1✔
125
                                Nette\Schema\Message::Deprecated,
1✔
126
                        );
127
                }
128
        }
1✔
129

130

131
        private function doTransform(mixed $value, Context $context): mixed
1✔
132
        {
133
                $isOk = $context->createChecker();
1✔
134
                foreach ($this->transforms as $handler) {
1✔
135
                        $value = $handler($value, $context);
1✔
136
                        if (!$isOk()) {
1✔
137
                                return null;
1✔
138
                        }
139
                }
140
                return $value;
1✔
141
        }
142

143

144
        /** @deprecated use Nette\Schema\Validators::validateType() */
145
        private function doValidate(mixed $value, string $expected, Context $context): bool
146
        {
147
                $isOk = $context->createChecker();
×
148
                Helpers::validateType($value, $expected, $context);
×
149
                return $isOk();
×
150
        }
151

152

153
        /** @deprecated use Nette\Schema\Validators::validateRange() */
154
        private static function doValidateRange(mixed $value, array $range, Context $context, string $types = ''): bool
155
        {
156
                $isOk = $context->createChecker();
×
157
                Helpers::validateRange($value, $range, $context, $types);
×
158
                return $isOk();
×
159
        }
160

161

162
        /** @deprecated use doTransform() */
163
        private function doFinalize(mixed $value, Context $context): mixed
164
        {
165
                return $this->doTransform($value, $context);
×
166
        }
167
}
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