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