@@ -35,6 +35,7 @@ class Endpoint
35
35
public const CHECK_OPTIONS_TEMPLATE = __DIR__ . '/template/check-options ' ;
36
36
public const SET_BULK_BODY_TEMPLATE = __DIR__ . '/template/set-bulk-body ' ;
37
37
public const DEPRECATED_PART = __DIR__ . '/template/deprecated ' ;
38
+ public const DEPRECATED_PARAM = __DIR__ . '/template/deprecated-param-master-timeout ' ;
38
39
public const PHP_RESERVED_WORDS = [
39
40
'abstract ' , 'and ' , 'array ' , 'as ' , 'break ' , 'callable ' , 'case ' , 'catch ' ,
40
41
'class ' , 'clone ' , 'const ' , 'continue ' , 'declare ' , 'default ' , 'die ' , 'do ' ,
@@ -58,8 +59,6 @@ class Endpoint
58
59
public $ name ;
59
60
public $ apiName ;
60
61
protected $ content ;
61
- protected $ version ;
62
- protected $ buildhash ;
63
62
protected $ parts = [];
64
63
protected $ requiredParts = [];
65
64
protected $ useNamespace = [];
@@ -69,8 +68,6 @@ class Endpoint
69
68
public function __construct (
70
69
string $ fileName ,
71
70
string $ content ,
72
- string $ version ,
73
- string $ buildhash
74
71
) {
75
72
$ this ->apiName = basename ($ fileName , '.json ' );
76
73
$ parts = explode ('. ' , $ this ->apiName );
@@ -95,8 +92,6 @@ public function __construct(
95
92
));
96
93
}
97
94
$ this ->content = $ this ->content [$ this ->apiName ];
98
- $ this ->version = $ version ;
99
- $ this ->buildhash = $ buildhash ;
100
95
101
96
$ this ->parts = $ this ->getPartsFromContent ($ this ->content );
102
97
$ this ->requiredParts = $ this ->getRequiredParts ($ this ->content );
@@ -127,6 +122,8 @@ private function getRequiredParts(array $content): array
127
122
}
128
123
if (count ($ required ) > 1 ) {
129
124
return call_user_func_array ('array_intersect ' , $ required );
125
+ } elseif (count ($ required ) === 1 ) {
126
+ return $ required [0 ];
130
127
}
131
128
return $ required ;
132
129
}
@@ -146,7 +143,7 @@ public function renderClass(): string
146
143
}
147
144
$ class = str_replace (
148
145
':uri ' ,
149
- $ this ->extractUrl ($ this ->content ['url ' ]['paths ' ]),
146
+ trim ( $ this ->extractUrl ($ this ->content ['url ' ]['paths ' ]) ),
150
147
$ class
151
148
);
152
149
$ class = str_replace (
@@ -164,11 +161,15 @@ public function renderClass(): string
164
161
165
162
// Set the HTTP method
166
163
$ action = $ this ->getMethod ();
167
- if (!empty ($ this ->content ['body ' ]) &&
168
- ($ action === ['GET ' , 'POST ' ] || $ action === ['POST ' , 'GET ' ])) {
169
- $ method = 'isset($this->body) ? \'POST \' : \'GET \'' ;
164
+ if ($ action === ['POST ' , 'PUT ' ] && $ this ->getClassName () !== 'Bulk ' ) {
165
+ $ method = "'PUT' " ;
170
166
} else {
171
- $ method = sprintf ("'%s' " , reset ($ action ));
167
+ if (!empty ($ this ->content ['body ' ]) &&
168
+ ($ action === ['GET ' , 'POST ' ] || $ action === ['POST ' , 'GET ' ])) {
169
+ $ method = 'isset($this->body) ? \'POST \' : \'GET \'' ;
170
+ } else {
171
+ $ method = sprintf ("'%s' " , reset ($ action ));
172
+ }
172
173
}
173
174
$ class = str_replace (':method ' , $ method , $ class );
174
175
@@ -186,19 +187,44 @@ public function renderClass(): string
186
187
if (in_array ($ part , ['type ' , 'index ' , 'id ' ])) {
187
188
continue ;
188
189
}
189
- if (isset ($ value ['type ' ]) && $ value ['type ' ] === 'list ' ) {
190
+ if (isset ($ value ['type ' ]) && $ value ['type ' ] === 'array ' ) {
190
191
$ parts .= $ this ->getSetPartList ($ part );
191
192
} else {
192
193
$ parts .= $ this ->getSetPart ($ part );
193
194
}
194
195
}
195
196
$ class = str_replace (':set-parts ' , $ parts , $ class );
196
197
$ class = str_replace (':endpoint ' , $ this ->getClassName (), $ class );
197
- $ class = str_replace (':version ' , $ this ->version , $ class );
198
- $ class = str_replace (':buildhash ' , $ this ->buildhash , $ class );
199
198
$ class = str_replace (':use-namespace ' , $ this ->getNamespaces (), $ class );
200
199
$ class = str_replace (':properties ' , $ this ->getProperties (), $ class );
200
+ if (isset ($ this ->content ['params ' ]['master_timeout ' ])) {
201
+ $ deprecatedContent = file_get_contents (self ::DEPRECATED_PARAM );
202
+ $ class = str_replace (':master_timeout_ParamDeprecation ' , $ deprecatedContent , $ class );
203
+ } else {
204
+ $ class = preg_replace ('/\s*:master_timeout_ParamDeprecation\s*\}/ ' , '} ' , $ class );
205
+ }
206
+
207
+ // Adding licence header
208
+ $ currentDir = dirname (__FILE__ );
209
+ $ baseDir = dirname ($ currentDir );
210
+ $ EndpointName = $ this ->getClassName ();
211
+
212
+ if (!empty ($ this ->namespace )) {
213
+ $ filePath = $ baseDir . "/src/OpenSearch/Endpoints/ $ this ->namespace / $ EndpointName.php " ;
214
+ } else {
215
+ $ filePath = $ baseDir . "/src/OpenSearch/Endpoints/ $ EndpointName.php " ;
216
+ }
217
+
218
+ if (file_exists ($ filePath )) {
219
+ $ content = file_get_contents ($ filePath );
201
220
221
+ if (strpos ($ content , 'Copyright OpenSearch ' ) !== false ) {
222
+ $ pattern = '/\/\*\*.*?\*\//s ' ;
223
+ if (preg_match ($ pattern , $ content , $ matches )) {
224
+ $ class = str_replace ('declare(strict_types=1); ' , 'declare(strict_types=1); ' . PHP_EOL . PHP_EOL . $ matches [0 ], $ class );
225
+ }
226
+ }
227
+ }
202
228
return str_replace (':apiname ' , $ this ->apiName , $ class );
203
229
}
204
230
@@ -291,7 +317,7 @@ private function extractUrl(array $paths): string
291
317
$ check = sprintf ("isset( \$%s) " , $ parts [0 ]);
292
318
}
293
319
$ url = str_replace ('{ ' . $ parts [0 ] .'} ' , '$ ' . $ parts [0 ], $ path );
294
- for ($ i= 1 ; $ i< count ($ parts ); $ i ++) {
320
+ for ($ i = 1 ; $ i < count ($ parts ); $ i ++) {
295
321
$ url = str_replace ('{ ' . $ parts [$ i ] .'} ' , '$ ' . $ parts [$ i ], $ url );
296
322
if (in_array ($ parts [$ i ], $ this ->requiredParts )) {
297
323
continue ;
@@ -351,7 +377,7 @@ private function extractPaths(array $paths): array
351
377
$ urls = $ this ->removePathWithSameParts ($ paths );
352
378
// Order the url based on descendant length
353
379
usort ($ urls , function ($ a , $ b ) {
354
- return strlen ($ b )- strlen ($ a );
380
+ return strlen ($ b ) - strlen ($ a );
355
381
});
356
382
357
383
return $ urls ;
@@ -494,8 +520,8 @@ private function extractPartsDescription(int $space): string
494
520
" * \$params['%s']%s = %s(%s) %s%s \n" ,
495
521
$ part ,
496
522
str_repeat (' ' , $ space - strlen ($ part )),
497
- $ part ==='type ' || (isset ($ values ['deprecated ' ]) && $ values ['deprecated ' ]) ? 'DEPRECATED ' : '' ,
498
- $ values ['type ' ],
523
+ $ part === 'type ' || (isset ($ values ['deprecated ' ]) && $ values ['deprecated ' ]) ? 'DEPRECATED ' : '' ,
524
+ $ values ['type ' ] ?? ' any ' ,
499
525
$ values ['description ' ] ?? '' ,
500
526
in_array ($ part , $ this ->requiredParts ) ? ' (Required) ' : ''
501
527
);
@@ -518,7 +544,7 @@ private function extractParamsDescription(int $space): string
518
544
" * \$params['%s']%s = (%s) %s%s%s%s \n" ,
519
545
$ param ,
520
546
str_repeat (' ' , $ space - strlen ($ param )),
521
- $ values ['type ' ],
547
+ $ values ['type ' ] ?? ' any ' ,
522
548
$ values ['description ' ] ?? '' ,
523
549
isset ($ values ['required ' ]) && $ values ['required ' ] ? ' (Required) ' : '' ,
524
550
isset ($ values ['options ' ]) ? sprintf (" (Options = %s) " , implode (', ' , $ values ['options ' ])) : '' ,
0 commit comments