From 2585bc33155334c3ce472588ad39c2122ff674cc Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Mon, 3 Feb 2025 19:15:36 +0000 Subject: [PATCH] - Added a new automated Form save --- meta/i18n/fr/lean.api.json | 17 ++++++ src/Attribute/EntityField.php | 1 + src/Form/Delete.php | 43 ++++++++++++++++ src/Form/Save.php | 97 +++++++++++++++++++++++++++++++++++ 4 files changed, 158 insertions(+) create mode 100644 meta/i18n/fr/lean.api.json create mode 100644 src/Form/Delete.php create mode 100644 src/Form/Save.php diff --git a/meta/i18n/fr/lean.api.json b/meta/i18n/fr/lean.api.json new file mode 100644 index 0000000..0574fdb --- /dev/null +++ b/meta/i18n/fr/lean.api.json @@ -0,0 +1,17 @@ +{ + "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}'" + }, + "success": { + "save": "Sauvegarde effectué avec succès" + } + }, + "delete": { + + } + } +} \ No newline at end of file diff --git a/src/Attribute/EntityField.php b/src/Attribute/EntityField.php index 95e53c5..1b01327 100644 --- a/src/Attribute/EntityField.php +++ b/src/Attribute/EntityField.php @@ -12,5 +12,6 @@ class EntityField public ?int $maxLength = null, public ?string $regexFormat = null, public mixed $example = null, + public ?string $field = null, ) {} } \ No newline at end of file diff --git a/src/Form/Delete.php b/src/Form/Delete.php new file mode 100644 index 0000000..175bc98 --- /dev/null +++ b/src/Form/Delete.php @@ -0,0 +1,43 @@ +entity; + } + + public function initialize(FormContextInterface $context) : void {} + + public function validate(FormContextInterface $context) : bool + { + return $context->valid(); + } + + public function execute(FormContextInterface $context) : void + { + try { + if ( $this->getEntity()::repository()->destroy($this->getEntity()) ) { + $context->pushMessage(Lib\Message::generateSuccess( + "Suppression effectué avec succès" + )); + } + else { + throw new \InvalidArgumentException("Suppression impossible; une erreur semble survenue..."); + } + } + catch(\Throwable $ex) { + throw new \ErrorException(sprintf("Une erreur est survenue : '%s'", $ex->getMessage())); + } + } +} diff --git a/src/Form/Save.php b/src/Form/Save.php new file mode 100644 index 0000000..95a2ab2 --- /dev/null +++ b/src/Form/Save.php @@ -0,0 +1,97 @@ +entity)) { + throw new \InvalidArgumentException($this->lang("lean.api.form.save.error.entity")); + } + + return $this->entity; + } + + public function validate(FormContextInterface $context) : bool + { + # Context is validating inputs + return $context->valid($this->getEntity()->isLoaded() ? $this->getEntity() : null); + } + + public function execute(FormContextInterface $context) : void + { + $entity = $this->getEntity(); + + if ($entity->isLoaded() ) { + $entity->updatedAt = new Datetime(); + } + else { + $entity->createdAt = new Datetime(); + } + + try { + $this->assignContextToEntity($context); + + if ( $entity::repository()->save($entity) ) { + $context->pushMessage(Lib\Message::generateSuccess( + $this->lang('lean.api.form.save.success.entity') + )); + } + else { + throw new \InvalidArgumentException($this->lang('lean.api.form.save.error.entity')); + } + } + catch(\Throwable $ex) { + throw new \ErrorException($this->lang('lean.api.form.save.error.pdo', [ 'error' => $ex->getMessage() ])); + } + } + + protected function lang(string $key, array $variables = []) + { + return $this->languageHandler->languageFromKey($key, $variables); + } + + protected function assignContextToEntity(FormContextInterface $context) : void + { + $entity = $this->getEntity(); + + foreach($entity::resolveEntity()->fieldList() as $key => $property) { + $field = $property->getAttribute(Field::class)->object; + + if (! $field->readonly) { + $apiField = $property->getAttribute(EntityField::class)->object ?? null; + + if ($apiField) { + if ($apiField->field) { + if ( isset($context->{$apiField->field}) ) { + $entity->$key = $context->{$apiField->field}; + } + } + else { + if ( isset($context->{$key}) ) { + $entity->$key = $context->{$key}; + } + } + } + } + } + } +}