1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This software package is licensed under `AGPL-3.0-only, proprietary` license[s]. |
5
|
|
|
* |
6
|
|
|
* @package maslosoft/manganel |
7
|
|
|
* @license AGPL-3.0-only, proprietary |
8
|
|
|
* |
9
|
|
|
* @copyright Copyright (c) Peter Maselkowski <[email protected]> |
10
|
|
|
* @link https://maslosoft.com/manganel/ |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Maslosoft\Manganel\Sanitizers; |
14
|
|
|
|
15
|
|
|
use DateTime; |
16
|
|
|
use Maslosoft\Mangan\Sanitizers\DateSanitizer; |
17
|
|
|
use MongoDate; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* UnixDateSanitizer |
21
|
|
|
* |
22
|
|
|
* This sanitizer allow storing date in elasticsearch in string format, |
23
|
|
|
* while reading it as MongoDate object. |
24
|
|
|
* |
25
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
26
|
|
|
*/ |
27
|
|
|
class DateWriteEsSanitizer extends DateSanitizer |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
const ISODate = 'Y-m-d*H:i:s*'; |
31
|
|
|
|
32
|
4 |
|
public function read($model, $dbValue) |
33
|
|
|
{ |
34
|
|
|
// Don't touch |
35
|
4 |
|
if ($dbValue instanceof MongoDate) |
36
|
|
|
{ |
37
|
|
|
return $dbValue; |
38
|
|
|
} |
39
|
|
|
// Assume current time |
40
|
4 |
|
if ((int) $dbValue === 0 || empty($dbValue)) |
41
|
|
|
{ |
42
|
|
|
$dbValue = time(); |
43
|
|
|
} |
44
|
|
|
// Assume timestamp |
45
|
4 |
|
if (is_int($dbValue) || (is_string($dbValue) && preg_match('~^\d+$~', $dbValue))) |
46
|
|
|
{ |
47
|
|
|
return new MongoDate((int) $dbValue); |
48
|
|
|
} |
49
|
4 |
|
elseif (is_string($dbValue)) |
50
|
|
|
{ |
51
|
|
|
// Assume ISO date |
52
|
4 |
|
$dt = DateTime::createFromFormat(DateTime::ISO8601, $dbValue); |
53
|
4 |
|
if (empty($dt)) |
54
|
|
|
{ |
55
|
|
|
return new MongoDate(time()); |
56
|
|
|
} |
57
|
4 |
|
$time = $dt->format('U'); |
58
|
4 |
|
return new MongoDate($time); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// Create from date time string |
62
|
|
|
$dt = new DateTime($dbValue); |
63
|
|
|
$time = $dt->format('U'); |
64
|
|
|
return new MongoDate($time); |
65
|
|
|
} |
66
|
|
|
|
67
|
5 |
|
public function write($model, $dbValue) |
68
|
|
|
{ |
69
|
5 |
|
if ($dbValue instanceof MongoDate) |
70
|
|
|
{ |
71
|
3 |
|
$time = $dbValue->sec; |
72
|
|
|
} |
73
|
2 |
|
elseif (!empty($time)) |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$time = $dbValue; |
76
|
|
|
} |
77
|
|
|
else |
78
|
|
|
{ |
79
|
2 |
|
$time = time(); |
80
|
|
|
} |
81
|
5 |
|
if (is_int($time)) |
82
|
|
|
{ |
83
|
4 |
|
$dt = DateTime::createFromFormat('U', $time); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
else |
86
|
|
|
{ |
87
|
1 |
|
$dt = DateTime::createFromFormat('c', $time); |
|
|
|
|
88
|
|
|
} |
89
|
5 |
|
return date('c', (int) (new MongoDate((int) $time))->sec); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
This check marks calls to
isset(...)
orempty(...)
that are found before the variable itself is defined. These will always have the same result.This is likely the result of code being shifted around. Consider removing these calls.