From 665955089521820992c952224777946d8ab687e1 Mon Sep 17 00:00:00 2001 From: Mark van Eijk Date: Thu, 15 Jun 2023 09:50:05 +0200 Subject: [PATCH] add transform method for transformations via array input --- src/UploadcareTransformation.php | 37 ++++++++++++++++++++------------ tests/TransformationTest.php | 20 +++++++++++++++++ 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/src/UploadcareTransformation.php b/src/UploadcareTransformation.php index bd3bce7..baa9fd6 100755 --- a/src/UploadcareTransformation.php +++ b/src/UploadcareTransformation.php @@ -20,6 +20,29 @@ public function filename(string $filename): string return $this; } + public function transform(array $transformations = []): self + { + foreach($transformations as $transformation => $parameters) { + $this->{$transformation}(...$parameters); + } + + return $this; + } + + /** + * Apply all (chained) transformations to the given URL. + */ + public function applyTransformations(string $url): string + { + $transformations = TransformationsFinder::for($this->transformations); + + foreach ($transformations as $transformation) { + $url = $transformation['class']::generateUrl($url, $transformation['values']); + } + + return $url; + } + public function getUrl(): string { $url = $this->applyTransformations($this->baseUrl . $this->uuid . '/'); @@ -50,18 +73,4 @@ public function __toString(): string { return $this->getUrl(); } - - /** - * Apply all (chained) transformations to the given URL. - */ - public function applyTransformations(string $url): string - { - $transformations = TransformationsFinder::for($this->transformations); - - foreach ($transformations as $transformation) { - $url = $transformation['class']::generateUrl($url, $transformation['values']); - } - - return $url; - } } diff --git a/tests/TransformationTest.php b/tests/TransformationTest.php index d348724..24708d1 100644 --- a/tests/TransformationTest.php +++ b/tests/TransformationTest.php @@ -1,5 +1,25 @@ transform([ + 'resize' => ['width' => 100, 'height' => 200], + ]); + + expect($url)->toBe('https://ucarecdn.com/12a3456b-c789-1234-1de2-3cfa83096e25/-/resize/100x200/'); + + // combine transform function with chaining + $url = (string) $uc->transform([ + 'resize' => ['width' => 200, 'height' => 200], + ]) + ->crop(300, 400); + + expect($url)->toBe('https://ucarecdn.com/12a3456b-c789-1234-1de2-3cfa83096e25/-/resize/200x200/-/crop/300x400/'); +}); + it('can crop by objects', function () { $uuid = '12a3456b-c789-1234-1de2-3cfa83096e25'; $transformation = uploadcare($uuid);