ldc-groupe/app/Helpers/RouteHelper.php

145 lines
4.5 KiB
PHP

<?php
namespace App\Helpers;
use App\Enums\Contact\ContactFormType;
use App\Models\DynamicPage;
use Illuminate\Routing\Route;
use Uwil\LaravelCommon\Controllers\Controller;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
class RouteHelper extends Controller
{
/**
* Check if a route name exists and return its URL.
*
* This method verifies if the specified route name is registered in Laravel.
* If the route exists, it returns the corresponding URL using the route helper.
* Otherwise, it returns a "#" as a fallback.
*
* @param string $routeName The name of the route to check.
* @param string|null $locale The target locale to get the route.
* @return string The route URL if it exists, or "#" otherwise.
*/
public static function routeNameChecker(string $routeName, mixed $locale = null): string
{
if (is_null($locale)) {
$locale = app()->getLocale();
}
$definitiveRouteName = sprintf("$routeName.%s", $locale);
if (\Illuminate\Support\Facades\Route::has($definitiveRouteName)) {
return route($definitiveRouteName);
}
return "#";
}
/**
* Return true or false, if the passed route is actually active
*
* @param string $routeName
* @param mixed $locale
* @return boolean
*/
public static function isRouteActive(string $routeName, mixed $locale = null): bool
{
if (is_null($locale)) {
$locale = app()->getLocale();
}
$definitiveRouteName = sprintf("$routeName.%s", $locale);
return \request()->routeIs($definitiveRouteName);
}
/**
* Get the localized url in the langage switcher
*
* @param string $localeCode LocaleCode from LaravelLocalization::getSupportedLocales()
* @return string
*/
public static function getLocalizedUrl(string $localeCode): string
{
$route = request()->route();
if (! $route) {
return LaravelLocalization::getLocalizedURL($localeCode);
}
return self::resolve(
self::getDynamicPageLocalizedURL($route, $localeCode),
self::getContactShowLocalizedURL($route, $localeCode),
) ?? LaravelLocalization::getLocalizedURL($localeCode, null, [], false);
}
/**
* Resolve the url to be returned for the localized switcher
*
* @param string|null ...$urls
* @return string|null
*/
private static function resolve(?string ...$urls): ?string
{
foreach ($urls as $url) {
if ($url !== null) {
return $url;
}
}
return null;
}
/**
* Return the localized url for contact page show
*
* @param Route $route
* @param string $localeCode
* @return string|false|null
*/
private static function getContactShowLocalizedURL(Route $route, string $localeCode)
{
$contactPage = $route->parameter('contactPage');
if ($contactPage) {
$localizedUrl = LaravelLocalization::getLocalizedURL(
$localeCode,
null,
[],
false
);
$parsed = parse_url($localizedUrl);
if (! isset($parsed['path'])) {
return $localizedUrl;
}
$segments = explode('/', trim($parsed['path'], '/'));
$currentSlug = end($segments);
$enum = ContactFormType::fromSlug(
$currentSlug,
app()->getLocale()
);
if ($enum) {
$translatedSlug = $enum->slug($localeCode);
if ($translatedSlug) {
$segments[count($segments) - 1] = $translatedSlug;
return '/' . implode('/', $segments);
}
}
return $localizedUrl;
}
}
/**
* Return the localized url for all dynamic pages
*
* @param \Illuminate\Routing\Route $route
* @param string $localeCode
* @return string|false|null
*/
private static function getDynamicPageLocalizedURL(Route $route, string $localeCode)
{
/** @var DynamicPage|null $page */
$dynamicPage = $route->parameter('dynamicPage');
if ($dynamicPage instanceof DynamicPage) {
$path = $dynamicPage->getLocalizedPath($localeCode);
if ($path) {
return LaravelLocalization::getLocalizedURL($localeCode, "/{$path}", [], false);
}
}
}
}