ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
StandardToastItem.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
26 
31 {
32  private const ACTION_SHOWN = 'shown';
33  private const ACTION_CLOSED = 'closed';
34  private const ACTION_VANISHED = 'vanished';
35 
36  protected string $title;
37  protected ?string $description = null;
38  protected ?Icon $icon = null;
39  protected array $additional_actions = [];
40 
44  protected ?ToastAction $handle_shown = null;
45 
49  protected ?ToastAction $handle_closed = null;
53  private ?ToastAction $handle_vanished = null;
56 
57  public function __construct(
58  IdentificationInterface $provider_identification,
59  ToastRenderer $renderer,
60  string $title,
61  ?Icon $icon = null
62  ) {
63  $this->provider_identification = $provider_identification;
64  $this->renderer = $renderer;
65  $this->title = $title;
66  $this->icon = $icon;
67  }
68 
69  private function packClosure(?\Closure $closure, string $identifier, string $title): ?ToastAction
70  {
71  if ($closure === null) {
72  return null;
73  }
74  return new ToastAction(
75  $identifier,
76  $title,
77  $closure
78  );
79  }
80 
81  public function getTitle(): string
82  {
83  return $this->title;
84  }
85 
86  public function withDescription(string $description): isStandardItem
87  {
88  $clone = clone $this;
89  $clone->description = $description;
90  return $clone;
91  }
92 
93  public function getDescription(): ?string
94  {
95  return $this->description;
96  }
97 
98  final public function withAdditionToastAction(ToastAction $action): isStandardItem
99  {
100  if (in_array(
101  $action->getIdentifier(),
102  [self::ACTION_SHOWN, self::ACTION_CLOSED, self::ACTION_VANISHED],
103  true
104  )) {
105  throw new \InvalidArgumentException(
106  'You cannot use the reserved identifiers shown, closed or vanished for additional actions'
107  );
108  }
109 
110  $existing = array_map(function (ToastAction $action): string {
111  return $action->getIdentifier();
113 
114  if (in_array($action->getIdentifier(), $existing, true)) {
115  throw new \InvalidArgumentException(
116  'You cannot use the same identifier twice'
117  );
118  }
119 
120  $clone = clone $this;
121  $clone->additional_actions[] = $action;
122  return $clone;
123  }
124 
125  final public function getAllToastActions(): array
126  {
127  $actions = $this->additional_actions;
128  $actions[] = $this->handle_shown;
129  $actions[] = $this->handle_closed;
130  $actions[] = $this->handle_vanished;
131 
132  $actions = array_filter($actions, function (?ToastAction $action): bool {
133  return $action !== null;
134  });
135 
136  return $actions;
137  }
138 
139  final public function getAdditionalToastActions(): array
140  {
142  }
143 
144  public function withIcon(Icon $icon): isStandardItem
145  {
146  $clone = clone $this;
147  $clone->icon = $icon;
148  return $clone;
149  }
150 
151  public function getIcon(): ?Icon
152  {
153  return $this->icon;
154  }
155 
157  {
159  }
160 
161  final public function withShownCallable(\Closure $handle_shown): isStandardItem
162  {
163  $clone = clone $this;
164  $clone->handle_shown = $this->packClosure(
165  $handle_shown,
166  self::ACTION_SHOWN,
167  self::ACTION_SHOWN
168  );
169 
170  return $clone;
171  }
172 
173  final public function getShownAction(): ToastAction
174  {
175  return $this->handle_shown;
176  }
177 
178  final public function hasShownAction(): bool
179  {
180  return $this->handle_shown !== null;
181  }
182 
183  final public function withClosedCallable(\Closure $handle_closed): isStandardItem
184  {
185  $clone = clone $this;
186  $clone->handle_closed = $this->packClosure(
187  $handle_closed,
188  self::ACTION_CLOSED,
189  self::ACTION_CLOSED
190  );
191 
192  return $clone;
193  }
194 
195  final public function getClosedAction(): ?ToastAction
196  {
197  return $this->handle_closed;
198  }
199 
200  final public function hasClosedAction(): bool
201  {
202  return $this->handle_closed !== null;
203  }
204 
205  public function withVanishedCallable(\Closure $handle_vanished): isStandardItem
206  {
207  $clone = clone $this;
208  $clone->handle_vanished = $this->packClosure(
209  $handle_vanished,
210  self::ACTION_VANISHED,
211  self::ACTION_VANISHED
212  );
213 
214  return $clone;
215  }
216 
217  public function getVanishedAction(): ?ToastAction
218  {
219  return $this->handle_vanished;
220  }
221 
222  public function hasVanishedAction(): bool
223  {
224  return $this->handle_vanished !== null;
225  }
226 
227  final public function getRenderer(): ToastRenderer
228  {
229  return $this->renderer;
230  }
231 }
getClosedAction()
Get the callable to be executed, when this specific item is closed.
withClosedCallable(\Closure $handle_closed)
Set the callable to be executed, when this specific item is closed by clicking the X button or after ...
hasClosedAction()
Get whether there are any callables to be executed the Toast is closed.
withAdditionToastAction(ToastAction $action)
A ToastAction leads into the rendered toast to a link that can be clicked by the user.
ToastAction $handle_shown
Callable to be executed, if the notification center has been opened.
ToastAction $handle_vanished
Callable to be executed, if this specific item has vanished.
withShownCallable(\Closure $handle_shown)
Set the callable to be executed, when the toast is shown.
hasVanishedAction()
Get whether there are any callables to be executed the Toast has vanished.
getVanishedAction()
Get the callable to be executed, when this specific item has vanished.
getShownAction()
Get the callable to be executed, when the test is shown in GUI.
ToastAction $handle_closed
Callable to be executed, if this specific item has been closed.
withVanishedCallable(\Closure $handle_vanished)
Set the callable to be executed, when this specific item is closed vanishing.
__construct(IdentificationInterface $provider_identification, ToastRenderer $renderer, string $title, ?Icon $icon=null)
packClosure(?\Closure $closure, string $identifier, string $title)