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

nette / schema / 21791156377

08 Feb 2026 02:54AM UTC coverage: 97.877%. Remained the same
21791156377

push

github

dg
used attribute Deprecated

461 of 471 relevant lines covered (97.88%)

0.98 hits per line

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

87.93
/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
                return $this->transform(function ($value, Context $context) use ($handler, $description, $expected) {
1✔
75
                        if ($handler($value)) {
1✔
76
                                return $value;
1✔
77
                        }
78
                        $context->addError(
1✔
79
                                'Failed assertion ' . ($description ? "'%assertion%'" : '%assertion%') . ' for %label% %path% with value %value%.',
1✔
80
                                Nette\Schema\Message::FailedAssertion,
1✔
81
                                ['value' => $value, 'assertion' => $expected],
1✔
82
                        );
83
                });
1✔
84
        }
85

86

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

94

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

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

108

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

115
                return $value;
1✔
116
        }
117

118

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

129

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

142

143
        #[\Deprecated('use Nette\Schema\Validators::validateType()')]
144
        private function doValidate(mixed $value, string $expected, Context $context): bool
145
        {
146
                $isOk = $context->createChecker();
×
147
                Helpers::validateType($value, $expected, $context);
×
148
                return $isOk();
×
149
        }
150

151

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

160

161
        #[\Deprecated('use doTransform()')]
162
        private function doFinalize(mixed $value, Context $context): mixed
163
        {
164
                return $this->doTransform($value, $context);
×
165
        }
166
}
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