Environment::array()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 12
rs 9.9666
1
<?php
2
declare(strict_types=1);
3
4
namespace HonkLegion;
5
6
use function array_filter;
7
use function array_map;
8
use function explode;
9
use function rtrim;
10
use function strtolower;
11
use function strtoupper;
12
13
class Environment
14
{
15
	public const String = 'string';
16
	public const Bool = 'bool';
17
	public const Int = 'int';
18
	public const Float = 'float';
19
	protected const Delimiter = '|';
20
	protected static string $prefix = '';
21
22
	/** @deprecated use Environment::String instead */
23
	public const STRING = self::String;
24
	/** @deprecated use Environment::String instead */
25
	public const INT = self::Int;
26
	/** @deprecated use Environment::Int instead */
27
	public const INTEGER = self::Int;
28
	/** @deprecated use Environment::Int instead */
29
	public const FLOAT = self::Float;
30
	/** @deprecated use Environment::Float instead */
31
	public const BOOL = self::Bool;
32
	/** @deprecated use Environment::Bool instead */
33
	public const BOOLEAN = self::Bool;
34
35
	public function __construct(
36
		private string $name,
37
		private string $default = ''
38
	) {
39
	}
40
41
	/**
42
	 * @api
43
	 */
44
	public static function setPrefix(string $prefix = ''): void
45
	{
46
		self::$prefix = $prefix ? strtoupper(rtrim($prefix, '_')) . '_' : '';
47
	}
48
49
	/**
50
	 * @api
51
	 */
52
	public static function bool(string $name, bool $default = false): bool
53
	{
54
		return (bool)(self::castIt(self::env($name), self::Bool) ?? $default);
55
	}
56
57
	private static function castIt(?string $value, string $cast): null|string|bool|int|float
58
	{
59
		return ($value !== null) ? match ($cast) {
60
			self::Bool => !($value === 'false' || !$value),
61
			self::Int => (int)$value,
62
			self::Float => (float)$value,
63
			default => $value,
64
		} : null;
65
	}
66
67
	private static function env(string $name): ?string
68
	{
69
		$env = getenv(self::$prefix . $name);
70
		return (false === $env) ? null : $env;
71
	}
72
73
	/**
74
	 * @api
75
	 */
76
	public static function int(string $name, int $default = 0): int
77
	{
78
		return (int)(self::env($name) ?? $default);
79
	}
80
81
	/**
82
	 * @api
83
	 */
84
	public static function float(string $name, float $default = 0): float
85
	{
86
		return (float)(self::castIt(self::env($name), self::Float) ?? $default);
87
	}
88
89
	/**
90
	 * @api
91
	 */
92
	public function __toString(): string
93
	{
94
		return self::env($this->name) ?? $this->default;
95
	}
96
97
	/**
98
	 * @api
99
	 */
100
	public function get(string $cast = self::String): string|bool|int|float
101
	{
102
		return self::castIt(self::env($this->name) ?? $this->default, strtolower($cast)) ?? '';
103
	}
104
105
	/**
106
	 * @api
107
	 * @param string $cast
108
	 * @return bool[]|float[]|int[]|string[]
109
	 */
110
	public function toArray(string $cast = self::String): array
111
	{
112
		return self::array($this->name, $this->default ?: self::Delimiter, $cast);
113
	}
114
115
	/**
116
	 * @api
117
	 * @param string $name
118
	 * @param non-empty-string $separator
119
	 * @param string $cast
120
	 * @return string[]|int[]|float[]|bool[]
121
	 */
122
	public static function array(string $name, string $separator = self::Delimiter, string $cast = self::String): array
123
	{
124
		return
125
			array_filter(
126
				array_map(
127
					static fn($item) => self::castIt($item, $cast),
128
					array_filter(
129
						explode($separator, self::string($name)),
130
						static fn($item) => $item !== ''
131
					)
132
				),
133
				static fn($item) => $item !== null
134
			);
135
	}
136
137
138
	/**
139
	 * @api
140
	 */
141
	public static function string(string $name, string $default = ''): string
142
	{
143
		return self::env($name) ?? $default;
144
	}
145
}
146