diff --git a/README.md b/README.md index 6323dd5..85bdb74 100644 --- a/README.md +++ b/README.md @@ -64,3 +64,10 @@ Get html form ```php $form = $client->htmlForm(); ``` +Rename files inside zip +```php +$file = (new FileRequest('http://example.com/image.png')) + ->fileOptions([ + 'name' => 'my-new-name.png' + ]); +``` diff --git a/src/FileRequest.php b/src/FileRequest.php index b2b7aaa..36c4e1b 100644 --- a/src/FileRequest.php +++ b/src/FileRequest.php @@ -6,6 +6,7 @@ class FileRequest { private $url; private $fallbackUrl; + private $fileOptions = []; public function __construct(string $url, string $fallbackUrl = null) { @@ -27,6 +28,18 @@ class FileRequest return $this; } + public function fileOptions(array $fileOptions) + { + $this->fileOptions = $fileOptions; + + return $this; + } + + public function getFileOptions(): array + { + return $this->fileOptions; + } + public function getUrl(): string { return $this->url; @@ -39,9 +52,10 @@ class FileRequest public function toArray(): array { - return [ + return array_filter([ 'url' => $this->url, - 'fallback_url' => $this->fallbackUrl, - ]; + 'fallbackUrl' => $this->fallbackUrl, + 'fileOptions' => $this->fileOptions, + ]); } } diff --git a/tests/MultiDownloaderClientTest.php b/tests/MultiDownloaderClientTest.php index 2f4c318..d5867c3 100644 --- a/tests/MultiDownloaderClientTest.php +++ b/tests/MultiDownloaderClientTest.php @@ -15,9 +15,10 @@ class MultiDownloaderClientTest extends TestCase private function testFiles() { - return array_map(function ($url) { - return new FileRequest($url); - }, $this->testFiles); + return array_map(function ($url, $i) { + return (new FileRequest($url)) + ->fileOptions(['name' => 'test' . $i . '.png']); + }, $this->testFiles, array_keys($this->testFiles)); } public function testDownloadAsString() @@ -30,14 +31,14 @@ class MultiDownloaderClientTest extends TestCase $md5 = md5($response); - $this->assertEquals('ea9726d2ecbe6b820899ba125bf0ae94', $md5); + $this->assertEquals('7542c2c2a0750ab9e62d50bbe83d4c1f', $md5); } public function testDownloadTo() { $client = new MultiDownloaderClient(['apiKey' => 'test', 'apiSecret' => 'test']); - $path = sys_get_temp_dir() . '/testDownloadTo.zip'; + $path = __DIR__ . '/testDownloadTo.zip'; $client->setFiles($this->testFiles()); $client->downloadTo($path); @@ -45,7 +46,7 @@ class MultiDownloaderClientTest extends TestCase $md5 = md5_file($path); $this->assertFileExists($path); - $this->assertEquals('ea9726d2ecbe6b820899ba125bf0ae94', $md5); + $this->assertEquals('7542c2c2a0750ab9e62d50bbe83d4c1f', $md5); } public function testForm() diff --git a/tests/testDownloadTo.zip b/tests/testDownloadTo.zip new file mode 100644 index 0000000..8a48d8d Binary files /dev/null and b/tests/testDownloadTo.zip differ