1-activity_toast.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace cx_newsletter;
  3. trait activity_toast {
  4. private function get_toast_template() : \cx_appengine\template {
  5. return (
  6. $this
  7. ->get_templates()
  8. ->prepare('toast')
  9. );
  10. }
  11. protected function success_toast(string $content) : self {
  12. return $this->make_toast($content, toast_type::success);
  13. }
  14. protected function warning_toast(string $content) : self {
  15. return $this->make_toast($content, toast_type::warning);
  16. }
  17. protected function error_toast(string $content) : self {
  18. return $this->make_toast($content, toast_type::error);
  19. }
  20. private function make_toast(string $content, toast_type $type) : self {
  21. $this->toast = __($content, 'cx_newsletter');
  22. $this->toast_type = $type;
  23. return $this;
  24. }
  25. protected function has_toast() : bool {
  26. return $this->toast !== null and $this->toast_type !== null;
  27. }
  28. private function get_toast_params() : array {
  29. return [
  30. 'content' => $this->toast,
  31. 'type' => $this->toast_type->value
  32. ];
  33. }
  34. protected function render_toast() : \cx_appengine\string_builder {
  35. if (!$this->has_toast()) {
  36. return new \cx_appengine\string_builder();
  37. }
  38. return (
  39. $this
  40. ->get_toast_template()
  41. ->render($this->get_toast_params())
  42. );
  43. }
  44. protected function construct_toast() : self {
  45. $this->toast = null;
  46. $this->toast_type = null;
  47. return $this;
  48. }
  49. private ?toast_type $toast_type;
  50. private ?string $toast;
  51. }