<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

if (!function_exists('prettyJson')) {

    function prettyJson($inputArray, $statusCode = 200) {
        return response()->json($inputArray, $statusCode, array('Content-Type' => 'application/json'), JSON_PRETTY_PRINT);
    }

}

if (!function_exists('slugify')) {

    function slugify($text) {
        // replace non letter or digits by -
        $text = preg_replace('~[^\pL\d]+~u', '-', $text);
        // transliterate
        $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
        // remove unwanted characters
        $text = preg_replace('~[^-\w]+~', '', $text);
        // trim
        $text = trim($text, '-');
        // remove duplicate -
        $text = preg_replace('~-+~', '-', $text);
        // lowercase
        $text = strtolower($text);
        if (empty($text)) {
            return 'n-a';
        }
        return $text;
    }

}

if (!function_exists('deleteDirectory')) {

    function deleteDirectory($dir) {
        if (!file_exists($dir)) {
            return true;
        }

        if (!is_dir($dir)) {
            return unlink($dir);
        }

        foreach (scandir($dir) as $item) {
            if ($item == '.' || $item == '..') {
                continue;
            }

            if (!deleteDirectory($dir . DIRECTORY_SEPARATOR . $item)) {
                return false;
            }
        }

        return rmdir($dir);
    }

}
if (!function_exists('showYearMonth')) {

    function showYearMonth($date) {
        return date("m-Y", strtotime($date));
    }

}

if (!function_exists('d')) {

    function d($data = '', $status = 0) {
        if (($data)) {
            echo '<pre>';
            print_r($data);
            '</pre>';
        }

        if ($status == 1)
            die;
    }

}

if (!function_exists('createfolder')) {

    function createfolder($dir) {
        if (!file_exists($dir)) {
            $d = File::makeDirectory($dir, 0777, true, true);
            return true;
        } else {
            return false;
        }
    }

}

if (!function_exists('createthumb')) {

    function createthumb($dir) {
        if (!file_exists($dir)) {
            $d = File::makeDirectory($dir . 'thumbnails', 0777, true, true);
            return true;
        } else {
            return false;
        }
    }

}

if (!function_exists('numberformat')) {

    function numberformat($number) {
        $number = number_format(floor($number * 100) / 100, 2, '.', '');
        return $number;
    }

}

if (!function_exists('randonString')) {

    function randonString($length = 16) {
        $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        return substr(str_shuffle(str_repeat($pool, 5)), 0, $length);
    }

}