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