Skip to content

Commit

Permalink
add transform method for transformations via array input
Browse files Browse the repository at this point in the history
  • Loading branch information
markvaneijk committed Jun 15, 2023
1 parent fd951cf commit 6659550
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
37 changes: 23 additions & 14 deletions src/UploadcareTransformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 . '/');
Expand Down Expand Up @@ -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;
}
}
20 changes: 20 additions & 0 deletions tests/TransformationTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
<?php

it('can transform using array input', function() {
$uuid = '12a3456b-c789-1234-1de2-3cfa83096e25';
$uc = uploadcare($uuid);

// transform using array input
$url = (string) $uc->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);
Expand Down

0 comments on commit 6659550

Please sign in to comment.