Changeset View
Changeset View
Standalone View
Standalone View
src/app/Utils.php
<?php | <?php | ||||
namespace App; | namespace App; | ||||
use Carbon\Carbon; | use Carbon\Carbon; | ||||
use Illuminate\Support\Facades\Auth; | use Illuminate\Support\Facades\Auth; | ||||
use Ramsey\Uuid\Uuid; | use Ramsey\Uuid\Uuid; | ||||
/** | /** | ||||
* Small utility functions for App. | * Small utility functions for App. | ||||
*/ | */ | ||||
class Utils | class Utils | ||||
{ | { | ||||
// Note: Removed '0', 'O', '1', 'I' as problematic with some fonts | |||||
public const CHARS = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ'; | |||||
/** | /** | ||||
* Return the number of days in the month prior to this one. | * Return the number of days in the month prior to this one. | ||||
* | * | ||||
* @return int | * @return int | ||||
*/ | */ | ||||
public static function daysInLastMonth() | public static function daysInLastMonth() | ||||
{ | { | ||||
$start = new Carbon('first day of last month'); | $start = new Carbon('first day of last month'); | ||||
Show All 32 Lines | public static function userEmailOrNull(): ?string | ||||
if (!$user) { | if (!$user) { | ||||
return null; | return null; | ||||
} | } | ||||
return $user->email; | return $user->email; | ||||
} | } | ||||
/** | /** | ||||
* Returns a random string consisting of a quantity of segments of a certain length joined. | |||||
* | |||||
* Example: | |||||
* | |||||
* ```php | |||||
* $roomName = strtolower(\App\Utils::randStr(3, 3, '-'); | |||||
* // $roomName == '3qb-7cs-cjj' | |||||
* ``` | |||||
* | |||||
* @param int $length The length of each segment | |||||
* @param int $qty The quantity of segments | |||||
* @param string $join The string to use to join the segments | |||||
* | |||||
* @return string | |||||
*/ | |||||
public static function randStr($length, $qty = 1, $join = '') | |||||
{ | |||||
$chars = env('SHORTCODE_CHARS', self::CHARS); | |||||
$randStrs = []; | |||||
for ($x = 0; $x < $qty; $x++) { | |||||
$randStrs[$x] = []; | |||||
for ($y = 0; $y < $length; $y++) { | |||||
$randStrs[$x][] = $chars[rand(0, strlen($chars) - 1)]; | |||||
} | |||||
shuffle($randStrs[$x]); | |||||
$randStrs[$x] = implode('', $randStrs[$x]); | |||||
} | |||||
return implode($join, $randStrs); | |||||
} | |||||
/** | |||||
* Returns a UUID in the form of an integer. | * Returns a UUID in the form of an integer. | ||||
* | * | ||||
* @return integer | * @return integer | ||||
*/ | */ | ||||
public static function uuidInt(): int | public static function uuidInt(): int | ||||
{ | { | ||||
$hex = Uuid::uuid4(); | $hex = Uuid::uuid4(); | ||||
$bin = pack('h*', str_replace('-', '', $hex)); | $bin = pack('h*', str_replace('-', '', $hex)); | ||||
Show All 36 Lines | private static function combine($input, $r, $index, $data, $i, &$output): void | ||||
// is passed, but index is not changed) | // is passed, but index is not changed) | ||||
self::combine($input, $r, $index, $data, $i + 1, $output); | self::combine($input, $r, $index, $data, $i + 1, $output); | ||||
} | } | ||||
/** | /** | ||||
* Create self URL | * Create self URL | ||||
* | * | ||||
* @param string $route Route/Path | * @param string $route Route/Path | ||||
* @todo Move this to App\Http\Controllers\Controller | |||||
* | * | ||||
* @return string Full URL | * @return string Full URL | ||||
*/ | */ | ||||
public static function serviceUrl(string $route): string | public static function serviceUrl(string $route): string | ||||
{ | { | ||||
$url = \config('app.public_url'); | $url = \config('app.public_url'); | ||||
if (!$url) { | if (!$url) { | ||||
$url = \config('app.url'); | $url = \config('app.url'); | ||||
} | } | ||||
return rtrim(trim($url, '/') . '/' . ltrim($route, '/'), '/'); | return rtrim(trim($url, '/') . '/' . ltrim($route, '/'), '/'); | ||||
} | } | ||||
/** | /** | ||||
* Create a configuration/environment data to be passed to | * Create a configuration/environment data to be passed to | ||||
* the UI | * the UI | ||||
* | * | ||||
* @todo For a lack of better place this is put here for now | * @todo Move this to App\Http\Controllers\Controller | ||||
* | * | ||||
* @return array Configuration data | * @return array Configuration data | ||||
*/ | */ | ||||
public static function uiEnv(): array | public static function uiEnv(): array | ||||
{ | { | ||||
$countries = include resource_path('countries.php'); | |||||
$req_domain = preg_replace('/:[0-9]+$/', '', request()->getHttpHost()); | |||||
$sys_domain = \config('app.domain'); | |||||
$path = request()->path(); | |||||
$opts = ['app.name', 'app.url', 'app.domain']; | $opts = ['app.name', 'app.url', 'app.domain']; | ||||
$env = \app('config')->getMany($opts); | $env = \app('config')->getMany($opts); | ||||
$countries = include resource_path('countries.php'); | |||||
$env['countries'] = $countries ?: []; | $env['countries'] = $countries ?: []; | ||||
$env['view'] = 'root'; | |||||
$env['jsapp'] = 'user.js'; | |||||
$isAdmin = strpos(request()->getHttpHost(), 'admin.') === 0; | if ($path == 'meet' || strpos($path, 'meet/') === 0) { | ||||
$env['jsapp'] = $isAdmin ? 'admin.js' : 'user.js'; | $env['view'] = 'meet'; | ||||
$env['jsapp'] = 'meet.js'; | |||||
} elseif ($req_domain == "admin.$sys_domain") { | |||||
$env['jsapp'] = 'admin.js'; | |||||
} | |||||
$env['paymentProvider'] = \config('services.payment_provider'); | $env['paymentProvider'] = \config('services.payment_provider'); | ||||
$env['stripePK'] = \config('services.stripe.public_key'); | $env['stripePK'] = \config('services.stripe.public_key'); | ||||
return $env; | return $env; | ||||
} | } | ||||
} | } |