Changeset View
Changeset View
Standalone View
Standalone View
src/app/Utils.php
<?php | <?php | ||||
namespace App; | namespace App; | ||||
use App\Rules\UserEmailLocal; | use App\Rules\UserEmailLocal; | ||||
use Carbon\Carbon; | |||||
use Illuminate\Support\Facades\Auth; | |||||
use Illuminate\Support\Facades\Validator; | use Illuminate\Support\Facades\Validator; | ||||
use Illuminate\Support\Str; | use Illuminate\Support\Str; | ||||
use Ramsey\Uuid\Uuid; | use Ramsey\Uuid\Uuid; | ||||
/** | /** | ||||
* Small utility functions for App. | * Small utility functions for App. | ||||
*/ | */ | ||||
class Utils | class Utils | ||||
{ | { | ||||
/** | /** | ||||
* Return the number of days in the month prior to this one. | |||||
* | |||||
* @return int | |||||
*/ | |||||
public static function daysInLastMonth() | |||||
{ | |||||
$start = new Carbon('first day of last month'); | |||||
$end = new Carbon('last day of last month'); | |||||
return $start->diffInDays($end); | |||||
} | |||||
/** | |||||
* Provide all unique combinations of elements in $input, with order and duplicates irrelevant. | * Provide all unique combinations of elements in $input, with order and duplicates irrelevant. | ||||
* | * | ||||
* @param array $input The input array of elements. | * @param array $input The input array of elements. | ||||
* | * | ||||
* @return array[] | * @return array[] | ||||
*/ | */ | ||||
public static function powerSet(array $input): array | public static function powerSet(array $input): array | ||||
{ | { | ||||
$output = []; | $output = []; | ||||
for ($x = 0; $x < count($input); $x++) { | for ($x = 0; $x < count($input); $x++) { | ||||
self::combine($input, $x + 1, 0, [], 0, $output); | self::combine($input, $x + 1, 0, [], 0, $output); | ||||
} | } | ||||
return $output; | return $output; | ||||
} | } | ||||
/** | /** | ||||
* Returns the current user's email address or null. | |||||
* | |||||
* @return string | |||||
*/ | |||||
public static function userEmailOrNull(): ?string | |||||
{ | |||||
$user = Auth::user(); | |||||
if (!$user) { | |||||
return null; | |||||
} | |||||
return $user->email; | |||||
} | |||||
/** | |||||
* 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 33 Lines | private static function combine($input, $r, $index, $data, $i, &$output): void | ||||
self::combine($input, $r, $index + 1, $data, $i + 1, $output); | self::combine($input, $r, $index + 1, $data, $i + 1, $output); | ||||
// current is excluded, replace it with next (Note that i+1 | // current is excluded, replace it with next (Note that i+1 | ||||
// 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 | |||||
* | |||||
* @param string $route Route/Path | |||||
* | |||||
* @return string Full URL | |||||
*/ | |||||
public static function serviceUrl(string $route): string | |||||
{ | |||||
$url = \url($route); | |||||
$app_url = trim(\config('app.url'), '/'); | |||||
$pub_url = trim(\config('app.public_url'), '/'); | |||||
if ($pub_url != $app_url) { | |||||
$url = str_replace($app_url, $pub_url, $url); | |||||
} | |||||
return $url; | |||||
} | |||||
/** | |||||
* 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 For a lack of better place this is put here for now | ||||
* | * | ||||
* @return array Configuration data | * @return array Configuration data | ||||
*/ | */ | ||||
public static function uiEnv(): array | public static function uiEnv(): array | ||||
Show All 9 Lines | public static function uiEnv(): array | ||||
$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; | ||||
} | } | ||||
/** | /** | ||||
* Create self URL | |||||
* | |||||
* @param string $route Route/Path | |||||
* | |||||
* @return string Full URL | |||||
*/ | |||||
public static function serviceUrl(string $route): string | |||||
{ | |||||
$url = \url($route); | |||||
$app_url = trim(\config('app.url'), '/'); | |||||
$pub_url = trim(\config('app.public_url'), '/'); | |||||
if ($pub_url != $app_url) { | |||||
$url = str_replace($app_url, $pub_url, $url); | |||||
} | |||||
return $url; | |||||
} | |||||
/** | |||||
* Email address (login or alias) validation | * Email address (login or alias) validation | ||||
* | * | ||||
* @param string $email Email address | * @param string $email Email address | ||||
* @param \App\User $user The account owner | * @param \App\User $user The account owner | ||||
* @param bool $is_alias The email is an alias | * @param bool $is_alias The email is an alias | ||||
* | * | ||||
* @return string Error message on validation error | * @return string Error message on validation error | ||||
*/ | */ | ||||
▲ Show 20 Lines • Show All 52 Lines • Show Last 20 Lines |