Elgg  Version 4.3
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 
10 
15 class Values {
16 
23  public static function getTrue() {
24  return true;
25  }
26 
33  public static function getFalse() {
34  return false;
35  }
36 
43  public static function getNull() {
44  }
45 
52  public static function getArray() {
53  return [];
54  }
55 
64  public static function normalizeTimestamp($time) {
65  return self::normalizeTime($time)->getTimestamp();
66  }
67 
76  public static function normalizeTime($time) {
77  try {
78  if ($time instanceof ElggDateTime) {
79  $dt = $time;
80  } elseif ($time instanceof PHPDateTime) {
81  $dt = new ElggDateTime($time->format(PHPDateTime::RFC3339_EXTENDED));
82  } elseif (is_numeric($time)) {
83  $dt = new ElggDateTime();
84  $dt->setTimestamp((int) $time);
85  } elseif (is_string($time)) {
86  $dt = new ElggDateTime($time);
87  } else {
88  $dt = new ElggDateTime();
89  }
90  } catch (Exception $e) {
91  throw new DataFormatException($e->getMessage());
92  }
93 
94  return $dt;
95  }
96 
105  public static function normalizeIds(...$args) {
106  if (empty($args)) {
108  }
109 
110  $ids = [];
111  foreach ($args as $arg) {
112  if (!isset($arg)) {
113  continue;
114  }
115  if (is_object($arg) && isset($arg->id)) {
116  $ids[] = (int) $arg->id;
117  } else if (is_array($arg)) {
118  foreach ($arg as $a) {
119  $el_ids = self::normalizeIds($a);
120  $ids = array_merge($ids, $el_ids);
121  }
122  } else if (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  if (is_object($arg) && isset($arg->guid)) {
152  $guids[] = (int) $arg->guid;
153  } else if (is_array($arg)) {
154  foreach ($arg as $a) {
155  $el_guids = self::normalizeGuids($a);
156  $guids = array_merge($guids, $el_guids);
157  }
158  } else if (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 ['__view_output' => ''];
179  }
180 
191  public static function isEmpty($value) {
192 
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
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:191
elgg_echo($message_key, array $args=[], $language="")
Elgg language module Functions to manage language and translations.
Definition: languages.php:18
static getNull()
Return null.
Definition: Values.php:43
$value
Definition: generic.php:51
static getFalse()
Return false.
Definition: Values.php:33
$dt
Definition: time.php:71
static normalizeTimestamp($time)
Returns timestamp value of the time representation.
Definition: Values.php:64
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 hook. ...
Definition: Values.php:177
static normalizeIds(...$args)
Prepare IDs.
Definition: Values.php:105
static getTrue()
Return true.
Definition: Values.php:23
const ELGG_ENTITIES_ANY_VALUE
Definition: constants.php:24
$guids
Activates all specified installed and inactive plugins.
Definition: activate_all.php:9
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof ElggRelationship) elseif(is_callable([$item, 'getType']))
Definition: item.php:48
Functions for use as plugin hook/event handlers or other situations where you need a globally accessi...
Definition: Values.php:15
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:52
static normalizeTime($time)
Returns DateTime object based on time representation.
Definition: Values.php:76