mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-07-16 22:05:20 +00:00
Ports #639 to 3.x — the original targets the now feature-frozen 2.x line. Address::country_name now resolves the localized country name for the current app locale via Symfony\Component\Intl\Countries, falling back to the stored name on lookup failure. Adds symfony/intl + a unit test. Co-authored-by: Lukas Selch <selchlukas@icloud.com>
56 lines
1.1 KiB
PHP
56 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Symfony\Component\Intl\Countries;
|
|
|
|
class Address extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
public const BILLING_TYPE = 'billing';
|
|
|
|
public const SHIPPING_TYPE = 'shipping';
|
|
|
|
protected $guarded = ['id'];
|
|
|
|
public function getCountryNameAttribute(): ?string
|
|
{
|
|
if (! $this->country) {
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
return Countries::getName(
|
|
$this->country->code,
|
|
app()->getLocale()
|
|
);
|
|
} catch (\Exception $e) {
|
|
return $this->country->name;
|
|
}
|
|
}
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function customer(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Customer::class);
|
|
}
|
|
|
|
public function company(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Company::class);
|
|
}
|
|
|
|
public function country(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Country::class);
|
|
}
|
|
}
|