Files
librenms-librenms/includes/helpers.php
T

81 lines
2.2 KiB
PHP
Raw Normal View History

2018-12-08 07:16:49 -06:00
<?php
/**
* helpers.php
*
* Functions available in both Laravel and Legacy code (must not call any other legacy functions)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2021-02-09 00:29:04 +01:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-12-08 07:16:49 -06:00
*
2021-02-09 00:29:04 +01:00
* @link https://www.librenms.org
2018-12-08 07:16:49 -06:00
* @copyright 2018 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
2021-04-29 22:42:18 -05:00
use LibreNMS\Util\Debug;
2019-03-12 23:59:03 -05:00
use LibreNMS\Util\Laravel;
2020-09-21 15:40:17 +02:00
if (! function_exists('d_echo')) {
2018-12-08 07:16:49 -06:00
/**
2021-04-29 22:42:18 -05:00
* Legacy convenience function - please use this instead of 'if (Debug::isEnabled()) { echo ...; }'
2018-12-08 07:16:49 -06:00
* Use Log directly in pure Laravel code!
*
* @param string|array $text The error message or array to print
* @param string $no_debug_text Text to print if debug is disabled
*/
function d_echo($text, $no_debug_text = null)
{
2019-03-12 23:59:03 -05:00
if (Laravel::isBooted()) {
2018-12-08 07:16:49 -06:00
\Log::debug(is_string($text) ? rtrim($text) : $text);
2021-04-29 22:42:18 -05:00
} elseif (Debug::isEnabled()) {
2018-12-08 07:16:49 -06:00
print_r($text);
}
2021-04-29 22:42:18 -05:00
if (! Debug::isEnabled() && $no_debug_text) {
2018-12-08 07:16:49 -06:00
echo "$no_debug_text";
}
}
}
2019-01-09 19:36:32 -06:00
2020-09-21 15:40:17 +02:00
if (! function_exists('array_pairs')) {
2019-06-19 16:01:53 -05:00
/**
* Get all consecutive pairs of values in an array.
* [1,2,3,4] -> [[1,2],[2,3],[3,4]]
*
* @param array $array
* @return array
*/
function array_pairs($array)
{
$pairs = [];
for ($i = 1; $i < count($array); $i++) {
$pairs[] = [$array[$i - 1], $array[$i]];
}
return $pairs;
}
}
2021-03-12 18:10:14 -06:00
/**
* Cast string to int or float.
* Returns 0 if string is not numeric
*
* @param string $number
* @return float|int
*/
function cast_number($number)
{
return \LibreNMS\Util\Number::cast($number);
}