|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace CodeZero\LocalizedRoutes; |
| 4 | + |
| 5 | +use CodeZero\LocalizedRoutes\Facades\LocaleConfig; |
| 6 | +use Illuminate\Http\Request; |
| 7 | +use Illuminate\Support\Collection; |
| 8 | +use Illuminate\Support\Facades\App; |
| 9 | +use Illuminate\Support\Facades\Route; |
| 10 | + |
| 11 | +class RouteHelper |
| 12 | +{ |
| 13 | + /** |
| 14 | + * The current Route. |
| 15 | + * |
| 16 | + * @var \Illuminate\Routing\Route |
| 17 | + */ |
| 18 | + protected $route; |
| 19 | + |
| 20 | + /** |
| 21 | + * Create a new RouteHelper instance. |
| 22 | + * |
| 23 | + * @param \Illuminate\Http\Request $request |
| 24 | + */ |
| 25 | + public function __construct(Request $request) |
| 26 | + { |
| 27 | + $this->route = $request->route(); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Check if the current route is a fallback route. |
| 32 | + * |
| 33 | + * @return bool |
| 34 | + */ |
| 35 | + public function isFallback(): bool |
| 36 | + { |
| 37 | + return $this->route && $this->route->isFallback; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Check if the current route is localized. |
| 42 | + * |
| 43 | + * @param string|array $patterns |
| 44 | + * @param string|array $locales |
| 45 | + * |
| 46 | + * @return bool |
| 47 | + */ |
| 48 | + public function isLocalized($patterns = null, $locales = '*'): bool |
| 49 | + { |
| 50 | + return $patterns === null |
| 51 | + ? $this->isCurrentRouteLocalized() |
| 52 | + : $this->isCurrentRouteLocalizedWithNamePattern($patterns, $locales); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Check if a localized route exists. |
| 57 | + * |
| 58 | + * @param string $name |
| 59 | + * @param string|null $locale |
| 60 | + * |
| 61 | + * @return bool |
| 62 | + */ |
| 63 | + public function hasLocalized(string $name, ?string $locale = null): bool |
| 64 | + { |
| 65 | + $locale = $locale ?: App::getLocale(); |
| 66 | + |
| 67 | + return Route::has("{$locale}.{$name}"); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Check if the current route is localized. |
| 72 | + * |
| 73 | + * @return bool |
| 74 | + */ |
| 75 | + protected function isCurrentRouteLocalized(): bool |
| 76 | + { |
| 77 | + $routeAction = LocaleConfig::getRouteAction(); |
| 78 | + |
| 79 | + return $this->route && $this->route->getAction($routeAction) !== null; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Check if the current route is localized and has a specific name. |
| 84 | + * |
| 85 | + * @param string|array $patterns |
| 86 | + * @param string|array $locales |
| 87 | + * |
| 88 | + * @return bool |
| 89 | + */ |
| 90 | + protected function isCurrentRouteLocalizedWithNamePattern($patterns = null, $locales = '*'): bool |
| 91 | + { |
| 92 | + $locales = Collection::make($locales); |
| 93 | + $names = Collection::make(); |
| 94 | + |
| 95 | + Collection::make($patterns)->each(function ($name) use ($locales, $names) { |
| 96 | + $locales->each(function ($locale) use ($name, $names) { |
| 97 | + $names->push($locale . '.' . $name); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + return Route::is($names->all()); |
| 102 | + } |
| 103 | +} |
0 commit comments