Elgg  Version 3.0
Values.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
5 use Elgg\I18n\DateTime as ElggDateTime;
7 use DateTime as PHPDateTime;
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  } else if (is_numeric($time)) {
83  $dt = new ElggDateTime();
84  $dt->setTimestamp((int) $time);
85  } else {
86  $dt = new ElggDateTime($time);
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  if (is_object($arg) && isset($arg->id)) {
114  $ids[] = (int) $arg->id;
115  } else if (is_array($arg)) {
116  foreach ($arg as $a) {
117  $el_ids = self::normalizeIds($a);
118  $ids = array_merge($ids, $el_ids);
119  }
120  } else if (is_numeric($arg)) {
121  $ids[] = (int) $arg;
122  } else {
123  $arg = print_r($arg, true);
124  throw new DataFormatException("Parameter '$arg' can not be resolved to a valid ID'");
125  }
126  }
127 
128  return array_unique($ids);
129  }
130 
139  public static function normalizeGuids(...$args) {
140  if (empty($args)) {
142  }
143 
144  $guids = [];
145  foreach ($args as $arg) {
146  if (!isset($arg)) {
147  continue;
148  }
149  if (is_object($arg) && isset($arg->guid)) {
150  $guids[] = (int) $arg->guid;
151  } else if (is_array($arg)) {
152  foreach ($arg as $a) {
153  $el_guids = self::normalizeGuids($a);
154  $guids = array_merge($guids, $el_guids);
155  }
156  } else if (is_numeric($arg)) {
157  $guids[] = (int) $arg;
158  } else {
159  $arg = print_r($arg, true);
160  throw new DataFormatException("Parameter '$arg' can not be resolved to a valid GUID'");
161  }
162  }
163 
164  return array_unique($guids);
165  }
166 
175  public static function preventViewOutput() {
176  return ['__view_output' => ''];
177  }
178 
189  public static function isEmpty($value) {
190 
191  if ($value === 0 || $value === '0' || $value === 0.0) {
192  return false;
193  }
194 
195  return empty($value);
196  }
197 }
static normalizeGuids(...$args)
Flatten an array of data into an array of GUIDs.
Definition: Values.php:139
$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:189
static getNull()
Return null.
Definition: Values.php:43
static getFalse()
Return false.
Definition: Values.php:33
Configuration exception.
$dt
Definition: time.php:69
static normalizeTimestamp($time)
Returns timestamp value of the time representation.
Definition: Values.php:64
if(!$entity instanceof ElggEntity) $time
Definition: time.php:21
static preventViewOutput()
Return array with __view_output set to prevent view output during view_vars hook. ...
Definition: Values.php:175
static normalizeIds(...$args)
Prepare IDs.
Definition: Values.php:103
static getTrue()
Return true.
Definition: Values.php:23
const ELGG_ENTITIES_ANY_VALUE
Definition: constants.php:24
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif(is_callable([$item, 'getType']))
Definition: item.php:39
$guids
Activates all specified installed and inactive plugins.
Definition: activate_all.php:9
$value
Definition: debugging.php:7
Functions for use as plugin hook/event handlers or other situations where you need a globally accessi...
Definition: Values.php:15
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