Skip to content

Commit

Permalink
Always attempt to decode attribute values (rapidez#520)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jade-GG authored Aug 14, 2024
1 parent 3a1a4a7 commit 77cc081
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions src/Models/QuoteItemOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Rapidez\Core\Models;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Support\Facades\DB;
use TorMorten\Eventy\Facades\Eventy;

class QuoteItemOption extends Model
{
Expand All @@ -23,26 +23,20 @@ public function quote_item()
protected function value(): Attribute
{
return Attribute::make(
get: fn (string $value) => match ($this->code) {
'info_buyRequest' => json_decode($value),
'attributes' => json_decode($value),
'option_ids' => explode(',', $value),
default => (function () use ($value) {
if (! $this->option) {
return;
}

if (in_array($this->option->type, ['drop_down', 'radio'])) {
return config('rapidez.models.product_option_type_value')::find($value)->title;
}

if ($this->option->type == 'file') {
return json_decode($value);
}

return $value;
})()
})->shouldCache();
get: function (string $value) {
$value = Eventy::filter('quote_item_option.value', $value, $this);

if (isset($this->option) && in_array($this->option->type, ['drop_down', 'radio'])) {
$value = config('rapidez.models.product_option_type_value')::find($value)->title;
}

if ($this->code === 'option_ids') {
$value = explode(',', $value);
}

return is_string($value) ? (json_decode($value) ?? $value) : $value;
}
)->shouldCache();
}

protected function label(): Attribute
Expand Down

0 comments on commit 77cc081

Please sign in to comment.