Merge pull request #4299 from dokuwiki-translate/lang_update_906_1721252389
[dokuwiki.git] / inc / Remote / OpenApiDoc / Type.php
blob209fba4706db8e4a460628747829cf3eb64b1ecd
1 <?php
3 namespace dokuwiki\Remote\OpenApiDoc;
5 class Type
7 protected $typehint;
8 protected $context;
10 /**
11 * @param string $typehint The typehint as read from the docblock
12 * @param string $context A fully qualified class name in which context the typehint is used
14 public function __construct($typehint, $context = '')
16 $this->typehint = $typehint;
17 $this->context = $context;
20 /**
21 * Return the typehint as read from the docblock
23 * @return string
25 public function __toString()
27 return $this->typehint;
30 /**
31 * Return the base type
33 * This is the type this variable is. Eg. a string[] is an array.
35 * @return string
37 public function getBaseType()
39 $typehint = $this->typehint;
41 if (str_ends_with($typehint, '[]')) {
42 return 'array';
45 if (in_array($typehint, ['boolean', 'false', 'true'])) {
46 return 'bool';
49 if (in_array($typehint, ['integer', 'date'])) {
50 return 'int';
53 if ($typehint === 'file') {
54 return 'string';
57 // fully qualified class name
58 if ($typehint[0] === '\\') {
59 return ltrim($typehint, '\\');
62 // relative class name, try to resolve
63 if ($this->context && ctype_upper($typehint[0])) {
64 return ClassResolver::getInstance()->resolve($typehint, $this->context);
67 return $typehint;
70 /**
71 * Return a primitive type understood by the XMLRPC server
73 * @param string $typehint
74 * @return string
76 public function getJSONRPCType()
78 return $this->getBaseType();
81 /**
82 * Get the base type as one of the supported OpenAPI types
84 * Formats (eg. int32 or double) are not supported
86 * @link https://swagger.io/docs/specification/data-models/data-types/
87 * @return string
89 public function getOpenApiType()
91 switch ($this->getBaseType()) {
92 case 'int':
93 return 'integer';
94 case 'bool':
95 return 'boolean';
96 case 'array':
97 return 'array';
98 case 'string':
99 case 'mixed':
100 return 'string';
101 case 'double':
102 case 'float':
103 return 'number';
104 default:
105 return 'object';
111 * If this is an array, return the type of the array elements
113 * @return Type|null null if this is not a typed array
115 public function getSubType()
117 $type = $this->typehint;
118 if (!str_ends_with($type, '[]')) {
119 return null;
121 $type = substr($type, 0, -2);
122 return new Type($type, $this->context);
126 * Return a type understood by the XMLRPC server
128 * @return string
130 public function getXMLRPCType()
132 $type = $this->typehint;
134 // keep custom types
135 if (in_array($type, ['date', 'file', 'struct'])) {
136 return $type;
139 $type = $this->getBaseType($this->typehint);
141 // primitive types
142 if (in_array($type, ['int', 'string', 'double', 'bool', 'array'])) {
143 return $type;
146 // everything else is an object
147 return 'object'; //should this return 'struct'?