diff --git a/src/app/Library/Uploaders/Support/Interfaces/UploaderInterface.php b/src/app/Library/Uploaders/Support/Interfaces/UploaderInterface.php index 185350c053..e286f45e37 100644 --- a/src/app/Library/Uploaders/Support/Interfaces/UploaderInterface.php +++ b/src/app/Library/Uploaders/Support/Interfaces/UploaderInterface.php @@ -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; } diff --git a/src/app/Library/Uploaders/Support/Traits/HandleRepeatableUploads.php b/src/app/Library/Uploaders/Support/Traits/HandleRepeatableUploads.php index 56f6b2f91b..7c15becc05 100644 --- a/src/app/Library/Uploaders/Support/Traits/HandleRepeatableUploads.php +++ b/src/app/Library/Uploaders/Support/Traits/HandleRepeatableUploads.php @@ -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 diff --git a/src/app/Library/Uploaders/Uploader.php b/src/app/Library/Uploaders/Uploader.php index eb298a89f6..9dcb418850 100644 --- a/src/app/Library/Uploaders/Uploader.php +++ b/src/app/Library/Uploaders/Uploader.php @@ -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 *******************************/ @@ -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; } @@ -277,7 +282,7 @@ private function performFileDeletion(Model $entry) } /******************************* - * Private helper methods + * helper methods *******************************/ private function getPathFromConfiguration(array $crudObject, array $configuration): string {