- Added a new ArrayCollection
- Changed Action attribute and transformed it into an abstract class
This commit is contained in:
1 parent
0443f5a811
commit
9b360c4010
18 files changed
+311
-86
No files matched your search
+23
-16
@@ -5,7 +5,7 @@ namespace Lean\Api\Form;
|
||||
use CSLSJ\Lean\Form\Session\EmailContext;
|
||||
use Picea\Ui\Method\{FormContext, FormContextInterface};
|
||||
|
||||
use Lean\Api\Attribute\ContextField;
|
||||
use Lean\Api\Attribute\{Action, Action\ActionTypeEnum, ContextField, Action\ValueSet};
|
||||
use Lean\Api\Exception\LeanApiException;
|
||||
use Lean\Api\Exception\MandatoryFieldException;
|
||||
use Lean\Api\Exception\MinimumLengthException;
|
||||
@@ -130,29 +130,36 @@ abstract class Save extends Form implements \Picea\Ui\Method\FormInterface {
|
||||
|
||||
$apiField = $entityField[0]->object ;
|
||||
$var = $apiField->field ?: $key;
|
||||
$attr = $this->reflectedProperties($context)[$var] ?? false;
|
||||
|
||||
if ( property_exists($context, $var) && ( new \ReflectionProperty($context, $var) )->isInitialized($context) ) {
|
||||
$contextField = $attr ? $attr->getAttribute(ContextField::class) : null;
|
||||
|
||||
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};
|
||||
elseif($attr) {
|
||||
$actions = [];
|
||||
|
||||
foreach($attr->getAttributes(Action::class) as $item) {
|
||||
$actions[] = $item->object->action($entity, $key, $context, $var);
|
||||
}
|
||||
|
||||
if (! in_array(ActionTypeEnum::Setter, $actions)) {
|
||||
new ValueSet()->action($entity, $key, $context, $var);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
elseif (! $entity->isLoaded()) {
|
||||
elseif (! $entity->isLoaded() && $contextField) {
|
||||
# Applies 'default' value assigned to ContextField property
|
||||
$attr = $this->reflectedProperties($context)[$var] ?? null;
|
||||
|
||||
if ($attr) {
|
||||
$contextField = $attr->getAttribute(ContextField::class);
|
||||
|
||||
if ($contextField) {
|
||||
if (isset($contextField->object->default)) {
|
||||
|
||||
$entity->$key = $contextField->object->default;
|
||||
}
|
||||
if (isset($contextField->object->default)) {
|
||||
if (is_callable($contextField->object->default)) {
|
||||
$entity->$key = call_user_func_array($contextField->object->default, [ $context, ]);
|
||||
}
|
||||
else {
|
||||
$entity->$key = $contextField->object->default;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user