Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
datetimefield.php
1<?php
10
15
22{
24 protected $useTimezone = true;
25
34 public function __construct($name, $parameters = array())
35 {
36 ScalarField::__construct($name, $parameters);
37 }
38
43 public function configureUseTimezone($use = true)
44 {
45 $this->useTimezone = (bool) $use;
46
47 return $this;
48 }
49
53 public function getFetchDataModifiers()
54 {
55 $modifiers = parent::getFetchDataModifiers();
56
57 if (!$this->useTimezone)
58 {
59 $modifiers[] = [__CLASS__, 'disableTimezoneFetchModifier'];
60 }
61
62 return $modifiers;
63 }
64
71 public function cast($value)
72 {
73 if ($value instanceof SqlExpression)
74 {
75 return $value;
76 }
77
78 if (!empty($value) && !($value instanceof DateTime))
79 {
80 $value = new DateTime($value);
81 }
82
83 if ($value instanceof DateTime)
84 {
85 $this->useTimezone
86 ? $value->enableUserTime()
87 : $value->disableUserTime();
88 }
89
90 return $value;
91 }
92
100 public function convertValueFromDb($value)
101 {
102 return $this->getConnection()->getSqlHelper()->convertFromDbDateTime($value);
103 }
104
112 public function convertValueToDb($value)
113 {
114 if ($value instanceof SqlExpression)
115 {
116 return $value;
117 }
118
119 try
120 {
121 return $value === null && $this->is_nullable
122 ? $value
123 : $this->getConnection()->getSqlHelper()->convertToDbDateTime($value);
124 }
125 catch (ArgumentTypeException $e)
126 {
127 $exceptionMsg = $this->entity
128 ? "Type error in `{$this->name}` of `{$this->entity->getFullName()}`"
129 : "Type error in `{$this->name}`";
130
131 throw new ArgumentException(
132 "{$exceptionMsg}: {$e->getMessage()}"
133 );
134 }
135 }
136
143 public static function disableTimezoneFetchModifier($time)
144 {
145 if ($time !== null)
146 {
147 $time->disableUserTime();
148 }
149
150 return $time;
151 }
152
156 public function getGetterTypeHint()
157 {
158 return $this->getNullableTypeHint('\\'.DateTime::class);
159 }
160
164 public function getSetterTypeHint()
165 {
166 return $this->getNullableTypeHint('\\'.DateTime::class);
167 }
168}
__construct($name, $parameters=array())
__construct($name, $parameters=array())