From ad310ffee55feeddf85591583d20da98788bee74 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Tue, 15 Apr 2025 18:47:58 +0000 Subject: [PATCH] - WIP on route descriptor --- meta/i18n/fr/lean.api.json | 11 +++++++++++ src/Attribute/EntityField.php | 1 + src/Form/Save.php | 26 ++++++++++++++++---------- view/lean-api/entity_descriptor.phtml | 6 +++++- view/lean-api/form_descriptor.phtml | 4 ++++ view/lean-api/route_descriptor.phtml | 11 +++++++++-- view/lean/layout/docs.phtml | 11 ++++++----- 7 files changed, 52 insertions(+), 18 deletions(-) diff --git a/meta/i18n/fr/lean.api.json b/meta/i18n/fr/lean.api.json index 54c4c42..0403645 100644 --- a/meta/i18n/fr/lean.api.json +++ b/meta/i18n/fr/lean.api.json @@ -1,4 +1,15 @@ { + "descriptor": { + "form": { + "none": "Aucun formulaire existant pour cet API" + }, + "entity": { + "none": "Aucune entité trouvée pour cet API" + }, + "route": { + "none": "Aucune route trouvée pour cet API" + } + }, "form": { "save": { "error": { diff --git a/src/Attribute/EntityField.php b/src/Attribute/EntityField.php index 1b01327..129a16c 100644 --- a/src/Attribute/EntityField.php +++ b/src/Attribute/EntityField.php @@ -13,5 +13,6 @@ class EntityField public ?string $regexFormat = null, public mixed $example = null, public ?string $field = null, + public ?string $setterMethod = null, ) {} } \ No newline at end of file diff --git a/src/Form/Save.php b/src/Form/Save.php index 9a36ecd..d5c69e3 100644 --- a/src/Form/Save.php +++ b/src/Form/Save.php @@ -33,10 +33,14 @@ abstract class Save implements \Picea\Ui\Method\FormInterface { $entity = $this->getEntity(); if ($entity->isLoaded() ) { - $entity->updatedAt = new Datetime(); + if (property_exists($entity, 'updatedAt')) { + $entity->updatedAt = new Datetime(); + } } else { - $entity->createdAt = new Datetime(); + if (property_exists($entity, 'createdAt')) { + $entity->createdAt = new Datetime(); + } } try { @@ -63,18 +67,20 @@ abstract class Save implements \Picea\Ui\Method\FormInterface { foreach($entity::resolveEntity()->fieldList() as $key => $property) { $field = $property->getAttribute(Field::class)->object; - if (! $field->readonly) { + if (! $field->readonly || ! $entity->isLoaded()) { $apiField = $property->getAttribute(EntityField::class)->object ?? null; if ($apiField) { - if ($apiField->field) { - if ( isset($context->{$apiField->field}) ) { - $entity->$key = $context->{$apiField->field}; + $var = $apiField->field ?: $key; + + if ( isset($context->{$var}) ) { + if ($apiField->setterMethod) { + # Use a setter method + call_user_func([ $entity, $apiField->setterMethod ], $context->{$var}); } - } - else { - if ( isset($context->{$key}) ) { - $entity->$key = $context->{$key}; + else { + # Direct property set + $entity->$key = $context->{$var}; } } } diff --git a/view/lean-api/entity_descriptor.phtml b/view/lean-api/entity_descriptor.phtml index 5ccda65..a6b2639 100644 --- a/view/lean-api/entity_descriptor.phtml +++ b/view/lean-api/entity_descriptor.phtml @@ -1,8 +1,10 @@ +{% language.set "lean.api.descriptor.entity" %} + {% function yesOrNo(bool $toggle) : void %} {{ $toggle ? 'oui' : 'non' }} {% endfunction %} -{% function length(int|null $length): void %} +{% function length(int|null|string $length): void %} {{ $length ?? "non-défini" }} {% endfunction %} @@ -61,5 +63,7 @@ {% endforeach %} + {% or %} + {% _ "none" %} {% endforeach %} \ No newline at end of file diff --git a/view/lean-api/form_descriptor.phtml b/view/lean-api/form_descriptor.phtml index 91c6e9f..7681698 100644 --- a/view/lean-api/form_descriptor.phtml +++ b/view/lean-api/form_descriptor.phtml @@ -1,3 +1,5 @@ +{% language.set "lean.api.descriptor.form" %} + {% function yesOrNo(bool $toggle) : void %} {{ $toggle ? 'oui' : 'non' }} {% endfunction %} @@ -34,5 +36,7 @@ {% endforeach %} + {% or %} + {% _ "none" %} {% endforeach %} \ No newline at end of file diff --git a/view/lean-api/route_descriptor.phtml b/view/lean-api/route_descriptor.phtml index e52f582..bc9fb28 100644 --- a/view/lean-api/route_descriptor.phtml +++ b/view/lean-api/route_descriptor.phtml @@ -1,14 +1,21 @@ +{% language.set "lean.api.descriptor.route" %} + \ No newline at end of file diff --git a/view/lean/layout/docs.phtml b/view/lean/layout/docs.phtml index 439ced5..b838b8b 100644 --- a/view/lean/layout/docs.phtml +++ b/view/lean/layout/docs.phtml @@ -35,12 +35,13 @@ .forms .form-name {background: #9cc5e6;color: #284168;font-size:110%} .routes-wrapper {padding:8px} - .routes-wrapper li + li {margin-top: 5px} - .routes-wrapper li {display:flex;align-items:center;border: 1px solid #ccc;} - .routes-wrapper .route-method {line-height: 1.8rem;padding:6px 5px 0 5px;width:80px;text-align:center;font-weight:bold;color:#fff;} - .routes-wrapper .route-link {line-height: 1.8rem;padding:0 10px} + .routes-wrapper li {display:flex;border: 1px solid #ccc;align-items: stretch;} + .routes-wrapper li + li {margin-top: 5px;} + .routes-wrapper .route-method {display:flex;align-items: center; justify-content: center; line-height: 1.8rem;padding:6px 5px 0 5px;min-width:80px;text-align:center;font-weight:bold;color:#fff;} + .routes-wrapper .route-link {line-height: 1.8rem;padding:0 10px;display: flex;align-items: center;} + .routes-wrapper .route-link span {margin-left: 7px;} .routes-wrapper .route-link a {font-family:monospace;font-size:.85em} - .routes-wrapper .route-name {margin-left:auto;font-weight:bold;width: 200px;text-align: right;background:rgba(0, 0, 0, 0.02);line-height: 1.8rem;padding:6px 7px 0 5px;} + .routes-wrapper .route-name {margin-left:auto;font-weight:bold;min-width: 20%;text-align: right;background:rgba(0, 0, 0, 0.02);line-height: 1.8rem;padding:6px 7px 0 5px;} .routes-wrapper li.method-get {background:#e7eff7;border-color: #bfcfdd;} .routes-wrapper li.method-get .route-method {background:#0f6ab4;} .routes-wrapper li.method-get .route-link a, .routes-wrapper li.method-get .route-name {color: #0f6ab4;}