Elgg /
Elgg
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Elgg; |
||
| 4 | |||
| 5 | /** |
||
| 6 | * Functions for use as plugin hook/event handlers or other situations where you need a |
||
| 7 | * globally accessible callable. |
||
| 8 | */ |
||
| 9 | class Values { |
||
| 10 | |||
| 11 | /** |
||
| 12 | * Return true |
||
| 13 | * |
||
| 14 | * @return true |
||
| 15 | * @since 1.12.0 |
||
| 16 | */ |
||
| 17 | 14 | public static function getTrue() { |
|
| 18 | 14 | return true; |
|
| 19 | } |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Return false |
||
| 23 | * |
||
| 24 | * @return false |
||
| 25 | * @since 1.12.0 |
||
| 26 | */ |
||
| 27 | 12 | public static function getFalse() { |
|
| 28 | 12 | return false; |
|
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Return null |
||
| 33 | * |
||
| 34 | * @return null |
||
| 35 | * @since 1.12.0 |
||
| 36 | */ |
||
| 37 | 2 | public static function getNull() { |
|
| 38 | 2 | } |
|
| 39 | |||
| 40 | /** |
||
| 41 | * Return empty array |
||
| 42 | * |
||
| 43 | * @return array |
||
| 44 | * @since 1.12.0 |
||
| 45 | */ |
||
| 46 | public static function getArray() { |
||
| 47 | return []; |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Returns timestamp value of the time representation |
||
| 52 | * |
||
| 53 | * @param DateTime|string|int $time Time |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 54 | * |
||
| 55 | * @return int |
||
| 56 | * @throws \DataFormatException |
||
| 57 | */ |
||
| 58 | 14 | public static function normalizeTimestamp($time) { |
|
| 59 | try { |
||
| 60 | 14 | if ($time instanceof \DateTime) { |
|
| 61 | 8 | return $time->getTimestamp(); |
|
| 62 | 6 | } else if (is_string($time)) { |
|
| 63 | 1 | $dt = new \DateTime($time); |
|
| 64 | |||
| 65 | 6 | return $dt->getTimestamp(); |
|
| 66 | } |
||
| 67 | } catch (\Exception $e) { |
||
| 68 | throw new \DataFormatException($e->getMessage()); |
||
| 69 | } |
||
| 70 | |||
| 71 | 5 | return (int) $time; |
|
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Prepare IDs |
||
| 76 | * |
||
| 77 | * @param array ...$args IDs |
||
| 78 | * |
||
| 79 | * @return int[] |
||
| 80 | * @throws \DataFormatException |
||
| 81 | */ |
||
| 82 | 1286 | public static function normalizeIds(...$args) { |
|
| 83 | 1286 | if (empty($args)) { |
|
| 84 | return ELGG_ENTITIES_ANY_VALUE; |
||
| 85 | } |
||
| 86 | |||
| 87 | 1286 | $ids = []; |
|
| 88 | 1286 | foreach ($args as $arg) { |
|
| 89 | 1286 | if (!isset($arg)) { |
|
| 90 | 1279 | continue; |
|
| 91 | } |
||
| 92 | 1197 | if (is_object($arg) && isset($arg->id)) { |
|
| 93 | 1 | $ids[] = (int) $arg->id; |
|
| 94 | 1197 | } else if (is_array($arg)) { |
|
| 95 | 1187 | foreach ($arg as $a) { |
|
| 96 | 41 | $el_ids = self::normalizeIds($a); |
|
| 97 | 1187 | $ids = array_merge($ids, $el_ids); |
|
| 98 | } |
||
| 99 | 605 | } else if (is_numeric($arg)) { |
|
| 100 | 605 | $ids[] = (int) $arg; |
|
| 101 | } else { |
||
| 102 | $arg = print_r($arg, true); |
||
| 103 | 1197 | throw new \DataFormatException("Parameter '$arg' can not be resolved to a valid ID'"); |
|
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | 1286 | return array_unique($ids); |
|
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Flatten an array of data into an array of GUIDs |
||
| 112 | * |
||
| 113 | * @param mixed ...$args Elements to normalize |
||
| 114 | * |
||
| 115 | * @return int[]|null |
||
| 116 | * @throws \DataFormatException |
||
| 117 | */ |
||
| 118 | 4199 | public static function normalizeGuids(...$args) { |
|
| 119 | 4199 | if (empty($args)) { |
|
| 120 | return ELGG_ENTITIES_ANY_VALUE; |
||
| 121 | } |
||
| 122 | |||
| 123 | 4199 | $guids = []; |
|
| 124 | 4199 | foreach ($args as $arg) { |
|
| 125 | 4199 | if (!isset($arg)) { |
|
| 126 | 1276 | continue; |
|
| 127 | } |
||
| 128 | 4199 | if (is_object($arg) && isset($arg->guid)) { |
|
| 129 | 492 | $guids[] = (int) $arg->guid; |
|
| 130 | 4199 | } else if (is_array($arg)) { |
|
| 131 | 4198 | foreach ($arg as $a) { |
|
| 132 | 4197 | $el_guids = self::normalizeGuids($a); |
|
| 133 | 4198 | $guids = array_merge($guids, $el_guids); |
|
| 134 | } |
||
| 135 | 4198 | } else if (is_numeric($arg)) { |
|
| 136 | 4198 | $guids[] = (int) $arg; |
|
| 137 | } else { |
||
| 138 | 9 | $arg = print_r($arg, true); |
|
| 139 | 4199 | throw new \DataFormatException("Parameter '$arg' can not be resolved to a valid GUID'"); |
|
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | 4199 | return array_unique($guids); |
|
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Return array with __view_output set to prevent view output during view_vars hook |
||
| 148 | * |
||
| 149 | * @see ViewsService->renderView() |
||
| 150 | * |
||
| 151 | * @return array |
||
| 152 | * @since 3.0 |
||
| 153 | */ |
||
| 154 | public static function preventViewOutput() { |
||
| 155 | return ['__view_output' => '']; |
||
| 156 | } |
||
| 157 | } |
||
| 158 |