Skip to content

Commit

Permalink
fix(common): only send to API file name when set (#234)
Browse files Browse the repository at this point in the history
Closes #232
  • Loading branch information
mariosimao committed May 30, 2023
1 parent 69aa929 commit 502226a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/Common/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
/**
* @psalm-type FileJson = array{
* type: "external"|"file",
* name: string,
* file?: array{ url: string, expiry_time: string },
* external?: array{ url: string },
* name?: string,
* }
*
* @psalm-immutable
Expand All @@ -20,20 +20,20 @@ private function __construct(
public readonly FileType $type,
public readonly string $url,
public readonly DateTimeImmutable|null $expiryTime,
public readonly string $name,
public readonly string|null $name,
) {
}

public static function createExternal(string $url): self
{
return new self(FileType::External, $url, null, "File");
return new self(FileType::External, $url, null, null);
}

public static function createInternal(
string $url,
DateTimeImmutable|null $expiryTime = null
): self {
return new self(FileType::Internal, $url, $expiryTime, "File");
return new self(FileType::Internal, $url, $expiryTime, null);
}

/**
Expand All @@ -51,7 +51,7 @@ public static function fromArray(array $array): self
FileType::from($type),
$file["url"] ?? "",
isset($file["expiry_time"]) ? new DateTimeImmutable($file["expiry_time"]) : null,
$array["name"] ?? "File",
$array["name"] ?? null,
);
}

Expand All @@ -63,7 +63,6 @@ public function toArray(): array
if ($type === FileType::Internal) {
$array = [
"type" => "file",
"name" => $this->name,
"file" => [
"url" => $this->url,
"expiry_time" => $this->expiryTime?->format(Date::FORMAT),
Expand All @@ -74,11 +73,14 @@ public function toArray(): array
if ($type === FileType::External) {
$array = [
"type" => "external",
"name" => $this->name,
"external" => [ "url" => $this->url ],
];
}

if ($this->name !== null) {
$array["name"] = $this->name;
}

return $array;
}

Expand Down
8 changes: 8 additions & 0 deletions src/Pages/Properties/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public static function create(File ...$files): self
{
$property = PropertyMetadata::create("", PropertyType::Files);

$files = array_map(function (File $f): File {
if ($f->name === null) {
$f = $f->changeName("File");
}

return $f;
}, $files);

return new self($property, $files);
}

Expand Down

0 comments on commit 502226a

Please sign in to comment.