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

nette / schema / 3615460175

pending completion
3615460175

push

github

David
constants are PascalCase

26 of 26 new or added lines in 5 files covered. (100.0%)

439 of 450 relevant lines covered (97.56%)

0.98 hits per line

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

96.08
/src/Schema/Elements/AnyOf.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 Nette\Schema\Schema;
16

17

18
final class AnyOf implements Schema
19
{
20
        use Base;
21
        use Nette\SmartObject;
22

23
        /** @var array */
24
        private $set;
25

26

27
        /**
28
         * @param  mixed|Schema  ...$set
29
         */
30
        public function __construct(...$set)
1✔
31
        {
32
                if (!$set) {
1✔
33
                        throw new Nette\InvalidStateException('The enumeration must not be empty.');
1✔
34
                }
35

36
                $this->set = $set;
1✔
37
        }
1✔
38

39

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

46

47
        public function nullable(): self
48
        {
49
                $this->set[] = null;
1✔
50
                return $this;
1✔
51
        }
52

53

54
        public function dynamic(): self
55
        {
56
                $this->set[] = new Type(Nette\Schema\DynamicParameter::class);
1✔
57
                return $this;
1✔
58
        }
59

60

61
        /********************* processing ****************d*g**/
62

63

64
        public function normalize($value, Context $context)
1✔
65
        {
66
                return $this->doNormalize($value, $context);
1✔
67
        }
68

69

70
        public function merge($value, $base)
71
        {
72
                if (is_array($value) && isset($value[Helpers::PreventMerging])) {
1✔
73
                        unset($value[Helpers::PreventMerging]);
×
74
                        return $value;
×
75
                }
76

77
                return Helpers::merge($value, $base);
1✔
78
        }
79

80

81
        public function complete($value, Context $context)
1✔
82
        {
83
                $expecteds = $innerErrors = [];
1✔
84
                foreach ($this->set as $item) {
1✔
85
                        if ($item instanceof Schema) {
1✔
86
                                $dolly = new Context;
1✔
87
                                $dolly->path = $context->path;
1✔
88
                                $res = $item->complete($item->normalize($value, $dolly), $dolly);
1✔
89
                                if (!$dolly->errors) {
1✔
90
                                        $context->warnings = array_merge($context->warnings, $dolly->warnings);
1✔
91
                                        return $this->doFinalize($res, $context);
1✔
92
                                }
93

94
                                foreach ($dolly->errors as $error) {
1✔
95
                                        if ($error->path !== $context->path || empty($error->variables['expected'])) {
1✔
96
                                                $innerErrors[] = $error;
1✔
97
                                        } else {
98
                                                $expecteds[] = $error->variables['expected'];
1✔
99
                                        }
100
                                }
101
                        } else {
102
                                if ($item === $value) {
1✔
103
                                        return $this->doFinalize($value, $context);
1✔
104
                                }
105

106
                                $expecteds[] = Nette\Schema\Helpers::formatValue($item);
1✔
107
                        }
108
                }
109

110
                if ($innerErrors) {
1✔
111
                        $context->errors = array_merge($context->errors, $innerErrors);
1✔
112
                } else {
113
                        $context->addError(
1✔
114
                                'The %label% %path% expects to be %expected%, %value% given.',
1✔
115
                                Nette\Schema\Message::TypeMismatch,
1✔
116
                                [
117
                                        'value' => $value,
1✔
118
                                        'expected' => implode('|', array_unique($expecteds)),
1✔
119
                                ]
120
                        );
121
                }
122
        }
1✔
123

124

125
        public function completeDefault(Context $context)
1✔
126
        {
127
                if ($this->required) {
1✔
128
                        $context->addError(
1✔
129
                                'The mandatory item %path% is missing.',
1✔
130
                                Nette\Schema\Message::MissingItem
1✔
131
                        );
132
                        return null;
1✔
133
                }
134

135
                if ($this->default instanceof Schema) {
1✔
136
                        return $this->default->completeDefault($context);
1✔
137
                }
138

139
                return $this->default;
1✔
140
        }
141
}
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