Elgg  Version master
Values.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
5 use DateTime as PHPDateTime;
6 use Elgg\I18n\DateTime as ElggDateTime;
8 use Exception;
9 
14 class Values {
15 
22  public static function getTrue() {
23  return true;
24  }
25 
32  public static function getFalse() {
33  return false;
34  }
35 
42  public static function getNull() {
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 ['__view_output' => ''];
180  }
181 
192  public static function isEmpty($value): bool {
193 
194  if ($value === 0 || $value === '0' || $value === 0.0) {
195  return false;
196  }
197 
198  return empty($value);
199  }
200 
212  public static function shortFormatOutput($n, $precision = 0) {
213  // return the input if not a number or less than 1000
214  if ($n < 1000 || !is_numeric($n)) {
215  return $n;
216  }
217 
218  $decimal_separator = substr(elgg_echo('number_counter:decimal_separator'), 0, 1);
219  $thousands_separator = substr(elgg_echo('number_counter:thousands_separator'), 0, 1);
220 
221  if ($n < 1000000) {
222  // 1.5K, 999.5K
223  $n = number_format($n / 1000, $precision, $decimal_separator, $thousands_separator);
224  $text_key = 'number_counter:view:thousand';
225  } else if ($n < 1000000000) {
226  // 1.5M, 999.5M
227  $n = number_format($n / 1000000, $precision, $decimal_separator, $thousands_separator);
228  $text_key = 'number_counter:view:million';
229  } else if ($n < 1000000000000) {
230  // 1.5B, 999.5B
231  $n = number_format($n / 1000000000, $precision, $decimal_separator, $thousands_separator);
232  $text_key = 'number_counter:view:billion';
233  } else {
234  // 1.5T
235  $n = number_format($n / 1000000000000, $precision, $decimal_separator, $thousands_separator);
236  $text_key = 'number_counter:view:trillion';
237  }
238 
239  if (stristr($n, $decimal_separator) !== false) {
240  // strip trailing zero's after decimal separator
241  $parts = explode($decimal_separator, $n);
242  $parts[1] = rtrim($parts[1], 0);
243 
244  $n = implode($decimal_separator, array_filter($parts));
245  }
246 
247  return elgg_echo($text_key, [$n]);
248  }
249 }
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:42
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:32
$dt
Definition: time.php:71
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:212
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:22
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:14
An exception thrown when there is a problem in the format of some data.
Extension of the DateTime class to support formating a date using the locale.
Definition: DateTime.php:12
static getArray()
Return empty array.
Definition: Values.php:51
static normalizeTime($time)
Returns DateTime object based on time representation.
Definition: Values.php:75