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

Размер файла: 2.19Kb
  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 $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. ];
  73. }
  74.  
  75. /**
  76. * Возвращает настройки сайта по ключу
  77. *
  78. * @return array данные
  79. */
  80. public static function getSettings(): array
  81. {
  82. try {
  83. $settings = Cache::rememberForever('settings', static function () {
  84. $settings = Setting::query()->pluck('value', 'name')->all();
  85. return array_map(static function ($value) {
  86. if (is_numeric($value)) {
  87. return strpos($value, '.') === false ? (int) $value : (float) $value;
  88. }
  89.  
  90. if ($value === '') {
  91. return null;
  92. }
  93.  
  94. return $value;
  95. }, $settings);
  96. });
  97. } catch (Exception $e) {
  98. $settings = [];
  99. }
  100.  
  101. return $settings;
  102. }
  103. }