Issues (69)

Bludata/Helpers/functions.php (3 issues)

1
<?php
2
3
/**
4
 * Get Enviorment variable.
5
 */
6
if (!function_exists('env')) {
7
    /**
8
     * @param string $key
9
     */
10
    function env($key, $defaultValue = '')
11
    {
12
        $env = getenv($key);
13
        if (!$env) {
14
            return $defaultValue;
15
        }
16
17
        return $env;
18
    }
19
}
20
21
if (!function_exists('onlyNumbers')) {
22
    /**
23
     * @param string $input
24
     */
25
    function onlyNumbers(string $input)
26
    {
27
        return preg_replace('/\D/i', '', $input);
28
    }
29
}
30
31
if (!function_exists('parseDate')) {
32
    /**
33
     * @param mixed  $date
34
     * @param string $from = 'Y-m-d'
35
     * @param string $to   = 'obj'
36
     */
37
    function parseDate($date, string $from = 'Y-m-d', string $to = 'obj')
38
    {
39
        if ($date instanceof DateTime && $to === 'obj') {
40
            return $date;
41
        }
42
43
        if ($date instanceof DateTime) {
44
            return $date->format($to);
45
        }
46
47
        $dateObject = DateTime::createFromFormat($from, $date);
48
49
        if ($to === 'obj') {
50
            return $dateObject;
51
        }
52
53
        if ($formattedDate = $dateObject->format($to)) {
54
            return $formattedDate;
55
        }
56
57
        throw new \InvalidArgumentException(
58
            sprintf(
59
                'Não foi possível converter a data "%s" do formato "%s" para o formato "%s"',
60
                $date,
61
                $from,
62
                $to
63
            )
64
        );
65
    }
66
}
67
68
/*
69
 * Dump and die.
70
 */
71
if (!function_exists('dd')) {
72
    function dd()
73
    {
74
        $args = func_get_args();
75
        foreach ($args as $arg) {
76
            var_dump($arg);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($arg) looks like debug code. Are you sure you do not want to remove it?
Loading history...
77
        }
78
        exit();
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
79
    }
80
}
81
82
/*
83
 * Print and die
84
 */
85
if (!function_exists('pd')) {
86
    function pd()
87
    {
88
        $args = func_get_args();
89
        foreach ($args as $arg) {
90
            print_r($arg);
91
        }
92
        exit();
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
93
    }
94
}
95
96
/*
97
 * Dump and return
98
 */
99
if (!function_exists('dr')) {
100
    function dr()
101
    {
102
        $args = func_get_args();
103
        $result = '';
104
        foreach ($args as $arg) {
105
            $result = '';
106
            $type = gettype($arg);
107
            if ($type == 'object') {
108
                $type = get_class($arg);
109
            }
110
            if ($type == 'boolean') {
111
                $result = $arg ? 'true' : 'false';
112
            } else {
113
                $result = print_r($arg, true);
114
            }
115
            $result = sprintf('(%s) %s', $type, $result);
116
        }
117
118
        return $result;
119
    }
120
}
121