- WIP on visual of API

This commit is contained in:
Dave Mc Nicoll committed 2025-04-10 17:54:04 +00:00
1 parent 3d81b71473
commit 21f315cc22
10 files changed
+230 -32

No files matched your search

+13
View File
@@ -0,0 +1,13 @@
<?php
namespace Lean\Api\Factory;
interface MessageFactoryInterface
{
public static function generateSuccess(string $message, ? string $header = null, ? string $class = null) : self;
public static function generateError(string $message, ? string $header = null, ? string $class = null) : self;
public static function generateWarning(string $message, ? string $header = null, ? string $class = null) : self;
public static function generateInformation(string $message, ? string $header = null, ? string $class = null) : self;
public static function generateDebug(string $message, ? string $header = null, ? string $class = null) : self;
public static function generateTrace(string $message, ? string $header = null, ? string $class = null) : self;
}
+4 -7
View File
@@ -6,11 +6,8 @@ use CSSLSJ\ExamenFga\Api\{Lib};
use Picea\Ui\Method\{FormContextInterface};
use Ulmus\Entity\EntityInterface;
class Delete implements \Picea\Ui\Method\FormInterface {
public function __construct(
public EntityInterface $entity,
) {}
abstract class Delete implements \Picea\Ui\Method\FormInterface {
use FormTrait;
public function getEntity() : EntityInterface
{
@@ -22,7 +19,7 @@ class Delete implements \Picea\Ui\Method\FormInterface {
public function validate(FormContextInterface $context) : bool
{
if ( ! $this->entity->isLoaded() ) {
$context->pushMessage(Lib\Message::generateError(
$context->pushMessage($this->message::generateError(
$this->lang('lean.api.form.delete.error.entity')
));
}
@@ -34,7 +31,7 @@ class Delete implements \Picea\Ui\Method\FormInterface {
{
try {
if ( $this->getEntity()::repository()->destroy($this->getEntity()) ) {
$context->pushMessage(Lib\Message::generateSuccess(
$context->pushMessage($this->message::generateSuccess(
$this->lang('lean.api.form.delete.success.save')
));
}
+20
View File
@@ -0,0 +1,20 @@
<?php
namespace Lean\Api\Form;
use Lean\Api\Factory\MessageFactoryInterface;
use Lean\LanguageHandler;
trait FormTrait
{
public function __construct(
protected LanguageHandler $languageHandler,
protected MessageFactoryInterface $message,
# public EntityInterface $entity,
) {}
protected function lang(string $key, array $variables = [])
{
return $this->languageHandler->languageFromKey($key, $variables);
}
}
+13
View File
@@ -0,0 +1,13 @@
<?php
namespace Lean\Api\Form;
enum MessageTypeEnum
{
case Error;
case Success;
case Information;
case Warning;
case Debug;
case Trace;
}
+3 -16
View File
@@ -2,24 +2,16 @@
namespace Lean\Api\Form;
use CSSLSJ\ExamenFga\Api\{Form\Location\SessionContext, Lib, Entity};
use Picea\Ui\Method\{ FormContextInterface, Message\ErrorMessage };
use Picea\Ui\Method\{ FormContextInterface, };
use Ulmus\Attribute\Property\Field;
use Lean\Api\Attribute\EntityField;
use Lean\LanguageHandler;
use Psr\Http\Message\ServerRequestInterface;
use Ulmus\Entity\EntityInterface;
use Ulmus\Entity\Field\Datetime;
use function CSSLSJ\ExamenFga\Api\View\{ lang };
abstract class Save implements \Picea\Ui\Method\FormInterface {
public function __construct(
protected LanguageHandler $languageHandler,
# public EntityInterface $entity,
) {}
use FormTrait;
public function getEntity() : EntityInterface
{
@@ -51,7 +43,7 @@ abstract class Save implements \Picea\Ui\Method\FormInterface {
$this->assignContextToEntity($context);
if ( $entity::repository()->save($entity) ) {
$context->pushMessage(Lib\Message::generateSuccess(
$context->pushMessage($this->message::generateSuccess(
$this->lang('lean.api.form.save.success.entity')
));
}
@@ -64,11 +56,6 @@ abstract class Save implements \Picea\Ui\Method\FormInterface {
}
}
protected function lang(string $key, array $variables = [])
{
return $this->languageHandler->languageFromKey($key, $variables);
}
protected function assignContextToEntity(FormContextInterface $context) : void
{
$entity = $this->getEntity();
+132
View File
@@ -0,0 +1,132 @@
<?php declare(strict_types=1);
namespace Lean\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);
}
}
+2 -1
View File
@@ -45,7 +45,8 @@ HTML;
'path' => $base.$path,
'cleaned' => $cleaned,
'description'=> $route->description,
'methods' =>implode(', ', (array)$route->method),
#'methods' =>implode(', ', (array)$route->method),
'methods' => (array) $route->method,
];
}
}