Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Currency.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace App\Models\Locale;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Eloquent\Relations\HasMany;
  5. use Illuminate\Support\Collection;
  6. use Illuminate\Support\Facades\Cache;
  7. class Currency extends Model
  8. {
  9. protected $table = 'currencies';
  10. protected $guarded = [];
  11. protected $casts = [
  12. 'code' => 'string',
  13. 'name' => 'string',
  14. 'symbol' => 'string',
  15. 'precision' => 'int',
  16. 'decimal_mark' => 'string',
  17. 'thousands_separator' => 'string',
  18. 'symbol_first' => 'bool',
  19. 'subunit' => 'int',
  20. ];
  21. public function countries(): HasMany
  22. {
  23. return $this->hasMany(Country::class, 'currency_code', 'code');
  24. }
  25. public static function allCached(): Collection
  26. {
  27. return collect(Cache::get('currencies') ?? []);
  28. }
  29. // To find a currency by its code
  30. public static function findByCode(string $code): ?self
  31. {
  32. return self::allCached()->firstWhere('code', $code);
  33. }
  34. // To find a currency by its name
  35. public static function findByName(string $name): ?self
  36. {
  37. return self::allCached()->firstWhere('name', $name);
  38. }
  39. // Get currency name by its code
  40. public static function getNameByCode(string $code): ?string
  41. {
  42. $currency = self::findByCode($code);
  43. return $currency->name ?? null;
  44. }
  45. // Get currency code by its name
  46. public static function getCodeByName(string $name): ?string
  47. {
  48. $currency = self::findByName($name);
  49. return $currency->code ?? null;
  50. }
  51. // Get currency symbol by its code
  52. public static function getSymbolByCode(string $code): ?string
  53. {
  54. $currency = self::findByCode($code);
  55. return $currency->symbol ?? null;
  56. }
  57. // Get currency symbol by its name
  58. public static function getSymbolByName(string $name): ?string
  59. {
  60. $currency = self::findByName($name);
  61. return $currency->symbol ?? null;
  62. }
  63. // Get currency precision by its code
  64. public static function getPrecisionByCode(string $code): ?int
  65. {
  66. $currency = self::findByCode($code);
  67. return $currency->precision ?? null;
  68. }
  69. // Get currency precision by its name
  70. public static function getPrecisionByName(string $name): ?int
  71. {
  72. $currency = self::findByName($name);
  73. return $currency->precision ?? null;
  74. }
  75. // Get currency decimal mark by its code
  76. public static function getDecimalMarkByCode(string $code): ?string
  77. {
  78. $currency = self::findByCode($code);
  79. return $currency->decimal_mark ?? null;
  80. }
  81. // Get currency decimal mark by its name
  82. public static function getDecimalMarkByName(string $name): ?string
  83. {
  84. $currency = self::findByName($name);
  85. return $currency->decimal_mark ?? null;
  86. }
  87. // Get currency thousands separator by its code
  88. public static function getThousandsSeparatorByCode(string $code): ?string
  89. {
  90. $currency = self::findByCode($code);
  91. return $currency->thousands_separator ?? null;
  92. }
  93. // Get currency thousands separator by its name
  94. public static function getThousandsSeparatorByName(string $name): ?string
  95. {
  96. $currency = self::findByName($name);
  97. return $currency->thousands_separator ?? null;
  98. }
  99. // Get currency symbol first by its code
  100. public static function getSymbolFirstByCode(string $code): ?bool
  101. {
  102. $currency = self::findByCode($code);
  103. return $currency->symbol_first ?? null;
  104. }
  105. // Get currency symbol first by its name
  106. public static function getSymbolFirstByName(string $name): ?bool
  107. {
  108. $currency = self::findByName($name);
  109. return $currency->symbol_first ?? null;
  110. }
  111. // Get currency subunit by its code
  112. public static function getSubunitByCode(string $code): ?int
  113. {
  114. $currency = self::findByCode($code);
  115. return $currency->subunit ?? null;
  116. }
  117. // Get currency subunit by its name
  118. public static function getSubunitByName(string $name): ?int
  119. {
  120. $currency = self::findByName($name);
  121. return $currency->subunit ?? null;
  122. }
  123. // Get all currency codes
  124. public static function getAllCodes(): Collection
  125. {
  126. return self::allCached()->pluck('code');
  127. }
  128. // Get all currency names
  129. public static function getAllNames(): Collection
  130. {
  131. return self::allCached()->pluck('name');
  132. }
  133. }