2017-07-04 11:53:49 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Facades;
|
|
|
|
|
2018-01-20 07:44:17 +08:00
|
|
|
use GuzzleHttp\Client;
|
2018-02-21 12:33:09 +08:00
|
|
|
use Illuminate\Support\Facades\Facade;
|
2017-07-04 11:53:49 +08:00
|
|
|
|
|
|
|
class Utils extends Facade
|
|
|
|
{
|
2018-03-20 09:50:40 +08:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-08-27 02:50:08 +08:00
|
|
|
protected static function getFacadeAccessor(): string
|
2017-12-05 07:39:45 +08:00
|
|
|
{
|
|
|
|
return 'utils';
|
|
|
|
}
|
|
|
|
|
2018-03-18 00:35:34 +08:00
|
|
|
/**
|
|
|
|
* Simple check on the first character if it's an object or not
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-18 00:35:34 +08:00
|
|
|
* @param $obj
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-18 00:35:34 +08:00
|
|
|
* @return bool
|
|
|
|
*/
|
2018-08-27 02:50:08 +08:00
|
|
|
public static function isObject($obj): bool
|
2018-03-18 00:35:34 +08:00
|
|
|
{
|
2018-03-20 09:50:40 +08:00
|
|
|
if (!$obj) {
|
2018-03-18 00:35:34 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($obj[0] === '{' || $obj[0] === '[') {
|
2018-03-18 00:35:34 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-20 07:44:17 +08:00
|
|
|
/**
|
|
|
|
* Download a URI. If a file is given, it will save the downloaded
|
|
|
|
* content into that file
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-08-27 02:50:08 +08:00
|
|
|
* @param string $uri
|
|
|
|
* @param null $file
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-20 07:44:17 +08:00
|
|
|
* @throws \RuntimeException
|
2018-08-27 02:50:08 +08:00
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return string
|
2018-01-20 07:44:17 +08:00
|
|
|
*/
|
2018-08-27 02:50:08 +08:00
|
|
|
public static function downloadUrl($uri, $file = null): string
|
2018-01-20 07:44:17 +08:00
|
|
|
{
|
|
|
|
$opts = [];
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($file !== null) {
|
2018-01-20 07:44:17 +08:00
|
|
|
$opts['sink'] = $file;
|
|
|
|
}
|
|
|
|
|
|
|
|
$client = new Client();
|
|
|
|
$response = $client->request('GET', $uri, $opts);
|
|
|
|
|
|
|
|
$body = $response->getBody()->getContents();
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($response->getHeader('content-type') === 'application/json') {
|
2018-01-20 07:44:17 +08:00
|
|
|
$body = \GuzzleHttp\json_decode($body);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $body;
|
|
|
|
}
|
|
|
|
|
2017-12-13 06:58:27 +08:00
|
|
|
/**
|
|
|
|
* Returns a 40 character API key that a user can use
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-13 06:58:27 +08:00
|
|
|
* @return string
|
|
|
|
*/
|
2018-08-27 02:50:08 +08:00
|
|
|
public static function generateApiKey(): string
|
2017-12-13 06:58:27 +08:00
|
|
|
{
|
2018-03-20 09:50:40 +08:00
|
|
|
$key = substr(sha1(time().mt_rand()), 0, 20);
|
2017-12-13 06:58:27 +08:00
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
|
2018-08-27 02:50:08 +08:00
|
|
|
/**
|
|
|
|
* @param string $minutes
|
2018-08-27 02:51:47 +08:00
|
|
|
*
|
2018-08-27 02:50:08 +08:00
|
|
|
* @return array
|
|
|
|
*/
|
2017-12-14 00:56:26 +08:00
|
|
|
public static function minutesToTimeParts($minutes): array
|
|
|
|
{
|
2017-12-24 01:17:16 +08:00
|
|
|
$hours = floor($minutes / 60);
|
|
|
|
$minutes %= 60;
|
|
|
|
|
|
|
|
return ['h' => $hours, 'm' => $minutes];
|
2017-12-14 00:56:26 +08:00
|
|
|
}
|
|
|
|
|
2017-12-15 01:50:47 +08:00
|
|
|
public static function minutesToTimeString($minutes): string
|
|
|
|
{
|
2017-12-24 01:17:16 +08:00
|
|
|
$hm = self::minutesToTimeParts($minutes);
|
2018-03-20 09:50:40 +08:00
|
|
|
return $hm['h'].'h '.$hm['m'].'m';
|
2017-12-15 01:50:47 +08:00
|
|
|
}
|
|
|
|
|
2017-12-04 04:34:32 +08:00
|
|
|
/**
|
|
|
|
* Convert seconds to an array of hours, minutes, seconds
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-08-27 02:50:08 +08:00
|
|
|
* @param int $seconds
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-08-27 02:50:08 +08:00
|
|
|
* @throws \Exception
|
2018-08-27 02:51:47 +08:00
|
|
|
*
|
|
|
|
* @return array['h', 'm', 's']
|
2017-12-04 04:34:32 +08:00
|
|
|
*/
|
|
|
|
public static function secondsToTimeParts($seconds): array
|
|
|
|
{
|
2018-08-27 00:40:04 +08:00
|
|
|
$dtF = new \DateTimeImmutable('@0', new \DateTimeZone('UTC'));
|
|
|
|
$dtT = new \DateTimeImmutable("@$seconds", new \DateTimeZone('UTC'));
|
2017-12-04 04:34:32 +08:00
|
|
|
|
|
|
|
$t = $dtF->diff($dtT);
|
|
|
|
|
|
|
|
$retval = [];
|
|
|
|
$retval['h'] = (int) $t->format('%h');
|
|
|
|
$retval['m'] = (int) $t->format('%i');
|
|
|
|
$retval['s'] = (int) $t->format('%s');
|
|
|
|
|
|
|
|
return $retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert seconds to HH MM format
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-08-27 02:50:08 +08:00
|
|
|
* @param int $seconds
|
2017-12-04 04:34:32 +08:00
|
|
|
* @param bool $incl_sec
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-08-27 02:50:08 +08:00
|
|
|
* @throws \Exception
|
2018-08-27 02:51:47 +08:00
|
|
|
*
|
|
|
|
* @return string
|
2017-12-04 04:34:32 +08:00
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
public static function secondsToTimeString($seconds, $incl_sec = false): string
|
2017-12-04 04:34:32 +08:00
|
|
|
{
|
|
|
|
$hms = self::secondsToTimeParts($seconds);
|
|
|
|
$format = $hms['h'].'h '.$hms['m'].'m';
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($incl_sec) {
|
2017-12-04 04:34:32 +08:00
|
|
|
$format .= ' '.$hms['s'].'s';
|
2017-08-03 02:13:08 +08:00
|
|
|
}
|
|
|
|
|
2017-12-04 04:34:32 +08:00
|
|
|
return $format;
|
2017-07-04 11:53:49 +08:00
|
|
|
}
|
2017-08-16 21:29:05 +08:00
|
|
|
|
2017-12-14 00:56:26 +08:00
|
|
|
/**
|
|
|
|
* @param $minutes
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-14 00:56:26 +08:00
|
|
|
* @return float|int
|
|
|
|
*/
|
|
|
|
public static function minutesToSeconds($minutes)
|
|
|
|
{
|
|
|
|
return $minutes * 60;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the seconds to minutes and then round it up
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-14 00:56:26 +08:00
|
|
|
* @param $seconds
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-14 00:56:26 +08:00
|
|
|
* @return float|int
|
|
|
|
*/
|
|
|
|
public static function secondsToMinutes($seconds)
|
|
|
|
{
|
2018-03-20 09:50:40 +08:00
|
|
|
return ceil($seconds / 60);
|
2017-12-14 00:56:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert hours to minutes. Pretty complex
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-14 00:56:26 +08:00
|
|
|
* @param $minutes
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-14 00:56:26 +08:00
|
|
|
* @return float|int
|
|
|
|
*/
|
|
|
|
public static function minutesToHours($minutes)
|
|
|
|
{
|
2018-03-20 09:50:40 +08:00
|
|
|
return $minutes / 60;
|
2017-12-14 00:56:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param $hours
|
2017-12-26 05:19:34 +08:00
|
|
|
* @param null $minutes
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-14 00:56:26 +08:00
|
|
|
* @return float|int
|
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
public static function hoursToMinutes($hours, $minutes = null)
|
2017-12-14 00:56:26 +08:00
|
|
|
{
|
2017-12-26 05:19:34 +08:00
|
|
|
$total = (int) $hours * 60;
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($minutes) {
|
2017-12-26 05:19:34 +08:00
|
|
|
$total += (int) $minutes;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $total;
|
2017-12-14 00:56:26 +08:00
|
|
|
}
|
|
|
|
|
2017-08-16 21:29:05 +08:00
|
|
|
/**
|
|
|
|
* Bitwise operator for setting days of week to integer field
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param int $datefield initial datefield
|
2017-08-16 21:29:05 +08:00
|
|
|
* @param array $day_enums Array of values from config("enum.days")
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-08-16 21:29:05 +08:00
|
|
|
* @return int
|
|
|
|
*/
|
2018-08-27 02:50:08 +08:00
|
|
|
public static function setDays(int $datefield, array $day_enums): int
|
2018-03-20 09:50:40 +08:00
|
|
|
{
|
|
|
|
foreach ($day_enums as $day) {
|
2017-08-16 21:29:05 +08:00
|
|
|
$datefield |= $day;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $datefield;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bit check if a day exists within a integer bitfield
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-08-16 21:29:05 +08:00
|
|
|
* @param int $datefield datefield from database
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param int $day_enum Value from config("enum.days")
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-08-16 21:29:05 +08:00
|
|
|
* @return bool
|
|
|
|
*/
|
2018-08-27 02:50:08 +08:00
|
|
|
public static function hasDay(int $datefield, int $day_enum): bool
|
2018-03-20 09:50:40 +08:00
|
|
|
{
|
2017-08-16 21:29:05 +08:00
|
|
|
return ($datefield & $day_enum) === $datefield;
|
|
|
|
}
|
2017-07-04 11:53:49 +08:00
|
|
|
}
|