|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Services; |
| 4 | + |
| 5 | +use Illuminate\Support\Facades\Http; |
| 6 | +use Illuminate\Support\Facades\Cache; |
| 7 | +use Illuminate\Support\Str; |
| 8 | + |
| 9 | +class FontService |
| 10 | +{ |
| 11 | + protected string $baseUrl = 'https://api.fontsource.org/v1'; |
| 12 | + |
| 13 | + public function getFonts(): array |
| 14 | + { |
| 15 | + return Cache::remember('fonts_list', 86400, function () { |
| 16 | + $response = Http::get("{$this->baseUrl}/fonts"); |
| 17 | + if ($response->failed()) { |
| 18 | + return []; |
| 19 | + } |
| 20 | + return $response->json(); |
| 21 | + }); |
| 22 | + } |
| 23 | + |
| 24 | + public function getFont(string $id): ?array |
| 25 | + { |
| 26 | + return Cache::remember("font_details_{$id}", 86400, function () use ($id) { |
| 27 | + $response = Http::get("{$this->baseUrl}/fonts/{$id}"); |
| 28 | + if ($response->failed()) { |
| 29 | + return null; |
| 30 | + } |
| 31 | + return $response->json(); |
| 32 | + }); |
| 33 | + } |
| 34 | + |
| 35 | + public function toRegistry(array $fontData): array |
| 36 | + { |
| 37 | + $id = $fontData['id']; |
| 38 | + $family = $fontData['family']; |
| 39 | + $importName = Str::studly($family); |
| 40 | + $variableName = "--font-{$id}"; |
| 41 | + $isVariable = $fontData['variable'] ?? false; |
| 42 | + $dependency = $isVariable ? "@fontsource-variable/{$id}" : "@fontsource/{$id}"; |
| 43 | + |
| 44 | + return [ |
| 45 | + 'name' => "font-{$id}", |
| 46 | + 'title' => $family, |
| 47 | + 'type' => 'registry:font', |
| 48 | + 'meta' => [ |
| 49 | + 'version' => '1.0.0', |
| 50 | + 'category' => 'fonts' |
| 51 | + ], |
| 52 | + 'author' => 'designbycode', |
| 53 | + 'font' => [ |
| 54 | + 'family' => "'{$family}', sans-serif", |
| 55 | + 'provider' => 'google', |
| 56 | + 'import' => $importName, |
| 57 | + 'variable' => $variableName, |
| 58 | + 'subsets' => $fontData['subsets'] ?? ['latin'], |
| 59 | + 'dependency' => $dependency |
| 60 | + ] |
| 61 | + ]; |
| 62 | + } |
| 63 | +} |
0 commit comments