forked from nbgrp/env-bundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayCastEnvVarProcessor.php
More file actions
149 lines (129 loc) · 5.2 KB
/
ArrayCastEnvVarProcessor.php
File metadata and controls
149 lines (129 loc) · 5.2 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
<?php
// SPDX-License-Identifier: BSD-3-Clause
declare(strict_types=1);
namespace Nbgrp\EnvBundle;
use Symfony\Component\DependencyInjection\EnvVarProcessorInterface;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
final class ArrayCastEnvVarProcessor implements EnvVarProcessorInterface
{
#[\Override]
public static function getProvidedTypes(): array
{
return [
'bool-array' => 'array',
'int-array' => 'array',
'float-array' => 'array',
'string-array' => 'array',
'base64-array' => 'array',
'base64url-array' => 'array',
];
}
/**
* @return array<array-key, scalar>
*
* @psalm-suppress MixedReturnTypeCoercion
*/
#[\Override]
public function getEnv(string $prefix, string $name, \Closure $getEnv): array
{
$env = (array) $getEnv($name);
return match ($prefix) {
'bool-array' => array_map(self::getBooleanMapper(), $env),
'int-array' => array_map(self::getIntegerMapper($name), $env),
'float-array' => array_map(self::getFloatMapper($name), $env),
'base64-array' => array_map(self::getBase64Mapper($name), $env),
'base64url-array' => array_map(self::getBase64UrlMapper($name), $env),
/* string-array */ default => array_map(self::getStringMapper($name), $env),
};
}
/**
* @psalm-pure
*/
private static function getBooleanMapper(): callable
{
/** @psalm-suppress RiskyTruthyFalsyComparison */
return static fn (mixed $value): bool => (bool) (filter_var($value, \FILTER_VALIDATE_BOOLEAN, ['flags' => \FILTER_NULL_ON_FAILURE]) ?? filter_var($value, \FILTER_VALIDATE_INT) ?: filter_var($value, \FILTER_VALIDATE_FLOAT));
}
/**
* @psalm-pure
*/
private static function getIntegerMapper(string $name): callable
{
return static function (mixed $value) use ($name): int {
/** @psalm-suppress RiskyTruthyFalsyComparison */
if ((filter_var($value, \FILTER_VALIDATE_INT) ?: filter_var($value, \FILTER_VALIDATE_FLOAT)) === false) {
throw new RuntimeException('Non-numeric member of environment variable "'.$name.'" cannot be cast to int.');
}
return (int) $value;
};
}
/**
* @psalm-pure
*/
private static function getFloatMapper(string $name): callable
{
return static function (mixed $value) use ($name): float {
if (filter_var($value, \FILTER_VALIDATE_FLOAT) === false) {
throw new RuntimeException('Non-numeric member of environment variable "'.$name.'" cannot be cast to float.');
}
return (float) $value;
};
}
/**
* @psalm-pure
*/
private static function getBase64Mapper(string $name): callable
{
return static function (mixed $value) use ($name): string {
if (!\is_string($value)) {
throw new RuntimeException('Environment variable "'.$name.'" must be a valid base64 string.');
}
if (\function_exists('sodium_base642bin')) {
try {
return \strlen($value) % 4 === 0
? sodium_base642bin($value, \SODIUM_BASE64_VARIANT_ORIGINAL)
: sodium_base642bin($value, \SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING);
} catch (\SodiumException) {
throw new RuntimeException('Environment variable "'.$name.'" must be a valid base64 string.');
}
}
$decoded = base64_decode(str_pad($value, \strlen($value) % 4, '='), true);
if ($decoded === false) {
throw new RuntimeException('Environment variable "'.$name.'" must be a valid base64 string.');
}
return $decoded;
};
}
/**
* @psalm-pure
*/
private static function getBase64UrlMapper(string $name): callable
{
return static function (mixed $value) use ($name): string {
if (!\is_string($value)) {
throw new RuntimeException('Environment variable "'.$name.'" must be a valid base64url string.');
}
if (\function_exists('sodium_base642bin')) {
try {
return \strlen($value) % 4 === 0
? sodium_base642bin($value, \SODIUM_BASE64_VARIANT_URLSAFE)
: sodium_base642bin($value, \SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING);
} catch (\SodiumException) {
throw new RuntimeException('Environment variable "'.$name.'" must be a valid base64url string.');
}
}
$decoded = base64_decode(str_pad(strtr($value, '-_', '+/'), \strlen($value) % 4, '='), true);
if ($decoded === false) {
throw new RuntimeException('Environment variable "'.$name.'" must be a valid base64url string.');
}
return $decoded;
};
}
/**
* @psalm-pure
*/
private static function getStringMapper(string $name): callable
{
return static fn (mixed $value): string => (string) $value;
}
}