feat(attachment): refactor image cropping functionality to use range objects and update related tests
This commit is contained in:
@@ -45,14 +45,44 @@ class WebsiteExtrasResource extends JsonResource
|
||||
),
|
||||
]),
|
||||
'resolved_extras' => $websiteExtras->mapWithKeys(fn ($extra) => [
|
||||
$extra->websiteTypeExtra->codigo => $this->formatConfig(
|
||||
$extra->resolvedConfig(),
|
||||
fn (Attachment $attachment): string => $attachment->getTemporaryUrl(1440)
|
||||
),
|
||||
$extra->websiteTypeExtra->codigo => $this->formatResolvedConfig($extra),
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
private function formatResolvedConfig(mixed $extra): mixed
|
||||
{
|
||||
$config = $extra->resolvedConfig();
|
||||
|
||||
if (
|
||||
$extra->websiteTypeExtra->codigo === 'heroConfig'
|
||||
&& is_array($config)
|
||||
&& ($config['background_image_id'] ?? null) instanceof Attachment
|
||||
) {
|
||||
$attachment = $config['background_image_id'];
|
||||
$config['background_image_id'] = $this->formatHeroAttachment($attachment);
|
||||
}
|
||||
|
||||
return $this->formatConfig(
|
||||
$config,
|
||||
fn (Attachment $attachment): string => $attachment->getTemporaryUrl(1440)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{url: string, crop_horizontal: array<string, float>, crop_vertical: array<string, float>}
|
||||
*/
|
||||
private function formatHeroAttachment(Attachment $attachment): array
|
||||
{
|
||||
$fullRange = ['start_percentage' => 0.0, 'end_percentage' => 100.0];
|
||||
|
||||
return [
|
||||
'url' => $attachment->getTemporaryUrl(1440),
|
||||
'crop_horizontal' => $attachment->crop_horizontal ?? $fullRange,
|
||||
'crop_vertical' => $attachment->crop_vertical ?? $fullRange,
|
||||
];
|
||||
}
|
||||
|
||||
private function formatConfig(mixed $value, callable $formatAttachment): mixed
|
||||
{
|
||||
if ($value instanceof Attachment) {
|
||||
|
||||
Reference in New Issue
Block a user