|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SaasReady\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
8
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
|
9
|
|
|
use SaasReady\Constants\CurrencyCode; |
|
10
|
|
|
use SaasReady\Database\Factories\CurrencyFactory; |
|
11
|
|
|
use SaasReady\Traits\EloquentBuilderMixin; |
|
12
|
|
|
use SaasReady\Traits\HasUuid; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @property-read int $id |
|
16
|
|
|
* @property CurrencyCode $code |
|
17
|
|
|
* @property string $name |
|
18
|
|
|
* @property string $symbol |
|
19
|
|
|
* @property int $decimals |
|
20
|
|
|
* @property string $decimal_separator |
|
21
|
|
|
* @property string $thousands_separator |
|
22
|
|
|
* @property bool $space_after_symbol |
|
23
|
|
|
* @property ?Carbon $created_at |
|
24
|
|
|
* @property ?Carbon $updated_at |
|
25
|
|
|
* @property ?Carbon $deleted_at |
|
26
|
|
|
* @property ?Carbon $activated_at |
|
27
|
|
|
* |
|
28
|
|
|
* @mixin EloquentBuilderMixin |
|
29
|
|
|
*/ |
|
30
|
|
|
class Currency extends Model |
|
31
|
|
|
{ |
|
32
|
|
|
use HasFactory; |
|
33
|
|
|
use SoftDeletes; |
|
34
|
|
|
use HasUuid; |
|
35
|
|
|
|
|
36
|
|
|
protected $table = 'currencies'; |
|
37
|
|
|
|
|
38
|
|
|
protected $fillable = [ |
|
39
|
|
|
'code', |
|
40
|
|
|
'name', |
|
41
|
|
|
'symbol', |
|
42
|
|
|
'thousands_separator', |
|
43
|
|
|
'decimals', |
|
44
|
|
|
'decimal_separator', |
|
45
|
|
|
'space_after_symbol', |
|
46
|
|
|
'activated_at', |
|
47
|
|
|
]; |
|
48
|
|
|
|
|
49
|
|
|
protected $casts = [ |
|
50
|
|
|
'code' => CurrencyCode::class, |
|
51
|
|
|
'decimals' => 'int', |
|
52
|
|
|
'space_after_symbol' => 'bool', |
|
53
|
|
|
'activated_at' => 'datetime', |
|
54
|
|
|
]; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* A global hashmap to cache the `findByCode` - shortage cache. |
|
58
|
|
|
* [ |
|
59
|
|
|
* 'USD' => Currency of USD, |
|
60
|
|
|
* ... |
|
61
|
|
|
* ] |
|
62
|
|
|
*/ |
|
63
|
|
|
public static array $currencyCaches = []; |
|
64
|
|
|
|
|
65
|
|
|
public static function findByCode(CurrencyCode $code): ?static |
|
|
|
|
|
|
66
|
|
|
{ |
|
67
|
|
|
return static::$currencyCaches[$code->value] |
|
68
|
|
|
??= static::whereCode($code)->first(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @codeCoverageIgnore |
|
73
|
|
|
*/ |
|
74
|
|
|
protected static function newFactory(): CurrencyFactory |
|
75
|
|
|
{ |
|
76
|
|
|
return CurrencyFactory::new(); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|