diff --git a/app/Platform/Actions/ComputeGameSortTitleAction.php b/app/Platform/Actions/ComputeGameSortTitleAction.php index 91e6968cc4..cbfe8cbdea 100644 --- a/app/Platform/Actions/ComputeGameSortTitleAction.php +++ b/app/Platform/Actions/ComputeGameSortTitleAction.php @@ -32,7 +32,7 @@ private function padNumbers(string $title): string return preg_replace_callback( // Match numbers not directly attached to letters. // In other words, don't convert "Mega Man X4" to "Mega Man X00004". - '/(?<=\b|\s)(\d+)(?=\b|\s)/', + '/(?words(2, true)); - $sortTitle = (new ComputeGameSortTitleAction())->execute($title); return [ 'Title' => $title, - 'sort_title' => $sortTitle, + 'sort_title' => null, 'ConsoleID' => 0, 'ImageIcon' => '/Images/000001.png', ]; } + + public function configure(): static + { + /** + * Derive the sort_title from both autogenerated and manually-provided titles. + * If the factory receives a custom sort_title, do nothing. + */ + + return $this->afterMaking(function (Game $game) { + if ($game->sort_title === null) { + $game->sort_title = (new ComputeGameSortTitleAction())->execute($game->title); + } + })->afterCreating(function (Game $game) { + if ($game->sort_title === null) { + $game->sort_title = (new ComputeGameSortTitleAction())->execute($game->title); + } + }); + } } diff --git a/tests/Feature/Platform/Action/ComputeGameSortTitleActionTest.php b/tests/Feature/Platform/Action/ComputeGameSortTitleActionTest.php index aca7abb468..a334ecb0fa 100644 --- a/tests/Feature/Platform/Action/ComputeGameSortTitleActionTest.php +++ b/tests/Feature/Platform/Action/ComputeGameSortTitleActionTest.php @@ -34,12 +34,14 @@ public static function titleProvider(): array '007: Agent Under Fire' => ['007: Agent Under Fire', '00007 agent under fire'], '101 Dalmatians' => ['101 Dalmatians', '00101 dalmatians'], '50 Cent: Blood on the Sand' => ['50 Cent: Blood on the Sand', '00050 cent blood on the sand'], + 'A-Mazing Tater' => ['A-Mazing Tater', 'a-mazing tater'], 'A Series of Unfortunate Events' => ['A Series of Unfortunate Events', 'series of unfortunate events'], 'American Tale, An' => ['American Tale, An', 'american tale'], 'An Unexpected Journey' => ['An Unexpected Journey', 'unexpected journey'], 'Bravely Défault II' => ['Bravely Défault II', 'bravely default 00002'], 'Final Fantasy IV' => ['Final Fantasy IV', 'final fantasy 00004'], 'Final Fantasy X' => ['Final Fantasy X', 'final fantasy 00010'], + 'Final Fantasy X-2' => ['Final Fantasy X-2', 'final fantasy 00010-00002'], 'Formula 1-97' => ['Formula 1-97', 'formula 00001-00097'], 'GIVX' => ['GIVX', 'givx'], 'Grand Day Out, A' => ['Grand Day Out, A', 'grand day out'], @@ -65,6 +67,7 @@ public static function titleProvider(): array 'Super Mario Bros. 3' => ['Super Mario Bros. 3', 'super mario bros 00003'], 'The Great Escape' => ['The Great Escape', 'great escape'], 'The Matrix Reloaded' => ['The Matrix Reloaded', 'matrix reloaded'], + 'V-Tetris' => ['V-Tetris', 'v-tetris'], ]; } }