11<?php
2+ /**
3+ * @package Mods
4+ * @subpackage Screenshots
5+ */
26
37declare (strict_types=1 );
48
59namespace CPMDB \Mods \Mod \Screenshots ;
610
11+ use AppUtils \ArrayDataCollection ;
712use AppUtils \Collections \BaseStringPrimaryCollection ;
813use AppUtils \FileHelper \FileInfo ;
914use AppUtils \FileHelper \FolderInfo ;
1015use AppUtils \FileHelper \JSONFile ;
1116use CPMDB \Mods \Mod \ModInfoInterface ;
17+ use function AppUtils \t ;
18+ use const CPMDB \Assets \KEY_SCREENSHOT_TITLE ;
1219
1320/**
1421 * Collection of screenshot files available for a mod.
1522 *
16- * @package CPMDB
17- * @subpackage Mod Collection
23+ * @package Mods
24+ * @subpackage Screenshots
1825 *
1926 * @method ModScreenshotInterface getByID(string $id)
2027 * @method ModScreenshotInterface getDefault()
2330class ModScreenshotCollection extends BaseStringPrimaryCollection
2431{
2532 public const DEFAULT_ID = '000default ' ;
26- private const KEY_SCREENSHOT_ID = 'screenshotID ' ;
33+ public const KEY_SCREENSHOT_ID = 'screenshotID ' ;
2734
2835 private ModInfoInterface $ mod ;
2936
@@ -64,18 +71,18 @@ public function getDefaultID() : string
6471
6572 protected function registerItems (): void
6673 {
67- foreach ($ this ->getDescriptions () as $ def ) {
74+ foreach ($ this ->getMetaData () as $ def ) {
6875 $ this ->registerScreenshot ($ def );
6976 }
7077 }
7178
7279 /**
73- * @param array<string,scalar> $def
80+ * @param ScreenshotMetaData $def
7481 * @return void
7582 */
76- private function registerScreenshot (array $ def ) : void
83+ private function registerScreenshot (ScreenshotMetaData $ def ) : void
7784 {
78- $ fileName = $ this ->resolveFileName ($ def[ self :: KEY_SCREENSHOT_ID ] );
85+ $ fileName = $ this ->resolveFileName ($ def-> getScreenshotID () );
7986 $ file = FileInfo::factory ($ this ->getScreensFolder ().'/ ' .$ fileName );
8087
8188 if (!$ file ->exists ()) {
@@ -84,7 +91,7 @@ private function registerScreenshot(array $def) : void
8491
8592 $ this ->registerItem (new ModScreenshot (
8693 $ this ,
87- $ def[ self :: KEY_SCREENSHOT_ID ] ,
94+ $ def-> getScreenshotID () ,
8895 $ file ,
8996 $ this ->getScreensURL ().'/ ' .$ fileName ,
9097 $ def
@@ -113,28 +120,42 @@ public function getSidecarFile() : JSONFile
113120 * screenshot. If no sidecar file is present, only the default
114121 * screenshot is returned.
115122 *
116- * @return array<int,array{string,scalar}>
123+ * @return ScreenshotMetaData[]
117124 */
118- public function getDescriptions () : array
125+ public function getMetaData () : array
119126 {
120127 $ sidecarFile = $ this ->getSidecarFile ();
121128
122129 $ descriptions = array ();
123130
124- $ descriptions [] = array (
125- self ::KEY_SCREENSHOT_ID => self ::DEFAULT_ID
126- );
131+ $ descriptions [] = new ScreenshotMetaData (self ::DEFAULT_ID , t ('Default mod screenshot ' ));
127132
128133 if ($ sidecarFile ->exists ()) {
129134 foreach ($ sidecarFile ->getData () as $ suffix => $ def ) {
130- $ def [self ::KEY_SCREENSHOT_ID ] = $ suffix ;
131- $ descriptions [] = $ def ;
135+ $ descriptions [] = $ this ->resolveMetaData ((string )$ suffix , $ def );
132136 }
133137 }
134138
135139 return $ descriptions ;
136140 }
137141
142+ /**
143+ * @param string $suffix
144+ * @param mixed $def
145+ * @return ScreenshotMetaData
146+ */
147+ private function resolveMetaData (string $ suffix , mixed $ def ) : ScreenshotMetaData
148+ {
149+ $ title = '' ;
150+
151+ if (is_array ($ def )) {
152+ $ data = ArrayDataCollection::create ($ def );
153+ $ title = $ data ->getString (KEY_SCREENSHOT_TITLE );
154+ }
155+
156+ return new ScreenshotMetaData ($ suffix , $ title );
157+ }
158+
138159 private function resolveFileName (?string $ suffix =null ) : string
139160 {
140161 $ fileName = $ this ->mod ->getModID ();
0 commit comments