1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @param int $red |
5
|
|
|
* @param int $green |
6
|
|
|
* @param int $blue |
7
|
|
|
* @return string |
8
|
|
|
* @see https://github.com/spatie-custom/blender/blob/master/app/Foundation/helpers.php |
9
|
|
|
*/ |
10
|
|
|
function rgb2hex(int $red, int $green, int $blue): string |
11
|
|
|
{ |
12
|
|
|
return '#' . collect([$red, $green, $blue]) |
13
|
|
|
->map(function (int $decimal): string { |
14
|
|
|
return str_pad(dechex($decimal), 2, STR_PAD_LEFT); |
15
|
|
|
}) |
16
|
|
|
->implode(''); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param float $val |
21
|
|
|
* @param int $precision |
22
|
|
|
* @param string $simbol |
23
|
|
|
* @return string |
24
|
|
|
*/ |
25
|
|
|
function format_money(float $val = 0, int $precision = 2, string $simbol = "") : string |
26
|
|
|
{ |
27
|
|
|
return "$simbol " . number_format($val, $precision, ',', '.'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Format float 1125.86 into string '&euro 1.125,86' |
32
|
|
|
* @param float $val |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
|
|
function format_euro(float $val = 0) : string |
36
|
|
|
{ |
37
|
|
|
return format_money($val, 2, '€ '); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Given a number, return the number + 'th' or 'rd' etc |
42
|
|
|
* @param $cdnl |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
function ordinal($cdnl) |
46
|
|
|
{ |
47
|
|
|
$test_c = abs($cdnl) % 10; |
48
|
|
|
$ext = ((abs($cdnl) % 100 < 21 && abs($cdnl) % 100 > 4) ? 'th' |
49
|
|
|
: (($test_c < 4) ? ($test_c < 3) ? ($test_c < 2) ? ($test_c < 1) |
50
|
|
|
? 'th' : 'st' : 'nd' : 'rd' : 'th')); |
51
|
|
|
return $cdnl . $ext; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (!function_exists('value')) { |
55
|
|
|
/** |
56
|
|
|
* Return the default value of the given value. |
57
|
|
|
* |
58
|
|
|
* @param mixed $value |
59
|
|
|
* @return mixed |
60
|
|
|
*/ |
61
|
|
|
function value($value) |
62
|
|
|
{ |
63
|
|
|
return $value instanceof Closure ? $value() : $value; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
if (!function_exists('with')) { |
67
|
|
|
/** |
68
|
|
|
* Return the given object. Useful for chaining. |
69
|
|
|
* |
70
|
|
|
* @param mixed $object |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
|
|
function with($object) |
74
|
|
|
{ |
75
|
|
|
return $object; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Set the default configuration of erro reporting for production. |
81
|
|
|
*/ |
82
|
|
|
function setErrorReportingForProduction() |
83
|
|
|
{ |
84
|
|
|
if (version_compare(PHP_VERSION, '5.4.0') >= 0) { |
85
|
|
|
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT); |
86
|
|
|
} elseif (version_compare(PHP_VERSION, '5.3.0') >= 0) { |
87
|
|
|
error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED); |
88
|
|
|
} else { |
89
|
|
|
error_reporting(E_ALL ^ E_NOTICE); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Check if PHP script was executed by shell. |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
|
|
function isExecutedByCLI() : bool |
98
|
|
|
{ |
99
|
|
|
return php_sapi_name() == 'cli'; |
100
|
|
|
} |
101
|
|
|
|