diff --git a/src/AmastyLabelServiceProvider.php b/src/AmastyLabelServiceProvider.php index e6ccb4e..3745e6c 100644 --- a/src/AmastyLabelServiceProvider.php +++ b/src/AmastyLabelServiceProvider.php @@ -3,8 +3,8 @@ namespace Rapidez\AmastyLabel; use Illuminate\Support\ServiceProvider; -use Rapidez\AmastyLabel\Models\Scopes\WithProductAmastyLabelScope; use Rapidez\AmastyLabel\Models\Casts\CastAmastyLabelVariables; +use Rapidez\AmastyLabel\Models\Scopes\WithProductAmastyLabelScope; use TorMorten\Eventy\Facades\Eventy; class AmastyLabelServiceProvider extends ServiceProvider @@ -15,7 +15,7 @@ public function boot() ->bootEventyFilters(); } - public function bootEventyFilters() : self + public function bootEventyFilters(): self { Eventy::addFilter('product.scopes', fn ($scopes) => array_merge($scopes ?: [], [WithProductAmastyLabelScope::class])); Eventy::addFilter('product.casts', fn ($casts) => array_merge($casts ?: [], ['amasty_label' => CastAmastyLabelVariables::class])); @@ -30,7 +30,7 @@ public function bootEventyFilters() : self return $this; } - public function bootViews() : self + public function bootViews(): self { $this->loadViewsFrom(__DIR__.'/../resources/views', 'amastylabel'); diff --git a/src/Models/Casts/CastAmastyLabelVariables.php b/src/Models/Casts/CastAmastyLabelVariables.php index 0e68531..39e5e52 100644 --- a/src/Models/Casts/CastAmastyLabelVariables.php +++ b/src/Models/Casts/CastAmastyLabelVariables.php @@ -3,14 +3,14 @@ namespace Rapidez\AmastyLabel\Models\Casts; use Illuminate\Contracts\Database\Eloquent\CastsAttributes; -use Rapidez\Core\Models\Model; use Illuminate\Support\Collection; +use Rapidez\Core\Models\Model; class CastAmastyLabelVariables implements CastsAttributes { protected $variableRegex = '/(?<={)[a-zA-Z0-9_:]+(?=})/'; - public function get($model, string $key, $value, array $attributes) : Collection + public function get($model, string $key, $value, array $attributes): Collection { $labels = collect(json_decode($value)); if (!$labels->count()) { @@ -36,16 +36,16 @@ public function get($model, string $key, $value, array $attributes) : Collection return $labels; } - protected function parseVariables(string $text, string $type, string $var, Model $model) : string + protected function parseVariables(string $text, string $type, string $var, Model $model): string { - return match($type) { - 'flat' => str_replace("{{$var}}", price($model->{strtolower($var)}), $text), - 'amount' => str_replace("{{$var}}", price($model->price - $model->special_price), $text), - 'percent' => str_replace("{{$var}}", (100 - floor(($model->special_price / $model->price) * 100)) . '%', $text), + return match ($type) { + 'flat' => str_replace("{{$var}}", price($model->{strtolower($var)}), $text), + 'amount' => str_replace("{{$var}}", price($model->price - $model->special_price), $text), + 'percent' => str_replace("{{$var}}", (100 - floor(($model->special_price / $model->price) * 100)).'%', $text), }; } - public function set($model, string $key, $value, array $attributes) : string + public function set($model, string $key, $value, array $attributes): string { return $value; } @@ -53,10 +53,10 @@ public function set($model, string $key, $value, array $attributes) : string protected function getType(string $type) { return [ - 'SPECIAL_PRICE' => 'flat', - 'PRICE' => 'flat', - 'SAVE_AMOUNT' => 'amount', - 'SAVE_PERCENT' => 'percent' + 'SPECIAL_PRICE' => 'flat', + 'PRICE' => 'flat', + 'SAVE_AMOUNT' => 'amount', + 'SAVE_PERCENT' => 'percent', ][$type] ?? null; } }