From 3ce356e1f27ac617c904aab75caa64f4f130fbde Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Fri, 25 Apr 2025 20:08:52 +0000 Subject: [PATCH] - WIP on save and delete forms --- meta/definitions/software.php | 2 +- meta/i18n/fr/lean.api.json | 4 +++- src/DescriptorTrait.php | 3 +++ src/Form/Delete.php | 9 +-------- src/Form/FormTrait.php | 21 +++++++++++++++++++++ src/Form/Save.php | 10 ++-------- src/Lib/Message.php | 4 ++-- 7 files changed, 33 insertions(+), 20 deletions(-) diff --git a/meta/definitions/software.php b/meta/definitions/software.php index d6f6794..f90df3d 100644 --- a/meta/definitions/software.php +++ b/meta/definitions/software.php @@ -36,5 +36,5 @@ return [ ],*/ ], - \Lean\Api\Factory\MessageFactoryInterface::class => DI\autowire(\Lean\Lib\Message::class), + \Lean\Api\Factory\MessageFactoryInterface::class => DI\autowire(\Lean\Api\Lib\Message::class), ]; \ No newline at end of file diff --git a/meta/i18n/fr/lean.api.json b/meta/i18n/fr/lean.api.json index 0403645..857bd49 100644 --- a/meta/i18n/fr/lean.api.json +++ b/meta/i18n/fr/lean.api.json @@ -11,9 +11,11 @@ } }, "form": { + "error": { + "entity": "Une propriété $entity est nécessaire dans ce form" + }, "save": { "error": { - "entity": "Une propriété $entity est nécessaire dans ce form", "save": "Une erreur est survenue en tenant de sauvegarder les données", "pdo": "Une erreur est survenue : '{$error}'" }, diff --git a/src/DescriptorTrait.php b/src/DescriptorTrait.php index 60a9c77..9640e04 100644 --- a/src/DescriptorTrait.php +++ b/src/DescriptorTrait.php @@ -15,6 +15,9 @@ trait DescriptorTrait elseif ($value instanceof \UnitEnum) { return $value::class . "::" . $value->name; } + elseif (is_array($value)) { + return sprintf('[ %s ]', implode(', ', array_map([ self::class, 'displayValue' ], $value))); + } return $value; } diff --git a/src/Form/Delete.php b/src/Form/Delete.php index 91617c5..3820214 100644 --- a/src/Form/Delete.php +++ b/src/Form/Delete.php @@ -9,13 +9,6 @@ use Ulmus\Entity\EntityInterface; abstract class Delete implements \Picea\Ui\Method\FormInterface { use FormTrait; - public function getEntity() : EntityInterface - { - return $this->entity; - } - - public function initialize(FormContextInterface $context) : void {} - public function validate(FormContextInterface $context) : bool { if ( ! $this->entity->isLoaded() ) { @@ -24,7 +17,7 @@ abstract class Delete implements \Picea\Ui\Method\FormInterface { )); } - return $context->valid(); + return $context->valid($this->getEntity()->isLoaded() ? $this->getEntity() : null); } public function execute(FormContextInterface $context) : void diff --git a/src/Form/FormTrait.php b/src/Form/FormTrait.php index 708cdb5..fd861e6 100644 --- a/src/Form/FormTrait.php +++ b/src/Form/FormTrait.php @@ -4,6 +4,8 @@ namespace Lean\Api\Form; use Lean\Api\Factory\MessageFactoryInterface; use Lean\LanguageHandler; +use Picea\Ui\Method\FormContextInterface; +use Ulmus\Entity\EntityInterface; trait FormTrait { @@ -13,8 +15,27 @@ trait FormTrait # public EntityInterface $entity, ) {} + public function initialize(FormContextInterface $context) : void + { + if(isset($this->contextClass) && ! $context instanceof $this->contextClass ) { + throw new \LogicException( + sprintf("Your context type should be a %s", $this->contextClass) + ); + } + } + protected function lang(string $key, array $variables = []) { return $this->languageHandler->languageFromKey($key, $variables); } + + public function getEntity() : EntityInterface + { + if (! isset($this->entity)) { + throw new \InvalidArgumentException($this->lang("lean.api.form.error.entity")); + } + + return $this->entity; + } + } \ No newline at end of file diff --git a/src/Form/Save.php b/src/Form/Save.php index d5c69e3..3bd9eaa 100644 --- a/src/Form/Save.php +++ b/src/Form/Save.php @@ -2,6 +2,7 @@ namespace Lean\Api\Form; +use CSLSJ\Lean\Form\Session\EmailContext; use Picea\Ui\Method\{ FormContextInterface, }; use Ulmus\Attribute\Property\Field; @@ -13,14 +14,7 @@ use Ulmus\Entity\Field\Datetime; abstract class Save implements \Picea\Ui\Method\FormInterface { use FormTrait; - public function getEntity() : EntityInterface - { - if (! isset($this->entity)) { - throw new \InvalidArgumentException($this->lang("lean.api.form.save.error.entity")); - } - - return $this->entity; - } + protected string $contextClass; public function validate(FormContextInterface $context) : bool { diff --git a/src/Lib/Message.php b/src/Lib/Message.php index 9e972a5..b3b3ed0 100644 --- a/src/Lib/Message.php +++ b/src/Lib/Message.php @@ -1,6 +1,6 @@ message = $message;