nur-sery/nur_src/v/bs3/Bs3Messenger.php

175 lines
6.0 KiB
PHP
Raw Normal View History

2023-12-03 22:10:18 +04:00
<?php
namespace nur\v\bs3;
use nur\A;
use nur\data\types\md_utils;
use nur\data\types\Metadata;
use nur\v\html5\Html5Messenger;
use nur\v\v;
use nur\v\vo;
class Bs3Messenger extends Html5Messenger {
const KEY_DISMISSIBLE = "dismissible";
const MESSAGE_OPTIONS_SCHEMA = [
self::KEY_DISMISSIBLE => ["bool", false, "le message peut-il être masqué par l'utilisateur?"],
];
/** @var Metadata */
private static $message_md;
protected static function message_md(): Metadata {
return md_utils::ensure_md(self::$message_md
, array_merge(self::MESSAGE_SCHEMA, self::MESSAGE_OPTIONS_SCHEMA));
}
const ICON_ERROR = Bs3IconManager::ERROR[0];
const ICON_WARNING = Bs3IconManager::WARNING[0];
const ICON_INFO = Bs3IconManager::INFO[0];
const ICON_DEBUG = Bs3IconManager::DEBUG[0];
const SECTION_LEVEL_CLASSES = [
# les clés doivent être ordonnées de la plus grande à la plus petite
self::LEVEL_CRITICAL => "msg-section-critical",
self::LEVEL_MAJOR => "msg-section-major",
self::LEVEL_NORMAL => "msg-section-normal",
self::LEVEL_MINOR => "msg-section-minor",
];
const SECTION_TYPE_CLASSES = [
# les clés doivent être ordonnées de la plus grande à la plus petite
self::TYPE_ERROR => "msg-section-danger",
self::TYPE_WARNING => "msg-section-warning",
self::TYPE_INFO => "msg-section-default",
self::TYPE_DEBUG => "msg-section-default",
];
const SECTION_TYPE_PREFIXES = [
# les clés doivent être ordonnées de la plus grande à la plus petite
self::LEVEL_CRITICAL => [
self::TYPE_ERROR => self::ICON_ERROR.self::ICON_ERROR." ",
self::TYPE_WARNING => self::ICON_WARNING.self::ICON_WARNING." ",
self::TYPE_DEBUG => self::ICON_INFO.self::ICON_INFO." ",
],
self::LEVEL_MAJOR => [
self::TYPE_ERROR => self::ICON_ERROR." ",
self::TYPE_WARNING => self::ICON_WARNING." ",
self::TYPE_DEBUG => self::ICON_INFO." ",
],
self::LEVEL_NORMAL => [
self::TYPE_DEBUG => null,
],
self::LEVEL_MINOR => [
self::TYPE_DEBUG => null,
],
];
const GROUP_LEVEL_CLASSES = [
# les clés doivent être ordonnées de la plus grande à la plus petite
self::LEVEL_CRITICAL => "msg-group-critical",
self::LEVEL_MAJOR => "msg-group-major",
self::LEVEL_NORMAL => "msg-group-normal",
self::LEVEL_MINOR => "msg-group-minor",
];
const GROUP_TYPE_CLASSES = [
# les clés doivent être ordonnées de la plus grande à la plus petite
self::TYPE_ERROR => "msg-group-danger",
self::TYPE_WARNING => "msg-group-warning",
self::TYPE_INFO => "msg-group-info",
self::TYPE_DEBUG => "msg-group-light",
];
const LEVEL_CLASSES = [
# les clés doivent être ordonnées de la plus grande à la plus petite
self::LEVEL_CRITICAL => "msg-critical",
self::LEVEL_MAJOR => "msg-major",
self::LEVEL_NORMAL => "msg-normal",
self::LEVEL_MINOR => "msg-minor",
];
const TYPE_CLASSES = [
# les clés doivent être ordonnées de la plus grande à la plus petite
self::TYPE_ERROR => "msg-danger",
self::TYPE_WARNING => "msg-warning",
self::TYPE_INFO => "msg-info",
self::TYPE_DEBUG => "msg-light",
];
const TYPE_PREFIXES = [
# les clés doivent être ordonnées de la plus grande à la plus petite
self::LEVEL_CRITICAL => [
self::TYPE_ERROR => self::ICON_ERROR.self::ICON_ERROR." ",
self::TYPE_WARNING => self::ICON_WARNING.self::ICON_WARNING." ",
self::TYPE_DEBUG => self::ICON_INFO.self::ICON_INFO." ",
],
self::LEVEL_MAJOR => [
self::TYPE_ERROR => self::ICON_ERROR." ",
self::TYPE_WARNING => self::ICON_WARNING." ",
self::TYPE_INFO => self::ICON_INFO." ",
self::TYPE_DEBUG => self::ICON_DEBUG." ",
],
self::LEVEL_NORMAL => [
self::TYPE_DEBUG => null,
],
self::LEVEL_MINOR => [
self::TYPE_ERROR => "[error] ",
self::TYPE_WARNING => "[warn] ",
self::TYPE_INFO => null,
self::TYPE_DEBUG => "[debug] ",
],
];
const RESULT_CLASS_PREFIXES = [
self::RESULT_FAILURE => ["msg-result-failure", ""],
self::RESULT_SUCCESS => ["msg-result-success", ""],
self::RESULT_NEUTRAL => ["msg-result-neutral", ". "],
self::RESULT_NONE => [null, null],
];
protected function printMsg(
?array $groups,
bool $showUser, $userMsg,
bool $showTech, $techMsg,
bool $showException, $exceptionMsg,
int $type, int $level, array $options
): void {
$group = A::last($groups);
if ($group === false) {
# groupe neutralisé
return;
}
$groupPrefix = $this->getGroupPrefix($group);
$result = $type & self::RESULT_MASK;
$type = $type & self::TYPE_MASK;
[$typeClass, $typePrefix] = $this->getClassPrefix($type, $level
, static::LEVEL_CLASSES
, static::TYPE_CLASSES
, static::TYPE_PREFIXES
);
[$resultClass, $resultPrefix] = $this->getResultClassPrefix($result);
if ($showUser || $showTech || $showException) {
$dismissible = $options[self::KEY_DISMISSIBLE];
$lines = v::sdiv([
"class" => ["msg", $typeClass, $resultClass, $dismissible? "alert msg-dismissible": null],
v::if($dismissible, v::tag("button", [
"type" => "button", "class" => "close", "aria-label" => "Close",
"data-dismiss" => "alert",
v::span(["aria-hidden" => "true", "&times;"]),
])),
$typePrefix, $resultPrefix, $groupPrefix,
]);
if ($showUser) $lines[] = $userMsg;
elseif ($userMsg) $lines[] = ["\n<!--", $userMsg, "-->"];
if ($showTech) $lines[] = $techMsg;
elseif ($techMsg) $lines[] = ["\n<!--", $techMsg, "-->"];
if ($showException) $lines[] = $exceptionMsg;
elseif ($exceptionMsg) $lines[] = ["\n<!--", $exceptionMsg, "-->"];
$lines[] = v::ediv();
} else {
$lines = ["\n<!--", $typePrefix, $resultPrefix];
if ($userMsg) $lines[] = ["\n", $userMsg];
if ($techMsg) $lines[] = ["\n", $techMsg];
if ($exceptionMsg) $lines[] = ["\n", $exceptionMsg];
$lines[] = "-->";
}
vo::print($lines);
}
}