ILIAS  trunk Revision v11.0_alpha-1861-g09f3d197f78
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
StandardToastItem.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
26 
31 {
35  private const ACTION_SHOWN = 'shown';
39  private const ACTION_CLOSED = 'closed';
43  private const ACTION_VANISHED = 'vanished';
44  protected ?string $description = null;
45  protected array $additional_actions = [];
46 
51 
60 
61  public function __construct(protected IdentificationInterface $provider_identification, protected ToastRenderer $renderer, protected string $title, protected ?Icon $icon = null)
62  {
63  }
64 
65  private function packClosure(?\Closure $closure, string $identifier, string $title): ?ToastAction
66  {
67  if ($closure === null) {
68  return null;
69  }
70  return new ToastAction(
71  $identifier,
72  $title,
73  $closure
74  );
75  }
76 
77  public function getTitle(): string
78  {
79  return $this->title;
80  }
81 
82  public function withDescription(string $description): isStandardItem
83  {
84  $clone = clone $this;
85  $clone->description = $description;
86  return $clone;
87  }
88 
89  public function getDescription(): ?string
90  {
91  return $this->description;
92  }
93 
94  final public function withAdditionToastAction(ToastAction $action): isStandardItem
95  {
96  if (in_array(
97  $action->getIdentifier(),
98  [self::ACTION_SHOWN, self::ACTION_CLOSED, self::ACTION_VANISHED],
99  true
100  )) {
101  throw new \InvalidArgumentException(
102  'You cannot use the reserved identifiers shown, closed or vanished for additional actions'
103  );
104  }
105 
106  $existing = array_map(fn(ToastAction $action): string => $action->getIdentifier(), $this->additional_actions);
107 
108  if (in_array($action->getIdentifier(), $existing, true)) {
109  throw new \InvalidArgumentException(
110  'You cannot use the same identifier twice'
111  );
112  }
113 
114  $clone = clone $this;
115  $clone->additional_actions[] = $action;
116  return $clone;
117  }
118 
119  final public function getAllToastActions(): array
120  {
121  $actions = $this->additional_actions;
122  $actions[] = $this->handle_shown;
123  $actions[] = $this->handle_closed;
124  $actions[] = $this->handle_vanished;
125 
126  $actions = array_filter($actions, fn(?ToastAction $action): bool => $action !== null);
127 
128  return $actions;
129  }
130 
131  final public function getAdditionalToastActions(): array
132  {
134  }
135 
136  public function withIcon(Icon $icon): isStandardItem
137  {
138  $clone = clone $this;
139  $clone->icon = $icon;
140  return $clone;
141  }
142 
143  public function getIcon(): ?Icon
144  {
145  return $this->icon;
146  }
147 
149  {
150  return $this->provider_identification;
151  }
152 
153  final public function withShownCallable(\Closure $handle_shown): isStandardItem
154  {
155  $clone = clone $this;
156  $clone->handle_shown = $this->packClosure(
157  $handle_shown,
158  self::ACTION_SHOWN,
159  self::ACTION_SHOWN
160  );
161 
162  return $clone;
163  }
164 
165  final public function getShownAction(): ToastAction
166  {
167  return $this->handle_shown;
168  }
169 
170  final public function hasShownAction(): bool
171  {
172  return $this->handle_shown !== null;
173  }
174 
175  final public function withClosedCallable(\Closure $handle_closed): isStandardItem
176  {
177  $clone = clone $this;
178  $clone->handle_closed = $this->packClosure(
179  $handle_closed,
180  self::ACTION_CLOSED,
181  self::ACTION_CLOSED
182  );
183 
184  return $clone;
185  }
186 
187  final public function getClosedAction(): ?ToastAction
188  {
189  return $this->handle_closed;
190  }
191 
192  final public function hasClosedAction(): bool
193  {
194  return $this->handle_closed !== null;
195  }
196 
197  public function withVanishedCallable(\Closure $handle_vanished): isStandardItem
198  {
199  $clone = clone $this;
200  $clone->handle_vanished = $this->packClosure(
201  $handle_vanished,
202  self::ACTION_VANISHED,
203  self::ACTION_VANISHED
204  );
205 
206  return $clone;
207  }
208 
209  public function getVanishedAction(): ?ToastAction
210  {
211  return $this->handle_vanished;
212  }
213 
214  public function hasVanishedAction(): bool
215  {
216  return $this->handle_vanished !== null;
217  }
218 
219  final public function getRenderer(): ToastRenderer
220  {
221  return $this->renderer;
222  }
223 }
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 ...
$renderer
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.
__construct(protected IdentificationInterface $provider_identification, protected ToastRenderer $renderer, protected string $title, protected ?Icon $icon=null)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
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.
packClosure(?\Closure $closure, string $identifier, string $title)