1<?php 2 3namespace dokuwiki\Remote\OpenApiDoc; 4 5class Type 6{ 7 protected $typehint; 8 protected $context; 9 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 13 */ 14 public function __construct($typehint, $context = '') 15 { 16 $this->typehint = $typehint; 17 $this->context = $context; 18 } 19 20 /** 21 * Return a primitive PHP type 22 * 23 * @param string $typehint 24 * @return string 25 */ 26 protected function toPrimitiveType($typehint) 27 { 28 if (str_ends_with($typehint, '[]')) { 29 return 'array'; 30 } 31 32 if (in_array($typehint, ['boolean', 'false', 'true'])) { 33 return 'bool'; 34 } 35 36 if (in_array($typehint, ['integer', 'date'])) { 37 return 'int'; 38 } 39 40 if ($typehint === 'file') { 41 return 'string'; 42 } 43 44 // fully qualified class name 45 if ($typehint[0] === '\\') { 46 return ltrim($typehint, '\\'); 47 } 48 49 // relative class name, try to resolve 50 if ($this->context && ctype_upper($typehint[0])) { 51 return ClassResolver::getInstance()->resolve($typehint, $this->context); 52 } 53 54 return $typehint; 55 } 56 57 /** 58 * Return a primitive type understood by the XMLRPC server 59 * 60 * @param string $typehint 61 * @return string 62 */ 63 public function getJSONRPCType() 64 { 65 return $this->toPrimitiveType($this->typehint); 66 } 67 68 /** 69 * If this is an array, return the type of the array elements 70 * 71 * @return Type|null null if this is not a typed array 72 */ 73 public function getSubType() 74 { 75 $type = $this->typehint; 76 if (!str_ends_with($type, '[]')) { 77 return null; 78 } 79 $type = substr($type, 0, -2); 80 return new Type($type, $this->context); 81 } 82 83 /** 84 * Return a type understood by the XMLRPC server 85 * 86 * @return string 87 */ 88 public function getXMLRPCType() 89 { 90 $type = $this->typehint; 91 92 // keep custom types 93 if (in_array($type, ['date', 'file', 'struct'])) { 94 return $type; 95 } 96 97 $type = $this->toPrimitiveType($this->typehint); 98 99 // primitive types 100 if (in_array($type, ['int', 'string', 'double', 'bool', 'array'])) { 101 return $type; 102 } 103 104 // everything else is an object 105 return 'object'; //should this return 'struct'? 106 } 107} 108