Alert.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace Encore\Admin\Widgets;
  3. use Illuminate\Contracts\Support\Renderable;
  4. class Alert extends Widget implements Renderable
  5. {
  6. /**
  7. * @var string
  8. */
  9. protected $view = 'admin::widgets.alert';
  10. /**
  11. * @var string|\Symfony\Component\Translation\TranslatorInterface
  12. */
  13. protected $title = '';
  14. /**
  15. * @var string
  16. */
  17. protected $content = '';
  18. /**
  19. * @var string
  20. */
  21. protected $style = 'danger';
  22. /**
  23. * @var string
  24. */
  25. protected $icon = 'ban';
  26. /**
  27. * Alert constructor.
  28. *
  29. * @param mixed $content
  30. * @param string $title
  31. * @param string $style
  32. */
  33. public function __construct($content, $title = '', $style = 'danger')
  34. {
  35. $this->content = (string) $content;
  36. $this->title = $title ?: trans('admin.alert');
  37. $this->style($style);
  38. }
  39. /**
  40. * Add style.
  41. *
  42. * @param string $style
  43. *
  44. * @return $this
  45. */
  46. public function style($style = 'info')
  47. {
  48. $this->style = $style;
  49. return $this;
  50. }
  51. /**
  52. * Add icon.
  53. *
  54. * @param string $icon
  55. *
  56. * @return $this
  57. */
  58. public function icon($icon)
  59. {
  60. $this->icon = $icon;
  61. return $this;
  62. }
  63. /**
  64. * @return array
  65. */
  66. protected function variables()
  67. {
  68. $this->class("alert alert-{$this->style} alert-dismissable");
  69. return [
  70. 'title' => $this->title,
  71. 'content' => $this->content,
  72. 'icon' => $this->icon,
  73. 'attributes' => $this->formatAttributes(),
  74. ];
  75. }
  76. /**
  77. * Render alter.
  78. *
  79. * @return string
  80. */
  81. public function render()
  82. {
  83. return view($this->view, $this->variables())->render();
  84. }
  85. }