File: D:/HostingSpaces/farmfun/reserveren.farmfun.be/app/Helpers/GlobalHelpers.php
<?php
declare(strict_types=1);
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Str;
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('site_config')) {
/**
* Get / set the specified configuration value.
*
* If an array is passed as the key, we will assume you want to set an array of values.
*
* @param string $key
* @return mixed|\Illuminate\Config\Repository
*/
function site_config($key)
{
$key = 'site.'.app('config')->get('site.site_version').'.'.$key;
return app('config')->get($key);
}
}
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));
}
}
if (! function_exists('euro_pricing_format')) {
function euro_pricing_format(int $amount, string $dec_point = ',', string $thousands_sep = '', bool $replaceZeros = false): string
{
$price = number_format($amount / 100, 2, $dec_point, $thousands_sep);
if ($replaceZeros && substr($price, -3) == ',00') {
$price = Str::replaceLast(',00', ',-', $price);
}
return $price;
}
}
if (! function_exists('calculate_price_excluding_vat')) {
/**
* Calculates the price from to without excluding vat
*
* @param int $price
* @param int $vatPercentage
* @return int
*/
function calculate_price_excluding_vat(int $price, int $vatPercentage): int
{
if ($price <= 0) {
return 0;
}
return (int) round($price / (100 + $vatPercentage) * 100);
}
}
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.');
} elseif (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};
}
}