1: | <?php |
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | |
19: | |
20: | |
21: | |
22: | |
23: | |
24: | |
25: | |
26: | |
27: | |
28: | namespace Ally\PetStore; |
29: | |
30: | |
31: | |
32: | |
33: | |
34: | |
35: | |
36: | |
37: | |
38: | |
39: | class Configuration |
40: | { |
41: | public const BOOLEAN_FORMAT_INT = 'int'; |
42: | public const BOOLEAN_FORMAT_STRING = 'string'; |
43: | |
44: | |
45: | |
46: | |
47: | private static $defaultConfiguration; |
48: | |
49: | |
50: | |
51: | |
52: | |
53: | |
54: | protected $apiKeys = []; |
55: | |
56: | |
57: | |
58: | |
59: | |
60: | |
61: | protected $apiKeyPrefixes = []; |
62: | |
63: | |
64: | |
65: | |
66: | |
67: | |
68: | protected $accessToken = ''; |
69: | |
70: | |
71: | |
72: | |
73: | |
74: | |
75: | protected $booleanFormatForQueryString = self::BOOLEAN_FORMAT_INT; |
76: | |
77: | |
78: | |
79: | |
80: | |
81: | |
82: | protected $username = ''; |
83: | |
84: | |
85: | |
86: | |
87: | |
88: | |
89: | protected $password = ''; |
90: | |
91: | |
92: | |
93: | |
94: | |
95: | |
96: | protected $host = 'http://petstore.swagger.io/v2'; |
97: | |
98: | |
99: | |
100: | |
101: | |
102: | |
103: | protected $userAgent = 'OpenAPI-Generator/1.0.0/PHP'; |
104: | |
105: | |
106: | |
107: | |
108: | |
109: | |
110: | protected $debug = false; |
111: | |
112: | |
113: | |
114: | |
115: | |
116: | |
117: | protected $debugFile = 'php://output'; |
118: | |
119: | |
120: | |
121: | |
122: | |
123: | |
124: | protected $tempFolderPath; |
125: | |
126: | |
127: | |
128: | |
129: | public function __construct() |
130: | { |
131: | $this->tempFolderPath = sys_get_temp_dir(); |
132: | } |
133: | |
134: | |
135: | |
136: | |
137: | |
138: | |
139: | |
140: | |
141: | |
142: | public function setApiKey($apiKeyIdentifier, $key) |
143: | { |
144: | $this->apiKeys[$apiKeyIdentifier] = $key; |
145: | return $this; |
146: | } |
147: | |
148: | |
149: | |
150: | |
151: | |
152: | |
153: | |
154: | |
155: | public function getApiKey($apiKeyIdentifier) |
156: | { |
157: | return isset($this->apiKeys[$apiKeyIdentifier]) ? $this->apiKeys[$apiKeyIdentifier] : null; |
158: | } |
159: | |
160: | |
161: | |
162: | |
163: | |
164: | |
165: | |
166: | |
167: | |
168: | public function setApiKeyPrefix($apiKeyIdentifier, $prefix) |
169: | { |
170: | $this->apiKeyPrefixes[$apiKeyIdentifier] = $prefix; |
171: | return $this; |
172: | } |
173: | |
174: | |
175: | |
176: | |
177: | |
178: | |
179: | |
180: | |
181: | public function getApiKeyPrefix($apiKeyIdentifier) |
182: | { |
183: | return isset($this->apiKeyPrefixes[$apiKeyIdentifier]) ? $this->apiKeyPrefixes[$apiKeyIdentifier] : null; |
184: | } |
185: | |
186: | |
187: | |
188: | |
189: | |
190: | |
191: | |
192: | |
193: | public function setAccessToken($accessToken) |
194: | { |
195: | $this->accessToken = $accessToken; |
196: | return $this; |
197: | } |
198: | |
199: | |
200: | |
201: | |
202: | |
203: | |
204: | public function getAccessToken() |
205: | { |
206: | return $this->accessToken; |
207: | } |
208: | |
209: | |
210: | |
211: | |
212: | |
213: | |
214: | |
215: | |
216: | public function setBooleanFormatForQueryString(string $booleanFormat) |
217: | { |
218: | $this->booleanFormatForQueryString = $booleanFormat; |
219: | |
220: | return $this; |
221: | } |
222: | |
223: | |
224: | |
225: | |
226: | |
227: | |
228: | public function getBooleanFormatForQueryString(): string |
229: | { |
230: | return $this->booleanFormatForQueryString; |
231: | } |
232: | |
233: | |
234: | |
235: | |
236: | |
237: | |
238: | |
239: | |
240: | public function setUsername($username) |
241: | { |
242: | $this->username = $username; |
243: | return $this; |
244: | } |
245: | |
246: | |
247: | |
248: | |
249: | |
250: | |
251: | public function getUsername() |
252: | { |
253: | return $this->username; |
254: | } |
255: | |
256: | |
257: | |
258: | |
259: | |
260: | |
261: | |
262: | |
263: | public function setPassword($password) |
264: | { |
265: | $this->password = $password; |
266: | return $this; |
267: | } |
268: | |
269: | |
270: | |
271: | |
272: | |
273: | |
274: | public function getPassword() |
275: | { |
276: | return $this->password; |
277: | } |
278: | |
279: | |
280: | |
281: | |
282: | |
283: | |
284: | |
285: | |
286: | public function setHost($host) |
287: | { |
288: | $this->host = $host; |
289: | return $this; |
290: | } |
291: | |
292: | |
293: | |
294: | |
295: | |
296: | |
297: | public function getHost() |
298: | { |
299: | return $this->host; |
300: | } |
301: | |
302: | |
303: | |
304: | |
305: | |
306: | |
307: | |
308: | |
309: | |
310: | public function setUserAgent($userAgent) |
311: | { |
312: | if (!is_string($userAgent)) { |
313: | throw new \InvalidArgumentException('User-agent must be a string.'); |
314: | } |
315: | |
316: | $this->userAgent = $userAgent; |
317: | return $this; |
318: | } |
319: | |
320: | |
321: | |
322: | |
323: | |
324: | |
325: | public function getUserAgent() |
326: | { |
327: | return $this->userAgent; |
328: | } |
329: | |
330: | |
331: | |
332: | |
333: | |
334: | |
335: | |
336: | |
337: | public function setDebug($debug) |
338: | { |
339: | $this->debug = $debug; |
340: | return $this; |
341: | } |
342: | |
343: | |
344: | |
345: | |
346: | |
347: | |
348: | public function getDebug() |
349: | { |
350: | return $this->debug; |
351: | } |
352: | |
353: | |
354: | |
355: | |
356: | |
357: | |
358: | |
359: | |
360: | public function setDebugFile($debugFile) |
361: | { |
362: | $this->debugFile = $debugFile; |
363: | return $this; |
364: | } |
365: | |
366: | |
367: | |
368: | |
369: | |
370: | |
371: | public function getDebugFile() |
372: | { |
373: | return $this->debugFile; |
374: | } |
375: | |
376: | |
377: | |
378: | |
379: | |
380: | |
381: | |
382: | |
383: | public function setTempFolderPath($tempFolderPath) |
384: | { |
385: | $this->tempFolderPath = $tempFolderPath; |
386: | return $this; |
387: | } |
388: | |
389: | |
390: | |
391: | |
392: | |
393: | |
394: | public function getTempFolderPath() |
395: | { |
396: | return $this->tempFolderPath; |
397: | } |
398: | |
399: | |
400: | |
401: | |
402: | |
403: | |
404: | public static function getDefaultConfiguration() |
405: | { |
406: | if (self::$defaultConfiguration === null) { |
407: | self::$defaultConfiguration = new Configuration(); |
408: | } |
409: | |
410: | return self::$defaultConfiguration; |
411: | } |
412: | |
413: | |
414: | |
415: | |
416: | |
417: | |
418: | |
419: | |
420: | public static function setDefaultConfiguration(Configuration $config) |
421: | { |
422: | self::$defaultConfiguration = $config; |
423: | } |
424: | |
425: | |
426: | |
427: | |
428: | |
429: | |
430: | public static function toDebugReport() |
431: | { |
432: | $report = 'PHP SDK (Ally\PetStore) Debug Report:' . PHP_EOL; |
433: | $report .= ' OS: ' . php_uname() . PHP_EOL; |
434: | $report .= ' PHP Version: ' . PHP_VERSION . PHP_EOL; |
435: | $report .= ' The version of the OpenAPI document: 1.0.0' . PHP_EOL; |
436: | $report .= ' Temp Folder Path: ' . self::getDefaultConfiguration()->getTempFolderPath() . PHP_EOL; |
437: | |
438: | return $report; |
439: | } |
440: | |
441: | |
442: | |
443: | |
444: | |
445: | |
446: | |
447: | |
448: | public function getApiKeyWithPrefix($apiKeyIdentifier) |
449: | { |
450: | $prefix = $this->getApiKeyPrefix($apiKeyIdentifier); |
451: | $apiKey = $this->getApiKey($apiKeyIdentifier); |
452: | |
453: | if ($apiKey === null) { |
454: | return null; |
455: | } |
456: | |
457: | if ($prefix === null) { |
458: | $keyWithPrefix = $apiKey; |
459: | } else { |
460: | $keyWithPrefix = $prefix . ' ' . $apiKey; |
461: | } |
462: | |
463: | return $keyWithPrefix; |
464: | } |
465: | |
466: | |
467: | |
468: | |
469: | |
470: | |
471: | public function getHostSettings() |
472: | { |
473: | return [ |
474: | [ |
475: | "url" => "http://petstore.swagger.io/v2", |
476: | "description" => "No description provided", |
477: | ] |
478: | ]; |
479: | } |
480: | |
481: | |
482: | |
483: | |
484: | |
485: | |
486: | |
487: | |
488: | |
489: | public static function getHostString(array $hostsSettings, $hostIndex, array $variables = null) |
490: | { |
491: | if (null === $variables) { |
492: | $variables = []; |
493: | } |
494: | |
495: | |
496: | if ($hostIndex < 0 || $hostIndex >= count($hostsSettings)) { |
497: | throw new \InvalidArgumentException("Invalid index $hostIndex when selecting the host. Must be less than ".count($hostsSettings)); |
498: | } |
499: | |
500: | $host = $hostsSettings[$hostIndex]; |
501: | $url = $host["url"]; |
502: | |
503: | |
504: | foreach ($host["variables"] ?? [] as $name => $variable) { |
505: | if (array_key_exists($name, $variables)) { |
506: | if (!isset($variable['enum_values']) || in_array($variables[$name], $variable["enum_values"], true)) { |
507: | $url = str_replace("{".$name."}", $variables[$name], $url); |
508: | } else { |
509: | throw new \InvalidArgumentException("The variable `$name` in the host URL has invalid value ".$variables[$name].". Must be ".join(',', $variable["enum_values"])."."); |
510: | } |
511: | } else { |
512: | |
513: | $url = str_replace("{".$name."}", $variable["default_value"], $url); |
514: | } |
515: | } |
516: | |
517: | return $url; |
518: | } |
519: | |
520: | |
521: | |
522: | |
523: | |
524: | |
525: | |
526: | |
527: | public function getHostFromSettings($index, $variables = null) |
528: | { |
529: | return self::getHostString($this->getHostSettings(), $index, $variables); |
530: | } |
531: | } |
532: | |