OpenApi Gen: add toString method for easier testing
[dokuwiki.git] / inc / Remote / OpenApiDoc / Type.php
blob9e424c292515c4009adb5da54da1411898fa3482
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 a primitive PHP type
33 * @param string $typehint
34 * @return string
36 protected function toPrimitiveType($typehint)
38 if (str_ends_with($typehint, '[]')) {
39 return 'array';
42 if (in_array($typehint, ['boolean', 'false', 'true'])) {
43 return 'bool';
46 if (in_array($typehint, ['integer', 'date'])) {
47 return 'int';
50 if ($typehint === 'file') {
51 return 'string';
54 // fully qualified class name
55 if ($typehint[0] === '\\') {
56 return ltrim($typehint, '\\');
59 // relative class name, try to resolve
60 if ($this->context && ctype_upper($typehint[0])) {
61 return ClassResolver::getInstance()->resolve($typehint, $this->context);
64 return $typehint;
67 /**
68 * Return a primitive type understood by the XMLRPC server
70 * @param string $typehint
71 * @return string
73 public function getJSONRPCType()
75 return $this->toPrimitiveType($this->typehint);
78 /**
79 * If this is an array, return the type of the array elements
81 * @return Type|null null if this is not a typed array
83 public function getSubType()
85 $type = $this->typehint;
86 if (!str_ends_with($type, '[]')) {
87 return null;
89 $type = substr($type, 0, -2);
90 return new Type($type, $this->context);
93 /**
94 * Return a type understood by the XMLRPC server
96 * @return string
98 public function getXMLRPCType()
100 $type = $this->typehint;
102 // keep custom types
103 if (in_array($type, ['date', 'file', 'struct'])) {
104 return $type;
107 $type = $this->toPrimitiveType($this->typehint);
109 // primitive types
110 if (in_array($type, ['int', 'string', 'double', 'bool', 'array'])) {
111 return $type;
114 // everything else is an object
115 return 'object'; //should this return 'struct'?