xref: /dokuwiki/inc/Remote/OpenApiDoc/Type.php (revision 093fe67e98c0cdb4b73fd46938e49b64971483c2)
18ddd9b69SAndreas Gohr<?php
28ddd9b69SAndreas Gohr
38ddd9b69SAndreas Gohrnamespace dokuwiki\Remote\OpenApiDoc;
48ddd9b69SAndreas Gohr
5*093fe67eSAndreas Gohrclass Type implements \Stringable
68ddd9b69SAndreas Gohr{
78ddd9b69SAndreas Gohr    protected $typehint;
88ddd9b69SAndreas Gohr    protected $context;
98ddd9b69SAndreas Gohr
108ddd9b69SAndreas Gohr    /**
118ddd9b69SAndreas Gohr     * @param string $typehint The typehint as read from the docblock
128ddd9b69SAndreas Gohr     * @param string $context A fully qualified class name in which context the typehint is used
138ddd9b69SAndreas Gohr     */
148ddd9b69SAndreas Gohr    public function __construct($typehint, $context = '')
158ddd9b69SAndreas Gohr    {
168ddd9b69SAndreas Gohr        $this->typehint = $typehint;
178ddd9b69SAndreas Gohr        $this->context = $context;
188ddd9b69SAndreas Gohr    }
198ddd9b69SAndreas Gohr
208ddd9b69SAndreas Gohr    /**
2153c2a557SAndreas Gohr     * Return the typehint as read from the docblock
2253c2a557SAndreas Gohr     *
2353c2a557SAndreas Gohr     * @return string
2453c2a557SAndreas Gohr     */
25*093fe67eSAndreas Gohr    public function __toString(): string
2653c2a557SAndreas Gohr    {
27*093fe67eSAndreas Gohr        return (string) $this->typehint;
2853c2a557SAndreas Gohr    }
2953c2a557SAndreas Gohr
3053c2a557SAndreas Gohr    /**
31dd7472d3SAndreas Gohr     * Return the base type
328ddd9b69SAndreas Gohr     *
33dd7472d3SAndreas Gohr     * This is the type this variable is. Eg. a string[] is an array.
34dd7472d3SAndreas Gohr     *
358ddd9b69SAndreas Gohr     * @return string
368ddd9b69SAndreas Gohr     */
37dd7472d3SAndreas Gohr    public function getBaseType()
388ddd9b69SAndreas Gohr    {
39dd7472d3SAndreas Gohr        $typehint = $this->typehint;
40dd7472d3SAndreas Gohr
418ddd9b69SAndreas Gohr        if (str_ends_with($typehint, '[]')) {
428ddd9b69SAndreas Gohr            return 'array';
438ddd9b69SAndreas Gohr        }
448ddd9b69SAndreas Gohr
458ddd9b69SAndreas Gohr        if (in_array($typehint, ['boolean', 'false', 'true'])) {
468ddd9b69SAndreas Gohr            return 'bool';
478ddd9b69SAndreas Gohr        }
488ddd9b69SAndreas Gohr
498ddd9b69SAndreas Gohr        if (in_array($typehint, ['integer', 'date'])) {
508ddd9b69SAndreas Gohr            return 'int';
518ddd9b69SAndreas Gohr        }
528ddd9b69SAndreas Gohr
538ddd9b69SAndreas Gohr        if ($typehint === 'file') {
548ddd9b69SAndreas Gohr            return 'string';
558ddd9b69SAndreas Gohr        }
568ddd9b69SAndreas Gohr
578ddd9b69SAndreas Gohr        // fully qualified class name
588ddd9b69SAndreas Gohr        if ($typehint[0] === '\\') {
598ddd9b69SAndreas Gohr            return ltrim($typehint, '\\');
608ddd9b69SAndreas Gohr        }
618ddd9b69SAndreas Gohr
628ddd9b69SAndreas Gohr        // relative class name, try to resolve
638ddd9b69SAndreas Gohr        if ($this->context && ctype_upper($typehint[0])) {
648ddd9b69SAndreas Gohr            return ClassResolver::getInstance()->resolve($typehint, $this->context);
658ddd9b69SAndreas Gohr        }
668ddd9b69SAndreas Gohr
678ddd9b69SAndreas Gohr        return $typehint;
688ddd9b69SAndreas Gohr    }
698ddd9b69SAndreas Gohr
708ddd9b69SAndreas Gohr    /**
718ddd9b69SAndreas Gohr     * Return a primitive type understood by the XMLRPC server
728ddd9b69SAndreas Gohr     *
738ddd9b69SAndreas Gohr     * @param string $typehint
748ddd9b69SAndreas Gohr     * @return string
758ddd9b69SAndreas Gohr     */
768ddd9b69SAndreas Gohr    public function getJSONRPCType()
778ddd9b69SAndreas Gohr    {
78dd7472d3SAndreas Gohr        return $this->getBaseType();
798ddd9b69SAndreas Gohr    }
808ddd9b69SAndreas Gohr
818ddd9b69SAndreas Gohr    /**
82dd7472d3SAndreas Gohr     * Get the base type as one of the supported OpenAPI types
83dd7472d3SAndreas Gohr     *
84dd7472d3SAndreas Gohr     * Formats (eg. int32 or double) are not supported
85dd7472d3SAndreas Gohr     *
86dd7472d3SAndreas Gohr     * @link https://swagger.io/docs/specification/data-models/data-types/
87dd7472d3SAndreas Gohr     * @return string
88dd7472d3SAndreas Gohr     */
89dd7472d3SAndreas Gohr    public function getOpenApiType()
90dd7472d3SAndreas Gohr    {
91*093fe67eSAndreas Gohr        return match ($this->getBaseType()) {
92*093fe67eSAndreas Gohr            'int' => 'integer',
93*093fe67eSAndreas Gohr            'bool' => 'boolean',
94*093fe67eSAndreas Gohr            'array' => 'array',
95*093fe67eSAndreas Gohr            'string', 'mixed' => 'string',
96*093fe67eSAndreas Gohr            'double', 'float' => 'number',
97*093fe67eSAndreas Gohr            default => 'object',
98*093fe67eSAndreas Gohr        };
99dd7472d3SAndreas Gohr    }
100dd7472d3SAndreas Gohr
101dd7472d3SAndreas Gohr
102dd7472d3SAndreas Gohr    /**
1038ddd9b69SAndreas Gohr     * If this is an array, return the type of the array elements
1048ddd9b69SAndreas Gohr     *
1058ddd9b69SAndreas Gohr     * @return Type|null null if this is not a typed array
1068ddd9b69SAndreas Gohr     */
1078ddd9b69SAndreas Gohr    public function getSubType()
1088ddd9b69SAndreas Gohr    {
1098ddd9b69SAndreas Gohr        $type = $this->typehint;
1108ddd9b69SAndreas Gohr        if (!str_ends_with($type, '[]')) {
1118ddd9b69SAndreas Gohr            return null;
1128ddd9b69SAndreas Gohr        }
1138ddd9b69SAndreas Gohr        $type = substr($type, 0, -2);
1148ddd9b69SAndreas Gohr        return new Type($type, $this->context);
1158ddd9b69SAndreas Gohr    }
1168ddd9b69SAndreas Gohr
1178ddd9b69SAndreas Gohr    /**
1188ddd9b69SAndreas Gohr     * Return a type understood by the XMLRPC server
1198ddd9b69SAndreas Gohr     *
1208ddd9b69SAndreas Gohr     * @return string
1218ddd9b69SAndreas Gohr     */
1228ddd9b69SAndreas Gohr    public function getXMLRPCType()
1238ddd9b69SAndreas Gohr    {
1248ddd9b69SAndreas Gohr        $type = $this->typehint;
1258ddd9b69SAndreas Gohr
1268ddd9b69SAndreas Gohr        // keep custom types
1278ddd9b69SAndreas Gohr        if (in_array($type, ['date', 'file', 'struct'])) {
1288ddd9b69SAndreas Gohr            return $type;
1298ddd9b69SAndreas Gohr        }
1308ddd9b69SAndreas Gohr
131dd7472d3SAndreas Gohr        $type = $this->getBaseType($this->typehint);
1328ddd9b69SAndreas Gohr
1338ddd9b69SAndreas Gohr        // primitive types
1348ddd9b69SAndreas Gohr        if (in_array($type, ['int', 'string', 'double', 'bool', 'array'])) {
1358ddd9b69SAndreas Gohr            return $type;
1368ddd9b69SAndreas Gohr        }
1378ddd9b69SAndreas Gohr
1388ddd9b69SAndreas Gohr        // everything else is an object
1398ddd9b69SAndreas Gohr        return 'object'; //should this return 'struct'?
1408ddd9b69SAndreas Gohr    }
1418ddd9b69SAndreas Gohr}
142