133 lines
3.4 KiB
PHP
133 lines
3.4 KiB
PHP
<?php declare(strict_types=1);
|
|
|
|
namespace Lean\Api\Lib;
|
|
|
|
use Lean\Api\Factory\MessageFactoryInterface;
|
|
use Picea\Ui\Method\FormMessage;
|
|
|
|
class Message implements FormMessage, MessageFactoryInterface {
|
|
|
|
const MESSAGE_TYPE = [
|
|
'success' => [
|
|
'class' => 'success',
|
|
'header' => 'Succès !',
|
|
'message' => ''
|
|
],
|
|
|
|
'error' => [
|
|
'class' => 'danger',
|
|
'header' => 'Error !',
|
|
'message' => ''
|
|
],
|
|
|
|
'warning' => [
|
|
'class' => 'warning',
|
|
'header' => 'Attention !',
|
|
'message' => ''
|
|
],
|
|
|
|
'information' => [
|
|
'class' => 'information',
|
|
'header' => 'À savoir !',
|
|
'message' => ''
|
|
],
|
|
|
|
'debug' => [
|
|
'class' => 'debug',
|
|
'header' => 'Debug :',
|
|
'message' => ''
|
|
],
|
|
|
|
'trace' => [
|
|
'class' => 'trace',
|
|
'header' => 'Trace :',
|
|
'message' => ''
|
|
]
|
|
];
|
|
|
|
public string $type;
|
|
|
|
public string $message;
|
|
|
|
public string $class;
|
|
|
|
public ? string $header = null;
|
|
|
|
public function __construct(string $message = "", string $type = "error", ? string $header = null, ? string $class = null)
|
|
{
|
|
$this->message = $message;
|
|
|
|
$this->type = $type;
|
|
|
|
if ( $header !== null ) {
|
|
$this->header = $header;
|
|
}
|
|
else {
|
|
$this->header = static::MESSAGE_TYPE[$type]['header'];
|
|
}
|
|
|
|
if ( $class !== null ) {
|
|
$this->class = $class;
|
|
}
|
|
else {
|
|
$this->class = static::MESSAGE_TYPE[$type]['class'];
|
|
}
|
|
}
|
|
|
|
public function render() : string
|
|
{
|
|
return <<<HTML
|
|
<article class="message is-{$this->class}" role="alert">
|
|
<div class="message-body">
|
|
<strong>{$this->header}</strong>
|
|
<span>{$this->message}</span>
|
|
</div>
|
|
</article>
|
|
HTML;
|
|
}
|
|
|
|
public function renderJson() : array
|
|
{
|
|
return [
|
|
'name' => 'message',
|
|
'type' => $this->type,
|
|
'message' => $this->message,
|
|
];
|
|
}
|
|
|
|
public function isError() : bool
|
|
{
|
|
return $this->type === "error";
|
|
}
|
|
|
|
public static function generateSuccess(string $message, ? string $header = null, ? string $class = null) : self
|
|
{
|
|
return new static($message, 'success', $header, $class);
|
|
}
|
|
|
|
public static function generateError(string $message, ? string $header = null, ? string $class = null) : self
|
|
{
|
|
return new static($message, 'error', $header, $class);
|
|
}
|
|
|
|
public static function generateWarning(string $message, ? string $header = null, ? string $class = null) : self
|
|
{
|
|
return new static($message, 'warning', $header, $class);
|
|
}
|
|
|
|
public static function generateInformation(string $message, ? string $header = null, ? string $class = null) : self
|
|
{
|
|
return new static($message, 'information', $header, $class);
|
|
}
|
|
|
|
public static function generateDebug(string $message, ? string $header = null, ? string $class = null) : self
|
|
{
|
|
return new static($message, 'debug', $header, $class);
|
|
}
|
|
|
|
public static function generateTrace(string $message, ? string $header = null, ? string $class = null) : self
|
|
{
|
|
return new static($message, 'trace', $header, $class);
|
|
}
|
|
}
|