datatable.directory codebase https://datatable.directory/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
datatable.directory/app/helpers.php

98 lines
2.2 KiB

6 years ago
<?php
6 years ago
const VALI_NAME = 'string|regex:/^[a-z0-9_.-]+$/i|max:255';
const VALI_PASSWORD = 'string|min:6|max:1024';
const VALI_EMAIL = 'string|email|max:255';
const VALI_TEXT = 'string|max:4095';
const VALI_LINE = 'string|max:255';
6 years ago
// global helpers
function authed() {
return ! \Auth::guest();
}
function guest() {
return \Auth::guest();
}
/**
* @return \App\Models\User|null
*/
function user() {
return \Auth::user();
}
function ellipsis($s, $len) {
return \MightyPork\Utils\Str::ellipsis($s, $len);
}
function faker() {
static $fac = null;
if ($fac !== null) return $fac;
return $fac = Faker\Factory::create();
}
6 years ago
function dark_mode($ifDark=true, $ifLight=false) {
return isset($_COOKIE["dark_mode"]) ? $ifDark : $ifLight;
}
6 years ago
/**
* Recursively expand validation rules
*
* @param $arr
* @return array
*/
function vali($arr) {
$result = [];
foreach ($arr as $key => $rules) {
// top level
if (is_array($rules)) {
$ar = [];
foreach ($rules as $rule) {
if (is_string($rule) && strpos($rule, '|') !== false) {
foreach (explode('|', $rule) as $rr) {
$ar[] = $rr;
}
} else if (is_array($rule)) {
// nested array, assume no further recursion
foreach ($rule as $rr) {
$ar[] = $rr;
}
} else {
// Rule
$ar[] = $rule;
}
}
$result[$key] = $ar;
} else {
// string or Rule
$result[$key] = $rules;
}
}
return $result;
}
/**
* like old(), but decodes stringified json
*
* @param string $name
* @param object|array $default
* @return object|array
*/
function old_json($name, $default) {
$old = old($name, null);
if (is_string($old)) return json_decode($old);
return $default;
}
// Safe JSON funcs
function toJSON($object) {
return \GuzzleHttp\json_encode($object, JSON_UNESCAPED_SLASHES + JSON_UNESCAPED_UNICODE);
}
function fromJSON($object, $assoc=false) {
return \GuzzleHttp\json_decode($object, $assoc);
}