Elgg  Version 6.1
Values.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
5 use DateTime as PHPDateTime;
8 
13 class Values {
14 
21  public static function getTrue() {
22  return true;
23  }
24 
31  public static function getFalse() {
32  return false;
33  }
34 
41  public static function getNull() {
42  return null;
43  }
44 
51  public static function getArray() {
52  return [];
53  }
54 
63  public static function normalizeTimestamp($time) {
64  return self::normalizeTime($time)->getTimestamp();
65  }
66 
75  public static function normalizeTime($time) {
76  try {
77  if ($time instanceof ElggDateTime) {
78  $dt = $time;
79  } elseif ($time instanceof PHPDateTime) {
80  $dt = new ElggDateTime($time->format(PHPDateTime::RFC3339_EXTENDED));
81  } elseif (is_numeric($time)) {
82  $dt = new ElggDateTime();
83  $dt->setTimestamp((int) $time);
84  } elseif (is_string($time)) {
85  $dt = new ElggDateTime($time);
86  } else {
87  $dt = new ElggDateTime();
88  }
89  } catch (\Exception $e) {
90  throw new DataFormatException($e->getMessage());
91  }
92 
93  return $dt;
94  }
95 
104  public static function normalizeIds(...$args) {
105  if (empty($args)) {
107  }
108 
109  $ids = [];
110  foreach ($args as $arg) {
111  if (!isset($arg)) {
112  continue;
113  }
114 
115  if (is_object($arg) && isset($arg->id)) {
116  $ids[] = (int) $arg->id;
117  } elseif (is_array($arg)) {
118  foreach ($arg as $a) {
119  $el_ids = self::normalizeIds($a);
120  $ids = array_merge($ids, $el_ids);
121  }
122  } elseif (is_numeric($arg)) {
123  $ids[] = (int) $arg;
124  } else {
125  $arg = print_r($arg, true);
126  throw new DataFormatException("Parameter '$arg' can not be resolved to a valid ID'");
127  }
128  }
129 
130  return array_unique($ids);
131  }
132 
141  public static function normalizeGuids(...$args) {
142  if (empty($args)) {
144  }
145 
146  $guids = [];
147  foreach ($args as $arg) {
148  if (!isset($arg)) {
149  continue;
150  }
151 
152  if (is_object($arg) && isset($arg->guid)) {
153  $guids[] = (int) $arg->guid;
154  } elseif (is_array($arg)) {
155  foreach ($arg as $a) {
156  $el_guids = self::normalizeGuids($a);
157  $guids = array_merge($guids, $el_guids);
158  }
159  } elseif (is_numeric($arg)) {
160  $guids[] = (int) $arg;
161  } else {
162  $arg = print_r($arg, true);
163  throw new DataFormatException("Parameter '$arg' can not be resolved to a valid GUID'");
164  }
165  }
166 
167  return array_unique($guids);
168  }
169 
178  public static function preventViewOutput() {
179  return [ViewsService::OUTPUT_KEY => ''];
180  }
181 
192  public static function isEmpty($value): bool {
193  if ($value === 0 || $value === '0' || $value === 0.0) {
194  return false;
195  }
196 
197  return empty($value);
198  }
199 
211  public static function shortFormatOutput($n, $precision = 0) {
212  // return the input if not a number or less than 1000
213  if ($n < 1000 || !is_numeric($n)) {
214  return $n;
215  }
216 
217  $decimal_separator = substr(elgg_echo('number_counter:decimal_separator'), 0, 1);
218  $thousands_separator = substr(elgg_echo('number_counter:thousands_separator'), 0, 1);
219 
220  if ($n < 1000000) {
221  // 1.5K, 999.5K
222  $n = number_format($n / 1000, $precision, $decimal_separator, $thousands_separator);
223  $text_key = 'number_counter:view:thousand';
224  } else if ($n < 1000000000) {
225  // 1.5M, 999.5M
226  $n = number_format($n / 1000000, $precision, $decimal_separator, $thousands_separator);
227  $text_key = 'number_counter:view:million';
228  } else if ($n < 1000000000000) {
229  // 1.5B, 999.5B
230  $n = number_format($n / 1000000000, $precision, $decimal_separator, $thousands_separator);
231  $text_key = 'number_counter:view:billion';
232  } else {
233  // 1.5T
234  $n = number_format($n / 1000000000000, $precision, $decimal_separator, $thousands_separator);
235  $text_key = 'number_counter:view:trillion';
236  }
237 
238  if (stristr($n, $decimal_separator) !== false) {
239  // strip trailing zero's after decimal separator
240  $parts = explode($decimal_separator, $n);
241  $parts[1] = rtrim($parts[1], 0);
242 
243  $n = implode($decimal_separator, array_filter($parts));
244  }
245 
246  return elgg_echo($text_key, [$n]);
247  }
248 }
static normalizeGuids(...$args)
Flatten an array of data into an array of GUIDs.
Definition: Values.php:141
elgg_echo(string $message_key, array $args=[], string $language= '')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
if(!$annotation instanceof ElggAnnotation) $time
Definition: time.php:20
$args
Some servers don&#39;t allow PHP to check the rewrite, so try via AJAX.
static isEmpty($value)
Check if a value isn&#39;t empty, but allow 0 and &#39;0&#39;.
Definition: Values.php:192
static getNull()
Return null.
Definition: Values.php:41
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof\ElggRelationship) elseif(is_callable([$item, 'getType']))
Definition: item.php:48
$value
Definition: generic.php:51
static getFalse()
Return false.
Definition: Values.php:31
$dt
Definition: time.php:70
static normalizeTimestamp($time)
Returns timestamp value of the time representation.
Definition: Values.php:63
static shortFormatOutput($n, $precision=0)
Use to convert large positive numbers in to short form like 1K, 1M, 1B or 1T Example: shortFormatOutp...
Definition: Values.php:211
static preventViewOutput()
Return array with __view_output set to prevent view output during view_vars event.
Definition: Values.php:178
static normalizeIds(...$args)
Prepare IDs.
Definition: Values.php:104
static getTrue()
Return true.
Definition: Values.php:21
const ELGG_ENTITIES_ANY_VALUE
Constant to request the value of a parameter be ignored in elgg_get_*() functions.
Definition: constants.php:21
$guids
Activates all specified installed and inactive plugins.
Definition: activate_all.php:9
Functions for use as event handlers or other situations where you need a globally accessible callable...
Definition: Values.php:13
An exception thrown when there is a problem in the format of some data.
static getArray()
Return empty array.
Definition: Values.php:51
static normalizeTime($time)
Returns DateTime object based on time representation.
Definition: Values.php:75