|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpList\RestApiClient\Endpoint; |
| 6 | + |
| 7 | +use PhpList\RestApiClient\Client; |
| 8 | +use PhpList\RestApiClient\Exception\ApiException; |
| 9 | +use PhpList\RestApiClient\Exception\NotFoundException; |
| 10 | +use PhpList\RestApiClient\Exception\ValidationException; |
| 11 | + |
| 12 | +/** |
| 13 | + * Client for editor uploads API endpoints. |
| 14 | + */ |
| 15 | +class UploadsClient |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @var Client The API client |
| 19 | + */ |
| 20 | + private Client $client; |
| 21 | + |
| 22 | + /** |
| 23 | + * UploadsClient constructor. |
| 24 | + * |
| 25 | + * @param Client $client The API client |
| 26 | + */ |
| 27 | + public function __construct(Client $client) |
| 28 | + { |
| 29 | + $this->client = $client; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * List files in an upload directory. |
| 34 | + * |
| 35 | + * @param string $directory Upload directory (e.g. uploads, images) |
| 36 | + * @return array Response containing files, directory and total |
| 37 | + * @throws NotFoundException If the directory does not exist |
| 38 | + * @throws ApiException If an API error occurs |
| 39 | + */ |
| 40 | + public function getUploads(string $directory = 'uploads'): array |
| 41 | + { |
| 42 | + return $this->client->get('editor-uploads', [ |
| 43 | + 'directory' => $directory, |
| 44 | + ]); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Upload an editor asset. |
| 49 | + * |
| 50 | + * Accepts either an uploaded file path or a CURLFile, depending on the |
| 51 | + * Client implementation. |
| 52 | + * |
| 53 | + * @param mixed $file File to upload |
| 54 | + * @param string $field Form field name ("upload" or "file") |
| 55 | + * @return array Uploaded file information |
| 56 | + * @throws ValidationException If validation fails |
| 57 | + * @throws ApiException If an API error occurs |
| 58 | + */ |
| 59 | + public function upload(mixed $file, string $field = 'upload'): array |
| 60 | + { |
| 61 | + $multipart = [ |
| 62 | + [ |
| 63 | + 'name' => $field, |
| 64 | + 'contents' => fopen($file, 'rb'), |
| 65 | + 'filename' => basename($file), |
| 66 | + ], |
| 67 | + ]; |
| 68 | + |
| 69 | + return $this->client->postMultipart( |
| 70 | + 'editor-uploads', |
| 71 | + $multipart, |
| 72 | + ); |
| 73 | + } |
| 74 | +} |
0 commit comments