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/blijegasten/blijegasten.be/app/Helpers/GlobalHelpers.php
<?php declare(strict_types=1);

use Illuminate\Support\Facades\App;
use Illuminate\Support\Collection;

if(!function_exists('localized_route')) {

    /**
     * Give this helper a named route, for example site.password.reset and it wil return the translated
     * route of for example nl/wachtwoord/reset for the dutch language. The parameters will fill in
     * the corresponding route parameters. If multiple language is on it will append the language iso.
     * Normally this will return an absolute route, but it possible to disable this.
     *
     * @param string $routeName
     * @param array $parameters
     * @param bool $absolute
     * @return string
     */
    function localized_route(string $routeName, array $parameters = [], $absolute = true): string {

        $languageIso2 = App::getLanguage()->iso_2;
        $base = url('/');

        // Check if key exists else throw an error
        if(!array_key_exists($routeName, __('site/routes'))) {
            throw new UnexpectedValueException('GlobalHelper: Unknown localized route key: "'. $routeName .'"');
        }

        $url = __('site/routes')[$routeName];

        // Replace the parameters just as laravel does
        foreach ($parameters as $key => $value){
            $url = str_replace('{' . $key . '}', $value, $url, $count);
            if($count == 0) throw new UnexpectedValueException('GlobalHelper: Unknown parameter: "' . $key . '" in the localized route key: "'. $routeName .'"');
        }

        // Throw error if the url contains any brackets
        if(Str::contains($url, ['{', '}'])) throw new BadMethodCallException('GlobalHelper: Parameter(s) missing in "' . $url . '" for localized route key: "'. $routeName .'".');

        // Check if we add the language iso before route / so if multiple languages is on or of
        if(config('app.multipleLanguages')) {
            if($absolute) return implode('/', [$base, $languageIso2, $url]);
            return implode('/', [$languageIso2, $url]);
        }

        // Check if we want an absolute route or not
        if($absolute) return implode('/', [$base, $url]);
        return '/' . $url;
    }
}

if(!function_exists('old_session')) {

    /**
     * Use the session to get the field values when old value is empty
     * Note: If you are lost where the value come from, then enable the config log_old_session
     *
     * @param  string  $name
     * @param  $undefinedValue
     * @param  string  $sessionKey
     * @return mixed|null
     */
    function old_session(string $name, $undefinedValue = null, $sessionKey = 'old_session') {

        if(config('site.log_old_session')) debug('old_session: resolved:' . $name);

        $value = old($name, null);
        if($value !== null) {
            if(config('site.log_old_session')) debug('old_session: Old input "'. $name .'" found.');
            return $value;
        }

        $oldSession = session($sessionKey);

        if(empty($oldSession)) {

            if(config('site.log_old_session')) debug('GlobalHelper: old_session; Nothing found in the session with key "' . $sessionKey . '" is empty or undefined.');
            else if( config('site.logged_old_session') == null ) {
                config(['site.logged_old_session' => true]);
                debug('GlobalHelper: old_session; Only logged once | Nothing found in the session with key "' . $sessionKey . '" is empty or undefined.');
            }
            return $undefinedValue;
        }

        if(!isset($oldSession->{$name})) {
            if(config('site.log_old_session')) debug('GlobalHelper: old_session; Key:"'. $name . '" has not been found in "' . $sessionKey . '".');
            return $undefinedValue;
        }

        if(config('site.log_old_session')) debug('old_session: session "'. $name .'" found.');
        return $oldSession->{$name};
    }
}

if(!function_exists('create_unique_slug')) {

    /**
     * Helper that will generate a unique slug for the name.
     * It will look up the slugified name in the given collection and append uniquifier if needed.
     * Till it's a unique slug then it will return
     *
     * @param  Collection  $collection
     * @param  string  $name
     * @param  int  $uniquifier
     * @return string
     */
    function create_unique_slug(Collection $collection, string $name, int $uniquifier = 0): string
    {
        // Check that we have items in our collection
        if($collection->count() == 0) throw new \InvalidArgumentException("GlobalHelper:: Provided collection is empty. Then this function could be skipped, instead of being called." );

        // Slugify the name
        if($uniquifier == 0) $slug = Str::slug($name);
        else $slug = Str::slug($name . '-' . $uniquifier);

        // Search if the slug is in the collection
        $results = $collection->where('slug', '=', $slug);

        // Return the generated slug
        if($results->count() == 0) return $slug;

        // Call again till we have a unique slug
        return create_unique_slug($collection, $name, ($uniquifier + 1));

    }
}