| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace IvyForms\Services\Template; |
| 6 |
|
| 7 |
// phpcs:disable PSR1.Files.SideEffects |
| 8 |
if (!defined('ABSPATH')) { |
| 9 |
exit; // Exit if accessed directly |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Class TemplateMapper |
| 14 |
* |
| 15 |
* Maps template IDs to their corresponding template classes |
| 16 |
* |
| 17 |
* @package IvyForms\Services\Template |
| 18 |
*/ |
| 19 |
class TemplateMapper |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Template ID to class mapping |
| 23 |
* |
| 24 |
* @var array<string, string> |
| 25 |
*/ |
| 26 |
private static array $templateMap = [ |
| 27 |
'contact_form' => ContactFormTemplate::class, |
| 28 |
'blank_form' => BlankFormTemplate::class, |
| 29 |
]; |
| 30 |
|
| 31 |
/** |
| 32 |
* Get template class by template ID |
| 33 |
* |
| 34 |
* @param string $templateId |
| 35 |
* @return string|null Template class name or null if not found |
| 36 |
*/ |
| 37 |
public static function getTemplateClass(string $templateId): ?string |
| 38 |
{ |
| 39 |
return self::$templateMap[$templateId] ?? null; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Get template data by template ID |
| 44 |
* |
| 45 |
* @param string $templateId |
| 46 |
* @return array<string, mixed>|null Template data or null if not found |
| 47 |
*/ |
| 48 |
public static function getTemplate(string $templateId): ?array |
| 49 |
{ |
| 50 |
$templateClass = self::getTemplateClass($templateId); |
| 51 |
|
| 52 |
if (!$templateClass || !class_exists($templateClass)) { |
| 53 |
return null; |
| 54 |
} |
| 55 |
|
| 56 |
return $templateClass::getTemplate(); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Get all template IDs |
| 61 |
* |
| 62 |
* @return array<int, string> |
| 63 |
*/ |
| 64 |
public static function getAllTemplateIds(): array |
| 65 |
{ |
| 66 |
return array_keys(self::$templateMap); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Check if template ID exists |
| 71 |
* |
| 72 |
* @param string $templateId |
| 73 |
* @return bool |
| 74 |
*/ |
| 75 |
public static function hasTemplate(string $templateId): bool |
| 76 |
{ |
| 77 |
return isset(self::$templateMap[$templateId]); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Get all templates as associative array |
| 82 |
* |
| 83 |
* @return array<string, array<string, mixed>> |
| 84 |
*/ |
| 85 |
public static function getAllTemplates(): array |
| 86 |
{ |
| 87 |
$templates = []; |
| 88 |
|
| 89 |
foreach (self::$templateMap as $templateId => $templateClass) { |
| 90 |
// Skip BlankFormTemplate from public template list |
| 91 |
if ($templateClass === BlankFormTemplate::class) { |
| 92 |
continue; |
| 93 |
} |
| 94 |
|
| 95 |
if (class_exists($templateClass)) { |
| 96 |
$templateData = $templateClass::getTemplate(); |
| 97 |
$templates[$templateId] = $templateData; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
return $templates; |
| 102 |
} |
| 103 |
} |
| 104 |
|