Skip to content

Commit

Permalink
allow the configuration of valueWithoutPath call.
Browse files Browse the repository at this point in the history
  • Loading branch information
pxpm committed Apr 17, 2024
1 parent bf48007 commit 381bf4e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ public function canHandleMultipleFiles(): bool;
public function isRelationship(): bool;

public function getPreviousFiles(Model $entry): mixed;

public function getValueWithoutPath(?string $value = null): ?string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -282,16 +282,16 @@ private function getPreviousRepeatableValues(Model $entry, UploaderInterface $up
return $previousValues ?? [];
}

private function getValuesWithPathStripped(array|string|null $item, UploaderInterface $upload)
private function getValuesWithPathStripped(array|string|null $item, UploaderInterface $uploader)
{
$uploadedValues = $item[$upload->getName()] ?? null;
$uploadedValues = $item[$uploader->getName()] ?? null;
if (is_array($uploadedValues)) {
return array_map(function ($value) use ($upload) {
return Str::after($value, $upload->getPath());
return array_map(function ($value) use ($uploader) {
return $uploader->getValueWithoutPath($value);
}, $uploadedValues);
}

return isset($uploadedValues) ? Str::after($uploadedValues, $upload->getPath()) : null;
return isset($uploadedValues) ? $uploader->getValueWithoutPath($uploadedValues) : null;
}

private function deleteRelationshipFiles(Model $entry): void
Expand Down
11 changes: 8 additions & 3 deletions src/app/Library/Uploaders/Uploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ public function getPreviousFiles(Model $entry): mixed
return $value[$this->getAttributeName()] ?? null;
}

public function getValueWithoutPath(?string $value = null): ?string
{
return $value ? Str::after($value, $this->path) : null;
}

/*******************************
* Setters - fluently configure the uploader
*******************************/
Expand Down Expand Up @@ -229,13 +234,13 @@ private function retrieveFiles(Model $entry): Model
$values = $entry->{$this->attachedToFakeField};
$values = is_string($values) ? json_decode($values, true) : (array) $values;

$values[$this->getAttributeName()] = isset($values[$this->getAttributeName()]) ? Str::after($values[$this->getAttributeName()], $this->path) : null;
$values[$this->getAttributeName()] = isset($values[$this->getAttributeName()]) ? $this->getValueWithoutPath($values[$this->getAttributeName()]) : null;
$entry->{$this->attachedToFakeField} = json_encode($values);

return $entry;
}

$entry->{$this->getAttributeName()} = Str::after($value, $this->path);
$entry->{$this->getAttributeName()} = $this->getValueWithoutPath($value);

return $entry;
}
Expand Down Expand Up @@ -277,7 +282,7 @@ private function performFileDeletion(Model $entry)
}

/*******************************
* Private helper methods
* helper methods
*******************************/
private function getPathFromConfiguration(array $crudObject, array $configuration): string
{
Expand Down

0 comments on commit 381bf4e

Please sign in to comment.