Files
visit.waterschurch.org/public/api/lib/Campuses.php
T

61 lines
1.8 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
/**
* Reads the shared campus configuration (config/campuses.json). The build
* copies it to api/config/campuses.json; in local development we fall back to
* the repo copy.
*/
final class Campuses
{
/** @var array<string,array<string,mixed>>|null */
private static ?array $byId = null;
/** @return array<string,array<string,mixed>> */
public static function all(): array
{
if (self::$byId !== null) {
return self::$byId;
}
$candidates = [
dirname(__DIR__) . '/config/campuses.json',
dirname(__DIR__, 3) . '/config/campuses.json',
];
foreach ($candidates as $file) {
if (is_readable($file)) {
$data = json_decode((string)file_get_contents($file), true);
if (is_array($data) && isset($data['campuses'])) {
$out = [];
foreach ($data['campuses'] as $c) {
$out[$c['id']] = $c;
}
return self::$byId = $out;
}
}
}
throw new RuntimeException('campuses.json not found');
}
/** @return array<string,mixed>|null */
public static function get(string $id): ?array
{
return self::all()[$id] ?? null;
}
/** @return string[] */
public static function openIds(): array
{
return array_keys(array_filter(self::all(), static fn($c) => ($c['status'] ?? 'open') === 'open'));
}
/** "Sunday 9:30 AM" style summary of the first service day. */
public static function timesSummary(array $campus): string
{
$parts = [];
foreach ($campus['serviceTimes'] ?? [] as $st) {
$parts[] = $st['day'] . ' ' . implode(', ', $st['times']);
}
return implode(' · ', $parts);
}
}