42 lines
1.2 KiB
PHP
42 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace Plugin\Loader;
|
|
|
|
class EnvVarLoader
|
|
{
|
|
public static function buildArray(array $map, array $defaultValues = []): array
|
|
{
|
|
$container = [];
|
|
|
|
foreach ($map as $key => $value) {
|
|
if (is_array($value)) {
|
|
$container[$key] = self::buildArray($value, $defaultValues);
|
|
} else {
|
|
$data = getenv($value);
|
|
|
|
if (false === $data) {
|
|
$data = $defaultValues[$value] ?? null;
|
|
}
|
|
|
|
if (str_ends_with($key, '_at') && ctype_digit($data)) {
|
|
$date = new \DateTime();
|
|
$date->setTimestamp((int) $data);
|
|
$data = $date;
|
|
} elseif (str_starts_with($key, 'is_')) {
|
|
if (in_array(strtolower($data), ['1', 'true', 'yes'])) {
|
|
$data = true;
|
|
} elseif (in_array(strtolower($data), ['0', 'false', 'no'])) {
|
|
$data = false;
|
|
} else {
|
|
$data = $defaultValues[$value] ?? false;
|
|
}
|
|
}
|
|
|
|
$container[$key] = $data;
|
|
}
|
|
}
|
|
|
|
return $container;
|
|
}
|
|
}
|