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

nette / schema / 6399489220

03 Oct 2023 11:22PM UTC coverage: 96.289% (+0.7%) from 95.607%
6399489220

push

github

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

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

467 of 485 relevant lines covered (96.29%)

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 array[] */
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, true];
1✔
68
                return $this;
1✔
69
        }
70

71

72
        public function assert(callable $handler, ?string $description = null): self
1✔
73
        {
74
                $this->transforms[] = [$handler, $description];
1✔
75
                return $this;
1✔
76
        }
77

78

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

86

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

97
                return $this->default;
1✔
98
        }
99

100

101
        public function doNormalize($value, Context $context)
1✔
102
        {
103
                if ($this->before) {
1✔
104
                        $value = ($this->before)($value);
1✔
105
                }
106

107
                return $value;
1✔
108
        }
109

110

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

121

122
        private function doTransform($value, Context $context)
1✔
123
        {
124
                $ok = $context->watch();
1✔
125
                foreach ($this->transforms as $i => [$handler, $type]) {
1✔
126
                        if ($type === true) { // transform()
1✔
127
                                $value = $handler($value, $context);
1✔
128

129
                        } elseif (!$handler($value)) { // assert()
1✔
130
                                $expected = $type ?: (is_string($handler) ? "$handler()" : "#$i");
1✔
131
                                $context->addError(
1✔
132
                                        'Failed assertion ' . ($type ? "'%assertion%'" : '%assertion%') . ' for %label% %path% with value %value%.',
1✔
133
                                        Nette\Schema\Message::FailedAssertion,
1✔
134
                                        ['value' => $value, 'assertion' => $expected]
1✔
135
                                );
136
                        }
137
                        if (!$ok()) {
1✔
138
                                return null;
1✔
139
                        }
140
                }
141

142
                return $value;
1✔
143
        }
144

145

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

154

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

163

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