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

TTimeZoneParameterBehavior::getTimeZoneParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * TTimeZoneParameterBehavior 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\Util\TBehavior;
15
16
/**
17
 * TTimeZoneParameterBehavior class.
18
 *
19
 * TTimeZoneParameterBehavior sets the date_default_timezone_set.
20
 * This parameterizes the TimeZone.   {@link TimeZoneParameter} is
21
 * the key to the Application Parameter for setting the TimeZone.
22
 *
23
 * This Behavior is designed to attach to TApplication, but can be
24
 * attached to any TComponent.
25
 *
26
 * <code>
27
 *		<behavior name="TimeZoneParameter" Class="Prado\Util\Behaviors\TTimeZoneParameterBehavior" AttachTo="Application" TimeZoneParameter="TimeZone" TimeZone="America/New_York"/>
28
 * </code>
29
 * This code will set the default timeZone to "America/New_York", and then
30
 * if there is any Application Parameter in "TimeZone", then that takes
31
 * precedence.  Setting the TimeZoneParameter to "" will disable the
32
 * parameter functionality and set the TimeZone from the attribute TimeZone.
33
 *
34
 * This routes changes in the Application Parameter {@link TimeZoneParameter}
35
 * to {@link setTimeZone}. The default TimeZoneParameter is 'prop:TimeZone'.
36
 *
37
 * @author Brad Anderson <[email protected]>
38
 * @package Prado\Util\Behaviors
39
 * @since 4.2.0
40
 */
41
class TTimeZoneParameterBehavior extends TBehavior
42
{
43
	/**
44
	 * Name of the Application Parameter Routing Behavior
45
	 */
46
	public const APP_PARAM_ROUTE_BEHAVIOR_NAME = 'TimeZoneParameter';
47
	
48
	/**
49
	 * Default TimeZoneParameter
50
	 */
51
	public const TIMEZONE_PARAMETER_NAME = 'prop:TimeZone';
52
	
53
	/**
54
	 * @var string the page theme is set to this parameter key
55
	 */
56
	private $_timeZoneParameter = self::TIMEZONE_PARAMETER_NAME;
57
	
58
	/**
59
	 * @var object {@link TMapRouteBehavior} that routes changes to the parameter
60
	 * is handled by setTimeZone.
61
	 */
62
	private $_paramBehavior;
63
	
64
	/**
65
	 * This sets the date_default_timezone_set with the value of the TimeZoneParameter
66
	 * in the application parameters.  It attaches the Application Parameter handler behavior.
67
	 * @param $owner object the object that this behavior is attached to.
68
	 */
69
	public function attach($owner)
70
	{
71
		parent::attach($owner);
72
		if (!$this->_timeZoneParameter) {
73
			return;
74
		}
75
		$appParams = Prado::getApplication()->getParameters();
76
		if ($default_timezone = $appParams->itemAt($this->_timeZoneParameter)) {
77
			$this->setTimeZone($default_timezone);
78
		}
79
		$this->_paramBehavior = new TMapRouteBehavior($this->_timeZoneParameter, [$this, 'setTimeZone']);
80
		$appParams->attachBehavior(self::APP_PARAM_ROUTE_BEHAVIOR_NAME, $this->_paramBehavior);
81
	}
82
	
83
	/**
84
	 * This removes the Application Parameter handler behavior
85
	 * @param $owner object the object that this behavior is attached to.
86
	 */
87
	public function detach($owner)
88
	{
89
		if ($this->_paramBehavior) {
90
			Prado::getApplication()->getParameters()->detachBehavior(self::APP_PARAM_ROUTE_BEHAVIOR_NAME);
91
		}
92
		parent::detach($owner);
93
	}
94
	
95
	/**
96
	 * @return string Application parameter key to set the php TimeZone.
97
	 */
98
	public function getTimeZoneParameter()
99
	{
100
		return $this->_timeZoneParameter;
101
	}
102
	
103
	/**
104
	 * @param $value string Application parameter key to set the php TimeZone.
105
	 */
106
	public function setTimeZoneParameter($value)
107
	{
108
		if ($this->_paramBehavior) {
109
			$this->_paramBehavior->setParameter($value);
110
		}
111
		$this->_timeZoneParameter = $value;
112
	}
113
	
114
	/**
115
	 * @return string the timeZone from date_default_timezone_get.
116
	 */
117
	public function getTimeZone()
118
	{
119
		return date_default_timezone_get();
120
	}
121
	
122
	/**
123
	 * @param $value string passthrough to date_default_timezone_set
124
	 */
125
	public function setTimeZone($value)
126
	{
127
		$set = true;
128
		try {
129
			date_default_timezone_set($value);
130
		} catch (\Exception $e) {
131
			$set = false;
132
		}
133
		return $set;
134
	}
135
}
136