Skip to content

Commit

Permalink
renamed/moved support classes
Browse files Browse the repository at this point in the history
  • Loading branch information
vazaha-nl committed Aug 10, 2023
1 parent f6788c4 commit 9caf42b
Show file tree
Hide file tree
Showing 16 changed files with 69 additions and 71 deletions.
8 changes: 4 additions & 4 deletions examples/post_statuses.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
declare(strict_types=1);

use Vazaha\Mastodon\Factories\ApiClientFactory;
use Vazaha\Mastodon\Support\File;
use Vazaha\Mastodon\Helpers\UploadFile;

require dirname(__DIR__) . '/vendor/autoload.php';

Expand Down Expand Up @@ -36,9 +36,9 @@
// post a status with media (image/movie/etc)
// first upload the image by creating a media attachment
$mediaAttachment = $client->methods()->media()->v2(
new File('/path/to/media.gif'), // use the \Vazaha\Mastodon\Support\File helper
new File('/path/to/thumbnail.gif'), // optional
'the description of the attachment',
new UploadFile('/path/to/media.gif'), // use the \Vazaha\Mastodon\Helpers\UploadFile helper
new UploadFile('/path/to/thumbnail.gif'),
'a picture of something cool',
);

// then post the status with a reference to the just created media attachment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Vazaha\Mastodon\Support;
namespace Vazaha\Mastodon\Helpers;

use InvalidArgumentException;

Expand Down Expand Up @@ -79,7 +79,7 @@ protected function constructParts(
];
}

if ($contents instanceof File) {
if ($contents instanceof UploadFile) {
return [
array_merge($contents->toArray(), [
'name' => $baseName,
Expand Down
4 changes: 2 additions & 2 deletions src/Support/File.php → src/Helpers/UploadFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

declare(strict_types=1);

namespace Vazaha\Mastodon\Support;
namespace Vazaha\Mastodon\Helpers;

use InvalidArgumentException;

/**
* Helper class for files to be uploaded to the Mastodon API.
*/
class File
class UploadFile
{
/**
* @param string $filePath The path of the file to be uploaded
Expand Down
10 changes: 5 additions & 5 deletions src/Proxies/AccountsProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Vazaha\Mastodon\Proxies;

use Vazaha\Mastodon\Exceptions\InvalidResponseException;
use Vazaha\Mastodon\Helpers\UploadFile;
use Vazaha\Mastodon\Models\AccountModel;
use Vazaha\Mastodon\Models\CredentialAccountModel;
use Vazaha\Mastodon\Models\RelationshipModel;
Expand Down Expand Up @@ -44,7 +45,6 @@
use Vazaha\Mastodon\Results\ListResult;
use Vazaha\Mastodon\Results\RelationshipResult;
use Vazaha\Mastodon\Results\StatusResult;
use Vazaha\Mastodon\Support\File;

class AccountsProxy extends Proxy
{
Expand Down Expand Up @@ -632,8 +632,8 @@ public function unpin(
*
* @param ?string $display_name the display name to use for the profile
* @param ?string $note the account bio
* @param ?File $avatar Avatar image encoded using `multipart/form-data`
* @param ?File $header Header image encoded using `multipart/form-data`
* @param ?UploadFile $avatar Avatar image encoded using `multipart/form-data`
* @param ?UploadFile $header Header image encoded using `multipart/form-data`
* @param ?bool $locked whether manual approval of follow requests is required
* @param ?bool $bot whether the account has a bot flag
* @param ?bool $discoverable whether the account should be shown in the profile directory
Expand All @@ -645,8 +645,8 @@ public function unpin(
public function updateCredentials(
?string $display_name = null,
?string $note = null,
?File $avatar = null,
?File $header = null,
?UploadFile $avatar = null,
?UploadFile $header = null,
?bool $locked = null,
?bool $bot = null,
?bool $discoverable = null,
Expand Down
36 changes: 18 additions & 18 deletions src/Proxies/MediaProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
namespace Vazaha\Mastodon\Proxies;

use Vazaha\Mastodon\Exceptions\InvalidResponseException;
use Vazaha\Mastodon\Helpers\UploadFile;
use Vazaha\Mastodon\Models\MediaAttachmentModel;
use Vazaha\Mastodon\Requests\Media\GetRequest;
use Vazaha\Mastodon\Requests\Media\UpdateRequest;
use Vazaha\Mastodon\Requests\Media\V1Request;
use Vazaha\Mastodon\Requests\Media\V2Request;
use Vazaha\Mastodon\Support\File;

class MediaProxy extends Proxy
{
Expand Down Expand Up @@ -45,16 +45,16 @@ public function get(
/**
* Update media attachment.
*
* @param string $id the ID of the MediaAttachment in the database
* @param ?File $thumbnail the custom thumbnail of the media to be attached, encoded using multipart form data
* @param ?string $description a plain-text description of the media, for accessibility purposes
* @param ?string $focus Two floating points (x,y), comma-delimited, ranging from -1.0 to 1.0. See [Focal points for cropping media thumbnails]({{< relref "api/guidelines#focal-points" >}}) for more information.
* @param string $id the ID of the MediaAttachment in the database
* @param ?UploadFile $thumbnail the custom thumbnail of the media to be attached, encoded using multipart form data
* @param ?string $description a plain-text description of the media, for accessibility purposes
* @param ?string $focus Two floating points (x,y), comma-delimited, ranging from -1.0 to 1.0. See [Focal points for cropping media thumbnails]({{< relref "api/guidelines#focal-points" >}}) for more information.
*
* @see https://docs.joinmastodon.org/methods/media/#update
*/
public function update(
string $id,
?File $thumbnail = null,
?UploadFile $thumbnail = null,
?string $description = null,
?string $focus = null,
): MediaAttachmentModel {
Expand All @@ -78,16 +78,16 @@ public function update(
/**
* (DEPRECATED) Upload media as an attachment.
*
* @param File $file The file to be attached, encoded using multipart form data. The file must have a MIME type.
* @param ?File $thumbnail the custom thumbnail of the media to be attached, encoded using multipart form data
* @param ?string $description a plain-text description of the media, for accessibility purposes
* @param ?string $focus Two floating points (x,y), comma-delimited, ranging from -1.0 to 1.0. See [Focal points for cropping media thumbnails]({{< relref "api/guidelines#focal-points" >}}) for more information.
* @param UploadFile $file The file to be attached, encoded using multipart form data. The file must have a MIME type.
* @param ?UploadFile $thumbnail the custom thumbnail of the media to be attached, encoded using multipart form data
* @param ?string $description a plain-text description of the media, for accessibility purposes
* @param ?string $focus Two floating points (x,y), comma-delimited, ranging from -1.0 to 1.0. See [Focal points for cropping media thumbnails]({{< relref "api/guidelines#focal-points" >}}) for more information.
*
* @see https://docs.joinmastodon.org/methods/media/#v1
*/
public function v1(
File $file,
?File $thumbnail = null,
UploadFile $file,
?UploadFile $thumbnail = null,
?string $description = null,
?string $focus = null,
): MediaAttachmentModel {
Expand All @@ -111,16 +111,16 @@ public function v1(
/**
* Upload media as an attachment (async).
*
* @param File $file The file to be attached, encoded using multipart form data. The file must have a MIME type.
* @param ?File $thumbnail the custom thumbnail of the media to be attached, encoded using multipart form data
* @param ?string $description a plain-text description of the media, for accessibility purposes
* @param ?string $focus Two floating points (x,y), comma-delimited, ranging from -1.0 to 1.0. See [Focal points for cropping media thumbnails]({{< relref "api/guidelines#focal-points" >}}) for more information.
* @param UploadFile $file The file to be attached, encoded using multipart form data. The file must have a MIME type.
* @param ?UploadFile $thumbnail the custom thumbnail of the media to be attached, encoded using multipart form data
* @param ?string $description a plain-text description of the media, for accessibility purposes
* @param ?string $focus Two floating points (x,y), comma-delimited, ranging from -1.0 to 1.0. See [Focal points for cropping media thumbnails]({{< relref "api/guidelines#focal-points" >}}) for more information.
*
* @see https://docs.joinmastodon.org/methods/media/#v2
*/
public function v2(
File $file,
?File $thumbnail = null,
UploadFile $file,
?UploadFile $thumbnail = null,
?string $description = null,
?string $focus = null,
): MediaAttachmentModel {
Expand Down
1 change: 0 additions & 1 deletion src/Proxies/ReportsProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Vazaha\Mastodon\Exceptions\InvalidResponseException;
use Vazaha\Mastodon\Models\ReportModel;
use Vazaha\Mastodon\Requests\Reports\PostRequest;
use Vazaha\Mastodon\Support\File;

class ReportsProxy extends Proxy
{
Expand Down
10 changes: 5 additions & 5 deletions src/Requests/Accounts/UpdateCredentialsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
namespace Vazaha\Mastodon\Requests\Accounts;

use Vazaha\Mastodon\Enums\HttpMethod;
use Vazaha\Mastodon\Helpers\UploadFile;
use Vazaha\Mastodon\Interfaces\RequestInterface;
use Vazaha\Mastodon\Support\File;

/**
* Update account credentials.
Expand All @@ -24,8 +24,8 @@ final class UpdateCredentialsRequest extends \Vazaha\Mastodon\Requests\AccountRe
/**
* @param ?string $display_name the display name to use for the profile
* @param ?string $note the account bio
* @param ?File $avatar Avatar image encoded using `multipart/form-data`
* @param ?File $header Header image encoded using `multipart/form-data`
* @param ?UploadFile $avatar Avatar image encoded using `multipart/form-data`
* @param ?UploadFile $header Header image encoded using `multipart/form-data`
* @param ?bool $locked whether manual approval of follow requests is required
* @param ?bool $bot whether the account has a bot flag
* @param ?bool $discoverable whether the account should be shown in the profile directory
Expand All @@ -35,8 +35,8 @@ final class UpdateCredentialsRequest extends \Vazaha\Mastodon\Requests\AccountRe
public function __construct(
public ?string $display_name = null,
public ?string $note = null,
public ?File $avatar = null,
public ?File $header = null,
public ?UploadFile $avatar = null,
public ?UploadFile $header = null,
public ?bool $locked = null,
public ?bool $bot = null,
public ?bool $discoverable = null,
Expand Down
12 changes: 6 additions & 6 deletions src/Requests/Media/UpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
namespace Vazaha\Mastodon\Requests\Media;

use Vazaha\Mastodon\Enums\HttpMethod;
use Vazaha\Mastodon\Helpers\UploadFile;
use Vazaha\Mastodon\Interfaces\RequestInterface;
use Vazaha\Mastodon\Support\File;

/**
* Update media attachment.
Expand All @@ -22,14 +22,14 @@
final class UpdateRequest extends \Vazaha\Mastodon\Requests\MediaAttachmentRequest implements RequestInterface
{
/**
* @param string $id the ID of the MediaAttachment in the database
* @param ?File $thumbnail the custom thumbnail of the media to be attached, encoded using multipart form data
* @param ?string $description a plain-text description of the media, for accessibility purposes
* @param ?string $focus Two floating points (x,y), comma-delimited, ranging from -1.0 to 1.0. See [Focal points for cropping media thumbnails]({{< relref "api/guidelines#focal-points" >}}) for more information.
* @param string $id the ID of the MediaAttachment in the database
* @param ?UploadFile $thumbnail the custom thumbnail of the media to be attached, encoded using multipart form data
* @param ?string $description a plain-text description of the media, for accessibility purposes
* @param ?string $focus Two floating points (x,y), comma-delimited, ranging from -1.0 to 1.0. See [Focal points for cropping media thumbnails]({{< relref "api/guidelines#focal-points" >}}) for more information.
*/
public function __construct(
public string $id,
public ?File $thumbnail = null,
public ?UploadFile $thumbnail = null,
public ?string $description = null,
public ?string $focus = null,
) {
Expand Down
14 changes: 7 additions & 7 deletions src/Requests/Media/V1Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
namespace Vazaha\Mastodon\Requests\Media;

use Vazaha\Mastodon\Enums\HttpMethod;
use Vazaha\Mastodon\Helpers\UploadFile;
use Vazaha\Mastodon\Interfaces\RequestInterface;
use Vazaha\Mastodon\Support\File;

/**
* (DEPRECATED) Upload media as an attachment.
Expand All @@ -22,14 +22,14 @@
final class V1Request extends \Vazaha\Mastodon\Requests\MediaAttachmentRequest implements RequestInterface
{
/**
* @param File $file The file to be attached, encoded using multipart form data. The file must have a MIME type.
* @param ?File $thumbnail the custom thumbnail of the media to be attached, encoded using multipart form data
* @param ?string $description a plain-text description of the media, for accessibility purposes
* @param ?string $focus Two floating points (x,y), comma-delimited, ranging from -1.0 to 1.0. See [Focal points for cropping media thumbnails]({{< relref "api/guidelines#focal-points" >}}) for more information.
* @param UploadFile $file The file to be attached, encoded using multipart form data. The file must have a MIME type.
* @param ?UploadFile $thumbnail the custom thumbnail of the media to be attached, encoded using multipart form data
* @param ?string $description a plain-text description of the media, for accessibility purposes
* @param ?string $focus Two floating points (x,y), comma-delimited, ranging from -1.0 to 1.0. See [Focal points for cropping media thumbnails]({{< relref "api/guidelines#focal-points" >}}) for more information.
*/
public function __construct(
public File $file,
public ?File $thumbnail = null,
public UploadFile $file,
public ?UploadFile $thumbnail = null,
public ?string $description = null,
public ?string $focus = null,
) {
Expand Down
14 changes: 7 additions & 7 deletions src/Requests/Media/V2Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
namespace Vazaha\Mastodon\Requests\Media;

use Vazaha\Mastodon\Enums\HttpMethod;
use Vazaha\Mastodon\Helpers\UploadFile;
use Vazaha\Mastodon\Interfaces\RequestInterface;
use Vazaha\Mastodon\Support\File;

/**
* Upload media as an attachment (async).
Expand All @@ -22,14 +22,14 @@
final class V2Request extends \Vazaha\Mastodon\Requests\MediaAttachmentRequest implements RequestInterface
{
/**
* @param File $file The file to be attached, encoded using multipart form data. The file must have a MIME type.
* @param ?File $thumbnail the custom thumbnail of the media to be attached, encoded using multipart form data
* @param ?string $description a plain-text description of the media, for accessibility purposes
* @param ?string $focus Two floating points (x,y), comma-delimited, ranging from -1.0 to 1.0. See [Focal points for cropping media thumbnails]({{< relref "api/guidelines#focal-points" >}}) for more information.
* @param UploadFile $file The file to be attached, encoded using multipart form data. The file must have a MIME type.
* @param ?UploadFile $thumbnail the custom thumbnail of the media to be attached, encoded using multipart form data
* @param ?string $description a plain-text description of the media, for accessibility purposes
* @param ?string $focus Two floating points (x,y), comma-delimited, ranging from -1.0 to 1.0. See [Focal points for cropping media thumbnails]({{< relref "api/guidelines#focal-points" >}}) for more information.
*/
public function __construct(
public File $file,
public ?File $thumbnail = null,
public UploadFile $file,
public ?UploadFile $thumbnail = null,
public ?string $description = null,
public ?string $focus = null,
) {
Expand Down
1 change: 0 additions & 1 deletion src/Requests/Reports/PostRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

use Vazaha\Mastodon\Enums\HttpMethod;
use Vazaha\Mastodon\Interfaces\RequestInterface;
use Vazaha\Mastodon\Support\File;

/**
* File a report.
Expand Down
2 changes: 1 addition & 1 deletion src/Requests/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Vazaha\Mastodon\Requests;

use Vazaha\Mastodon\Helpers\MultipartFormData;
use Vazaha\Mastodon\Interfaces\RequestInterface;
use Vazaha\Mastodon\Requests\Concerns\HasPaging;
use Vazaha\Mastodon\Results\Result;
use Vazaha\Mastodon\Support\MultipartFormData;

/**
* @implements \Vazaha\Mastodon\Interfaces\RequestInterface<\Vazaha\Mastodon\Results\Result>
Expand Down
14 changes: 7 additions & 7 deletions tests/Unit/MultiPartFormDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,33 @@

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Vazaha\Mastodon\Support\File;
use Vazaha\Mastodon\Support\MultipartFormData;
use Vazaha\Mastodon\Helpers\MultipartFormData;
use Vazaha\Mastodon\Helpers\UploadFile;

class MultiPartFormDataTest extends TestCase
{
public function testInvalidFilePathThrowsException(): void
{
$this->expectException(InvalidArgumentException::class);
$file = new File('invalid');
$file = new UploadFile('invalid');
}

public function testPathToDirThrowsException(): void
{
$this->expectException(InvalidArgumentException::class);
$file = new File('/home');
$file = new UploadFile('/home');
}

public function testUnreadableFileThrowsException(): void
{
$this->expectException(InvalidArgumentException::class);
$file = new File('/etc/shadow');
$file = new UploadFile('/etc/shadow');
}

public function testFileObject(): void
{
$path = __DIR__ . '/assets/account.json';
$file = new File($path);
$file = new UploadFile($path);

$array = $file->toArray();
self::assertSame('account.json', $array['filename']);
Expand All @@ -44,7 +44,7 @@ public function testFileObject(): void
public function testMultipartGeneration(): void
{
$path = __DIR__ . '/assets/account.json';
$file = new File($path);
$file = new UploadFile($path);

$params = [
'single' => 'value',
Expand Down
2 changes: 1 addition & 1 deletion tools/src/ClassProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ClassProperty
'array' => 'array',
'object' => 'array',
'array of string' => 'array',
'file' => 'File',
'file' => 'UploadFile',
];

public string $name;
Expand Down
Loading

0 comments on commit 9caf42b

Please sign in to comment.