PluginProbe
Packeta / 1.6.1
Packeta v1.6.1
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / deps / nette / schema / src / Schema / Elements / AnyOf.php

AnyOf.php in Packeta 1.6.1, at deps/nette/schema/src/Schema/Elements/AnyOf.php

102 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 declare (strict_types=1);
8 namespace Packetery\Nette\Schema\Elements;
9
10 use Packetery\Nette;
11 use Packetery\Nette\Schema\Context;
12 use Packetery\Nette\Schema\Helpers;
13 use Packetery\Nette\Schema\Schema;
14 final class AnyOf implements Schema
15 {
16 use Base;
17 use \Packetery\Nette\SmartObject;
18 /** @var array */
19 private $set;
20 /**
21 * @param mixed|Schema ...$set
22 */
23 public function __construct(...$set)
24 {
25 if (!$set) {
26 throw new \Packetery\Nette\InvalidStateException('The enumeration must not be empty.');
27 }
28 $this->set = $set;
29 }
30 public function firstIsDefault() : self
31 {
32 $this->default = $this->set[0];
33 return $this;
34 }
35 public function nullable() : self
36 {
37 $this->set[] = null;
38 return $this;
39 }
40 public function dynamic() : self
41 {
42 $this->set[] = new Type(\Packetery\Nette\Schema\DynamicParameter::class);
43 return $this;
44 }
45 /********************* processing ****************d*g**/
46 public function normalize($value, Context $context)
47 {
48 return $this->doNormalize($value, $context);
49 }
50 public function merge($value, $base)
51 {
52 if (\is_array($value) && isset($value[Helpers::PREVENT_MERGING])) {
53 unset($value[Helpers::PREVENT_MERGING]);
54 return $value;
55 }
56 return Helpers::merge($value, $base);
57 }
58 public function complete($value, Context $context)
59 {
60 $expecteds = $innerErrors = [];
61 foreach ($this->set as $item) {
62 if ($item instanceof Schema) {
63 $dolly = new Context();
64 $dolly->path = $context->path;
65 $res = $item->complete($item->normalize($value, $dolly), $dolly);
66 if (!$dolly->errors) {
67 $context->warnings = \array_merge($context->warnings, $dolly->warnings);
68 return $this->doFinalize($res, $context);
69 }
70 foreach ($dolly->errors as $error) {
71 if ($error->path !== $context->path || empty($error->variables['expected'])) {
72 $innerErrors[] = $error;
73 } else {
74 $expecteds[] = $error->variables['expected'];
75 }
76 }
77 } else {
78 if ($item === $value) {
79 return $this->doFinalize($value, $context);
80 }
81 $expecteds[] = \Packetery\Nette\Schema\Helpers::formatValue($item);
82 }
83 }
84 if ($innerErrors) {
85 $context->errors = \array_merge($context->errors, $innerErrors);
86 } else {
87 $context->addError('The %label% %path% expects to be %expected%, %value% given.', \Packetery\Nette\Schema\Message::TYPE_MISMATCH, ['value' => $value, 'expected' => \implode('|', \array_unique($expecteds))]);
88 }
89 }
90 public function completeDefault(Context $context)
91 {
92 if ($this->required) {
93 $context->addError('The mandatory item %path% is missing.', \Packetery\Nette\Schema\Message::MISSING_ITEM);
94 return null;
95 }
96 if ($this->default instanceof Schema) {
97 return $this->default->completeDefault($context);
98 }
99 return $this->default;
100 }
101 }
102