Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Just some useful helper functions for Laravel
<?php
use Illuminate\Support\Carbon;
if (! function_exists('carbon')) {
/**
* Create a new Carbon instance for the current time.
*
* @param \DateTimeInterface|string|null $time
* @param \DateTimeZone|string|null $tz
* @return \Illuminate\Support\Carbon
*/
function carbon($time = null, $tz = null)
{
return Carbon::parse($time, $tz);
}
}
if (! function_exists('empty_response')) {
/**
* Return an empty response (useful for CORS).
*
* @return \Illuminate\Http\Response
*/
function empty_response()
{
return response('', 204);
}
}
if (! function_exists('is_domain_name')) {
/**
* Determine whether a string is a valid domain name format.
*
* This is not perfect, but broad enough to weed out most invalid formats.
*
* 1. Hostname only (no scheme, port, path, query, or fragment)
* 2. 255 characters or less
* 3. Contains at least one period
* 4. Contains only alphanumeric characters, hyphens, and periods
* 5. Does not begin with a hyphen or period
* 6. Does not end with a number, hyphen, or period
*
* @param mixed $input
* @return bool
*/
function is_domain_name($input): bool
{
// foo.bar.com/path/?query=arg => foo.bar.com
$host = parse_url("//{$input}", PHP_URL_HOST);
return $input === $host && strlen($input) <= 255 && strpos($input, '.') > 0 && strpos($input, '-') !== 0 && preg_match('/^[a-z0-9\-\.]+(?<![0-9\-\.])$/', $input);
}
}
if (! function_exists('is_not_null')) {
/**
* Determine whether a value is null.
*
* Useful as a callback function for array_filter().
*
* @param mixed $input
* @return bool
*/
function is_not_null($input): bool
{
return ! is_null($input);
}
}
if (! function_exists('is_url')) {
/**
* Determine whether a string is a URL.
*
* @param mixed $input
* @return bool
*/
function is_url($input): bool
{
return (bool) preg_match('~^(https?:)?//.+~', $input);
}
}
if (! function_exists('prd') && config('app.debug')) {
/**
* Print human-readable data and die.
*
* @return never
*/
function prd(...$vars)
{
foreach ($vars as $var) {
print_r($var);
}
exit(1);
}
}
if (! function_exists('leadingslashit')) {
/**
* Ensure a string beings with exactly one slash.
*
* @param string $string
* @return string
*/
function leadingslashit(string $string): string
{
return '/'.unleadingslashit($string);
}
}
if (! function_exists('trailingslashit')) {
/**
* Ensure a string ends with exactly one slash.
*
* @param string $string
* @return string
*/
function trailingslashit(string $string): string
{
return untrailingslashit($string).'/';
}
}
if (! function_exists('unleadingslashit')) {
/**
* Ensure a string begins without any slashes.
*
* @param string $string
* @return string
*/
function unleadingslashit(string $string): string
{
return preg_replace('~^/+~', '', trim($string));
}
}
if (! function_exists('untrailingslashit')) {
/**
* Ensure a string ends without any slashes.
*
* @param string $string
* @return string
*/
function untrailingslashit(string $string): string
{
return preg_replace('~/+$~', '', trim($string));
}
}
if (! function_exists('vdd') && config('app.debug')) {
/**
* Var dump and die.
*
* @return never
*/
function vdd(...$vars)
{
foreach ($vars as $var) {
var_dump($var);
}
exit(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment