Просмотр файла app/Models/Setting.php

Размер файла: 2.22Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Models;
  6.  
  7. use Exception;
  8. use Illuminate\Database\Eloquent\Model;
  9. use Illuminate\Database\Eloquent\Factories\HasFactory;
  10. use Illuminate\Support\Facades\Cache;
  11.  
  12. /**
  13. * Class Setting
  14. *
  15. * @property string name
  16. * @property string value
  17. */
  18. class Setting extends Model
  19. {
  20. use HasFactory;
  21.  
  22. /**
  23. * Indicates if the model should be timestamped.
  24. *
  25. * @var bool
  26. */
  27. public $timestamps = false;
  28.  
  29. /**
  30. * The attributes that aren't mass assignable.
  31. *
  32. * @var array
  33. */
  34. protected $guarded = [];
  35.  
  36. /**
  37. * Custom settings
  38. *
  39. * @var array
  40. */
  41. private static array $settings = [];
  42.  
  43. /**
  44. * Возвращает список допустимых страниц настроек
  45. *
  46. * @return array
  47. */
  48. public static function getActions(): array
  49. {
  50. return [
  51. 'mains',
  52. 'mails',
  53. 'info',
  54. 'guestbook',
  55. 'news',
  56. 'comments',
  57. 'forums',
  58. 'photos',
  59. 'messages',
  60. 'contacts',
  61. 'loads',
  62. 'blogs',
  63. 'pages',
  64. 'others',
  65. 'protects',
  66. 'prices',
  67. 'adverts',
  68. 'files',
  69. 'stickers',
  70. 'offers',
  71. 'feeds',
  72. 'invitations',
  73. ];
  74. }
  75.  
  76. /**
  77. * Возвращает настройки сайта по ключу
  78. *
  79. * @return array данные
  80. */
  81. public static function getSettings(): array
  82. {
  83. try {
  84. $settings = Cache::rememberForever('settings', static function () {
  85. $settings = Setting::query()->pluck('value', 'name')->all();
  86. return array_map(static function ($value) {
  87. if (is_numeric($value)) {
  88. return ! str_contains($value, '.') ? (int) $value : (float) $value;
  89. }
  90.  
  91. if ($value === '') {
  92. return null;
  93. }
  94.  
  95. return $value;
  96. }, $settings);
  97. });
  98. } catch (Exception) {
  99. $settings = [];
  100. }
  101.  
  102. return $settings;
  103. }
  104. }