Completed
Pull Request — master (#33)
by Martijn
02:27
created

TypeRegistry   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 55
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 8 3
A remove() 0 4 1
A has() 0 4 1
A get() 0 4 2
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