woodpecker-email/src/Loader/EnvVarLoader.php
Simon Vieille 65f4049c28
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
refactor all by using PHP instead of Golang
2024-01-15 12:38:30 +01:00

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;
}
}