File: D:/HostingSpaces/SBogers10/base.komma.pro/vendor/komma/kms/src/Helpers/GlobalHelpers.php
<?php declare(strict_types=1);
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Str;
if(!function_exists('kms_asset')) {
/**
* 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
*/
/**
* Small interception function that will populate the desired img to the vendor folder
*
* @param string $assetPath
* @return string
*/
function kms_asset(string $assetPath): string {
return '/' . config('kms.published_path') . '/' . $assetPath;
}
}
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, __('routes'))) {
throw new UnexpectedValueException('GlobalHelper: Unknown localized route key: "'. $routeName .'"');
}
$url = __('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('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));
}
}