1
|
|
|
<?php defined('SYSPATH') OR die('No direct script access.'); |
2
|
|
|
/** |
3
|
|
|
* Handles timestamps and conversions to and from different formats |
4
|
|
|
* |
5
|
|
|
* All timestamps are represented internally by UNIX timestamps, regardless |
6
|
|
|
* of their format in the database. When the model is saved, the value is |
7
|
|
|
* converted back to the format specified by $format (which is a valid |
8
|
|
|
* date() string). |
9
|
|
|
* |
10
|
|
|
* This means that you can have timestamp logic exist relatively independently |
11
|
|
|
* of your database's format. If, one day, you wish to change the format used |
12
|
|
|
* to represent dates in the database, you just have to update the $format |
13
|
|
|
* property for the field. |
14
|
|
|
* |
15
|
|
|
* @package Jam |
16
|
|
|
* @category Fields |
17
|
|
|
* @author Jonathan Geiger |
18
|
|
|
* @copyright (c) 2010-2011 Jonathan Geiger |
19
|
|
|
* @license http://www.opensource.org/licenses/isc-license.txt |
20
|
|
|
*/ |
21
|
|
|
abstract class Kohana_Jam_Field_Timestamp extends Jam_Field { |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var int default is NULL, which implies no date |
25
|
|
|
*/ |
26
|
|
|
public $default = NULL; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var boolean whether or not to automatically set now() on creation |
30
|
|
|
*/ |
31
|
|
|
public $auto_now_create = FALSE; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var boolean whether or not to automatically set now() on update |
35
|
|
|
*/ |
36
|
|
|
public $auto_now_update = FALSE; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string a date formula representing the time in the database |
40
|
|
|
*/ |
41
|
|
|
public $format = NULL; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Jam_Timezone object for manipulating timezones |
45
|
|
|
* @var Jam_Timezone |
46
|
|
|
*/ |
47
|
|
|
public $timezone = NULL; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Convert empty values by default because some DB's |
51
|
|
|
* will convert empty strings to zero timestamps |
52
|
|
|
*/ |
53
|
|
|
public $convert_empty = TRUE; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Sets the default to 0 if we have no format, or an empty string otherwise. |
57
|
|
|
* |
58
|
|
|
* @param array $options |
59
|
|
|
*/ |
60
|
8 |
|
public function __construct($options = array()) |
61
|
|
|
{ |
62
|
8 |
|
parent::__construct($options); |
63
|
|
|
|
64
|
8 |
|
if ( ! isset($options['default']) AND ! $this->allow_null) |
65
|
|
|
{ |
66
|
|
|
// Having a implies saving we're format a string, so we want a proper default |
67
|
|
|
$this->default = $this->format ? '' : 0; |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
8 |
|
if ($this->timezone === NULL) |
71
|
|
|
{ |
72
|
6 |
|
$this->timezone = Jam_Timezone::instance(); |
73
|
|
|
} |
74
|
8 |
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
4 |
|
public function get(Jam_Validated $model, $value, $is_loaded) |
78
|
|
|
{ |
79
|
4 |
|
if ($this->timezone !== FALSE AND $this->timezone->is_active() AND ! $is_loaded) |
80
|
|
|
{ |
81
|
1 |
|
if ( ! is_numeric($value) AND FALSE !== strtotime($value)) |
82
|
|
|
{ |
83
|
1 |
|
$value = strtotime($value); |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
if (is_numeric($value) AND $value AND $this->timezone !== FALSE) |
87
|
|
|
{ |
88
|
1 |
|
$value = $this->timezone->convert($value, Jam_Timezone::MASTER_TIMEZONE, Jam_Timezone::USER_TIMEZONE); |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
if ($this->format AND is_numeric($value)) |
92
|
|
|
{ |
93
|
1 |
|
$value = date($this->format, $value); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
4 |
|
return $value; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Automatically creates or updates the time and |
102
|
|
|
* converts it, if necessary. |
103
|
|
|
* |
104
|
|
|
* @param Jam_Model $model |
105
|
|
|
* @param mixed $value |
106
|
|
|
* @param boolean $is_loaded |
107
|
|
|
* @return int|string |
108
|
|
|
*/ |
109
|
6 |
|
public function convert(Jam_Validated $model, $value, $is_loaded) |
110
|
|
|
{ |
111
|
|
|
// Do we need to provide a default since we're creating or updating |
112
|
6 |
|
if ((empty($value) AND ! $is_loaded AND $this->auto_now_create) OR ($is_loaded AND $this->auto_now_update)) |
113
|
|
|
{ |
114
|
3 |
|
$value = ($this->timezone !== FALSE) ? $this->timezone->time() : time(); |
115
|
|
|
} |
116
|
|
|
else |
117
|
|
|
{ |
118
|
|
|
// Convert to UNIX timestamp |
119
|
6 |
|
if (is_numeric($value)) |
120
|
|
|
{ |
121
|
4 |
|
$value = (int) $value; |
122
|
|
|
} |
123
|
4 |
|
elseif (FALSE !== ($to_time = strtotime($value))) |
124
|
|
|
{ |
125
|
2 |
|
$value = $to_time; |
126
|
|
|
} |
127
|
|
|
|
128
|
6 |
|
if (is_numeric($value) AND $value AND $this->timezone !== FALSE) |
129
|
|
|
{ |
130
|
6 |
|
$value = $this->timezone->convert($value, Jam_Timezone::USER_TIMEZONE, Jam_Timezone::MASTER_TIMEZONE); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
// Convert if necessary |
135
|
6 |
|
if ($this->format AND is_numeric($value)) |
136
|
|
|
{ |
137
|
3 |
|
$value = date($this->format, $value); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
|
141
|
6 |
|
return $value; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
} // End Kohana_Jam_Field_Timestamp |
145
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.