forked from modstart/ModStartBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTime.php
More file actions
74 lines (64 loc) · 1.68 KB
/
Copy pathTime.php
File metadata and controls
74 lines (64 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
namespace ModStart\Field;
class Time extends AbstractField
{
/**
* 使用JSON
*/
const SERIALIZE_TYPE_DEFAULT = null;
/**
* 使用冒号分割
*/
const SERIALIZE_TYPE_SECOND = 1;
protected function setup()
{
$this->addVariables([
'serializeType' => null,
]);
}
public function serializeType($value)
{
$this->addVariables(['serializeType' => $value]);
return $this;
}
public function serializeAsSecond()
{
$this->serializeType(self::SERIALIZE_TYPE_SECOND);
return $this;
}
public function unserializeValue($value, AbstractField $field)
{
if ('' === $value || null === $value) {
return null;
}
switch ($this->getVariable('serializeType')) {
case self::SERIALIZE_TYPE_SECOND:
return sprintf('%02d:%02d:%02d', $value / 3600, $value / 60 % 60, $value % 60);
default:
return $value;
}
}
public function serializeValue($value, $model)
{
if ('' === $value || null === $value) {
return null;
}
switch ($this->getVariable('serializeType')) {
case self::SERIALIZE_TYPE_SECOND:
$time = explode(':', $value);
if (count($time) !== 3) {
return 0;
}
return $time[0] * 3600 + $time[1] * 60 + $time[2];
default:
return $value;
}
}
public function prepareInput($value, $model)
{
if ('' === $value || null === $value) {
return null;
}
return $value;
}
}