ILIAS  release_8 Revision v8.19-1-g4e8f2f9140c
All Data Structures Namespaces Files Functions Variables Modules Pages
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  protected ?int $vanish_time = null;
58  protected ?int $delay_time = null;
59 
60  public function __construct(
61  IdentificationInterface $provider_identification,
62  ToastRenderer $renderer,
63  string $title,
64  ?Icon $icon = null
65  ) {
66  $this->provider_identification = $provider_identification;
67  $this->renderer = $renderer;
68  $this->title = $title;
69  $this->icon = $icon;
70  }
71 
72  private function packClosure(?\Closure $closure, string $identifier, string $title): ?ToastAction
73  {
74  if ($closure === null) {
75  return null;
76  }
77  return new ToastAction(
78  $identifier,
79  $title,
80  $closure
81  );
82  }
83 
84  public function getTitle(): string
85  {
86  return $this->title;
87  }
88 
89  public function withDescription(string $description): isStandardItem
90  {
91  $clone = clone $this;
92  $clone->description = $description;
93  return $clone;
94  }
95 
96  public function getDescription(): ?string
97  {
98  return $this->description;
99  }
100 
101  final public function withAdditionToastAction(ToastAction $action): isStandardItem
102  {
103  if (in_array(
104  $action->getIdentifier(),
105  [self::ACTION_SHOWN, self::ACTION_CLOSED, self::ACTION_VANISHED],
106  true
107  )) {
108  throw new \InvalidArgumentException(
109  'You cannot use the reserved identifiers shown, closed or vanished for additional actions'
110  );
111  }
112 
113  $existing = array_map(function (ToastAction $action): string {
114  return $action->getIdentifier();
116 
117  if (in_array($action->getIdentifier(), $existing, true)) {
118  throw new \InvalidArgumentException(
119  'You cannot use the same identifier twice'
120  );
121  }
122 
123  $clone = clone $this;
124  $clone->additional_actions[] = $action;
125  return $clone;
126  }
127 
128  final public function getAllToastActions(): array
129  {
130  $actions = $this->additional_actions;
131  $actions[] = $this->handle_shown;
132  $actions[] = $this->handle_closed;
133  $actions[] = $this->handle_vanished;
134 
135  $actions = array_filter($actions, function (?ToastAction $action): bool {
136  return $action !== null;
137  });
138 
139  return $actions;
140  }
141 
142  final public function getAdditionalToastActions(): array
143  {
145  }
146 
147  public function withIcon(Icon $icon): isStandardItem
148  {
149  $clone = clone $this;
150  $clone->icon = $icon;
151  return $clone;
152  }
153 
154  public function getIcon(): ?Icon
155  {
156  return $this->icon;
157  }
158 
160  {
162  }
163 
164  final public function withShownCallable(\Closure $handle_shown): isStandardItem
165  {
166  $clone = clone $this;
167  $clone->handle_shown = $this->packClosure(
168  $handle_shown,
169  self::ACTION_SHOWN,
170  self::ACTION_SHOWN
171  );
172 
173  return $clone;
174  }
175 
176  final public function getShownAction(): ToastAction
177  {
178  return $this->handle_shown;
179  }
180 
181  final public function hasShownAction(): bool
182  {
183  return $this->handle_shown !== null;
184  }
185 
186  final public function withClosedCallable(\Closure $handle_closed): isStandardItem
187  {
188  $clone = clone $this;
189  $clone->handle_closed = $this->packClosure(
190  $handle_closed,
191  self::ACTION_CLOSED,
192  self::ACTION_CLOSED
193  );
194 
195  return $clone;
196  }
197 
198  final public function getClosedAction(): ?ToastAction
199  {
200  return $this->handle_closed;
201  }
202 
203  final public function hasClosedAction(): bool
204  {
205  return $this->handle_closed !== null;
206  }
207 
208  public function withVanishedCallable(\Closure $handle_vanished): isStandardItem
209  {
210  $clone = clone $this;
211  $clone->handle_vanished = $this->packClosure(
212  $handle_vanished,
213  self::ACTION_VANISHED,
214  self::ACTION_VANISHED
215  );
216 
217  return $clone;
218  }
219 
220  public function getVanishedAction(): ?ToastAction
221  {
222  return $this->handle_vanished;
223  }
224 
225  public function hasVanishedAction(): bool
226  {
227  return $this->handle_vanished !== null;
228  }
229 
230  public function withVanishTime(int $miliseconds): isStandardItem
231  {
232  $clone = clone $this;
233  $clone->vanish_time = $miliseconds;
234  return $clone;
235  }
236 
237  public function getVanishTime(): ?int
238  {
239  return $this->vanish_time;
240  }
241 
242  public function withDelayTime(int $miliseconds): isStandardItem
243  {
244  $clone = clone $this;
245  $clone->delay_time = $miliseconds;
246  return $clone;
247  }
248 
249  public function getDelayTime(): ?int
250  {
251  return $this->delay_time;
252  }
253 
254  final public function getRenderer(): ToastRenderer
255  {
256  return $this->renderer;
257  }
258 }
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.
This describes how an icon could be modified during construction of UI.
Definition: Icon.php:28
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)