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

nette / schema / 6398768683

03 Oct 2023 09:48PM UTC coverage: 95.607% (-2.0%) from 97.561%
6398768683

push

github

dg
refactoring

94 of 94 new or added lines in 6 files covered. (100.0%)

457 of 478 relevant lines covered (95.61%)

0.96 hits per line

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

84.13
/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

15

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

24
        /** @var mixed */
25
        private $default;
26

27
        /** @var callable|null */
28
        private $before;
29

30
        /** @var array[] */
31
        private $asserts = [];
32

33
        /** @var string|null */
34
        private $castTo;
35

36
        /** @var string|null */
37
        private $deprecated;
38

39

40
        public function default($value): self
41
        {
42
                $this->default = $value;
1✔
43
                return $this;
1✔
44
        }
45

46

47
        public function required(bool $state = true): self
1✔
48
        {
49
                $this->required = $state;
1✔
50
                return $this;
1✔
51
        }
52

53

54
        public function before(callable $handler): self
1✔
55
        {
56
                $this->before = $handler;
1✔
57
                return $this;
1✔
58
        }
59

60

61
        public function castTo(string $type): self
1✔
62
        {
63
                $this->castTo = $type;
1✔
64
                return $this;
1✔
65
        }
66

67

68
        public function assert(callable $handler, ?string $description = null): self
1✔
69
        {
70
                $this->asserts[] = [$handler, $description];
1✔
71
                return $this;
1✔
72
        }
73

74

75
        /** Marks as deprecated */
76
        public function deprecated(string $message = 'The item %path% is deprecated.'): self
1✔
77
        {
78
                $this->deprecated = $message;
1✔
79
                return $this;
1✔
80
        }
81

82

83
        public function completeDefault(Context $context)
1✔
84
        {
85
                if ($this->required) {
1✔
86
                        $context->addError(
1✔
87
                                'The mandatory item %path% is missing.',
1✔
88
                                Nette\Schema\Message::MissingItem
1✔
89
                        );
90
                        return null;
1✔
91
                }
92

93
                return $this->default;
1✔
94
        }
95

96

97
        public function doNormalize($value, Context $context)
1✔
98
        {
99
                if ($this->before) {
1✔
100
                        $value = ($this->before)($value);
1✔
101
                }
102

103
                return $value;
1✔
104
        }
105

106

107
        private function doDeprecation(Context $context): void
1✔
108
        {
109
                if ($this->deprecated !== null) {
1✔
110
                        $context->addWarning(
1✔
111
                                $this->deprecated,
1✔
112
                                Nette\Schema\Message::Deprecated
1✔
113
                        );
114
                }
115
        }
1✔
116

117

118
        private function doTransform(&$value, Context $context): void
1✔
119
        {
120
                if ($this->castTo) {
1✔
121
                        if (Nette\Utils\Reflection::isBuiltinType($this->castTo)) {
1✔
122
                                settype($value, $this->castTo);
1✔
123
                        } else {
124
                                $object = new $this->castTo;
1✔
125
                                foreach ($value as $k => $v) {
1✔
126
                                        $object->$k = $v;
1✔
127
                                }
128
                                $value = $object;
1✔
129
                        }
130
                }
131
        }
1✔
132

133

134
        private function doAsserts($value, Context $context): void
1✔
135
        {
136
                foreach ($this->asserts as $i => [$handler, $description]) {
1✔
137
                        if (!$handler($value)) {
1✔
138
                                $expected = $description ?: (is_string($handler) ? "$handler()" : "#$i");
1✔
139
                                $context->addError(
1✔
140
                                        'Failed assertion ' . ($description ? "'%assertion%'" : '%assertion%') . ' for %label% %path% with value %value%.',
1✔
141
                                        Nette\Schema\Message::FailedAssertion,
1✔
142
                                        ['value' => $value, 'assertion' => $expected]
1✔
143
                                );
144
                                return;
1✔
145
                        }
146
                }
147
        }
1✔
148

149

150
        /** @deprecated use Nette\Schema\Validators::validateType() */
151
        private function doValidate($value, string $expected, Context $context): bool
152
        {
153
                $ok = $context->watch();
×
154
                Nette\Schema\Helpers::validateType($value, $expected, $context);
×
155
                return $ok();
×
156
        }
157

158

159
        /** @deprecated use Nette\Schema\Validators::validateRange() */
160
        private static function doValidateRange($value, array $range, Context $context, string $types = ''): bool
161
        {
162
                $ok = $context->watch();
×
163
                Nette\Schema\Helpers::validateRange($value, $range, $context, $types);
×
164
                return $ok();
×
165
        }
166

167

168
        /** @deprecated use doTransform() */
169
        private function doFinalize($value, Context $context)
170
        {
171
                $ok = $context->watch();
×
172
                $this->doTransform($value, $context);
×
173
                $ok() && $this->doAsserts($value, $context);
×
174
                return $ok() ? $value : null;
×
175
        }
176
}
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