@@ -32,6 +32,25 @@ abstract public function getKeywords(): array;
3232 */
3333 abstract public function getIdentifierOverrides (): array ;
3434
35+ /**
36+ * Get the static access operator for the language (e.g. '::' for PHP, '.' for JS)
37+ * @return string
38+ */
39+ abstract public function getStaticAccessOperator (): string ;
40+
41+ /**
42+ * Get the string quote character for the language (e.g. '"' for PHP, "'" for JS)
43+ * @return string
44+ */
45+ abstract public function getStringQuote (): string ;
46+
47+ /**
48+ * Wrap elements in an array syntax for the language
49+ * @param string $elements Comma-separated elements
50+ * @return string
51+ */
52+ abstract public function getArrayOf (string $ elements ): string ;
53+
3554 /**
3655 * @return array<array>
3756 */
@@ -137,4 +156,124 @@ protected function toUpperSnakeCase($str): string
137156 {
138157 return \strtoupper ($ this ->toSnakeCase ($ str ));
139158 }
159+
160+ public function isPermissionString (string $ string ): bool
161+ {
162+ $ pattern = '/^\["(read|update|delete|write)\( \\"[^ \\"]+ \\"\)"(,\s*"(read|update|delete|write)\( \\"[^ \\"]+ \\"\)")*\]$/ ' ;
163+ return preg_match ($ pattern , $ string ) === 1 ;
164+ }
165+
166+ public function extractPermissionParts (string $ string ): array
167+ {
168+ $ inner = substr ($ string , 1 , -1 );
169+ preg_match_all ('/"(read|update|delete|write)\( \\"([^ \\"]+) \\"\)"/ ' , $ inner , $ matches , PREG_SET_ORDER );
170+
171+ $ result = [];
172+ foreach ($ matches as $ match ) {
173+ $ action = $ match [1 ];
174+ $ roleString = $ match [2 ];
175+
176+ $ role = null ;
177+ $ id = null ;
178+ $ innerRole = null ;
179+
180+ if (strpos ($ roleString , ': ' ) !== false ) {
181+ $ role = explode (': ' , $ roleString , 2 )[0 ];
182+ $ idString = explode (': ' , $ roleString , 2 )[1 ];
183+
184+ if (strpos ($ idString , '/ ' ) !== false ) {
185+ $ id = explode ('/ ' , $ idString , 2 )[0 ];
186+ $ innerRole = explode ('/ ' , $ idString , 2 )[1 ];
187+ } else {
188+ $ id = $ idString ;
189+ }
190+ } else {
191+ $ role = $ roleString ;
192+ }
193+
194+ $ result [] = [
195+ 'action ' => $ action ,
196+ 'role ' => $ role ,
197+ 'id ' => $ id ?? null ,
198+ 'innerRole ' => $ innerRole
199+ ];
200+ }
201+
202+ return $ result ;
203+ }
204+
205+ public function hasPermissionParam (array $ parameters ): bool
206+ {
207+ foreach ($ parameters as $ param ) {
208+ $ example = $ param ['example ' ] ?? '' ;
209+ if (!empty ($ example ) && is_string ($ example ) && $ this ->isPermissionString ($ example )) {
210+ return true ;
211+ }
212+ }
213+ return false ;
214+ }
215+
216+ /**
217+ * Get the prefix for Permission and Role classes (e.g., 'sdk.' for Node)
218+ * @return string
219+ */
220+ protected function getPermissionPrefix (): string
221+ {
222+ return '' ;
223+ }
224+
225+ /**
226+ * Transform permission action name for language-specific casing
227+ * Override in child classes if needed (e.g., DotNet uses ucfirst)
228+ * @param string $action
229+ * @return string
230+ */
231+ protected function transformPermissionAction (string $ action ): string
232+ {
233+ return $ action ;
234+ }
235+
236+ /**
237+ * Transform permission role name for language-specific casing
238+ * Override in child classes if needed (e.g., DotNet uses ucfirst)
239+ * @param string $role
240+ * @return string
241+ */
242+ protected function transformPermissionRole (string $ role ): string
243+ {
244+ return $ role ;
245+ }
246+
247+ /**
248+ * Generate permission example code for the language
249+ * @param string $example Permission string example
250+ * @return string
251+ */
252+ public function getPermissionExample (string $ example ): string
253+ {
254+ $ permissions = [];
255+ $ staticOp = $ this ->getStaticAccessOperator ();
256+ $ quote = $ this ->getStringQuote ();
257+ $ prefix = $ this ->getPermissionPrefix ();
258+
259+ foreach ($ this ->extractPermissionParts ($ example ) as $ permission ) {
260+ $ args = [];
261+ if ($ permission ['id ' ] !== null ) {
262+ $ args [] = $ quote . $ permission ['id ' ] . $ quote ;
263+ }
264+ if ($ permission ['innerRole ' ] !== null ) {
265+ $ args [] = $ quote . $ permission ['innerRole ' ] . $ quote ;
266+ }
267+ $ argsString = implode (', ' , $ args );
268+
269+ $ action = $ permission ['action ' ];
270+ $ role = $ permission ['role ' ];
271+ $ action = $ this ->transformPermissionAction ($ action );
272+ $ role = $ this ->transformPermissionRole ($ role );
273+
274+ $ permissions [] = $ prefix . 'Permission ' . $ staticOp . $ action . '( ' . $ prefix . 'Role ' . $ staticOp . $ role . '( ' . $ argsString . ')) ' ;
275+ }
276+
277+ return $ this ->getArrayOf (implode (', ' , $ permissions ));
278+ }
140279}
0 commit comments