This repository has been archived by the owner on Mar 9, 2019. It is now read-only.
/
helpers.php
198 lines (162 loc) · 5.44 KB
/
helpers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
if (!function_exists('ebr')) {
function ebr($string)
{
$string = e(preg_replace('#<br\s*/?>#', "\n", $string));
return nl2br($string);
}
}
if (!function_exists('set_locale')) {
function set_locale($category, $locale = false)
{
return Clumsy\Utils\Facades\EnvironmentLocale::set($category, $locale);
}
}
if (!function_exists('get_possible_locales')) {
function get_possible_locales($locale)
{
return Clumsy\Utils\Facades\EnvironmentLocale::getPossibleLocales($locale);
}
}
if (!function_exists('floatAsInt')) {
function floatAsInt($number, $scale = 100)
{
return (int) round(bcmul($number, $scale, 2));
}
}
if (!function_exists('inCents')) {
function inCents($number)
{
return floatAsInt($number, 100);
}
}
if (!function_exists('n')) {
function n($number)
{
if (class_exists('NumberFormatter')) {
$formatter = new NumberFormatter(Clumsy\Utils\Facades\EnvironmentLocale::preferred(), NumberFormatter::DECIMAL);
return $formatter->format($number);
}
return number_format($number);
}
}
if (!function_exists('money')) {
function money($number)
{
if (class_exists('NumberFormatter')) {
$formatter = new NumberFormatter(Clumsy\Utils\Facades\EnvironmentLocale::preferred(), NumberFormatter::CURRENCY);
return $formatter->formatCurrency((int)$number, $formatter->getTextAttribute(NumberFormatter::CURRENCY_CODE));
}
extract(localeconv());
$space = $p_sep_by_space ? ' ' : '';
$n = n($number);
return $p_cs_precedes ? $currency_symbol.$space.$n : $n.$space.$currency_symbol;
}
}
if (!function_exists('pc')) {
function pc($number)
{
if (class_exists('NumberFormatter')) {
$formatter = new NumberFormatter(Clumsy\Utils\Facades\EnvironmentLocale::preferred(), NumberFormatter::PERCENT);
return $formatter->format($number);
}
return n(bcmul($number, 100)).'%';
}
}
if (!function_exists('display_date')) {
function display_date($date, $format)
{
return Clumsy\Utils\Facades\Date::format($date, $format);
}
}
/*
|--------------------------------------------------------------------------
| Array helpers
|--------------------------------------------------------------------------
|
*/
if (!function_exists('array_is_nested')) {
function array_is_nested($array)
{
return (bool)is_array($array) && is_array(current($array));
}
}
/*
|--------------------------------------------------------------------------
| String helpers
|--------------------------------------------------------------------------
|
*/
if (!function_exists('parseLinks')) {
function parseLinks($string)
{
// Links
$string = preg_replace("/([^\w\/])(www\.[a-z0-9\-]+\.[a-z0-9\-]+)/ui", "$1http://$2", $string);
$string = preg_replace("/([\w]+:\/\/[\w-_?&;#~%=\.\/\@]+[\w\/])/ui", "<a target=\"_blank\" href=\"$1\">$1</a>", $string);
// e-mail
$string = preg_replace("/([\w-_?&;#~%=\.\/]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?))/ui", "<a href=\"mailto:$1\">$1</a>", $string);
return $string;
}
}
if (!function_exists('parseTweet')) {
function parseTweet($string)
{
$string = parseLinks($string);
// Twitter users
$string = preg_replace("/@(\w+)/", "<a target=\"_blank\" href=\"https://twitter.com/$1\">@$1</a>", $string);
// Twitter hashtags
$string = preg_replace("/\s+#(\w+)/", "<a target=\"_blank\" href=\"https://twitter.com/hashtag/$1?src=hash\">#$1</a>", $string);
return $string;
}
}
/*
|--------------------------------------------------------------------------
| Grouped image
|--------------------------------------------------------------------------
|
| Ouput an image which belongs to a group of images. The advantage of this
| is having variables and events which know when all images of a certain
| group have been loaded (i.e. when it's safe to load a slider script to
| cycle through them all).
|
*/
if (!function_exists('groupedImage')) {
function groupedImage($src, $group = null, $alt = null, array $attributes = [], $secure = null)
{
app('clumsy.assets')->load('grouped-images-loader');
$attributes = array_merge(
[
'data-group' => $group,
'class' => 'grouped-image',
'onload' => 'groupedImageLoaded(this)',
],
$attributes
);
return app('html')->image($src, $alt, $attributes, $secure);
}
}
/*
|--------------------------------------------------------------------------
| Lazy loading image
|--------------------------------------------------------------------------
|
| Output an image tag which will load "lazily", complete with embedded
| 1x1 pixels transparent GIF
|
| Note: actual loading must be done by a script -- this is HTML only
|
*/
if (!function_exists('lazyLoad')) {
function lazyLoad($src, $alt = null, array $attributes = [])
{
$attributes = app('html')->attributes(array_merge(
array(
'data-src' => $src,
'alt' => $alt,
'class' => 'lazy-load',
),
$attributes
));
return '<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"'.$attributes.'>';
}
}