Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

134 lines
3.7 KiB
PHP
Raw Permalink 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
2021-09-10 20:09:53 +02:00
*
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;
2018-12-08 07:16:49 -06:00
if (! function_exists('d_echo')) {
/**
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!
*
2021-11-14 14:58:13 -06:00
* @param string|array|float|int $text The error message or array to print
2021-09-08 23:35:56 +02:00
* @param string $no_debug_text Text to print if debug is disabled
2018-12-08 07:16:49 -06:00
*/
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
2019-06-19 16:01:53 -05:00
if (! function_exists('array_pairs')) {
/**
* Get all consecutive pairs of values in an array.
* [1,2,3,4] -> [[1,2],[2,3],[3,4]]
*
2021-09-08 23:35:56 +02:00
* @param array $array
2019-06-19 16:01:53 -05:00
* @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
*
2021-09-08 23:35:56 +02:00
* @param string $number
2021-03-12 18:10:14 -06:00
* @return float|int
*/
function cast_number($number)
{
return \LibreNMS\Util\Number::cast($number);
}
if (! function_exists('trans_fb')) {
/**
* Translate the given message with a fallback string if none exists.
*
* @param string $key
* @param string $fallback
2021-09-08 23:35:56 +02:00
* @param array $replace
* @param string $locale
* @return \Symfony\Component\Translation\TranslatorInterface|string
*/
function trans_fb($key, $fallback, $replace = [], $locale = null)
{
return ($key === ($translation = trans($key, $replace, $locale))) ? $fallback : $translation;
}
}
2022-03-12 16:14:32 -06:00
if (! function_exists('preg_match_any')) {
/**
* Check an array of regexes against a subject if any match, return true
*
* @param string $subject the string to match against
* @param array|string $regexes an array of regexes or single regex to check
* @return bool if any of the regexes matched, return true
*/
function preg_match_any($subject, $regexes)
{
foreach ((array) $regexes as $regex) {
if (preg_match($regex, $subject)) {
return true;
}
}
return false;
}
}
if (! function_exists('toast')) {
/**
* send a toastr popup or return ToastInterface
*/
function toast(?string $title = null, ?string $message = null, string $level = 'info', ?array $options = null)
{
$toast = app(\App\Http\Interfaces\ToastInterface::class);
if (! is_null($message)) {
return $toast->message($title, $message, $level, $options);
}
return $toast;
}
}