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

nette / schema / 22292073757

23 Feb 2026 03:39AM UTC coverage: 96.579%. Remained the same
22292073757

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
        /** @param  callable(mixed): mixed  $handler */
47
        public function before(callable $handler): self
1✔
48
        {
49
                $this->before = $handler(...);
1✔
50
                return $this;
1✔
51
        }
52

53

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

59

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

67

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

84

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

92

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

103
                return $this->default;
1✔
104
        }
105

106

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

113
                return $value;
1✔
114
        }
115

116

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

127

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

140

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

149

150
        /**
151
         * @deprecated use Nette\Schema\Validators::validateRange()
152
         * @param  array{?float, ?float}  $range
153
         */
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