1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Elgg; |
4
|
|
|
|
5
|
|
|
use DataFormatException; |
6
|
|
|
use DateTime; |
7
|
|
|
use DateTimeZone; |
8
|
|
|
use Exception; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Functions for use as plugin hook/event handlers or other situations where you need a |
13
|
|
|
* globally accessible callable. |
14
|
|
|
*/ |
15
|
|
|
class Values { |
16
|
|
|
|
17
|
14 |
|
/** |
18
|
14 |
|
* Return true |
19
|
|
|
* |
20
|
|
|
* @return true |
21
|
|
|
* @since 1.12.0 |
22
|
|
|
*/ |
23
|
|
|
public static function getTrue() { |
24
|
|
|
return true; |
25
|
|
|
} |
26
|
|
|
|
27
|
12 |
|
/** |
28
|
12 |
|
* Return false |
29
|
|
|
* |
30
|
|
|
* @return false |
31
|
|
|
* @since 1.12.0 |
32
|
|
|
*/ |
33
|
|
|
public static function getFalse() { |
34
|
|
|
return false; |
35
|
|
|
} |
36
|
|
|
|
37
|
2 |
|
/** |
38
|
2 |
|
* Return null |
39
|
|
|
* |
40
|
|
|
* @return null |
41
|
|
|
* @since 1.12.0 |
42
|
|
|
*/ |
43
|
|
|
public static function getNull() { |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Return empty array |
48
|
|
|
* |
49
|
|
|
* @return array |
50
|
|
|
* @since 1.12.0 |
51
|
|
|
*/ |
52
|
|
|
public static function getArray() { |
53
|
|
|
return []; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns timestamp value of the time representation |
58
|
14 |
|
* |
59
|
|
|
* @param DateTime|string|int $time Time |
60
|
14 |
|
* |
61
|
8 |
|
* @return int |
62
|
6 |
|
* @throws DataFormatException |
63
|
1 |
|
*/ |
64
|
|
|
public static function normalizeTimestamp($time) { |
65
|
6 |
|
return self::normalizeTime($time)->getTimestamp(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns DateTime object based on time representation |
70
|
|
|
* |
71
|
5 |
|
* @param DateTime|string|int $time Time |
72
|
|
|
* |
73
|
|
|
* @return DateTime |
74
|
|
|
* @throws DataFormatException |
75
|
|
|
*/ |
76
|
|
|
public static function normalizeTime($time) { |
77
|
|
|
try { |
78
|
|
|
if ($time instanceof DateTime) { |
79
|
|
|
$dt = $time; |
80
|
|
|
} else if (is_numeric($time)) { |
81
|
|
|
$dt = new DateTime(null, new DateTimeZone('UTC')); |
82
|
1286 |
|
$dt->setTimestamp((int) $time); |
83
|
1286 |
|
} else { |
84
|
|
|
$dt = new DateTime($time); |
85
|
|
|
} |
86
|
|
|
} catch (Exception $e) { |
87
|
1286 |
|
throw new DataFormatException($e->getMessage()); |
88
|
1286 |
|
} |
89
|
1286 |
|
|
90
|
1279 |
|
return $dt; |
91
|
|
|
} |
92
|
1197 |
|
|
93
|
1 |
|
/** |
94
|
1197 |
|
* Prepare IDs |
95
|
1187 |
|
* |
96
|
41 |
|
* @param array ...$args IDs |
97
|
1187 |
|
* |
98
|
|
|
* @return int[] |
99
|
605 |
|
* @throws DataFormatException |
100
|
605 |
|
*/ |
101
|
|
|
public static function normalizeIds(...$args) { |
102
|
|
|
if (empty($args)) { |
103
|
1197 |
|
return ELGG_ENTITIES_ANY_VALUE; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$ids = []; |
107
|
1286 |
|
foreach ($args as $arg) { |
108
|
|
|
if (!isset($arg)) { |
109
|
|
|
continue; |
110
|
|
|
} |
111
|
|
|
if (is_object($arg) && isset($arg->id)) { |
112
|
|
|
$ids[] = (int) $arg->id; |
113
|
|
|
} else if (is_array($arg)) { |
114
|
|
|
foreach ($arg as $a) { |
115
|
|
|
$el_ids = self::normalizeIds($a); |
116
|
|
|
$ids = array_merge($ids, $el_ids); |
117
|
|
|
} |
118
|
4199 |
|
} else if (is_numeric($arg)) { |
119
|
4199 |
|
$ids[] = (int) $arg; |
120
|
|
|
} else { |
121
|
|
|
$arg = print_r($arg, true); |
122
|
|
|
throw new DataFormatException("Parameter '$arg' can not be resolved to a valid ID'"); |
123
|
4199 |
|
} |
124
|
4199 |
|
} |
125
|
4199 |
|
|
126
|
1276 |
|
return array_unique($ids); |
127
|
|
|
} |
128
|
4199 |
|
|
129
|
492 |
|
/** |
130
|
4199 |
|
* Flatten an array of data into an array of GUIDs |
131
|
4198 |
|
* |
132
|
4197 |
|
* @param mixed ...$args Elements to normalize |
133
|
4198 |
|
* |
134
|
|
|
* @return int[]|null |
135
|
4198 |
|
* @throws DataFormatException |
136
|
4198 |
|
*/ |
137
|
|
|
public static function normalizeGuids(...$args) { |
138
|
9 |
|
if (empty($args)) { |
139
|
4199 |
|
return ELGG_ENTITIES_ANY_VALUE; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$guids = []; |
143
|
4199 |
|
foreach ($args as $arg) { |
144
|
|
|
if (!isset($arg)) { |
145
|
|
|
continue; |
146
|
|
|
} |
147
|
|
|
if (is_object($arg) && isset($arg->guid)) { |
148
|
|
|
$guids[] = (int) $arg->guid; |
149
|
|
|
} else if (is_array($arg)) { |
150
|
|
|
foreach ($arg as $a) { |
151
|
|
|
$el_guids = self::normalizeGuids($a); |
152
|
|
|
$guids = array_merge($guids, $el_guids); |
153
|
|
|
} |
154
|
|
|
} else if (is_numeric($arg)) { |
155
|
|
|
$guids[] = (int) $arg; |
156
|
|
|
} else { |
157
|
|
|
$arg = print_r($arg, true); |
158
|
|
|
throw new DataFormatException("Parameter '$arg' can not be resolved to a valid GUID'"); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return array_unique($guids); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Return array with __view_output set to prevent view output during view_vars hook |
167
|
|
|
* |
168
|
|
|
* @see ViewsService->renderView() |
169
|
|
|
* |
170
|
|
|
* @return array |
171
|
|
|
* @since 3.0 |
172
|
|
|
*/ |
173
|
|
|
public static function preventViewOutput() { |
174
|
|
|
return ['__view_output' => '']; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|