1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\domain\services\converters\json; |
4
|
|
|
|
5
|
|
|
use EE_Datetime; |
6
|
|
|
use EE_Error; |
7
|
|
|
use EE_Event; |
8
|
|
|
use EventEspresso\core\exceptions\InvalidDataTypeException; |
9
|
|
|
use EventEspresso\core\exceptions\InvalidInterfaceException; |
10
|
|
|
use EventEspresso\core\services\converters\json\ModelObjectToJsonConverterInterface; |
11
|
|
|
use InvalidArgumentException; |
12
|
|
|
use ReflectionException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class DatetimeToJson |
16
|
|
|
* converts EE_Datetime model object to JSON |
17
|
|
|
* |
18
|
|
|
* @package EventEspresso\core\domain\services\converters\json |
19
|
|
|
* @author Brent Christensen |
20
|
|
|
* @since $VID:$ |
21
|
|
|
*/ |
22
|
|
|
class DatetimeToJson implements ModelObjectToJsonConverterInterface |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param EE_Datetime[] $datetimes |
27
|
|
|
* @return array |
28
|
|
|
* @throws EE_Error |
29
|
|
|
* @throws InvalidArgumentException |
30
|
|
|
* @throws InvalidDataTypeException |
31
|
|
|
* @throws InvalidInterfaceException |
32
|
|
|
* @throws ReflectionException |
33
|
|
|
* @since $VID:$ |
34
|
|
|
*/ |
35
|
|
View Code Duplication |
public function convertAndEncodeArrayOf(array $datetimes) |
36
|
|
|
{ |
37
|
|
|
$jsonDates = []; |
38
|
|
|
foreach ($datetimes as $datetime) { |
39
|
|
|
if ($datetime instanceof EE_Datetime) { |
40
|
|
|
$jsonDates[ $datetime->ID() ] = $this->convertAndEncode($datetime); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
return $jsonDates; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param EE_Datetime $datetime |
49
|
|
|
* @return false|string |
50
|
|
|
* @throws EE_Error |
51
|
|
|
* @throws InvalidArgumentException |
52
|
|
|
* @throws InvalidDataTypeException |
53
|
|
|
* @throws InvalidInterfaceException |
54
|
|
|
* @throws ReflectionException |
55
|
|
|
* @since $VID:$ |
56
|
|
|
*/ |
57
|
|
|
public function convertAndEncode($datetime) |
58
|
|
|
{ |
59
|
|
|
return $datetime instanceof EE_Datetime ? $this->encode($this->convert($datetime)) : false; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param EE_Datetime[] $datetimes |
65
|
|
|
* @return array |
66
|
|
|
* @throws EE_Error |
67
|
|
|
* @throws InvalidArgumentException |
68
|
|
|
* @throws InvalidDataTypeException |
69
|
|
|
* @throws InvalidInterfaceException |
70
|
|
|
* @throws ReflectionException |
71
|
|
|
* @since $VID:$ |
72
|
|
|
*/ |
73
|
|
|
public function convertArrayOf(array $datetimes) |
74
|
|
|
{ |
75
|
|
|
$arrayOfDates = []; |
76
|
|
|
foreach ($datetimes as $datetime) { |
77
|
|
|
if ($datetime instanceof EE_Datetime) { |
78
|
|
|
$arrayOfDates[ $datetime->ID() ] = $this->convert($datetime); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
return $arrayOfDates; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param EE_Datetime $datetime |
87
|
|
|
* @return array |
88
|
|
|
* @throws EE_Error |
89
|
|
|
* @throws InvalidDataTypeException |
90
|
|
|
* @throws InvalidInterfaceException |
91
|
|
|
* @throws InvalidArgumentException |
92
|
|
|
* @throws ReflectionException |
93
|
|
|
* @since $VID:$ |
94
|
|
|
*/ |
95
|
|
|
public function convert($datetime) |
96
|
|
|
{ |
97
|
|
|
return $datetime instanceof EE_Datetime ? [ |
98
|
|
|
'DTT_ID' => $datetime->ID(), |
99
|
|
|
'EVT_ID' => $datetime->event() instanceof EE_Event ? $datetime->event()->ID() : 0, |
100
|
|
|
'DTT_name' => $datetime->name(), |
101
|
|
|
'DTT_description' => $datetime->description(), |
102
|
|
|
'DTT_EVT_start' => $datetime->start_date(DATE_ATOM), |
103
|
|
|
'DTT_EVT_end' => $datetime->end_date(DATE_ATOM), |
104
|
|
|
'DTT_sold' => $datetime->sold(), |
105
|
|
|
'DTT_reserved' => $datetime->reserved(), |
106
|
|
|
'DTT_reg_limit' => $datetime->reg_limit() === INF ? -1 : $datetime->reg_limit(), |
107
|
|
|
'DTT_is_primary' => $datetime->get_active_status(), |
108
|
|
|
'DTT_order' => $datetime->order(), |
109
|
|
|
'DTT_parent' => $datetime->parent(), |
110
|
|
|
'DTT_deleted' => $datetime->get('DTT_deleted'), |
111
|
|
|
] : []; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param EE_Datetime[] $datetimes |
117
|
|
|
* @return array |
118
|
|
|
* @throws EE_Error |
119
|
|
|
* @throws InvalidArgumentException |
120
|
|
|
* @throws InvalidDataTypeException |
121
|
|
|
* @throws InvalidInterfaceException |
122
|
|
|
* @throws ReflectionException |
123
|
|
|
* @since $VID:$ |
124
|
|
|
*/ |
125
|
|
View Code Duplication |
public function encodeArrayOf(array $datetimes) |
126
|
|
|
{ |
127
|
|
|
$jsonDates = []; |
128
|
|
|
foreach ($datetimes as $datetime) { |
129
|
|
|
if ($datetime instanceof EE_Datetime) { |
130
|
|
|
$jsonDates[ $datetime->ID() ] = $this->encode($datetime); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
return $jsonDates; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param array $datetime_array |
139
|
|
|
* @return false|string |
140
|
|
|
* @since $VID:$ |
141
|
|
|
*/ |
142
|
|
|
public function encode(array $datetime_array) |
143
|
|
|
{ |
144
|
|
|
return wp_json_encode($datetime_array); |
145
|
|
|
} |
146
|
|
|
} |