Skip to content

Commit

Permalink
Extract message type match from Generator::getMessage
Browse files Browse the repository at this point in the history
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed May 16, 2024
1 parent cf44e8b commit d17e46c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
10 changes: 1 addition & 9 deletions src/Html/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ public static function getStartAndNumberOfRowsFieldsetData(string $sqlQuery): ar
public static function getMessage(
Message|string $message,
string|null $sqlQuery = null,
MessageType|string $type = MessageType::Notice,
MessageType $type = MessageType::Notice,
): string {
$retval = '';

Expand All @@ -422,14 +422,6 @@ public static function getMessage(
}

if (is_string($message)) {
if (is_string($type)) {
$type = match ($type) {
'success' => MessageType::Success,
'error' => MessageType::Error,
default => MessageType::Notice,
};
}

$message = new Message($message, $type);
}

Expand Down
17 changes: 16 additions & 1 deletion src/Twig/MessageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpMyAdmin\Html\Generator;
use PhpMyAdmin\Message;
use PhpMyAdmin\MessageType;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
Expand Down Expand Up @@ -41,6 +42,20 @@ public function getFilters(): array
/** @inheritDoc */
public function getFunctions(): array
{
return [new TwigFunction('statement_message', Generator::getMessage(...), ['is_safe' => ['html']])];
return [
new TwigFunction(
'statement_message',
static function (string $message, string $statement, string $context): string {
$type = match ($context) {
'success' => MessageType::Success,
'error', 'danger' => MessageType::Error,
default => MessageType::Notice,
};

return Generator::getMessage($message, $statement, $type);
},
['is_safe' => ['html']],
),
];
}
}

0 comments on commit d17e46c

Please sign in to comment.