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

nette / schema / 6424119556

05 Oct 2023 08:24PM UTC coverage: 96.115%. Remained the same
6424119556

push

github

dg
castTo() allows you to create objects [Closes #44][Closes #47][Closes #58][Closes #46]

8 of 8 new or added lines in 1 file covered. (100.0%)

470 of 489 relevant lines covered (96.11%)

0.96 hits per line

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

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

16

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

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

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

31
        /** @var callable[] */
32
        private $transforms = [];
33

34
        /** @var string|null */
35
        private $deprecated;
36

37

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

44

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

51

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

58

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

64

65
        public function transform(callable $handler): self
1✔
66
        {
67
                $this->transforms[] = $handler;
1✔
68
                return $this;
1✔
69
        }
70

71

72
        public function assert(callable $handler, ?string $description = null): self
1✔
73
        {
74
                $expected = $description ?: (is_string($handler) ? "$handler()" : '#' . count($this->transforms));
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)
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($value, Context $context)
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($value, Context $context)
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($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($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($value, Context $context)
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