-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgramStorage.php
More file actions
72 lines (67 loc) · 2.33 KB
/
ProgramStorage.php
File metadata and controls
72 lines (67 loc) · 2.33 KB
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
<?php
declare(strict_types=1);
namespace BOA\Programs;
/**
* @author shimomo
*/
final class ProgramStorage
{
/**
* @phpstan-type NormalizedBoat array{
* racer_boat_number: int,
* racer_name: string,
* racer_number: int,
* racer_class_number: int,
* racer_branch_number: int,
* racer_birthplace_number: int,
* racer_age: int,
* racer_weight: float|int,
* racer_flying_count: int,
* racer_late_count: int,
* racer_average_start_timing: float|int,
* racer_national_top_1_percent: float|int,
* racer_national_top_2_percent: float|int,
* racer_national_top_3_percent: float|int,
* racer_local_top_1_percent: float|int,
* racer_local_top_2_percent: float|int,
* racer_local_top_3_percent: float|int,
* racer_assigned_motor_number: int,
* racer_assigned_motor_top_2_percent: float|int,
* racer_assigned_motor_top_3_percent: float|int,
* racer_assigned_boat_number: int,
* racer_assigned_boat_top_2_percent: float|int,
* racer_assigned_boat_top_3_percent: float|int
* }
* @phpstan-type NormalizedRace array{
* race_date: string,
* race_stadium_number: int,
* race_number: int,
* race_closed_at: string,
* race_grade_number: int,
* race_title: string,
* race_subtitle: string,
* race_distance: int,
* boats: array<int, NormalizedBoat>
* }
* @phpstan-type NormalizedRaces array<int, NormalizedRace>
*
* @param array<int, NormalizedRaces> $programs
* @param non-empty-string $path
* @return void
* @throws \RuntimeException
*/
public function save(array $programs, string $path): void
{
$contents = json_encode(['programs' => $programs]);
if ($contents === false) {
throw new \RuntimeException("Failed to encode programs to JSON");
}
$dir = dirname($path);
if (!is_dir($dir) && !mkdir($dir, 0777, true) && !is_dir($dir)) {
throw new \RuntimeException("Failed to create directory: {$dir}");
}
if (file_put_contents($path, $contents) === false) {
throw new \RuntimeException("Failed to save programs to {$path}");
}
}
}