add search support
[stouyapi.git] / bin / filters.php
1 <?php
2 function filterByAge($origGames, $age)
3 {
4     $filtered = [];
5     foreach ($origGames as $game) {
6         if ($age == $game->contentRating) {
7             $filtered[] = $game;
8         }
9     }
10     return $filtered;
11 }
12
13 function filterByGenre($origGames, $genre, $remove = false)
14 {
15     $filtered = [];
16     foreach ($origGames as $game) {
17         if ($remove) {
18             if (array_search($genre, $game->genres) === false) {
19                 $filtered[] = $game;
20             }
21         } else {
22             if (array_search($genre, $game->genres) !== false) {
23                 $filtered[] = $game;
24             }
25         }
26     }
27     return $filtered;
28 }
29
30 function filterByLetter($origGames, $letter)
31 {
32     $filtered = [];
33     foreach ($origGames as $game) {
34         $gameLetter = strtoupper($game->title{0});
35         if (!preg_match('#^[A-Z]$#', $gameLetter)) {
36             $gameLetter = 'Other';
37         }
38         if ($letter == $gameLetter) {
39             $filtered[] = $game;
40         }
41     }
42     return $filtered;
43 }
44
45 function filterByPackageNames($origGames, $packageNames)
46 {
47     $names = array_flip($packageNames);
48     $filtered = [];
49     foreach ($origGames as $game) {
50         if (isset($names[$game->packageName])) {
51             $filtered[] = $game;
52         }
53     }
54     return $filtered;
55 }
56
57 function filterByPlayers($origGames, $numOfPlayers)
58 {
59     $filtered = [];
60     foreach ($origGames as $game) {
61         if (array_search($numOfPlayers, $game->players) !== false) {
62             $filtered[] = $game;
63         }
64     }
65     return $filtered;
66 }
67
68 function filterBySearchWord($origGames, $searchWord)
69 {
70     $filtered = [];
71     foreach ($origGames as $game) {
72         if (stripos($game->title, $searchWord) !== false) {
73             $filtered[] = $game;
74         }
75     }
76     return $filtered;
77 }
78
79 function filterLastUpdated($origGames, $limit)
80 {
81     $games = array_values($origGames);
82     usort(
83         $games,
84         function ($gameA, $gameB) {
85             return strtotime($gameB->latestRelease->date) - strtotime($gameA->latestRelease->date);
86         }
87     );
88
89     return array_slice($games, 0, $limit);
90 }
91
92 function filterBestRated($origGames, $limit)
93 {
94     $games = array_values($origGames);
95     usort(
96         $games,
97         function ($gameA, $gameB) {
98             return ($gameB->rating->rank - $gameA->rating->rank) * 100;
99         }
100     );
101
102     return array_slice($games, 0, $limit);
103 }
104
105 function sortByTitle($games)
106 {
107     usort(
108         $games,
109         function ($gameA, $gameB) {
110             return strcasecmp($gameA->title, $gameB->title);
111         }
112     );
113     return $games;
114 }
115 ?>