Skip to content

Commit

Permalink
Added contained fields fill callbacks handling. References #33
Browse files Browse the repository at this point in the history
  • Loading branch information
toonvandenbos committed Aug 21, 2019
1 parent 3e794b7 commit 26ab122
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
23 changes: 18 additions & 5 deletions src/Flexible.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public function resolve($resource, $attribute = null)
* @param string $requestAttribute
* @param object $model
* @param string $attribute
* @return void
* @return null|Closure
*/
protected function fillAttribute(NovaRequest $request, $requestAttribute, $model, $attribute)
{
Expand All @@ -217,17 +217,25 @@ protected function fillAttribute(NovaRequest $request, $requestAttribute, $model

$this->buildGroups($model, $attribute);

$this->syncAndFillGroups($request, $requestAttribute);
$callbacks = collect($this->syncAndFillGroups($request, $requestAttribute));

$this->value = $this->resolver->set($model, $attribute, $this->groups);

if($callbacks->isEmpty()) {
return;
}

return function() use ($callbacks) {
$callbacks->each->__invoke();
};
}

/**
* Process an incoming POST Request
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @param string $requestAttribute
* @return void
* @return array
*/
protected function syncAndFillGroups(NovaRequest $request, $requestAttribute)
{
Expand All @@ -236,7 +244,9 @@ protected function syncAndFillGroups(NovaRequest $request, $requestAttribute)
return;
}

$this->groups = collect($raw)->map(function($item, $key) use ($request) {
$callbacks = [];

$this->groups = collect($raw)->map(function($item, $key) use ($request, &$callbacks) {
$layout = $item['layout'];
$key = $item['key'];
$attributes = $item['attributes'];
Expand All @@ -245,10 +255,13 @@ protected function syncAndFillGroups(NovaRequest $request, $requestAttribute)

if(!$group) return;

$group->fill(ScopedRequest::scopeFrom($request, $attributes, $key));
$scope = ScopedRequest::scopeFrom($request, $attributes, $key);
$callbacks = array_merge($callbacks, $group->fill($scope));

return $group;
});

return $callbacks;
}

/**
Expand Down
21 changes: 14 additions & 7 deletions src/Http/ScopedRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected function getScopeState($group, $attributes)

// Sub-objects could contain files that need to be kept
if($attribute->isAggregate()) {
$scope['files'] = array_merge($scope['files'], $this->getNestedFiles($value, $attribute->group));
$scope['files'] = array_merge($scope['files'], $this->pullNestedFiles($value, $attribute->group));
$scope['input'][$attribute->name] = $value;
continue;
}
Expand All @@ -81,30 +81,37 @@ protected function getScopeState($group, $attributes)
}

/**
* Get nested file attributes
* Get & remove nested file attributes from given array
*
* @param array $iterable
* @param null|string $group
* @return array
*/
protected function getNestedFiles($iterable, $group = null)
protected function pullNestedFiles(&$iterable, $group = null)
{
$files = [];
$key = $this->isFlexibleStructure($iterable) ? $iterable['key'] : $group;

foreach ($iterable as $attribute => $value) {
foreach ($iterable as $original => $value) {
if(is_array($value)) {
$files = array_merge($files, $this->getNestedFiles($value, $key));
$files = array_merge($files, $this->pullNestedFiles($value, $key));
$iterable[$original] = $value ? $value : null;
continue;
}

$attribute = FlexibleAttribute::make($attribute, $group);
$attribute = FlexibleAttribute::make($original, $group);

if(!$attribute->isFlexibleFile($value)) continue;
if(!$attribute->isFlexibleFile($value)) {
continue;
}

$files[] = $attribute->getFlexibleFileAttribute($value);

$iterable[$original] = null;
}

$iterable = array_filter($iterable);

return $files;
}

Expand Down
13 changes: 9 additions & 4 deletions src/Layouts/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,18 @@ public function getResolvedValue()
* Fill attributes using underlaying fields and incoming request
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @return void
* @return array
*/
public function fill(ScopedRequest $request)
{
$this->fields->each(function($field) use ($request) {
$field->fill($request, $this);
});
return $this->fields->map(function($field) use ($request) {
return $field->fill($request, $this);
})
->filter(function($callback) {
return is_callable($callback);
})
->values()
->all();
}

/**
Expand Down

0 comments on commit 26ab122

Please sign in to comment.