diff --git a/meta/definitions/software.php b/meta/definitions/software.php index 867b671..d6f6794 100644 --- a/meta/definitions/software.php +++ b/meta/definitions/software.php @@ -35,4 +35,6 @@ return [ 'Lean\\Api\\Controller' => implode(DIRECTORY_SEPARATOR, [ $path, "src", "Controller", "" ]), ],*/ ], + + \Lean\Api\Factory\MessageFactoryInterface::class => DI\autowire(\Lean\Lib\Message::class), ]; \ No newline at end of file diff --git a/src/Factory/MessageFactoryInterface.php b/src/Factory/MessageFactoryInterface.php new file mode 100644 index 0000000..f72820a --- /dev/null +++ b/src/Factory/MessageFactoryInterface.php @@ -0,0 +1,13 @@ +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') )); } diff --git a/src/Form/FormTrait.php b/src/Form/FormTrait.php new file mode 100644 index 0000000..708cdb5 --- /dev/null +++ b/src/Form/FormTrait.php @@ -0,0 +1,20 @@ +languageHandler->languageFromKey($key, $variables); + } +} \ No newline at end of file diff --git a/src/Form/MessageTypeEnum.php b/src/Form/MessageTypeEnum.php new file mode 100644 index 0000000..5747c56 --- /dev/null +++ b/src/Form/MessageTypeEnum.php @@ -0,0 +1,13 @@ +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(); diff --git a/src/Lib/Message.php b/src/Lib/Message.php new file mode 100644 index 0000000..9e972a5 --- /dev/null +++ b/src/Lib/Message.php @@ -0,0 +1,132 @@ + [ + '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 << +
+ {$this->header} + {$this->message} +
+ + 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); + } +} diff --git a/src/RouteDescriptor.php b/src/RouteDescriptor.php index 9db4e8d..2031254 100644 --- a/src/RouteDescriptor.php +++ b/src/RouteDescriptor.php @@ -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, ]; } } diff --git a/view/lean-api/route_descriptor.phtml b/view/lean-api/route_descriptor.phtml index 86143ef..e52f582 100644 --- a/view/lean-api/route_descriptor.phtml +++ b/view/lean-api/route_descriptor.phtml @@ -1,9 +1,14 @@ -