-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
OptionConfigurator.php
146 lines (125 loc) · 3.49 KB
/
OptionConfigurator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\OptionsResolver;
use Symfony\Component\OptionsResolver\Exception\AccessException;
final class OptionConfigurator
{
public function __construct(
private string $name,
private OptionsResolver $resolver,
) {
$this->resolver->setDefined($name);
}
/**
* Adds allowed types for this option.
*
* @return $this
*
* @throws AccessException If called from a lazy option or normalizer
*/
public function allowedTypes(string ...$types): static
{
$this->resolver->setAllowedTypes($this->name, $types);
return $this;
}
/**
* Sets allowed values for this option.
*
* @param mixed ...$values One or more acceptable values/closures
*
* @return $this
*
* @throws AccessException If called from a lazy option or normalizer
*/
public function allowedValues(mixed ...$values): static
{
$this->resolver->setAllowedValues($this->name, $values);
return $this;
}
/**
* Sets the default value for this option.
*
* @return $this
*
* @throws AccessException If called from a lazy option or normalizer
*/
public function default(mixed $value): static
{
$this->resolver->setDefault($this->name, $value);
return $this;
}
/**
* Defines an option configurator with the given name.
*/
public function define(string $option): self
{
return $this->resolver->define($option);
}
/**
* Marks this option as deprecated.
*
* @param string $package The name of the composer package that is triggering the deprecation
* @param string $version The version of the package that introduced the deprecation
* @param string|\Closure $message The deprecation message to use
*
* @return $this
*/
public function deprecated(string $package, string $version, string|\Closure $message = 'The option "%name%" is deprecated.'): static
{
$this->resolver->setDeprecated($this->name, $package, $version, $message);
return $this;
}
/**
* Sets the normalizer for this option.
*
* @return $this
*
* @throws AccessException If called from a lazy option or normalizer
*/
public function normalize(\Closure $normalizer): static
{
$this->resolver->setNormalizer($this->name, $normalizer);
return $this;
}
/**
* Marks this option as required.
*
* @return $this
*
* @throws AccessException If called from a lazy option or normalizer
*/
public function required(): static
{
$this->resolver->setRequired($this->name);
return $this;
}
/**
* Sets an info message for an option.
*
* @return $this
*
* @throws AccessException If called from a lazy option or normalizer
*/
public function info(string $info): static
{
$this->resolver->setInfo($this->name, $info);
return $this;
}
/**
* Sets whether ignore undefined options.
*
* @return $this
*/
public function ignoreUndefined(bool $ignore = true): static
{
$this->resolver->setIgnoreUndefined($ignore);
return $this;
}
}