mirror of
https://github.com/scummvm/scummvm-web.git
synced 2026-05-21 05:40:47 +00:00
93d3aa56a1
Sometimes variables may be null. Handle the cases when passing to functions expecting something non-null.
100 lines
2.9 KiB
PHP
100 lines
2.9 KiB
PHP
<?php
|
|
namespace ScummVM\Objects;
|
|
|
|
use ScummVM\OrmObjects\Download;
|
|
use ScummVM\OrmObjects\GameDownload;
|
|
|
|
use Propel\Runtime\Map\TableMap;
|
|
|
|
/**
|
|
* The DownloadsSection object represents a section on the downloads page.
|
|
*/
|
|
class DownloadsSection extends BasicSection
|
|
{
|
|
private string $notes;
|
|
/** @var array<WebLink|File> */
|
|
private array $items;
|
|
|
|
/**
|
|
* @param array{'title': string, 'anchor': string, 'subsection'?: array<mixed>, 'notes': string} $data
|
|
*/
|
|
public function __construct(array $data)
|
|
{
|
|
parent::__construct($data);
|
|
$this->notes = $data['notes'];
|
|
$this->items = [];
|
|
}
|
|
|
|
/**
|
|
* @param Download|GameDownload $item
|
|
*/
|
|
public function addItem(object $item): void
|
|
{
|
|
if ($item->getCategoryIcon()) {
|
|
/** @phpstan-ignore argument.type */
|
|
$this->items[] = new File($item->toArray(TableMap::TYPE_FIELDNAME));
|
|
// If this item is for an old version, sort all items by version, descending, then by autoId
|
|
if (self::hasOldVersion($item)) {
|
|
usort($this->items, function ($a, $b) {
|
|
$aFile = $a instanceof File;
|
|
$bFile = $b instanceof File;
|
|
|
|
if (!$aFile && !$bFile) {
|
|
return strcmp($a->getURL(), $b->getURL());
|
|
} elseif (!$aFile) {
|
|
return 1;
|
|
} elseif (!$bFile) {
|
|
return -1;
|
|
}
|
|
|
|
// Return 0 if equal, -1 if $a->getVersion() is larger, 1 if $b->getVersion() is larger
|
|
$versionSortResult = -version_compare($a->getVersion() ?? '', $b->getVersion() ?? '');
|
|
if ($versionSortResult == 0) {
|
|
// Return 0 if equal, -1 if $a->getAutoId() is smaller, 1 if $b->getAutoId() is smaller
|
|
return $a->getAutoId() <=> $b->getAutoId();
|
|
} else {
|
|
return $versionSortResult;
|
|
}
|
|
});
|
|
}
|
|
} else {
|
|
/** @phpstan-ignore argument.type */
|
|
$this->items[] = new WebLink($item->toArray(TableMap::TYPE_FIELDNAME));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the optional notes.
|
|
*/
|
|
public function getNotes(): ?string
|
|
{
|
|
return $this->notes;
|
|
}
|
|
|
|
/**
|
|
* Get the list of items.
|
|
*
|
|
* @return array<WebLink|File>
|
|
*/
|
|
public function getItems(): array
|
|
{
|
|
return $this->items;
|
|
}
|
|
|
|
/**
|
|
* @param static $section
|
|
*/
|
|
public function addSubsection(DownloadsSection $section): void
|
|
{
|
|
$this->subsections[$section->getAnchor()] = $section;
|
|
}
|
|
|
|
/**
|
|
* @param Download|GameDownload $item
|
|
*/
|
|
private static function hasOldVersion(object $item): bool
|
|
{
|
|
return method_exists($item, 'getVersion') && $item->getVersion() !== RELEASE && $item->getVersion() !== 'Daily';
|
|
}
|
|
}
|