DateValueFactory.php
1.98 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
namespace ACP\Search\Helper;
use ACP\Search\Value;
use DateTime;
use Exception;
class DateValueFactory {
/**
* @var string
*/
protected $type;
/**
* @var string
*/
protected $format;
/**
* @param string $type
* @param string $format
*/
public function __construct( $type, $format = null ) {
if ( null === $format ) {
$format = $this->get_format_from_type( $type );
}
$this->type = $type;
$this->format = $format;
}
/**
* @param string $type
*
* @return string
*/
protected function get_format_from_type( $type ) {
if ( $type === Value::INT ) {
return 'U';
}
return 'Y-m-d H:i:s';
}
/**
* @param DateTime $start
* @param DateTime $end
*
* @return Value
*/
public function create_range( DateTime $start, DateTime $end ) {
return new Value(
[
$start->format( $this->format ),
$end->format( $this->format ),
],
$this->type
);
}
/**
* @return Value
* @throws Exception
*/
public function create_range_today() {
return $this->create_range_single_day( new DateTime() );
}
/**
* @param DateTime $day
*
* @return Value
*/
public function create_range_single_day( DateTime $day ) {
$day->setTime( 0, 0 );
$end = clone $day;
$end->modify( '+1 day -1 second' );
return $this->create_range( $day, $end );
}
/**
* @param int $days
*
* @return Value
*/
public function create_less_than_days_ago( $days ) {
$date = new DateTime();
$date->setTime( 0, 0 );
$date->modify( sprintf( '-%s day', $days ) );
return $this->create_range( $date, new DateTime() );
}
/**
* @param DateTime $day
*
* @return Value
*/
public function create_single_day( DateTime $day ) {
$day->setTime( 0, 0 );
return new Value(
$day->format( $this->format ),
$this->type
);
}
/**
* @return Value
* @throws Exception
*/
public function create_today() {
$date = new DateTime();
$date->setTime( 0, 0 );
return new Value(
$date->format( $this->format ),
$this->type
);
}
}