roast-api 1.4.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/RaaS.php DELETED
@@ -1,133 +0,0 @@
1
- <?php
2
-
3
- namespace Maizied\RoastApi;
4
-
5
- /**
6
- * RaaS — Roast as a Service PHP Client
7
- *
8
- * A lightweight PHP client for fetching developer roasts from the
9
- * RaaS static JSON API hosted on GitHub Pages.
10
- *
11
- * @package maizied/roast-api
12
- * @author Maizied <mdshuvo40@gmail.com>
13
- * @license MIT
14
- * @link https://maijied.github.io/roast-as-a-service/
15
- */
16
- class RaaS
17
- {
18
- private const BASE_URL = 'https://maijied.github.io/roast-as-a-service/api';
19
-
20
- /** @var array|null In-memory manifest cache */
21
- private static ?array $manifestCache = null;
22
-
23
- /** @var array<string, array> In-memory shard cache */
24
- private static array $shardCache = [];
25
-
26
- /**
27
- * Fetch the API manifest.
28
- *
29
- * @return array Decoded manifest data
30
- * @throws \RuntimeException If the fetch fails
31
- */
32
- public static function getManifest(): array
33
- {
34
- if (self::$manifestCache !== null) {
35
- return self::$manifestCache;
36
- }
37
-
38
- $url = self::BASE_URL . '/manifest.json';
39
- $json = @file_get_contents($url);
40
-
41
- if ($json === false) {
42
- throw new \RuntimeException("Failed to fetch manifest from {$url}");
43
- }
44
-
45
- self::$manifestCache = json_decode($json, true);
46
- return self::$manifestCache;
47
- }
48
-
49
- /**
50
- * Fetch a specific shard.
51
- *
52
- * @param string $lang Language code (e.g., 'en', 'bn')
53
- * @param int $shard Shard number (default: 1)
54
- * @return array Decoded shard data
55
- * @throws \RuntimeException If the fetch fails
56
- */
57
- public static function getShard(string $lang = 'en', int $shard = 1): array
58
- {
59
- $key = "{$lang}-{$shard}";
60
-
61
- if (isset(self::$shardCache[$key])) {
62
- return self::$shardCache[$key];
63
- }
64
-
65
- $url = self::BASE_URL . "/{$lang}/roasts-{$lang}-{$shard}.json";
66
- $json = @file_get_contents($url);
67
-
68
- if ($json === false) {
69
- throw new \RuntimeException("Failed to fetch shard from {$url}");
70
- }
71
-
72
- self::$shardCache[$key] = json_decode($json, true);
73
- return self::$shardCache[$key];
74
- }
75
-
76
- /**
77
- * Get a random roast.
78
- *
79
- * @param array $options {
80
- * @type string $lang Language code ('en' or 'bn'). Default: 'en'.
81
- * @type int $intensity Filter by intensity (1–3). Default: null (any).
82
- * @type int $maxLength Maximum text length. Default: null (any).
83
- * }
84
- * @return array Roast object with keys: id, text, intensity, length
85
- * @throws \RuntimeException If fetch fails or no roasts match filters
86
- */
87
- public static function getRandomRoast(array $options = []): array
88
- {
89
- $lang = $options['lang'] ?? 'en';
90
- $intensity = $options['intensity'] ?? null;
91
- $maxLength = $options['maxLength'] ?? null;
92
-
93
- $shard = self::getShard($lang);
94
- $roasts = $shard['roasts'] ?? [];
95
-
96
- // Apply filters
97
- if ($intensity !== null) {
98
- $roasts = array_filter($roasts, fn($r) => ($r['intensity'] ?? 0) === $intensity);
99
- }
100
- if ($maxLength !== null) {
101
- $roasts = array_filter($roasts, fn($r) => ($r['length'] ?? 0) <= $maxLength);
102
- }
103
-
104
- $roasts = array_values($roasts);
105
-
106
- if (empty($roasts)) {
107
- throw new \RuntimeException('No roasts match the given filters.');
108
- }
109
-
110
- return $roasts[array_rand($roasts)];
111
- }
112
-
113
- /**
114
- * Get all roasts for a language.
115
- *
116
- * @param string $lang Language code. Default: 'en'.
117
- * @return array List of roast objects
118
- */
119
- public static function getAllRoasts(string $lang = 'en'): array
120
- {
121
- $shard = self::getShard($lang);
122
- return $shard['roasts'] ?? [];
123
- }
124
-
125
- /**
126
- * Clear the in-memory cache.
127
- */
128
- public static function clearCache(): void
129
- {
130
- self::$manifestCache = null;
131
- self::$shardCache = [];
132
- }
133
- }