Passed
Push — master ( d6b975...b2dbc7 )
by Fabio
04:24
created

TBehavior   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 89.47%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 13
c 2
b 0
f 0
dl 0
loc 81
ccs 17
cts 19
cp 0.8947
rs 10
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setEnabled() 0 3 1
A events() 0 3 1
A init() 0 2 1
A detach() 0 6 2
A getEnabled() 0 3 1
A getOwner() 0 3 1
A attach() 0 5 2
1
<?php
2
/**
3
 * TBehavior class file.
4
 *
5
 * @author Qiang Xue <[email protected]>
6
 * @link http://www.yiiframework.com/
7
 * @license http://www.yiiframework.com/license/
8
 */
9
10
namespace Prado\Util;
11
12
use Prado\TPropertyValue;
13
14
/**
15
 * TBehavior is a convenient base class for behavior classes.
16
 * @author Qiang Xue <[email protected]>
17
 * @since 3.2.3
18
 */
19
class TBehavior extends \Prado\TComponent implements IBehavior
20
{
21
	private $_enabled = true;
22
	private $_owner;
23
24
	/**
25
	 * This processes configuration elements from TBehaviorsModule.  This is usually
26
	 * called after attach but cannot be guaranteed to be called outside the {@link
27
	 * TBehaviorsModule} environment. This is only needed for complex behavior
28
	 * configurations.
29
	 * @param array|\Prado\Xml\TXmlElement $config any innards to the behavior configuration.
30
	 */
31 26
	public function init($config)
32
	{
33 26
	}
34
35
	/**
36
	 * Declares events and the corresponding event handler methods.
37
	 * The events are defined by the {@link owner} component, while the handler
38
	 * methods by the behavior class. The handlers will be attached to the corresponding
39
	 * events when the behavior is attached to the {@link owner} component; and they
40
	 * will be detached from the events when the behavior is detached from the component.
41
	 * @return array events (array keys) and the corresponding event handler methods (array values).
42
	 */
43 26
	public function events()
44
	{
45 26
		return [];
46 26
	}
47
48
	/**
49 26
	 * Attaches the behavior object to the component.
50
	 * The default implementation will set the {@link owner} property
51
	 * and attach event handlers as declared in {@link events}.
52
	 * Make sure you call the parent implementation if you override this method.
53
	 * @param \Prado\TComponent $owner the component that this behavior is to be attached to.
54
	 */
55
	public function attach($owner)
56
	{
57
		$this->_owner = $owner;
58 16
		foreach ($this->events() as $event => $handler) {
59
			$owner->attachEventHandler($event, [$this, $handler]);
60 16
		}
61
	}
62
63 16
	/**
64 16
	 * Detaches the behavior object from the component.
65
	 * The default implementation will unset the {@link owner} property
66
	 * and detach event handlers declared in {@link events}.
67
	 * Make sure you call the parent implementation if you override this method.
68
	 * @param \Prado\TComponent $owner the component that this behavior is to be detached from.
69 2
	 */
70
	public function detach($owner)
71 2
	{
72
		foreach ($this->events() as $event => $handler) {
73
			$owner->detachEventHandler($event, [$this, $handler]);
74
		}
75
		$this->_owner = null;
76
	}
77 24
78
	/**
79 24
	 * @return \Prado\TComponent the owner component that this behavior is attached to.
80
	 */
81
	public function getOwner()
82
	{
83
		return $this->_owner;
84
	}
85 26
86
	/**
87 26
	 * @return bool whether this behavior is enabled
88 26
	 */
89
	public function getEnabled()
90
	{
91
		return $this->_enabled;
92
	}
93
94
	/**
95
	 * @param bool $value whether this behavior is enabled
96
	 */
97
	public function setEnabled($value)
98
	{
99
		$this->_enabled = TPropertyValue::ensureBoolean($value);
100
	}
101
}
102