Skip to content

Commit 4030614

Browse files
committed
Merge remote-tracking branch 'origin/master' into feat-operators
# Conflicts: # templates/react-native/src/index.ts.twig # templates/web/src/index.ts.twig
2 parents 304388a + 9359bef commit 4030614

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+795
-78
lines changed

src/SDK/Language.php

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/SDK/Language/Dart.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,21 @@ public function getIdentifierOverrides(): array
121121
];
122122
}
123123

124+
public function getStaticAccessOperator(): string
125+
{
126+
return '.';
127+
}
128+
129+
public function getStringQuote(): string
130+
{
131+
return "'";
132+
}
133+
134+
public function getArrayOf(string $elements): string
135+
{
136+
return '[' . $elements . ']';
137+
}
138+
124139
/**
125140
* @param array $parameter
126141
* @return string
@@ -240,7 +255,8 @@ public function getParamExample(array $param): string
240255
}
241256

242257
return match ($type) {
243-
self::TYPE_ARRAY, self::TYPE_FILE, self::TYPE_INTEGER, self::TYPE_NUMBER => $example,
258+
self::TYPE_ARRAY => $this->isPermissionString($example) ? $this->getPermissionExample($example) : $example,
259+
self::TYPE_FILE, self::TYPE_INTEGER, self::TYPE_NUMBER => $example,
244260
self::TYPE_BOOLEAN => ($example) ? 'true' : 'false',
245261
self::TYPE_OBJECT => ($decoded = json_decode($example, true)) !== null
246262
? (empty($decoded) && $example === '{}'

src/SDK/Language/Deno.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,21 @@ public function getName(): string
1212
return 'Deno';
1313
}
1414

15+
public function getStaticAccessOperator(): string
16+
{
17+
return '.';
18+
}
19+
20+
public function getStringQuote(): string
21+
{
22+
return "'";
23+
}
24+
25+
public function getArrayOf(string $elements): string
26+
{
27+
return '[' . $elements . ']';
28+
}
29+
1530
/**
1631
* @return array
1732
*/
@@ -186,7 +201,8 @@ public function getParamExample(array $param): string
186201
}
187202

188203
return match ($type) {
189-
self::TYPE_ARRAY, self::TYPE_INTEGER, self::TYPE_NUMBER => $example,
204+
self::TYPE_ARRAY => $this->isPermissionString($example) ? $this->getPermissionExample($example) : $example,
205+
self::TYPE_INTEGER, self::TYPE_NUMBER => $example,
190206
self::TYPE_FILE => 'InputFile.fromPath(\'/path/to/file.png\', \'file.png\')',
191207
self::TYPE_BOOLEAN => ($example) ? 'true' : 'false',
192208
self::TYPE_OBJECT => ($example === '{}')

src/SDK/Language/DotNet.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,31 @@ public function getIdentifierOverrides(): array
146146
];
147147
}
148148

149+
public function getStaticAccessOperator(): string
150+
{
151+
return '.';
152+
}
153+
154+
public function getStringQuote(): string
155+
{
156+
return '"';
157+
}
158+
159+
public function getArrayOf(string $elements): string
160+
{
161+
return 'new List<string> { ' . $elements . ' }';
162+
}
163+
164+
protected function transformPermissionAction(string $action): string
165+
{
166+
return ucfirst($action);
167+
}
168+
169+
protected function transformPermissionRole(string $role): string
170+
{
171+
return ucfirst($role);
172+
}
173+
149174
public function getPropertyOverrides(): array
150175
{
151176
return [
@@ -282,9 +307,11 @@ public function getParamExample(array $param): string
282307
case self::TYPE_FILE:
283308
case self::TYPE_NUMBER:
284309
case self::TYPE_INTEGER:
285-
case self::TYPE_ARRAY:
286310
$output .= $example;
287311
break;
312+
case self::TYPE_ARRAY:
313+
$output .= $this->isPermissionString($example) ? $this->getPermissionExample($example) : $example;
314+
break;
288315
case self::TYPE_OBJECT:
289316
if ($example === '{}') {
290317
$output .= '[object]';

src/SDK/Language/Go.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ public function getKeywords(): array
4444
];
4545
}
4646

47+
public function getStaticAccessOperator(): string
48+
{
49+
return '.';
50+
}
51+
52+
public function getStringQuote(): string
53+
{
54+
return '"';
55+
}
56+
57+
public function getArrayOf(string $elements): string
58+
{
59+
return '[' . $elements . ']';
60+
}
61+
4762
/**
4863
* @return array
4964
*/

src/SDK/Language/GraphQL.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,21 @@ public function getName(): string
1212
return 'GraphQL';
1313
}
1414

15+
public function getStaticAccessOperator(): string
16+
{
17+
return '.';
18+
}
19+
20+
public function getStringQuote(): string
21+
{
22+
return '"';
23+
}
24+
25+
public function getArrayOf(string $elements): string
26+
{
27+
return '[' . $elements . ']';
28+
}
29+
1530
/**
1631
* @param $type
1732
* @return string

src/SDK/Language/Kotlin.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,21 @@ public function getIdentifierOverrides(): array
9999
return [];
100100
}
101101

102+
public function getStaticAccessOperator(): string
103+
{
104+
return '.';
105+
}
106+
107+
public function getStringQuote(): string
108+
{
109+
return '"';
110+
}
111+
112+
public function getArrayOf(string $elements): string
113+
{
114+
return 'listOf(' . $elements . ')';
115+
}
116+
102117
/**
103118
* @param array $parameter
104119
* @param array $spec
@@ -257,13 +272,17 @@ public function getParamExample(array $param): string
257272
$output .= $example;
258273
break;
259274
case self::TYPE_ARRAY:
260-
if (\str_starts_with($example, '[')) {
261-
$example = \substr($example, 1);
262-
}
263-
if (\str_ends_with($example, ']')) {
264-
$example = \substr($example, 0, -1);
275+
if ($this->isPermissionString($example)) {
276+
$output .= $this->getPermissionExample($example);
277+
} else {
278+
if (\str_starts_with($example, '[')) {
279+
$example = \substr($example, 1);
280+
}
281+
if (\str_ends_with($example, ']')) {
282+
$example = \substr($example, 0, -1);
283+
}
284+
$output .= 'listOf(' . $example . ')';
265285
}
266-
$output .= 'listOf(' . $example . ')';
267286
break;
268287
case self::TYPE_BOOLEAN:
269288
$output .= ($example) ? 'true' : 'false';

src/SDK/Language/Node.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,26 @@ public function getName(): string
1212
return 'NodeJS';
1313
}
1414

15+
public function getStaticAccessOperator(): string
16+
{
17+
return '.';
18+
}
19+
20+
public function getStringQuote(): string
21+
{
22+
return "'";
23+
}
24+
25+
public function getArrayOf(string $elements): string
26+
{
27+
return '[' . $elements . ']';
28+
}
29+
30+
protected function getPermissionPrefix(): string
31+
{
32+
return 'sdk.';
33+
}
34+
1535
public function getTypeName(array $parameter, array $method = []): string
1636
{
1737
if (isset($parameter['enumName'])) {
@@ -140,7 +160,8 @@ public function getParamExample(array $param): string
140160
}
141161

142162
return match ($type) {
143-
self::TYPE_ARRAY, self::TYPE_FILE, self::TYPE_INTEGER, self::TYPE_NUMBER => $example,
163+
self::TYPE_ARRAY => $this->isPermissionString($example) ? $this->getPermissionExample($example) : $example,
164+
self::TYPE_FILE, self::TYPE_INTEGER, self::TYPE_NUMBER => $example,
144165
self::TYPE_BOOLEAN => ($example) ? 'true' : 'false',
145166
self::TYPE_OBJECT => ($example === '{}')
146167
? '{}'

0 commit comments

Comments
 (0)