1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace SwaggerGen;
|
4
|
|
|
|
5
|
|
|
/*
|
6
|
|
|
* @todo add class instead of classname
|
7
|
|
|
* @todo support non-default format names?
|
8
|
|
|
* @todo get rid of static `getFormats()`.
|
9
|
|
|
* @todo PSR-11 compatibility (ready, except for exceptions)
|
10
|
|
|
* @todo Overwrite standard types (should work; needs testing)
|
11
|
|
|
* @todo Ability to "disable" standard types
|
12
|
|
|
* - Should `remove()` remove only an overwrite or disable the format name completely?
|
13
|
|
|
* - Alternative methods?
|
14
|
|
|
* @todo Support for composite (fallback) formats (ip for ipv4/ipv6)?
|
15
|
|
|
*/
|
16
|
|
|
|
17
|
|
|
/**
|
18
|
|
|
* Registry of custom types.
|
19
|
|
|
*
|
20
|
|
|
* @package SwaggerGen
|
21
|
|
|
* @author Martijn van der Lee <[email protected]>
|
22
|
|
|
* @copyright 2014-2017 Martijn van der Lee
|
23
|
|
|
* @license https://opensource.org/licenses/MIT MIT
|
24
|
|
|
*/
|
25
|
|
|
class TypeRegistry
|
26
|
|
|
{
|
27
|
|
|
|
28
|
|
|
/**
|
29
|
|
|
* Map of format-name => class-name
|
30
|
|
|
*
|
31
|
|
|
* @var array
|
32
|
|
|
*/
|
33
|
|
|
private $formats = array();
|
34
|
|
|
|
35
|
|
|
/**
|
36
|
|
|
* Add a type name from classname
|
37
|
|
|
*
|
38
|
|
|
* @param type $classname
|
39
|
|
|
*/
|
40
|
|
|
public function add($classname)
|
41
|
|
|
{
|
42
|
|
|
if (is_subclass_of($classname, '\\SwaggerGen\\Swagger\\Type\\AbstractType', true)) {
|
43
|
|
|
foreach ($classname::getFormats() as $format) {
|
44
|
|
|
$this->formats[$format] = $classname;
|
45
|
|
|
}
|
46
|
|
|
}
|
47
|
|
|
}
|
48
|
|
|
|
49
|
|
|
/**
|
50
|
|
|
* Remove type format by explicitely nulling it (disables it)
|
51
|
|
|
*
|
52
|
|
|
* @param string $name
|
53
|
|
|
*/
|
54
|
|
|
public function remove($name)
|
55
|
|
|
{
|
56
|
|
|
$this->formats[$name] = null;
|
57
|
|
|
}
|
58
|
|
|
|
59
|
|
|
/**
|
60
|
|
|
* Is a type format known?
|
61
|
|
|
*
|
62
|
|
|
* @return bool
|
63
|
|
|
*/
|
64
|
|
|
public function has($name)
|
65
|
|
|
{
|
66
|
|
|
return !empty($this->formats[$name]);
|
67
|
|
|
}
|
68
|
|
|
|
69
|
|
|
/**
|
70
|
|
|
* Get the format class name
|
71
|
|
|
*
|
72
|
|
|
* @return null|string
|
73
|
|
|
*/
|
74
|
|
|
public function get($name)
|
75
|
|
|
{
|
76
|
|
|
return !empty($this->formats[$name]) ? $this->formats[$name] : null;
|
77
|
|
|
}
|
78
|
|
|
|
79
|
|
|
}
|
80
|
|
|
|