-
Notifications
You must be signed in to change notification settings - Fork 0
/
Config.php
87 lines (67 loc) · 2.46 KB
/
Config.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
<?php
declare(strict_types=1);
namespace Snicco\Component\Kernel\Configuration;
use Webmozart\Assert\Assert;
abstract class Config
{
/**
* @param mixed $default
*
* @return mixed
*/
abstract public function get(string $key, $default = null);
final public function getString(string $key, ?string $default = null): string
{
$val = $this->get($key, $default);
Assert::string($val, "Expected a string for config key [{$key}].\nGot: [%s]");
return $val;
}
final public function getStringOrNull(string $key, ?string $default = null): ?string
{
$val = $this->get($key, $default);
Assert::nullOrstring($val, "Expected a string or null for config key [{$key}].\nGot: [%s]");
return $val;
}
final public function getInteger(string $key, ?int $default = null): int
{
$val = $this->get($key, $default);
Assert::integer($val, "Expected an integer for config key [{$key}].\nGot: [%s]");
return $val;
}
final public function getIntegerOrNull(string $key, ?int $default = null): ?int
{
$val = $this->get($key, $default);
Assert::nullOrInteger($val, "Expected an integer or null for config key [{$key}].\nGot: [%s]");
return $val;
}
final public function getBoolean(string $key, ?bool $default = null): bool
{
$val = $this->get($key, $default);
Assert::boolean($val, "Expected a boolean for config key [{$key}].\nGot: [%s]");
return $val;
}
final public function getBooleanOrNull(string $key, ?bool $default = null): ?bool
{
$val = $this->get($key, $default);
Assert::nullOrBoolean($val, "Expected a boolean or null for config key [{$key}].\nGot: [%s]");
return $val;
}
/**
* @param list<string> $default
*
* @return list<string>
*/
final public function getListOfStrings(string $key, ?array $default = null): array
{
$val = $this->get($key, $default);
Assert::isList($val, sprintf('Config value for key [%s] is not a list of strings.', $key));
Assert::allString($val, "Config value for key [{$key}] is not a list of strings.\nGot: [%s].");
return $val;
}
final public function getArray(string $key, ?array $default = null): array
{
$val = $this->get($key, $default);
Assert::isArray($val, "Expected an array for config key [{$key}].\nGot: [%s]");
return $val;
}
}