File: D:/HostingSpaces/SBogers10/shop.komma.nl/app/ShippingCosts/ShippingCost.php
<?php declare(strict_types=1);
namespace App\ShippingCosts;
use App\Vat\FinancialProperties;
use App\Vat\HasFinancialPropertiesInterface;
use Komma\KMS\Core\Attributes\Models\Traits\HasThumbnailInterface;
use Komma\KMS\Core\Attributes\Models\Traits\HasThumbnailTrait;
use Komma\KMS\Core\Entities\DisplayNameInterface;
use Komma\KMS\Core\Entities\DisplayNameTrait;
use Illuminate\Database\Eloquent\Model;
use Komma\KMS\Globalization\RegionInfo;
/**
* App\ShippingCosts\ShippingCost
*
* @property int $id
* @property string $code The ISO 3166-1 alpha 3 value of the country to ship to
* @property int $cost The cost in cents
* @property string $vat_scenario_enum
* @property int $is_default For falling back when no country could be determined from the users input
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read mixed $name
* @method static \Illuminate\Database\Eloquent\Builder|ShippingCost newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|ShippingCost newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|ShippingCost query()
* @method static \Illuminate\Database\Eloquent\Builder|ShippingCost whereCode($value)
* @method static \Illuminate\Database\Eloquent\Builder|ShippingCost whereCost($value)
* @method static \Illuminate\Database\Eloquent\Builder|ShippingCost whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|ShippingCost whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|ShippingCost whereIsDefault($value)
* @method static \Illuminate\Database\Eloquent\Builder|ShippingCost whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|ShippingCost whereVatScenarioEnum($value)
* @mixin \Eloquent
*/
class ShippingCost extends Model implements DisplayNameInterface, HasThumbnailInterface, HasFinancialPropertiesInterface
{
use DisplayNameTrait;
use HasThumbnailTrait;
use FinancialProperties;
protected $fillable = [
'code',
'cost',
'vat_scenario_enum',
'is_default',
];
/** @var RegionInfo $neutralCulture */
public $neutralCulture;
public function setCodeAttribute(string $value)
{
$this->attributes['code'] = $value;
$this->updateNeutralCulture();
}
public function getNameAttribute()
{
$this->updateNeutralCulture();
if($this->neutralCulture) return $this->neutralCulture->getDisplayName();
return $this->country;
}
private function updateNeutralCulture()
{
$this->neutralCulture = RegionInfo::getNeutralCultures()->filter(function(RegionInfo $regionInfo) {
return $regionInfo->getThreeLetterISORegionName() == $this->getAttribute('code');
})->first();
}
/**
* @inheritDoc
*/
public function getPrice(): float
{
return (float) $this->cost;
}
}