forked from Furgas/php-api-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kyCustomFieldDate.php
94 lines (85 loc) · 1.97 KB
/
kyCustomFieldDate.php
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
/**
* Class for date custom field.
*
* @author Tomasz Sawicki (https://github.com/Furgas)
* @since Kayako version 4.40.1079
* @package Object\CustomField
*
* @noinspection PhpDocSignatureInspection
*/
class kyCustomFieldDate extends kyCustomField {
/**
* Timestamp representation of date.
* @var int
*/
private $timestamp;
protected function parseData($data) {
parent::parseData($data);
$this->timestamp = strtotime($data['_contents']);
}
/**
* Returns field value as timestamp.
*
* @return int
*/
public function getTimestamp() {
return $this->timestamp;
}
/**
* Sets the date using timestamp.
*
* @param int $timestamp Timestamp.
* @return kyCustomFieldDate
*/
public function setTimestamp($timestamp) {
$this->timestamp = ky_assure_int($timestamp, 0);
$this->raw_value = date('m/d/Y', $this->timestamp);
return $this;
}
/**
* Returns field value as formatted date.
*
* @param string $format Output format of the date. If null the format set in client configuration is used.
* @return string
*/
public function getDate($format = null) {
if ($format === null) {
$format = kyConfig::get()->getDateFormat();
}
return date($format, $this->timestamp);
}
/**
* Sets the date.
*
* @param string $date Date in format understood by PHP strtotime.
* @return kyCustomFieldDate
*/
public function setDate($date) {
$this->setTimestamp(strtotime($date));
return $this;
}
/**
* Returns field value as formatted date. Default format from client configuration is used.
* @see kyCustomField::getValue()
* @see kyCustomFieldDate::getDate()
*
* @return string
*/
public function getValue() {
return $this->getDate();
}
/**
* Sets the date.
*
* @see kyCustomField::setValue()
* @see kyCustomFieldDate::setDate()
*
* @param string $value Date in format understood by PHP strtotime.
* @return kyCustomFieldDate
*/
public function setValue($value) {
$this->setDate($value);
return $this;
}
}