Skip to content

Commit 7188309

Browse files
authored
Merge pull request #43 from helloiamlukas/fix-issue-42
replace only the first occurrence of the storage zone name
2 parents bf6ab90 + 16388ed commit 7188309

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/BunnyCDNAdapter.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,12 @@ public function listContents(string $path = '', bool $deep = false): iterable
147147
*/
148148
protected function normalizeObject(array $bunny_file_array): StorageAttributes
149149
{
150-
$bunny_file_array['Path'] = str_replace($this->prependPrefix(''), '', $bunny_file_array['Path']);
150+
$bunny_file_array['Path'] = $this->replaceFirst($this->prependPrefix(''), '', $bunny_file_array['Path']);
151151

152152
return match ($bunny_file_array['IsDirectory']) {
153153
true => new DirectoryAttributes(
154154
Util::normalizePath(
155-
str_replace(
155+
$this->replaceFirst(
156156
$bunny_file_array['StorageZoneName'].'/',
157157
'/',
158158
$bunny_file_array['Path'].$bunny_file_array['ObjectName']
@@ -161,7 +161,7 @@ protected function normalizeObject(array $bunny_file_array): StorageAttributes
161161
),
162162
false => new FileAttributes(
163163
Util::normalizePath(
164-
str_replace(
164+
$this->replaceFirst(
165165
$bunny_file_array['StorageZoneName'].'/',
166166
'/',
167167
$bunny_file_array['Path'].$bunny_file_array['ObjectName']
@@ -525,4 +525,15 @@ private function prependPrefix(string $path): string
525525

526526
return $this->prefixPath.'/'.$path;
527527
}
528+
529+
private function replaceFirst($search, $replace, $subject)
530+
{
531+
$position = strpos($subject, $search);
532+
533+
if ($position !== false) {
534+
return substr_replace($subject, $replace, $position, strlen($search));
535+
}
536+
537+
return $subject;
538+
}
528539
}

0 commit comments

Comments
 (0)