44namespace Synapse \Prompts ;
55
66use Cake \Core \Configure ;
7+ use Mcp \Exception \PromptGetException ;
78
89/**
910 * Abstract Prompt Base Class
@@ -30,4 +31,82 @@ public function __construct()
3031 $ this ->cakephpVersion = Configure::read ('Synapse.prompts.cakephp_version ' , '5.x ' );
3132 $ this ->phpVersion = Configure::read ('Synapse.prompts.php_version ' , '8.2 ' );
3233 }
34+
35+ /**
36+ * Validate that a parameter value is one of the allowed values
37+ *
38+ * @param string $value The parameter value
39+ * @param array<string> $allowed Allowed values
40+ * @param string $paramName The parameter name (for error message)
41+ * @param string $promptName The prompt name (for error message)
42+ * @throws \Mcp\Exception\PromptGetException
43+ */
44+ protected function validateEnumParameter (
45+ string $ value ,
46+ array $ allowed ,
47+ string $ paramName ,
48+ string $ promptName ,
49+ ): void {
50+ if (!in_array ($ value , $ allowed , true )) {
51+ $ allowedStr = implode (', ' , $ allowed );
52+ throw new PromptGetException (
53+ sprintf ("Invalid value for parameter '%s': '%s'. " , $ paramName , $ value ) .
54+ sprintf ('Expected one of: %s. ' , $ allowedStr ) .
55+ 'Prompt: ' . $ promptName ,
56+ );
57+ }
58+ }
59+
60+ /**
61+ * Validate that a parameter is not empty
62+ *
63+ * @param string $value The parameter value
64+ * @param string $paramName The parameter name (for error message)
65+ * @param string $promptName The prompt name (for error message)
66+ * @throws \Mcp\Exception\PromptGetException
67+ */
68+ protected function validateNonEmptyParameter (
69+ string $ value ,
70+ string $ paramName ,
71+ string $ promptName ,
72+ ): void {
73+ if ($ value === '' || $ value === '0 ' ) {
74+ throw new PromptGetException (
75+ sprintf ("Parameter '%s' cannot be empty. Prompt: %s " , $ paramName , $ promptName ),
76+ );
77+ }
78+ }
79+
80+ /**
81+ * Validate comma-separated values against allowed list
82+ *
83+ * @param string $value Comma-separated values
84+ * @param array<string> $allowed Allowed values
85+ * @param string $paramName The parameter name (for error message)
86+ * @param string $promptName The prompt name (for error message)
87+ * @throws \Mcp\Exception\PromptGetException
88+ */
89+ protected function validateCommaSeparatedParameter (
90+ string $ value ,
91+ array $ allowed ,
92+ string $ paramName ,
93+ string $ promptName ,
94+ ): void {
95+ if ($ value === '' || $ value === '0 ' ) {
96+ return ;
97+ }
98+
99+ $ values = array_map ('trim ' , explode (', ' , $ value ));
100+ $ invalid = array_diff ($ values , $ allowed );
101+
102+ if ($ invalid !== []) {
103+ $ invalidStr = implode (', ' , $ invalid );
104+ $ allowedStr = implode (', ' , $ allowed );
105+ throw new PromptGetException (
106+ sprintf ("Invalid values for parameter '%s': %s. " , $ paramName , $ invalidStr ) .
107+ sprintf ('Expected one of: %s. ' , $ allowedStr ) .
108+ 'Prompt: ' . $ promptName ,
109+ );
110+ }
111+ }
33112}
0 commit comments