ldc-cocotte/app/Helpers/BladeDirectivesHelper.php
2026-02-05 16:45:01 +01:00

80 lines
2.2 KiB
PHP

<?php
namespace App\Helpers;
use Illuminate\Support\Facades\Auth;
use App\Models\SellUnit;
use \Illuminate\Support\Str;
class BladeDirectivesHelper
{
/**
* Format specific value.
* Used for blade directive @fmt_currency_without_auth.
*
* @param mixed $value
* @return string
*/
public static function formatPriceValue($value): string
{
if (is_float($value)) {
return sprintf('%s €', number_format($value, 2, ',', ' '));
}
return $value;
}
/**
* Show the formated value only if authorized.
* Used for blade directive @fmt_currency and @fmt_currency_without_ttc.
*
* @param mixed $value
* @return string
*/
public static function formatCurrency($value): string
{
if (!self::shouldShowPrices()) {
return '-.--';
}
return self::formatPriceValue($value);
}
/**
* Check if prices must be shown or not.
*
* @return bool
*/
public static function shouldShowPrices(): bool
{
return (config('bosettings.show_prices_authenticated') === "authenticated" && Auth::guard('customer')->check())
|| config('bosettings.show_prices_guest') == "guest";
}
/**
* Format the sales unit according to configuration.
* Used for blade directive @fmt_sellunit.
*
* @param mixed $expression
* @return string
*/
public static function formatSellUnit($expression): string
{
if (config('bosettings.sell_unit_management') == "strict") {
return !empty($expression) ? sprintf('/ %s', SellUnit::make($expression)->label) : '';
}
return !empty($expression) ? sprintf('/ %s', $expression) : '';
}
/**
* Extract a title produced according to the theme configuration.
* Used for blade directive @excerpt_product_title.
*
* @param string $expression
* @param int|null $limit
* @return string
*/
public static function excerptTitle(string $expression, $limit = null): string
{
$limit = $limit ?? config('theme.max_product_title_length');
return Str::limit($expression, $limit);
}
}