• 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

96.55
/src/Schema/Elements/AnyOf.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 Nette\Schema\Schema;
14
use function array_merge, array_unique, implode, is_array;
15

16

17
final class AnyOf implements Schema
18
{
19
        use Base;
20

21
        /** @var mixed[] */
22
        private array $set;
23

24

25
        public function __construct(mixed ...$set)
1✔
26
        {
27
                if (!$set) {
1✔
28
                        throw new Nette\InvalidStateException('The enumeration must not be empty.');
1✔
29
                }
30

31
                $this->set = $set;
1✔
32
        }
1✔
33

34

35
        public function firstIsDefault(): self
36
        {
37
                $this->default = $this->set[0];
1✔
38
                return $this;
1✔
39
        }
40

41

42
        public function nullable(): self
43
        {
44
                $this->set[] = null;
1✔
45
                return $this;
1✔
46
        }
47

48

49
        public function dynamic(): self
50
        {
51
                $this->set[] = new Type(Nette\Schema\DynamicParameter::class);
1✔
52
                return $this;
1✔
53
        }
54

55

56
        /********************* processing ****************d*g**/
57

58

59
        public function normalize(mixed $value, Context $context): mixed
1✔
60
        {
61
                return $this->doNormalize($value, $context);
1✔
62
        }
63

64

65
        public function merge(mixed $value, mixed $base): mixed
1✔
66
        {
67
                if (is_array($value) && isset($value[Helpers::PreventMerging])) {
1✔
68
                        unset($value[Helpers::PreventMerging]);
×
69
                        return $value;
×
70
                }
71

72
                return Helpers::merge($value, $base);
1✔
73
        }
74

75

76
        public function complete(mixed $value, Context $context): mixed
1✔
77
        {
78
                $isOk = $context->createChecker();
1✔
79
                $value = $this->findAlternative($value, $context);
1✔
80
                $isOk() && $value = $this->doTransform($value, $context);
1✔
81
                return $isOk() ? $value : null;
1✔
82
        }
83

84

85
        private function findAlternative(mixed $value, Context $context): mixed
1✔
86
        {
87
                $expecteds = $innerErrors = [];
1✔
88
                foreach ($this->set as $item) {
1✔
89
                        if ($item instanceof Schema) {
1✔
90
                                $dolly = new Context;
1✔
91
                                $dolly->path = $context->path;
1✔
92
                                $res = $item->complete($item->normalize($value, $dolly), $dolly);
1✔
93
                                if (!$dolly->errors) {
1✔
94
                                        $context->warnings = array_merge($context->warnings, $dolly->warnings);
1✔
95
                                        return $res;
1✔
96
                                }
97

98
                                foreach ($dolly->errors as $error) {
1✔
99
                                        if ($error->path !== $context->path || empty($error->variables['expected'])) {
1✔
100
                                                $innerErrors[] = $error;
1✔
101
                                        } else {
102
                                                $expecteds[] = $error->variables['expected'];
1✔
103
                                        }
104
                                }
105
                        } else {
106
                                if ($item === $value) {
1✔
107
                                        return $value;
1✔
108
                                }
109

110
                                $expecteds[] = Nette\Schema\Helpers::formatValue($item);
1✔
111
                        }
112
                }
113

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

127
                return null;
1✔
128
        }
129

130

131
        public function completeDefault(Context $context): mixed
1✔
132
        {
133
                if ($this->required) {
1✔
134
                        $context->addError(
1✔
135
                                'The mandatory item %path% is missing.',
1✔
136
                                Nette\Schema\Message::MissingItem,
1✔
137
                        );
138
                        return null;
1✔
139
                }
140

141
                if ($this->default instanceof Schema) {
1✔
142
                        return $this->default->completeDefault($context);
1✔
143
                }
144

145
                return $this->default;
1✔
146
        }
147
}
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