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 Xoops\Core\Kernel\Dtype;
16: use Xoops\Core\Kernel\XoopsObject;
17: use Xoops\Core\Locale\Time;
18:
19: /**
20: * DtypeDateTime
21: *
22: * Data is stored as integer unix timestamp, returned as \DateTime 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 DtypeDateTime 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: $value = Time::cleanTime($storedValue);
52: break;
53: }
54: return $value;
55: }
56:
57: /**
58: * cleanVar prepare variable for persistence
59: *
60: * @param XoopsObject $obj object containing variable
61: * @param string $key name of variable
62: *
63: * @return int
64: */
65: public function cleanVar(XoopsObject $obj, $key)
66: {
67: $value = $obj->vars[$key]['value'];
68: if ($value instanceof DateTime) {
69: $cleanValue = $value->getTimestamp();
70: } elseif (is_string($value)) {
71: $cleanValue = strtotime($value);
72: } else {
73: $cleanValue = (int) $value;
74: }
75: return $cleanValue;
76: }
77: }
78: