HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/farmfun/reserveren.farmfun.be/app/Http/Wildcards/_AvailabilityWildcard.php
<?php

namespace App\Http\Wildcards;

use App\Komma\Locations\Models\Location;
use App\Komma\Locations\Models\LocationTranslation;
use App\Komma\ProductCategories\Models\ProductCategoryTranslation;
use App\Komma\Products\Models\ProductTranslation;
use Illuminate\Http\Request;

class _AvailabilityWildcard implements WildcardInterface
{
    /**
     * @param Request $request
     * @param string $route
     * @param string $tail
     * @return Request
     */
    public function handle(Request $request, string $route, string $tail): Request
    {
        $tailParts = explode('/', $tail);

        // Check if the first segment is found in the ProductCategoryTranslation
        if (($productCategoryTranslation = ProductCategoryTranslation::where('slug', $tailParts[0])->first()) &&
            count($tailParts) == 1) {
            //Set the request URI and the original path
            $request->server->set('REQUEST_URI', 'availability/product-category/'.$productCategoryTranslation->product_category_id);

            return $request;
        }

        //--------- WILDCARDS THAT REQUIRE 2 SLUGS
        if (count($tailParts) < 2) {
            return $request;
        }

        /**
         * Check if the combination for availability routing is correct.
         * NOTE: we check in the controller if the combination truly exists
         */
        if (($locationTranslation = LocationTranslation::where('slug', $tailParts[0])->first()) &&
            ($productTranslation = ProductTranslation::where('slug', $tailParts[1])->first()) &&
            count($tailParts) == 2) {
            /**
             * Store the product and location in the request
             * We later use this in the controller so we don't have to look it up again.
             */
            $request->attributes->add([
                'location_id' => $locationTranslation->location_id,
                'product_id' => $productTranslation->product_id,
            ]);

            /**
             * Set the request URI and the original path
             * NOTE: We could create a route like 'availability/{location}/{product}
             * But because it would be such a wildcardly route, we didn't do this.
             */
            $request->server->set('REQUEST_URI', 'availability/show');

            return $request;
        }

        return $request;
    }
}