Translated using Weblate (Portuguese)
[phpmyadmin.git] / src / Message.php
blob0f22a41ee065ea2a76f2fd6ec61f9d0418f293f8
1 <?php
3 declare(strict_types=1);
5 namespace PhpMyAdmin;
7 use Stringable;
9 use function __;
10 use function _ngettext;
11 use function htmlspecialchars;
12 use function is_float;
13 use function is_int;
14 use function md5;
15 use function sprintf;
17 use const ENT_COMPAT;
19 /**
20 * a single message
22 * simple usage examples:
23 * <code>
24 * // display simple error message 'Error'
25 * echo Message::error()->getDisplay();
27 * // get simple success message 'Success'
28 * $message = Message::success();
30 * // get special notice
31 * $message = Message::notice(__('This is a localized notice'));
32 * </code>
34 * more advanced usage example:
35 * <code>
36 * // create another message, a hint, with a localized string which expects
37 * $hint = Message::notice('Read the %smanual%s');
38 * // replace placeholders with the following params
39 * $hint->addParam('[doc@cfg_Example]');
40 * $hint->addParam('[/doc]');
41 * // add this hint as a tooltip
42 * $hint = showHint($hint);
44 * // add the retrieved tooltip reference to the original message
45 * $message->addMessage($hint);
46 * </code>
48 class Message implements Stringable
50 public const SUCCESS = 1; // 0001
51 public const NOTICE = 2; // 0010
52 public const ERROR = 8; // 1000
54 /**
55 * The locale string identifier
57 protected string $string = '';
59 /**
60 * The formatted message
62 protected string $message = '';
64 /**
65 * Whether the message was already displayed
67 protected bool $isDisplayed = false;
69 /**
70 * Whether to use BB code when displaying.
72 protected bool $useBBCode = true;
74 /**
75 * Unique id
77 protected string|null $hash = null;
79 /**
80 * holds parameters
82 * @var mixed[]
84 protected array $params = [];
86 /**
87 * holds additional messages
89 * @var (string|Message)[]
91 protected array $addedMessages = [];
93 /**
94 * @param string $string The message to be displayed
95 * @param int $type A numeric representation of the type of message
96 * @param mixed[] $params An array of parameters to use in the message
97 * constant definitions above
98 * @psalm-param self::SUCCESS|self::NOTICE|self::ERROR $type
100 public function __construct(
101 string $string = '',
102 private int $type = self::NOTICE,
103 array $params = [],
105 $this->setString($string);
106 $this->setParams($params);
110 * magic method: return string representation for this object
112 public function __toString(): string
114 return $this->getMessage();
118 * get Message of type success
120 * shorthand for getting a simple success message
122 * @param string $string A localized string
123 * e.g. __('Your SQL query has been
124 * executed successfully')
126 public static function success(string $string = ''): self
128 if ($string === '') {
129 $string = __('Your SQL query has been executed successfully.');
132 return new Message($string, self::SUCCESS);
136 * get Message of type error
138 * shorthand for getting a simple error message
140 * @param string $string A localized string e.g. __('Error')
142 public static function error(string $string = ''): self
144 if ($string === '') {
145 $string = __('Error');
148 return new Message($string, self::ERROR);
152 * get Message of type notice
154 * shorthand for getting a simple notice message
156 * @param string $string A localized string
157 * e.g. __('The additional features for working with
158 * linked tables have been deactivated. To find out
159 * why click %shere%s.')
161 public static function notice(string $string): self
163 return new Message($string, self::NOTICE);
167 * get Message with customized content
169 * shorthand for getting a customized message
171 * @param string $message A localized string
172 * @param int $type A numeric representation of the type of message
173 * @psalm-param self::SUCCESS|self::NOTICE|self::ERROR $type
175 public static function raw(string $message, int $type = self::NOTICE): self
177 $r = new Message('', $type);
178 $r->setMessage($message);
179 $r->setBBCode(false);
181 return $r;
185 * get Message for type of affected rows
187 * shorthand for getting a customized message
189 * @param int $rows Number of rows
191 public static function getMessageForAffectedRows(int $rows): self
193 $message = self::success(
194 _ngettext('%1$d row affected.', '%1$d rows affected.', $rows),
196 $message->addParam($rows);
198 return $message;
202 * get Message for type of deleted rows
204 * shorthand for getting a customized message
206 * @param int $rows Number of rows
208 public static function getMessageForDeletedRows(int $rows): self
210 $message = self::success(
211 _ngettext('%1$d row deleted.', '%1$d rows deleted.', $rows),
213 $message->addParam($rows);
215 return $message;
219 * get Message for type of inserted rows
221 * shorthand for getting a customized message
223 * @param int $rows Number of rows
225 public static function getMessageForInsertedRows(int $rows): self
227 $message = self::success(
228 _ngettext('%1$d row inserted.', '%1$d rows inserted.', $rows),
230 $message->addParam($rows);
232 return $message;
236 * get Message of type error with custom content
238 * shorthand for getting a customized error message
240 * @param string $message A localized string
242 public static function rawError(string $message): self
244 return self::raw($message, self::ERROR);
248 * get Message of type notice with custom content
250 * shorthand for getting a customized notice message
252 * @param string $message A localized string
254 public static function rawNotice(string $message): self
256 return self::raw($message);
260 * get Message of type success with custom content
262 * shorthand for getting a customized success message
264 * @param string $message A localized string
266 public static function rawSuccess(string $message): self
268 return self::raw($message, self::SUCCESS);
271 public function isSuccess(): bool
273 return $this->type === self::SUCCESS;
276 public function isNotice(): bool
278 return $this->type === self::NOTICE;
281 public function isError(): bool
283 return $this->type === self::ERROR;
287 * Set whether we should use BB Code when rendering.
289 * @param bool $useBBCode Use BB Code?
291 public function setBBCode(bool $useBBCode): void
293 $this->useBBCode = $useBBCode;
297 * set raw message (overrides string)
299 * @param string $message A localized string
301 public function setMessage(string $message): void
303 $this->message = $message;
307 * set string (does not take effect if raw message is set)
309 * @param string $string string to set
311 public function setString(string $string): void
313 $this->string = $string;
317 * set message type type
319 * @param int $type message type type to set
320 * @psalm-param self::SUCCESS|self::NOTICE|self::ERROR $type
322 public function setType(int $type): void
324 $this->type = $type;
328 * add string or Message parameter
330 * usage
331 * <code>
332 * $message->addParam('[em]some string[/em]');
333 * </code>
335 * @param mixed $param parameter to add
337 public function addParam(mixed $param): void
339 if ($param instanceof self || is_float($param) || is_int($param)) {
340 $this->params[] = $param;
341 } else {
342 $this->params[] = htmlspecialchars((string) $param, ENT_COMPAT);
347 * add parameter as raw HTML, usually in conjunction with strings
349 * usage
350 * <code>
351 * $message->addParamHtml('<img src="img">');
352 * </code>
354 * @param string $param parameter to add
356 public function addParamHtml(string $param): void
358 $this->params[] = self::notice($param);
362 * add a bunch of messages at once
364 * @param Message[] $messages to be added
365 * @param string $separator to use between this and previous string/message
367 public function addMessages(array $messages, string $separator = ' '): void
369 foreach ($messages as $message) {
370 $this->addMessage($message, $separator);
375 * add a bunch of messages at once
377 * @param string[] $messages to be added
378 * @param string $separator to use between this and previous string/message
380 public function addMessagesString(array $messages, string $separator = ' '): void
382 foreach ($messages as $message) {
383 $this->addText($message, $separator);
388 * Real implementation of adding message
390 * @param Message $message to be added
391 * @param string $separator to use between this and previous string/message
393 private function addMessageToList(self $message, string $separator): void
395 if ($separator !== '') {
396 $this->addedMessages[] = $separator;
399 $this->addedMessages[] = $message;
403 * add another raw message to be concatenated on displaying
405 * @param self $message to be added
406 * @param string $separator to use between this and previous string/message
408 public function addMessage(self $message, string $separator = ' '): void
410 $this->addMessageToList($message, $separator);
414 * add another raw message to be concatenated on displaying
416 * @param string $message to be added
417 * @param string $separator to use between this and previous string/message
419 public function addText(string $message, string $separator = ' '): void
421 $this->addMessageToList(self::notice(htmlspecialchars($message)), $separator);
425 * add another html message to be concatenated on displaying
427 * @param string $message to be added
428 * @param string $separator to use between this and previous string/message
430 public function addHtml(string $message, string $separator = ' '): void
432 $this->addMessageToList(self::rawNotice($message), $separator);
436 * set all params at once, usually used in conjunction with string
438 * @param mixed[] $params parameters to set
440 public function setParams(array $params): void
442 $this->params = $params;
446 * return all parameters
448 * @return mixed[]
450 public function getParams(): array
452 return $this->params;
456 * return all added messages
458 * @return (string|Message)[]
460 public function getAddedMessages(): array
462 return $this->addedMessages;
466 * returns unique Message::$hash, if not exists it will be created
468 * @return string Message::$hash
470 public function getHash(): string
472 if ($this->hash === null) {
473 $this->hash = md5($this->type . $this->string . $this->message);
476 return $this->hash;
480 * returns compiled message
482 * @return string complete message
484 public function getMessage(): string
486 $message = $this->message;
488 if ($message === '') {
489 $message = $this->getString();
492 /** @infection-ignore-all */
493 if ($this->isDisplayed()) {
494 $message = $this->getMessageWithIcon($message);
497 if ($this->params !== []) {
498 $message = sprintf($message, ...$this->params);
501 if ($this->useBBCode) {
502 $message = Sanitize::convertBBCode($message, true);
505 foreach ($this->getAddedMessages() as $addMessage) {
506 $message .= $addMessage;
509 return $message;
513 * Returns only message string without image & other HTML.
515 public function getOnlyMessage(): string
517 return $this->message;
521 * returns Message::$string
523 * @return string Message::$string
525 public function getString(): string
527 return $this->string;
531 * returns level of message
533 * @return string level of message
535 public function getLevel(): string
537 return match ($this->type) {
538 self::SUCCESS => 'success',
539 self::NOTICE => 'notice',
540 self::ERROR => 'error'
544 public function getContext(): string
546 return match ($this->getLevel()) {
547 'error' => 'danger',
548 'success' => 'success',
549 default => 'primary',
554 * returns HTML code for displaying this message
556 * @return string whole message box
558 public function getDisplay(): string
560 $this->isDisplayed(true);
562 $context = $this->getContext();
564 $template = new Template();
566 return $template->render('message', ['context' => $context, 'message' => $this->getMessage()]);
570 * sets and returns whether the message was displayed or not
572 * @param bool $isDisplayed whether to set displayed flag
574 * @infection-ignore-all
576 public function isDisplayed(bool $isDisplayed = false): bool
578 if ($isDisplayed) {
579 $this->isDisplayed = true;
582 return $this->isDisplayed;
586 * Returns the message with corresponding image icon
588 * @param string $message the message(s)
590 * @return string message with icon
592 public function getMessageWithIcon(string $message): string
594 $image = match ($this->getLevel()) {
595 'error' => 's_error',
596 'success' => 's_success',
597 default =>'s_notice',
600 return self::notice(Html\Generator::getImage($image)) . ' ' . $message;