Passed
Push — master ( cb3898...d20083 )
by Fabio
05:58
created

TPageGlobalizationCharsetBehavior   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 58
rs 10
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getCheckMetaCharset() 0 3 1
B addCharsetMeta() 0 20 11
A events() 0 3 1
A setCheckMetaCharset() 0 3 1
1
<?php
2
3
/**
4
 * TPageGlobalizationCharsetBehavior class file.
5
 *
6
 * @author Brad Anderson <[email protected]>
7
 * @link https://github.com/pradosoft/prado
8
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
9
 */
10
11
namespace Prado\Util\Behaviors;
12
13
use Prado\Prado;
14
use Prado\TPropertyValue;
15
use Prado\Web\UI\WebControls\TMetaTag;
16
17
/**
18
 * TPageGlobalizationCharsetBehavior class.
19
 *
20
 * TPageGlobalizationCharsetBehavior attaches to pages and adds charset
21
 * meta to the head from globalization.  If there is no globalization,
22
 * the default charset is used, 'utf-8'.
23
 * @author Brad Anderson <[email protected]>
24
 * @package Prado\Util\Behaviors
25
 * @since 4.2.0
26
 */
27
class TPageGlobalizationCharsetBehavior extends \Prado\Util\TBehavior
28
{
29
	/** @var bool check the existing meta tags for the charset before adding it */
30
	private $_checkMetaCharset = false;
31
	
32
	/**
33
	 * This handles the TPage.OnInitComplete event to place no-cache
34
	 * meta in the head.
35
	 * @return array of events as keys and methods as values
36
	 */
37
	public function events()
38
	{
39
		return ['OnInitComplete' => 'addCharsetMeta'];
40
	}
41
	
42
	/**
43
	 * This method places no-cache meta in the head.
44
	 * @param $page object raising the event
45
	 * @param $param mixed the parameter of the raised event
46
	 */
47
	public function addCharsetMeta($page, $param)
0 ignored issues
show
Unused Code introduced by
The parameter $param is not used and could be removed. ( Ignorable by Annotation )

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

47
	public function addCharsetMeta($page, /** @scrutinizer ignore-unused */ $param)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
48
	{
49
		if ($head = $page->getHead()) {
50
			$hasCharset = false;
51
			$metatags = $head->getMetaTags();
52
			if ($this->_checkMetaCharset) {
53
				foreach ($metatags as $meta) {
54
					if (empty($meta->getHttpEquiv()) && empty($meta->getContent()) && empty($meta->getName()) && empty($meta->getScheme()) && !empty($meta->getCharset())) {
55
						$hasCharset = true;
56
					}
57
				}
58
			}
59
			if (!$hasCharset) {
60
				$charset = 'utf-8';
61
				if ($globalization = Prado::getApplication()->getGlobalization()) {
62
					$charset = $globalization->getCharset();
63
				}
64
				$meta = new TMetaTag();
65
				$meta->setCharset($charset);
66
				$metatags->add($meta);
67
			}
68
		}
69
	}
70
	
71
	/**
72
	 * @return bool checks existing meta tags for no cache
73
	 */
74
	public function getCheckMetaCharset()
75
	{
76
		return $this->_checkMetaCharset;
77
	}
78
	
79
	/**
80
	 * @param bool $value checks existing meta tags for no cache
81
	 */
82
	public function setCheckMetaCharset($value)
83
	{
84
		$this->_checkMetaCharset = TPropertyValue::ensureBoolean($value);
85
	}
86
}
87