Files
Le Philousophe e2f72c8d8b WEB: Modify all extra attributes at once
This makes PHPStan happy (because the array type is expected to be
either empty or full).
2025-08-24 12:32:47 +02:00

119 lines
3.6 KiB
PHP

<?php
namespace ScummVM\Objects;
use ScummVM\FileUtils;
/**
* The File object represents a file on the website.
*/
class File extends BasicObject
{
private ?int $autoId;
private string $category;
private string $category_icon;
private string $url;
/** @var array{'size': string, 'sha256': string, 'ext': string, 'date': string}|array{} */
private array $extra_info;
private string $notes;
private string $user_agent;
private ?string $version;
/**
* @param array{'description'?: string, 'name'?: string, 'auto_id'?: int, 'category': string,
* 'category_icon': string, 'notes'?: string, 'user_agent'?: string, 'version'?: string,
* 'url': string|array{0: string, '@attributes': array<mixed>}} $data
*/
public function __construct($data)
{
parent::__construct($data);
$this->autoId = $data['auto_id'] ?? null;
$this->category = $data['category'];
$this->category_icon = $data['category_icon'];
$this->notes = $data['notes'] ?? '';
$this->user_agent = $data['user_agent'] ?? '';
$this->version = isset($data['version']) ? strtolower($data['version']) : null;
/* If it's not an array, we didn't get any attributes. */
if (!is_array($data['url'])) {
$url = $data['url'];
$attributes = array();
} else {
$url = $data['url'][0];
$attributes = $data['url']['@attributes'];
}
$this->extra_info = array();
if (preg_match('/^((https?)|(ftp)):\/\//', $url)) {
// If the URL is given, keep it as is
$this->url = $url;
} else {
// Construct the URL based on its type
if ($this->category == 'games' || $this->category == 'addons') {
$fname = DOWNLOADS_EXTRAS_URL . $url;
} elseif (str_starts_with($url, '/frs') || str_starts_with($url, 'http')) {
$fname = $url;
} else {
// E.g. frs/scummvm/1.2.3/scummvm-1.2.3.zip
$fname = 'frs/' . $this->category . '/' . $this->version . '/' . $url;
}
$fname = str_replace('{$release_tools}', RELEASE_TOOLS, $fname);
$fname = str_replace('{$version}', "$this->version", $fname);
if (FileUtils::exists($fname)) {
$extra_info = [];
$extra_info['size'] = FileUtils::getFileSize($fname);
$extra_info['sha256'] = FileUtils::getSha256($fname);
$extra_info['ext'] = FileUtils::getExtension($fname);
$extra_info['date'] = FileUtils::getLastModified($fname);
$this->extra_info = $extra_info;
}
$this->url = $fname;
}
}
/* Get the category icon. */
public function getCategoryIcon(): string
{
return $this->category_icon;
}
/* Get the URL. */
public function getURL(): string
{
return $this->url;
}
/**
* Get the extra information.
*
* @return array{'size': string, 'sha256': string, 'ext': string, 'date': string}|array{}
*/
public function getExtraInfo(): array
{
return $this->extra_info;
}
public function getNotes(): string
{
return $this->notes;
}
/* Get the user-agent. */
public function getUserAgent(): string
{
return $this->user_agent;
}
/* Get the auto-id (the order it is listed in the data source). */
public function getAutoId(): ?int
{
return $this->autoId;
}
/* Get the version. */
public function getVersion(): ?string
{
return $this->version;
}
}