| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Core\Routing; |
| 6 |
|
| 7 |
use Yatra\Services\SettingsService; |
| 8 |
|
| 9 |
/** |
| 10 |
* Route Matcher for determining which page handler should handle a request |
| 11 |
* |
| 12 |
* Centralizes route matching logic that was scattered across handler methods |
| 13 |
*/ |
| 14 |
class RouteMatcher |
| 15 |
{ |
| 16 |
/** |
| 17 |
* Match account page route |
| 18 |
* |
| 19 |
* @param string $path Request path |
| 20 |
* @return array|null Route data or null if no match |
| 21 |
*/ |
| 22 |
public static function matchAccountPage(string $path): ?array |
| 23 |
{ |
| 24 |
$account_base = SettingsService::getAccountBase(); |
| 25 |
$path_trim = rtrim($path, '/'); |
| 26 |
$base_trim = rtrim($account_base, '/'); |
| 27 |
|
| 28 |
if ($path_trim === $base_trim) { |
| 29 |
$tab = isset($_GET['tab']) ? sanitize_key((string) $_GET['tab']) : ''; |
| 30 |
$allowed = ['dashboard', 'bookings', 'payments', 'documents', 'profile', 'saved-trips']; |
| 31 |
$page = ($tab !== '' && in_array($tab, $allowed, true)) ? $tab : 'dashboard'; |
| 32 |
|
| 33 |
return [ |
| 34 |
'type' => 'account', |
| 35 |
'page' => $page, |
| 36 |
'base' => $account_base, |
| 37 |
]; |
| 38 |
} |
| 39 |
|
| 40 |
$slug = UrlParser::extractSlugFromPath($path, $account_base); |
| 41 |
if (!$slug) { |
| 42 |
return null; |
| 43 |
} |
| 44 |
|
| 45 |
$slug = \Yatra\Helpers\SlugHelper::generate($slug); |
| 46 |
$map = [ |
| 47 |
'dashboard' => 'dashboard', |
| 48 |
'profile' => 'profile', |
| 49 |
'bookings' => 'bookings', |
| 50 |
'payments' => 'payments', |
| 51 |
'documents' => 'documents', |
| 52 |
'support' => 'dashboard', |
| 53 |
'saved-trips' => 'saved-trips', |
| 54 |
'wishlist' => 'saved-trips', |
| 55 |
'settings' => 'profile', |
| 56 |
]; |
| 57 |
$page = $map[$slug] ?? null; |
| 58 |
if ($page === null) { |
| 59 |
return null; |
| 60 |
} |
| 61 |
|
| 62 |
return [ |
| 63 |
'type' => 'account', |
| 64 |
'page' => $page, |
| 65 |
'base' => $account_base, |
| 66 |
]; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Match trip single page route |
| 71 |
* |
| 72 |
* @param string $path Request path |
| 73 |
* @return array|null Route data or null if no match |
| 74 |
*/ |
| 75 |
public static function matchTripPage(string $path): ?array |
| 76 |
{ |
| 77 |
$trip_base = SettingsService::getTripBase(); |
| 78 |
$slug = UrlParser::extractSlugFromPath($path, $trip_base); |
| 79 |
|
| 80 |
if ($slug) { |
| 81 |
return [ |
| 82 |
'type' => 'trip', |
| 83 |
'slug' => $slug, |
| 84 |
'base' => $trip_base |
| 85 |
]; |
| 86 |
} |
| 87 |
|
| 88 |
return null; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Match listing page route |
| 93 |
* |
| 94 |
* @param string $path Request path |
| 95 |
* @return array|null Route data or null if no match |
| 96 |
*/ |
| 97 |
public static function matchListingPage(string $path): ?array |
| 98 |
{ |
| 99 |
$bases = [ |
| 100 |
'destination' => SettingsService::getDestinationBase(), |
| 101 |
'activity' => SettingsService::getActivityBase(), |
| 102 |
'category' => SettingsService::getTripCategoryBase(), |
| 103 |
]; |
| 104 |
|
| 105 |
foreach ($bases as $type => $base) { |
| 106 |
if ($path === $base) { |
| 107 |
return [ |
| 108 |
'type' => 'listing', |
| 109 |
'listing_type' => $type, |
| 110 |
'base' => $base |
| 111 |
]; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
return null; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Match taxonomy single page route |
| 120 |
* |
| 121 |
* @param string $path Request path |
| 122 |
* @return array|null Route data or null if no match |
| 123 |
*/ |
| 124 |
public static function matchTaxonomyPage(string $path): ?array |
| 125 |
{ |
| 126 |
$bases = [ |
| 127 |
'destination' => SettingsService::getDestinationBase(), |
| 128 |
'activity' => SettingsService::getActivityBase(), |
| 129 |
'category' => SettingsService::getTripCategoryBase(), |
| 130 |
]; |
| 131 |
|
| 132 |
foreach ($bases as $type => $base) { |
| 133 |
$slug = UrlParser::extractSlugFromPath($path, $base); |
| 134 |
if ($slug) { |
| 135 |
return [ |
| 136 |
'type' => 'taxonomy', |
| 137 |
'taxonomy_type' => $type, |
| 138 |
'slug' => $slug, |
| 139 |
'base' => $base |
| 140 |
]; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
return null; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Match booking page route |
| 149 |
* |
| 150 |
* @param string $path Request path |
| 151 |
* @return array|null Route data or null if no match |
| 152 |
*/ |
| 153 |
public static function matchBookingPage(string $path): ?array |
| 154 |
{ |
| 155 |
$booking_base = SettingsService::getBookingBase(); |
| 156 |
|
| 157 |
if ($path === $booking_base && !SettingsService::useCustomBookingPage()) { |
| 158 |
return [ |
| 159 |
'type' => 'booking', |
| 160 |
'page' => 'main', |
| 161 |
'base' => $booking_base |
| 162 |
]; |
| 163 |
} |
| 164 |
|
| 165 |
return null; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Match booking confirmation page route |
| 170 |
* |
| 171 |
* @param string $path Request path |
| 172 |
* @return array|null Route data or null if no match |
| 173 |
*/ |
| 174 |
public static function matchBookingConfirmationPage(string $path): ?array |
| 175 |
{ |
| 176 |
$booking_base = trim((string) SettingsService::getBookingBase(), '/'); |
| 177 |
if ($booking_base === '') { |
| 178 |
$booking_base = 'book'; |
| 179 |
} |
| 180 |
|
| 181 |
$token = UrlParser::extractTokenFromPath( |
| 182 |
$path, |
| 183 |
'/^' . preg_quote($booking_base, '/') . '\/confirmation\/([a-zA-Z0-9_-]+)$/' |
| 184 |
); |
| 185 |
|
| 186 |
if ($token) { |
| 187 |
return [ |
| 188 |
'type' => 'booking_confirmation', |
| 189 |
'confirmation_id' => $token, |
| 190 |
]; |
| 191 |
} |
| 192 |
|
| 193 |
$token = UrlParser::extractTokenFromPath($path, '/^booking-confirmation\/([a-zA-Z0-9_-]+)$/'); |
| 194 |
|
| 195 |
if ($token) { |
| 196 |
return [ |
| 197 |
'type' => 'booking_confirmation', |
| 198 |
'confirmation_id' => $token, |
| 199 |
]; |
| 200 |
} |
| 201 |
|
| 202 |
$booking_id = $_GET['booking_id'] ?? null; |
| 203 |
if ($booking_id && (strpos($path, $booking_base . '/confirmation') !== false || strpos($path, 'booking-confirmation') !== false)) { |
| 204 |
return [ |
| 205 |
'type' => 'booking_confirmation', |
| 206 |
'confirmation_id' => (string) $booking_id, |
| 207 |
]; |
| 208 |
} |
| 209 |
|
| 210 |
return null; |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Match checkout page route |
| 215 |
* |
| 216 |
* @param string $path Request path |
| 217 |
* @return array|null Route data or null if no match |
| 218 |
*/ |
| 219 |
public static function matchCheckoutPage(string $path): ?array |
| 220 |
{ |
| 221 |
$token = UrlParser::extractTokenFromPath($path, '/^checkout\/([a-zA-Z0-9_-]+)$/'); |
| 222 |
|
| 223 |
if ($token) { |
| 224 |
return [ |
| 225 |
'type' => 'checkout', |
| 226 |
'token' => $token |
| 227 |
]; |
| 228 |
} |
| 229 |
|
| 230 |
return null; |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Match email verification route |
| 235 |
* |
| 236 |
* @param string $path Request path |
| 237 |
* @return array|null Route data or null if no match |
| 238 |
*/ |
| 239 |
public static function matchEmailVerification(string $path): ?array |
| 240 |
{ |
| 241 |
$token = UrlParser::extractTokenFromPath($path, '/^yatra-verify-email\/([a-zA-Z0-9_-]+)$/'); |
| 242 |
|
| 243 |
if ($token) { |
| 244 |
return [ |
| 245 |
'type' => 'email_verification', |
| 246 |
'token' => $token |
| 247 |
]; |
| 248 |
} |
| 249 |
|
| 250 |
return null; |
| 251 |
} |
| 252 |
} |
| 253 |
|