-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgramScraper.php
More file actions
95 lines (87 loc) · 2.95 KB
/
ProgramScraper.php
File metadata and controls
95 lines (87 loc) · 2.95 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
declare(strict_types=1);
namespace BOA\Programs;
use Carbon\CarbonImmutable as Carbon;
use Carbon\CarbonInterface;
/**
* @author shimomo
*/
final class ProgramScraper
{
/**
* @param \BOA\Programs\ScraperInterface $scraper
*/
public function __construct(private readonly ScraperInterface $scraper)
{
//
}
/**
* @phpstan-type ScrapedRace array<non-empty-string, array<int, string|float|int>|string|int>
* @phpstan-type ScrapedRaces array<int, ScrapedRace>
* @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 \Carbon\CarbonInterface|string $date
* @return array<int, NormalizedRaces>
*/
public function scrape(CarbonInterface|string $date = 'today'): array
{
$date = Carbon::parse($date, 'Asia/Tokyo');
/** @var array<int, ScrapedRaces> $programs */
$programs = $this->scraper->scrapePrograms($date);
return $this->normalize($programs);
}
/**
* @param array<int, ScrapedRaces> $programs
* @return array<int, NormalizedRaces>
*/
private function normalize(array $programs): array
{
$newPrograms = [];
foreach (array_values($programs) as $data) {
foreach (array_values($data) as $program) {
$program['boats'] = isset($program['boats'])
? array_values((array) $program['boats'])
: [];
$newPrograms[] = $program;
}
}
/** @var array<int, NormalizedRaces> */
return $newPrograms;
}
}