diff --git a/src/Common/File.php b/src/Common/File.php index fba7f006..f872162c 100644 --- a/src/Common/File.php +++ b/src/Common/File.php @@ -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 @@ -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); } /** @@ -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, ); } @@ -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), @@ -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; } diff --git a/src/Pages/Properties/Files.php b/src/Pages/Properties/Files.php index 8d5c2317..407fe430 100644 --- a/src/Pages/Properties/Files.php +++ b/src/Pages/Properties/Files.php @@ -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); }