<?php

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';

// global helpers
function authed() {
    return ! \Auth::guest();
}

function guest() {
    return \Auth::guest();
}

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();
}

/**
 * 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;
}