PHP_SMART_HOME_V3/app/models/Utilities.php

182 lines
4.4 KiB
PHP
Raw Normal View History

2020-05-16 15:18:27 +00:00
<?php
2020-08-25 13:12:25 +00:00
2020-05-16 15:18:27 +00:00
/**
2020-08-25 13:12:25 +00:00
*
*/
2020-05-16 15:18:27 +00:00
class Utilities
{
2020-08-25 13:12:25 +00:00
static function cleanString($text)
{
2020-05-16 15:18:27 +00:00
$utf8 = array(
'/[áàâãªä]/u' => 'a',
'/[ÁÀÂÃÄ]/u' => 'A',
'/[ÍÌÎÏ]/u' => 'I',
'/[íìîï]/u' => 'i',
'/[ěéèêë]/u' => 'e',
'/[ĚÉÈÊË]/u' => 'E',
'/[óòôõºö]/u' => 'o',
'/[ÓÒÔÕÖ]/u' => 'O',
'/[úùûü]/u' => 'u',
'/[ÚÙÛÜ]/u' => 'U',
'/Š/' => 'S',
'/š/' => 's',
'/Č/' => 'C',
'/č/' => 'c',
'/ř/' => 'r',
'/Ř/' => 'R',
'/Ý/' => 'Y',
'/ý/' => 'y',
'/ç/' => 'c',
'/Ç/' => 'C',
'/ñ/' => 'n',
'/Ñ/' => 'N',
'//' => '-', // UTF-8 hyphen to "normal" hyphen
'/[]/u' => ' ', // Literally a single quote
'/[“”«»„]/u' => ' ', // Double quote
'/ /' => ' ', // nonbreaking space (equiv. to 0x160)
);
return preg_replace(array_keys($utf8), array_values($utf8), $text);
}
2020-08-25 13:12:25 +00:00
static function stringInsert($str, $insertstr, $pos)
2020-05-16 15:18:27 +00:00
{
$str = substr($str, 0, $pos) . $insertstr . substr($str, $pos);
return $str;
}
/**
2020-08-25 13:12:25 +00:00
* [generateGraphJson description]
* @param string $type [line/bar]
* @param array $data [description]
* @param array $options [description]
* @return [type] [description]
*/
2020-05-16 15:18:27 +00:00
2020-08-25 13:12:25 +00:00
static function generateGraphJson(string $type = 'line', array $data = [], array $options = [])
{
2020-05-16 15:18:27 +00:00
$array = [
'type' => $type,
'data' => [
'datasets' => [
[
'data' => $data,
'borderColor' => "#d4def7",
'backgroundColor' => "#d4def7"
]
]
],
'options' => [
'scales' => [
'xAxes' => [
[
'type' => 'time',
'distribution' => 'linear',
]
],
'yAxes' => [
[
'ticks' => [
'min' => $options['min'],
'max' => $options['max'],
'steps' => $options['scale']
]
]
]
],
'legend' => [
'display' => false
],
'tooltips' => [
'enabled' => true
],
'hover' => [
'mode' => true
]
]
];
return json_encode($array, JSON_PRETTY_PRINT);
}
2020-08-25 13:12:25 +00:00
static function ago($datetime)
2020-05-16 15:18:27 +00:00
{
2020-08-25 13:12:25 +00:00
$interval = date_create('now')->diff($datetime);
$suffix = ($interval->invert ? ' ago' : '');
if ($v = $interval->y >= 1) return self::pluralize($interval->m, 'month') . $suffix;
if ($v = $interval->d >= 1) return self::pluralize($interval->d, 'day') . $suffix;
if ($v = $interval->h >= 1) return self::pluralize($interval->h, 'hour') . $suffix;
if ($v = $interval->i >= 1) return self::pluralize($interval->i, 'minute') . $suffix;
return self::pluralize($interval->s, 'second') . $suffix;
2020-05-16 15:18:27 +00:00
}
2020-08-25 13:12:25 +00:00
static function pluralize($count, $text)
2020-05-16 15:18:27 +00:00
{
2020-08-25 13:12:25 +00:00
return $count . (($count == 1) ? (" $text") : (" ${text}s"));
2020-05-16 15:18:27 +00:00
}
2020-08-25 13:12:25 +00:00
static function checkOperator($value1, $operator, $value2)
{
2020-05-16 15:18:27 +00:00
switch ($operator) {
case '<': // Less than
2020-08-25 13:12:25 +00:00
return $value1 < $value2;
2020-05-16 15:18:27 +00:00
case '<=': // Less than or equal to
2020-08-25 13:12:25 +00:00
return $value1 <= $value2;
2020-05-16 15:18:27 +00:00
case '>': // Greater than
2020-08-25 13:12:25 +00:00
return $value1 > $value2;
2020-05-16 15:18:27 +00:00
case '>=': // Greater than or equal to
2020-08-25 13:12:25 +00:00
return $value1 >= $value2;
2020-05-16 15:18:27 +00:00
case '==': // Equal
2020-08-25 13:12:25 +00:00
return ($value1 == $value2);
2020-05-16 15:18:27 +00:00
case '===': // Identical
2020-08-25 13:12:25 +00:00
return $value1 === $value2;
2020-05-16 15:18:27 +00:00
case '!==': // Not Identical
2020-08-25 13:12:25 +00:00
return $value1 !== $value2;
2020-05-16 15:18:27 +00:00
case '!=': // Not equal
case '<>': // Not equal
2020-08-25 13:12:25 +00:00
return $value1 != $value2;
2020-05-16 15:18:27 +00:00
case '||': // Or
case 'or': // Or
2020-08-25 13:12:25 +00:00
return $value1 || $value2;
2020-05-16 15:18:27 +00:00
case '&&': // And
case 'and': // And
2020-08-25 13:12:25 +00:00
return $value1 && $value2;
2020-05-16 15:18:27 +00:00
case 'xor': // Or
2020-08-25 13:12:25 +00:00
return $value1 xor $value2;
2020-05-16 15:18:27 +00:00
default:
2020-08-25 13:12:25 +00:00
return FALSE;
2020-05-16 15:18:27 +00:00
} // end switch
}
2020-08-25 13:12:25 +00:00
static function CallAPI($method, $url, $data = false)
{
$curl = curl_init();
switch ($method) {
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
// Optional Authentication:
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
2020-05-16 15:18:27 +00:00
}