Skip to content

Commit a6d8d2b

Browse files
committed
Added the screenshot collection.
1 parent d952d25 commit a6d8d2b

File tree

3 files changed

+217
-0
lines changed

3 files changed

+217
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CPMDB\Mods\Mod\Screenshots;
6+
7+
use AppUtils\ArrayDataCollection;
8+
use AppUtils\FileHelper\FileInfo;
9+
10+
class ModScreenshot implements ModScreenshotInterface
11+
{
12+
public const META_KEY_TITLE = 'title';
13+
14+
private FileInfo $imageFile;
15+
private string $id;
16+
private ModScreenshotCollection $collection;
17+
private string $imageURL;
18+
private ArrayDataCollection $metaData;
19+
20+
public function __construct(ModScreenshotCollection $collection, string $screenshotID, FileInfo $imageFile, string $imageURL, array $metaData)
21+
{
22+
$this->collection = $collection;
23+
$this->imageFile = $imageFile;
24+
$this->id = $screenshotID;
25+
$this->imageURL = $imageURL;
26+
$this->metaData = ArrayDataCollection::create($metaData);
27+
}
28+
29+
public function getID() : string
30+
{
31+
return $this->id;
32+
}
33+
34+
public function isDefault() : bool
35+
{
36+
return $this->id === $this->collection->getDefaultID();
37+
}
38+
39+
public function getURL() : string
40+
{
41+
return $this->imageURL;
42+
}
43+
44+
public function getImageFile() : FileInfo
45+
{
46+
return $this->imageFile;
47+
}
48+
49+
public function getTitle() : string
50+
{
51+
return $this->metaData->getString(self::META_KEY_TITLE);
52+
}
53+
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CPMDB\Mods\Mod\Screenshots;
6+
7+
use AppUtils\FileHelper\FileInfo;
8+
use AppUtils\Interfaces\StringPrimaryRecordInterface;
9+
10+
interface ModScreenshotInterface extends StringPrimaryRecordInterface
11+
{
12+
public function isDefault() : bool;
13+
public function getURL() : string;
14+
public function getImageFile() : FileInfo;
15+
public function getTitle() : string;
16+
}

0 commit comments

Comments
 (0)