TActiveLabel::getActiveControl()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * TActiveLabel class file.
5
 *
6
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
7
 * @link https://github.com/pradosoft/prado
8
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
9
 */
10
11
namespace Prado\Web\UI\ActiveControls;
12
13
/**
14
 * Load active control adapter.
15
 */
16
use Prado\Prado;
17
use Prado\Web\UI\WebControls\TLabel;
18
19
/**
20
 * TActiveLabel class
21
 *
22
 * The active control counterpart of TLabel component. When
23
 * {@see \Prado\Web\UI\ActiveControls\TBaseActiveControl::setEnableUpdate ActiveControl.EnableUpdate}
24
 * property is true the during a callback request, setting {@see setText Text}
25
 * property will also set the text of the label on the client upon callback
26
 * completion. Similarly, setting {@see setForControl ForControl} will also set
27
 * the client-side "for" attribute on the label.
28
 *
29
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
30
 * @since 3.1
31
 * @method TActiveControlAdapter getAdapter()
32
 */
33
class TActiveLabel extends TLabel implements IActiveControl
34
{
35
	/**
36
	 * Creates a new callback control, sets the adapter to
37
	 * TActiveControlAdapter. If you override this class, be sure to set the
38
	 * adapter appropriately by, for example, by calling this constructor.
39
	 */
40
	public function __construct()
41
	{
42
		parent::__construct();
43
		$this->setAdapter(new TActiveControlAdapter($this));
44
	}
45
46
	/**
47
	 * @return TBaseActiveControl basic active control options.
48
	 */
49
	public function getActiveControl()
50
	{
51
		return $this->getAdapter()->getBaseActiveControl();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getAdapter...>getBaseActiveControl() also could return the type Prado\Web\UI\ActiveControls\TBaseActiveControl which includes types incompatible with the return type mandated by Prado\Web\UI\ActiveContr...rol::getActiveControl() of Prado\Web\UI\ActiveContr...seActiveCallbackControl. Consider adding a type-check to rule them out.
Loading history...
52
	}
53
54
	/**
55
	 * On callback response, the inner HTML of the label is updated.
56
	 * @param string $value the text value of the label
57
	 */
58
	public function setText($value)
59
	{
60
		if (parent::getText() === $value) {
61
			return;
62
		}
63
64
		parent::setText($value);
65
		if ($this->getActiveControl()->canUpdateClientSide()) {
66
			$this->getPage()->getCallbackClient()->update($this, $value);
0 ignored issues
show
Bug introduced by
$value of type string is incompatible with the type Prado\Web\UI\TControl expected by parameter $content of Prado\Web\UI\ActiveContr...kClientScript::update(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
			$this->getPage()->getCallbackClient()->update($this, /** @scrutinizer ignore-type */ $value);
Loading history...
67
		}
68
	}
69
70
	/**
71
	 * Sets the ID of the control that the label is associated with.
72
	 * The control must be locatable via {@see \Prado\Web\UI\TControl::findControl} using the ID.
73
	 * On callback response, the For attribute of the label is updated.
74
	 * @param string $value the associated control ID
75
	 */
76
	public function setForControl($value)
77
	{
78
		if (parent::getForControl() === $value) {
79
			return;
80
		}
81
82
		parent::setForControl($value);
83
		if ($this->getActiveControl()->canUpdateClientSide()) {
84
			$id = $this->findControl($value)->getClientID();
85
			$this->getPage()->getCallbackClient()->setAttribute($this, 'for', $id);
86
		}
87
	}
88
89
	/**
90
	 * Adds attribute id to the renderer.
91
	 * @param \Prado\Web\UI\THtmlWriter $writer the writer used for the rendering purpose
92
	 */
93
	protected function addAttributesToRender($writer)
94
	{
95
		$writer->addAttribute('id', $this->getClientID());
96
		parent::addAttributesToRender($writer);
97
	}
98
}
99