Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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)
31
	{
32
		if (!$set) {
33
			throw new Nette\InvalidStateException('The enumeration must not be empty.');
34
		}
35
 
36
		$this->set = $set;
37
	}
38
 
39
 
40
	public function firstIsDefault(): self
41
	{
42
		$this->default = $this->set[0];
43
		return $this;
44
	}
45
 
46
 
47
	public function nullable(): self
48
	{
49
		$this->set[] = null;
50
		return $this;
51
	}
52
 
53
 
54
	public function dynamic(): self
55
	{
56
		$this->set[] = new Type(Nette\Schema\DynamicParameter::class);
57
		return $this;
58
	}
59
 
60
 
61
	/********************* processing ****************d*g**/
62
 
63
 
64
	public function normalize($value, Context $context)
65
	{
66
		return $this->doNormalize($value, $context);
67
	}
68
 
69
 
70
	public function merge($value, $base)
71
	{
72
		if (is_array($value) && isset($value[Helpers::PREVENT_MERGING])) {
73
			unset($value[Helpers::PREVENT_MERGING]);
74
			return $value;
75
		}
76
 
77
		return Helpers::merge($value, $base);
78
	}
79
 
80
 
81
	public function complete($value, Context $context)
82
	{
83
		$expecteds = $innerErrors = [];
84
		foreach ($this->set as $item) {
85
			if ($item instanceof Schema) {
86
				$dolly = new Context;
87
				$dolly->path = $context->path;
88
				$res = $item->complete($item->normalize($value, $dolly), $dolly);
89
				if (!$dolly->errors) {
90
					$context->warnings = array_merge($context->warnings, $dolly->warnings);
91
					return $this->doFinalize($res, $context);
92
				}
93
 
94
				foreach ($dolly->errors as $error) {
95
					if ($error->path !== $context->path || empty($error->variables['expected'])) {
96
						$innerErrors[] = $error;
97
					} else {
98
						$expecteds[] = $error->variables['expected'];
99
					}
100
				}
101
			} else {
102
				if ($item === $value) {
103
					return $this->doFinalize($value, $context);
104
				}
105
 
106
				$expecteds[] = Nette\Schema\Helpers::formatValue($item);
107
			}
108
		}
109
 
110
		if ($innerErrors) {
111
			$context->errors = array_merge($context->errors, $innerErrors);
112
		} else {
113
			$context->addError(
114
				'The %label% %path% expects to be %expected%, %value% given.',
115
				Nette\Schema\Message::TYPE_MISMATCH,
116
				[
117
					'value' => $value,
118
					'expected' => implode('|', array_unique($expecteds)),
119
				]
120
			);
121
		}
122
	}
123
 
124
 
125
	public function completeDefault(Context $context)
126
	{
127
		if ($this->required) {
128
			$context->addError(
129
				'The mandatory item %path% is missing.',
130
				Nette\Schema\Message::MISSING_ITEM
131
			);
132
			return null;
133
		}
134
 
135
		if ($this->default instanceof Schema) {
136
			return $this->default->completeDefault($context);
137
		}
138
 
139
		return $this->default;
140
	}
141
}