-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathParameterMetadata.php
More file actions
205 lines (180 loc) · 8.31 KB
/
Copy pathParameterMetadata.php
File metadata and controls
205 lines (180 loc) · 8.31 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
/*
* Copyright (c) 2024 Jan Böhmer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Jbtronics\SettingsBundle\Metadata;
use Jbtronics\SettingsBundle\ParameterTypes\ParameterTypeInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Contracts\Translation\TranslatableInterface;
/**
* This class represents contains the relevant information about a settings parameter.
*/
class ParameterMetadata
{
private readonly object|array|string|null $envVarMapper;
/**
* @param class-string $className The class name of the settings class, which contains this parameter.
* @param string $propertyName The name of the property, which is marked as a settings parameter.
* @param string $type The type of this configuration entry. This must be a class string of a service implementing ConfigEntryTypeInterface
* @phpstan-param class-string<ParameterTypeInterface> $type
* @param bool $nullable Whether the value of the property can be null.
* @param string|null $name The optional name of this configuration entry. If not set, the name of the property is used.
* @param string|TranslatableInterface|null $label A user friendly label for this configuration entry, which is shown in the UI.
* @param string|TranslatableInterface|null $description A small descrpiton for this configuration entry, which is shown in the UI.
* @param array $options An array of extra options, which are passed to the ConfigEntryTypeInterface implementation.
* @param string|null $formType The form type to use for this configuration entry. If not set, the form type is guessed from the parameter type.
* @phpstan-param class-string<AbstractType>|null $formType
* @param array $formOptions An array of extra options, which are passed to the form type. This will override the values from the parameterType
* @param string[] $groups The groups, which this parameter should belong to. Groups can be used to only render subsets of the configuration entries in the UI.
* @param string|null $envVar The name of the environment variable, which should be used to fill this parameter. If not set, the parameter is not filled by an environment variable.
* @param EnvVarMode $envVarMode The mode in which the environment variable should be used to fill the parameter. Defaults to EnvVarMode::INITIAL
* @param callable|string|null $envVarMapper A mapper, which is used to map the value from the environment variable to the parameter value. It can be either a ParameterTypeInterface service, or a callable, which takes the value from the environment variable as argument and returns the mapped value.
* @phpstan-param callable(mixed): mixed|class-string<ParameterTypeInterface>|null $envVarMapper
* @param bool $cloneable Whether this property should be cloned (a new instance is created), when the settings class is cloned. If false, the property instance is shared between the original and the cloned settings class. This does only affect object properties.
*/
public function __construct(
private readonly string $className,
private readonly string $propertyName,
private readonly string $type,
private readonly bool $nullable,
private readonly ?string $name = null,
private readonly string|TranslatableInterface|null $label = null,
private readonly string|TranslatableInterface|null $description = null,
private readonly array $options = [],
private readonly ?string $formType = null,
private readonly array $formOptions = [],
private readonly array $groups = [],
private readonly ?string $envVar = null,
private readonly EnvVarMode $envVarMode = EnvVarMode::INITIAL,
callable|string|null $envVarMapper = null,
private readonly bool $cloneable = true,
) {
$this->envVarMapper = $envVarMapper;
}
public function getClassName(): string
{
return $this->className;
}
public function getPropertyName(): string
{
return $this->propertyName;
}
public function getType(): string
{
return $this->type;
}
public function getName(): string
{
return $this->name ?? $this->propertyName;
}
public function getLabel(): TranslatableInterface|string
{
return $this->label ?? $this->getName();
}
public function getDescription(): TranslatableInterface|string|null
{
return $this->description;
}
public function getOptions(): array
{
return $this->options;
}
public function getFormType(): ?string
{
return $this->formType;
}
public function getFormOptions(): array
{
return $this->formOptions;
}
/**
* Checks whether the value of this parameter can be null.
* @return bool
*/
public function isNullable(): bool
{
return $this->nullable;
}
/**
* Returns the groups, which this parameter belongs to.
* @return string[]
*/
public function getGroups(): array
{
return $this->groups;
}
/**
* Returns the name of the environment variable, which should be used to fill this parameter.
* Null if no environment variable should be used to fill this parameter.
* @return string|null
*/
public function getEnvVar(): ?string
{
return $this->envVar;
}
/**
* If the environment variable is set, this method returns the base env var, which is the last part of the expression.
* So if the env var expression is bool:FOO_BAR, the base env var is FOO_BAR.
* If no environment variable is set, this method returns null.
* @return string|null
*/
public function getBaseEnvVar(): ?string
{
if ($this->envVar === null){
return null;
}
//If the expressions dont contain any colons, the base env var is the same as the expression
if (!str_contains($this->envVar, ':')) {
return $this->envVar;
}
//We assume that all env var expressions are using the prefix syntax, and therefore the base env var is the last part
return substr($this->envVar, strrpos($this->envVar, ':') + 1);
}
/**
* Returns the mode in which the environment variable should be used to fill the parameter.
* @return EnvVarMode
*/
public function getEnvVarMode(): EnvVarMode
{
return $this->envVarMode;
}
/**
* Returns the mapper, which is used to map the value from the environment variable to the parameter value.
* This can be a closure or the name of the ParameterType service, which should be used to map the value.
* Null if no mapping function is set.
* @return callable|string|null
* @phpstan-return callable(mixed): mixed|class-string<ParameterTypeInterface>|null
*/
public function getEnvVarMapper(): callable|string|null
{
return $this->envVarMapper;
}
/**
* Whether this property should be cloned (a new instance is created), when the settings class is cloned.
* If false, the property instance is shared between the original and the cloned settings class.
* This does only affect object properties.
* @return bool
*/
public function isCloneable(): bool
{
return $this->cloneable;
}
}