diff --git a/src/Action/Console/EntityGenerate.php b/src/Action/Console/EntityGenerate.php index 44445b6..1180a11 100644 --- a/src/Action/Console/EntityGenerate.php +++ b/src/Action/Console/EntityGenerate.php @@ -58,7 +58,9 @@ class EntityGenerate $webPath = $this->pathFromNamespace($this->webNamespace, $this->application->routes); if (empty($webPath)) { - throw new \InvalidArgumentException("Could not find web path routes from given namespaces."); + throw new \InvalidArgumentException( + sprintf("Could not find web path routes from given namespaces. Given info : %s, %s", $this->webNamespace, json_encode($this->application->routes)) + ); } } @@ -66,7 +68,9 @@ class EntityGenerate $apiPath = $this->pathFromNamespace($this->apiNamespace, $this->application->routes); if (empty($apiPath)) { - throw new \InvalidArgumentException("Could not find web path routes from given namespaces."); + throw new \InvalidArgumentException( + sprintf("Could not find web path routes from given namespaces. Given info : %s, %s", $this->apiNamespace, json_encode($this->application->routes)) + ); } if ($this->includeDocs) { @@ -101,12 +105,12 @@ class EntityGenerate } if (isset($formPath)) { - if ($this->saveForm) { + if ($this->saveForm ?? false) { $this->writeToFile($formPath, $this->saveForm, $this->compiledSaveForm,); $this->writeToFile($formPath, $this->saveContext, $this->compiledSaveFormContext,); } - if ($this->deleteForm) { + if ($this->deleteForm ?? false) { $this->writeToFile($formPath, $this->deleteForm, $this->compiledDeleteForm,); $this->writeToFile($formPath, $this->deleteContext, $this->compiledDeleteFormContext,); } diff --git a/src/Form/Save.php b/src/Form/Save.php index 053b412..d21fe9c 100644 --- a/src/Form/Save.php +++ b/src/Form/Save.php @@ -26,6 +26,11 @@ abstract class Save extends Form implements \Picea\Ui\Method\FormInterface { protected string $messageWarningSave = 'lean.api.form.save.warning.entity'; + protected array $fields = [ + 'updatedAt' => 'updatedAt', + 'createdAt' => 'createdAt' + ]; + protected array $reflectedProperties; protected string $contextClass = FormContext::class; @@ -72,6 +77,8 @@ abstract class Save extends Form implements \Picea\Ui\Method\FormInterface { } } + $this->handleTimestamp($entity); + try { $this->assignContextToEntity($context); @@ -112,38 +119,38 @@ abstract class Save extends Form implements \Picea\Ui\Method\FormInterface { { $entity = $this->getEntity(); - foreach($entity::resolveEntity()->fieldList() as $key => $property) { - $field = $property->getAttribute(Field::class)->object; + foreach($entity::resolveEntity()->getPropertyHavingAttribute(EntityField::class) as $key => $entityField) { + $property = $entity::resolveEntity()->reflectedClass->getProperties(true)[$key]; - if (! $field->readonly || ! $entity->isLoaded()) { + if ($property) { + if (($property->getAttribute(Field::class)->object->readonly ?? false) && $entity->isLoaded()) { + continue; + } - $apiField = $property->getAttribute(EntityField::class)->object ?? null; + $apiField = $entityField[0]->object ; + $var = $apiField->field ?: $key; - if ($apiField) { - $var = $apiField->field ?: $key; - - if ( property_exists($context, $var) && ( new \ReflectionProperty($context, $var) )->isInitialized($context) ) { - if ($apiField->setterMethod) { - # Use a setter method - call_user_func([ $entity, $apiField->setterMethod ], $context->{$var}); - } - else { - # Direct property set - $entity->$key = $context->{$var}; - } + if ( property_exists($context, $var) && ( new \ReflectionProperty($context, $var) )->isInitialized($context) ) { + if ($apiField->setterMethod) { + # Use a setter method + call_user_func([ $entity, $apiField->setterMethod ], $context->{$var}); } - elseif (! $entity->isLoaded()) { - # Applies 'default' value assigned to ContextField property - $attr = $this->reflectedProperties($context)[$var] ?? null; + else { + # Direct property set + $entity->$key = $context->{$var}; + } + } + elseif (! $entity->isLoaded()) { + # Applies 'default' value assigned to ContextField property + $attr = $this->reflectedProperties($context)[$var] ?? null; - if ($attr) { - $contextField = $attr->getAttribute(ContextField::class); + if ($attr) { + $contextField = $attr->getAttribute(ContextField::class); - if ($contextField) { - if (isset($contextField->object->default)) { + if ($contextField) { + if (isset($contextField->object->default)) { - $entity->$key = $contextField->object->default; - } + $entity->$key = $contextField->object->default; } } } @@ -152,6 +159,25 @@ abstract class Save extends Form implements \Picea\Ui\Method\FormInterface { } } + protected function handleTimestamp(EntityInterface $entity) : void + { + if ($entity->isLoaded()) { + if (property_exists($entity, $this->fields['updatedAt']) && $entity->repository()->generateDatasetDiff($entity) ) { + $field = $this->fields['updatedAt']; + } + } + else { + if (property_exists($entity, $this->fields['createdAt']) && empty($entity->{$this->fields['createdAt']})) { + $field = $this->fields['createdAt']; + } + } + + if ($field ?? false) { + $cls = $entity::resolveEntity()->field($field)->type->type; + $entity->{$field} = new $cls(); + } + } + private function reflectedProperties(FormContextInterface $context) : array { $this->reflectedProperties ??= ObjectReflection::fromClass($context::class)->reflectProperties(\ReflectionProperty::IS_PUBLIC); diff --git a/src/Lib/FormContext.php b/src/Lib/FormContext.php index 36c9b91..6d33dc1 100644 --- a/src/Lib/FormContext.php +++ b/src/Lib/FormContext.php @@ -26,25 +26,25 @@ class FormContext extends \Picea\Ui\Method\FormContext { return $this->languageHandler->languageFromKey($key, $variables); } - public function pushSuccessMessage($key, $variables = []) { + public function pushSuccessMessage($key, $variables = []) : void { $this->pushMessage($this->message::generateSuccess( $this->lang($key, $variables) )); } - public function pushWarningMessage($key, $variables = []) { + public function pushWarningMessage($key, $variables = []) : void { $this->pushMessage($this->message::generateWarning( $this->lang($key, $variables) )); } - public function pushErrorMessage($key, $variables = []) { + public function pushErrorMessage($key, $variables = []) : void { $this->pushMessage($this->message::generateError( $a = $this->lang($key, $variables) )); } - public function pushInfoMessage($key, $variables = []) { + public function pushInfoMessage($key, $variables = []) : void { $this->pushMessage($this->message::generateInfo( $this->lang($key, $variables) )); diff --git a/src/Middleware/CORS.php b/src/Middleware/CORS.php index 766f413..3ee74b0 100644 --- a/src/Middleware/CORS.php +++ b/src/Middleware/CORS.php @@ -15,8 +15,6 @@ class CORS implements MiddlewareInterface { public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface { - - var_dump($request->getMethod() ); if ($request->getMethod() === "option") { die("an option :)"); } diff --git a/view/lean/layout/docs.phtml b/view/lean/layout/docs.phtml index 291995b..429efa8 100644 --- a/view/lean/layout/docs.phtml +++ b/view/lean/layout/docs.phtml @@ -22,7 +22,7 @@ h4{background:#e0f7ed;padding:6px 12px; font-weight: bold!important;font-size:100%;margin-top:0} h5{text-decoration: underline} h3 {display: flex;align-items: center;padding: 0 15px 0 15px;font-variant: small-caps;border-bottom: 5px solid #ccc;line-height: 60px;} - li.odd-even{border-top:1px solid #ccc;margin:10px 0;padding:15px 15px 10px 5px} + li.odd-even{border-top:1px solid #ccc;margin:10px 0;padding:15px 10px 10px 10px} li.odd-even:first-child{border:0} input, button {padding:5px; font-size:1em;margin:0} @@ -84,13 +84,15 @@ .forms{background: #e8f7ff;padding: 5px;} + .forms ol {padding-left:0;list-style-type: none;} + .entities {background: #f0e0e0;padding:5px;} .entity-wrapper .entity-name {background:#eaa1af;color: #682828;font-size:110%} .entity-wrapper .fields-wrapper {border-color: #c14141} .entity-wrapper .header-fields {background:#c14141} .entity-wrapper .field-desc {margin-top:10px;;background:#fff;padding:5px;font-size:0.9em} .entity-wrapper .field-desc .array-of {font-weight:bold;padding-left:15px;color:#955252} - .entity-wrapper ol {background: #e3d0d0;} + .entity-wrapper ol {background: #e3d0d0;padding-left:0;list-style-type: none;} .entity-wrapper li {border-color: #ae8585;} .entity-wrapper li .default {color:#bf7d4d;} @@ -98,7 +100,7 @@ .search-request-wrapper {padding: 15px;margin-top:5px;border-left: 3px solid #72886a;background: #eeffe6;} .search-request-wrapper .header-fields {background:#395332} .search-request-wrapper .fields-wrapper{border-color:#395332} - .search-request-wrapper ol {background: #d8f0cd;} + .search-request-wrapper ol {background: #d8f0cd;padding-left:0;list-style-type: none;} .search-request-wrapper li {border-color: #72886a;} .search-request-wrapper .default {color:#72886a;}