1: <?php
2: /*
3: You may not change or alter any portion of this comment or credits
4: of supporting developers from this source code or any supporting source code
5: which is considered copyrighted (c) material of the original comment or credit authors.
6:
7: This program is distributed in the hope that it will be useful,
8: but WITHOUT ANY WARRANTY; without even the implied warranty of
9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: */
11:
12: namespace Xoops\Core\Kernel\Dtype;
13:
14: use \DateTime;
15: use \DateTimeZone;
16: use Xoops\Core\Kernel\Dtype;
17: use Xoops\Core\Kernel\XoopsObject;
18:
19: /**
20: * DtypeTimeZone
21: *
22: * Data is stored as varchar(32) string, returned as \DateTimeZone object
23: *
24: * @category Xoops\Core\Kernel\Dtype\DtypeOldTime
25: * @package Xoops\Core\Kernel
26: * @author trabis <lusopoemas@gmail.com>
27: * @copyright 2011-2015 XOOPS Project (http://xoops.org)
28: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
29: * @link http://xoops.org
30: */
31: class DtypeTimeZone extends DtypeAbstract
32: {
33: /**
34: * getVar get variable prepared according to format
35: *
36: * @param XoopsObject $obj object containing variable
37: * @param string $key name of variable
38: * @param string $format Dtype::FORMAT_* constant indicating desired formatting
39: *
40: * @return int|DateTime
41: */
42: public function getVar(XoopsObject $obj, $key, $format)
43: {
44: $storedValue = $obj->vars[$key]['value'];
45: switch (strtolower($format)) {
46: case Dtype::FORMAT_NONE:
47: case 'n':
48: $value = $storedValue;
49: break;
50: default:
51: try {
52: $value = new DateTimeZone($storedValue);
53: } catch (\Exception $e) {
54: $value = new DateTimeZone('UTC');
55: }
56: break;
57: }
58: return $value;
59: }
60:
61: /**
62: * cleanVar prepare variable for persistence
63: *
64: * @param XoopsObject $obj object containing variable
65: * @param string $key name of variable
66: *
67: * @return int
68: */
69: public function cleanVar(XoopsObject $obj, $key)
70: {
71: $value = $obj->vars[$key]['value'];
72: if ($value instanceof DateTimeZone) {
73: $cleanValue = $value->getName();
74: } elseif ($value instanceof DateTime) {
75: $cleanValue = $value->getTimezone()->getName();
76: } else {
77: try {
78: $temp = new DateTimeZone($value);
79: $cleanValue = $temp->getName();
80: } catch (\Exception $e) {
81: $cleanValue = 'UTC';
82: }
83: }
84:
85: return $cleanValue;
86: }
87: }
88: