|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace CPMDB\Mods\Mod\Screenshots; |
| 6 | + |
| 7 | +use AppUtils\Collections\BaseStringPrimaryCollection; |
| 8 | +use AppUtils\FileHelper\FileInfo; |
| 9 | +use AppUtils\FileHelper\FolderInfo; |
| 10 | +use AppUtils\FileHelper\JSONFile; |
| 11 | +use CPMDB\Mods\Mod\ModInfoInterface; |
| 12 | + |
| 13 | +/** |
| 14 | + * Collection of screenshot files available for a mod. |
| 15 | + * |
| 16 | + * @package CPMDB |
| 17 | + * @subpackage Mod Collection |
| 18 | + * |
| 19 | + * @method ModScreenshotInterface getByID(string $id) |
| 20 | + * @method ModScreenshotInterface getDefault() |
| 21 | + * @method ModScreenshotInterface[] getAll() |
| 22 | + */ |
| 23 | +class ModScreenshotCollection extends BaseStringPrimaryCollection |
| 24 | +{ |
| 25 | + public const DEFAULT_ID = '000default'; |
| 26 | + private const KEY_SCREENSHOT_ID = 'screenshotID'; |
| 27 | + |
| 28 | + private ModInfoInterface $mod; |
| 29 | + |
| 30 | + public function __construct(ModInfoInterface $mod) |
| 31 | + { |
| 32 | + $this->mod = $mod; |
| 33 | + } |
| 34 | + |
| 35 | + public function hasScreenshots() : bool |
| 36 | + { |
| 37 | + return $this->countRecords() > 0; |
| 38 | + } |
| 39 | + |
| 40 | + public function countScreenshots() : int |
| 41 | + { |
| 42 | + return $this->countRecords(); |
| 43 | + } |
| 44 | + |
| 45 | + public function getMod() : ModInfoInterface |
| 46 | + { |
| 47 | + return $this->mod; |
| 48 | + } |
| 49 | + |
| 50 | + public function getScreensURL() : string |
| 51 | + { |
| 52 | + return $this->mod->getCategory()->getScreensURL(); |
| 53 | + } |
| 54 | + |
| 55 | + public function getScreensFolder() : FolderInfo |
| 56 | + { |
| 57 | + return $this->mod->getCategory()->getScreensFolder(); |
| 58 | + } |
| 59 | + |
| 60 | + public function getDefaultID() : string |
| 61 | + { |
| 62 | + return self::DEFAULT_ID; |
| 63 | + } |
| 64 | + |
| 65 | + protected function registerItems(): void |
| 66 | + { |
| 67 | + foreach($this->getDescriptions() as $def) { |
| 68 | + $this->registerScreenshot($def); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @param array<string,scalar> $def |
| 74 | + * @return void |
| 75 | + */ |
| 76 | + private function registerScreenshot(array $def) : void |
| 77 | + { |
| 78 | + $fileName = $this->resolveFileName($def[self::KEY_SCREENSHOT_ID]); |
| 79 | + $file = FileInfo::factory($this->getScreensFolder().'/'.$fileName); |
| 80 | + |
| 81 | + if(!$file->exists()) { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + $this->registerItem(new ModScreenshot( |
| 86 | + $this, |
| 87 | + $def[self::KEY_SCREENSHOT_ID], |
| 88 | + $file, |
| 89 | + $this->getScreensURL().'/'.$fileName, |
| 90 | + $def |
| 91 | + )); |
| 92 | + } |
| 93 | + |
| 94 | + private ?JSONFile $sidecarFile = null; |
| 95 | + |
| 96 | + /** |
| 97 | + * Gets the path to the optional sidecar file used to |
| 98 | + * document additional mod screenshots. |
| 99 | + * |
| 100 | + * @return JSONFile |
| 101 | + */ |
| 102 | + public function getSidecarFile() : JSONFile |
| 103 | + { |
| 104 | + if(!isset($this->sidecarFile)) { |
| 105 | + $this->sidecarFile = JSONFile::factory($this->getScreensFolder().'/'.$this->mod->getModID().'.json'); |
| 106 | + } |
| 107 | + |
| 108 | + return $this->sidecarFile; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Gets all screenshot descriptions, including the default |
| 113 | + * screenshot. If no sidecar file is present, only the default |
| 114 | + * screenshot is returned. |
| 115 | + * |
| 116 | + * @return array<int,array{string,scalar}> |
| 117 | + */ |
| 118 | + public function getDescriptions() : array |
| 119 | + { |
| 120 | + $sidecarFile = $this->getSidecarFile(); |
| 121 | + |
| 122 | + $descriptions = array(); |
| 123 | + |
| 124 | + $descriptions[] = array( |
| 125 | + self::KEY_SCREENSHOT_ID => self::DEFAULT_ID |
| 126 | + ); |
| 127 | + |
| 128 | + if($sidecarFile->exists()) { |
| 129 | + foreach($sidecarFile->getData() as $suffix => $def) { |
| 130 | + $def[self::KEY_SCREENSHOT_ID] = $suffix; |
| 131 | + $descriptions[] = $def; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return $descriptions; |
| 136 | + } |
| 137 | + |
| 138 | + private function resolveFileName(?string $suffix=null) : string |
| 139 | + { |
| 140 | + $fileName = $this->mod->getModID(); |
| 141 | + |
| 142 | + if(!empty($suffix) && $suffix !== self::DEFAULT_ID) { |
| 143 | + $fileName .= '-'.$suffix; |
| 144 | + } |
| 145 | + |
| 146 | + return $fileName.'.jpg'; |
| 147 | + } |
| 148 | +} |
0 commit comments