*/ public function buildImagesFromRequest(Request $request, WooStore $store): array { /** @var list> $images */ $images = json_decode((string) $request->input('product_images', '[]'), true) ?? []; if (! is_array($images)) { $images = []; } if ($request->hasFile('featured_image_upload')) { $uploaded = $this->upload($store, $request->file('featured_image_upload'), (string) $request->input('featured_image_alt', '')); if ($uploaded !== null) { $images[0] = $uploaded; } } elseif ($src = trim((string) $request->input('featured_image_src', ''))) { $images[0] = [ 'src' => $src, 'alt' => trim((string) $request->input('featured_image_alt', '')), ]; } foreach ($request->file('gallery_uploads', []) as $file) { if ($file instanceof UploadedFile && $file->isValid()) { $uploaded = $this->upload($store, $file, ''); if ($uploaded !== null) { $images[] = $uploaded; } } } foreach ((array) $request->input('gallery_image_srcs', []) as $src) { if (! is_string($src)) { continue; } $src = trim($src); if ($src !== '') { $images[] = ['src' => $src, 'alt' => '']; } } return $this->normalizeImages($images); } /** @return array{id: int, src: string, alt: string}|null */ public function upload(WooStore $store, UploadedFile $file, string $alt = ''): ?array { $response = $this->client->uploadMedia($store, $file, $alt); if (! is_array($response) || empty($response['src'])) { return null; } return [ 'id' => (int) ($response['id'] ?? 0), 'src' => (string) $response['src'], 'alt' => (string) ($response['alt'] ?? $alt), ]; } /** @param list> $images */ private function normalizeImages(array $images): array { $normalized = []; foreach (array_values($images) as $position => $image) { if (! is_array($image)) { continue; } $src = trim((string) ($image['src'] ?? '')); $id = (int) ($image['id'] ?? 0); if ($src === '' && $id <= 0) { continue; } $row = [ 'alt' => trim((string) ($image['alt'] ?? '')), 'position' => $position, ]; if ($id > 0) { $row['id'] = $id; } if ($src !== '') { $row['src'] = $src; } $normalized[] = $row; } return $normalized; } }