Completed
Push — master ( 0636af...e088fc )
by Adam
07:34
created

DecoratorsManager::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 1
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * DecoratorsManager.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        http://www.ipublikuj.eu
7
 * @author         Adam Kadlec http://www.ipublikuj.eu
8
 * @package        iPublikuj:Widgets!
9
 * @subpackage     Managers
10
 * @since          1.0.0
11
 *
12
 * @date           16.06.15
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\Widgets\Managers;
18
19
use Nette;
20
21
use IPub;
22
use IPub\Widgets\Decorators;
23
24
/**
25
 * Registered widgets decorators manager
26
 *
27
 * @package        iPublikuj:Widgets!
28
 * @subpackage     Managers
29
 *
30
 * @author         Adam Kadlec <[email protected]>
31
 */
32 1
final class DecoratorsManager extends Nette\Object implements \ArrayAccess, \IteratorAggregate
33
{
34
	/**
35
	 * @var Decorators\IFactory[]
36
	 */
37
	protected $decorators = [];
38
39
	/**
40
	 * Check if a decorator is registered
41
	 *
42
	 * @param string $name
43
	 *
44
	 * @return boolean
45
	 */
46
	public function has(string $name) : bool
47
	{
48
		return isset($this->decorators[$name]);
49
	}
50
51
	/**
52
	 * Returns a registered decorator class
53
	 *
54
	 * @param string $name
55
	 *
56
	 * @return Decorators\IFactory|NULL
57
	 */
58
	public function get(string $name)
59
	{
60
		return $this->has($name) ? $this->decorators[$name] : NULL;
61
	}
62
63
	/**
64
	 * Register a decorator component factory
65
	 *
66
	 * @param Decorators\IFactory $decorator
67
	 * @param string $name
68
	 */
69
	public function register(Decorators\IFactory $decorator, string $name)
70
	{
71 1
		$this->unregister($name);
72
73 1
		$this->decorators[$name] = $decorator;
74
75 1
		krsort($this->decorators);
76 1
	}
77
78
	/**
79
	 * Un-registers a decorator
80
	 *
81
	 * @param string $name
82
	 */
83
	public function unregister(string $name)
84
	{
85 1
		if (array_key_exists($name, $this->decorators)) {
86
			unset($this->decorators[$name]);
87
		}
88 1
	}
89
90
	/**
91
	 * {@inheritdoc}
92
	 */
93
	public function getIterator()
94
	{
95
		return new \ArrayIterator($this->decorators);
96
	}
97
98
	/**
99
	 * Implements the \ArrayAccess
100
	 *
101
	 * @param  string $offset
102
	 *
103
	 * @return bool
104
	 */
105
	public function offsetExists($offset)
106
	{
107
		return isset($this->decorators[$offset]);
108
	}
109
110
	/**
111
	 * Implements the \ArrayAccess
112
	 *
113
	 * @param string $offset
114
	 *
115
	 * @return Decorators\IFactory
116
	 */
117
	public function offsetGet($offset)
118
	{
119
		return $this->decorators[$offset];
120
	}
121
122
	/**
123
	 * Implements the \ArrayAccess
124
	 *
125
	 * @param string $offset
126
	 * @param Decorators\IFactory|NULL $value
127
	 */
128
	public function offsetSet($offset, $value)
129
	{
130
		$this->decorators[$offset] = $value;
131
	}
132
133
	/**
134
	 * Implements the \ArrayAccess
135
	 *
136
	 * @param string $offset
137
	 */
138
	public function offsetUnset($offset)
139
	{
140
		unset($this->decorators[$offset]);
141
	}
142
}
143