1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
namespace Enjoys\Dotenv; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
use Webmozart\Assert\Assert; |
10
|
|
|
|
11
|
|
|
final class ValuesHandler |
12
|
|
|
{ |
13
|
13 |
|
public static function quotes(string $value): string |
14
|
|
|
{ |
15
|
13 |
|
return preg_replace('/^\"(.+)\"$/', '\\1', $value); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param string $value |
20
|
|
|
* @return bool|float|int|string|null |
21
|
|
|
*/ |
22
|
13 |
|
public static function cast(string $value) |
23
|
|
|
{ |
24
|
13 |
|
preg_match('/^\*(\w+)[\s+]?(.+)?/', $value, $match); |
25
|
|
|
|
26
|
13 |
|
$v = $match[2] ?? ''; |
27
|
|
|
|
28
|
13 |
|
switch ($match[1] ?? '') { |
29
|
13 |
|
case 'true': |
30
|
1 |
|
return true; |
31
|
13 |
|
case 'false': |
32
|
1 |
|
return false; |
33
|
13 |
|
case 'null': |
34
|
1 |
|
return null; |
35
|
13 |
|
case 'int': |
36
|
2 |
|
Assert::notEmpty($v, 'Invalid parameter for *int type, the value must not be empty'); |
37
|
1 |
|
return (int)$v; |
38
|
12 |
|
case 'int8': |
39
|
2 |
|
Assert::notEmpty($v, 'Invalid parameter for *int type, the value must not be empty'); |
40
|
1 |
|
return octdec($v); |
41
|
11 |
|
case 'int16': |
42
|
2 |
|
Assert::notEmpty($v, 'Invalid parameter for *int type, the value must not be empty'); |
43
|
1 |
|
return hexdec($v); |
44
|
10 |
|
case 'float': |
45
|
2 |
|
Assert::notEmpty($v, 'Invalid parameter for *float type, the value must not be empty'); |
46
|
1 |
|
return (float)str_replace(',', '.', $v); |
47
|
9 |
|
case 'string': |
48
|
2 |
|
Assert::notEmpty($v, 'Invalid parameter for *string type, the value must not be empty'); |
49
|
1 |
|
return $v; |
50
|
|
|
default: |
51
|
8 |
|
return $value; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
13 |
|
public static function handleVariables(string $key, string $value, Dotenv $dotenv): string |
56
|
|
|
{ |
57
|
13 |
|
$result = preg_replace_callback( |
58
|
13 |
|
'/(\${(.+?)})/', |
59
|
13 |
|
function (array $matches) use ($dotenv){ |
60
|
|
|
/** @var string[] $matches */ |
61
|
2 |
|
return $dotenv->getEnvArray()[$matches[2]] ?? ''; |
62
|
13 |
|
}, |
63
|
|
|
$value |
64
|
|
|
); |
65
|
13 |
|
$dotenv->setEnvArrayOne($key, $result); |
66
|
13 |
|
return $result; |
67
|
|
|
} |
68
|
|
|
} |