-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlbumList.php
70 lines (55 loc) · 1.56 KB
/
AlbumList.php
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
<?php
namespace P2;
class AlbumList
{
# Properties
private $albums;
# Methods
public function __construct($json)
{
# Load chart data
$chartJson = file_get_contents('charts.json');
$this->albums = json_decode($chartJson, true);
}
public function getAll()
{
return $this->albums;
}
public function getByYear(Int $year)
{
$results = [];
# Filter album list according to year
foreach ($this->albums as $key => $album) {
$match = $year == $album['year'];
if ($match) {
$results[$album['position'] . '-' . $album['title']] = $album;
}
}
return $results;
}
public function getByChart(String $chart)
{
$results = [];
# Filter album list according to Billboard chart
foreach ($this->albums as $key => $album) {
$match = $chart == $album['chart'];
if ($match) {
$results[$album['position'] . '-' . $album['title']] = $album;
}
}
return $results;
}
public function getByYearAndChart(Int $year, String $chart)
{
$results = [];
$yearAlbums = $this->getByYear($year);
$chartAlbums = $this->getByChart($chart);
$mergedAlbums = array_intersect_key($yearAlbums, $chartAlbums);
// Sort by Position
foreach ($mergedAlbums as $key => $album) {
$results[$album['position']] = $mergedAlbums[$key];
}
ksort($results);
return $results;
}
}