1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Arquivo de funções |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @package NwLaravel |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
use \Carbon\Carbon; |
11
|
|
|
use \Illuminate\Database\Eloquent\Model; |
12
|
|
|
use \Illuminate\Support\Facades\DB; |
13
|
|
|
|
14
|
|
|
if (! function_exists('arrayFilterClean')) { |
15
|
|
|
|
16
|
|
|
function arrayFilterClean(array $input) |
17
|
|
|
{ |
18
|
|
|
return array_filter($input, function ($value) { |
19
|
1 |
|
return (!empty($value) || $value == "0"); |
20
|
1 |
|
}); |
21
|
|
|
} |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
if (! function_exists('asCurrency')) { |
25
|
|
|
/** |
26
|
|
|
* Return a timestamp as DateTime object. |
27
|
|
|
* |
28
|
|
|
* @param mixed $value Mixed Value |
29
|
|
|
* |
30
|
|
|
* @return Carbon |
31
|
|
|
*/ |
32
|
|
|
function asCurrency($value) |
33
|
|
|
{ |
34
|
|
|
// Numeric Pt |
35
|
41 |
|
$matchPt = (bool) preg_match('/^[0-9]{1,3}(\.[0-9]{3})*(\,[0-9]+)?$/', $value); |
36
|
41 |
|
$matchNumericPt = (bool) preg_match('/^[0-9]+(\,[0-9]+)$/', $value); |
37
|
41 |
|
if ($matchPt || $matchNumericPt) { |
38
|
15 |
|
$value = str_replace('.', '', $value); |
39
|
15 |
|
$value = str_replace(',', '.', $value); |
40
|
15 |
|
} |
41
|
|
|
|
42
|
41 |
|
$matchEn = (bool) preg_match('/^[0-9]{1,3}(,[0-9]{3})*(\.[0-9]+)?$/', $value); |
43
|
41 |
|
if ($matchEn) { |
44
|
17 |
|
$value = str_replace(',', '', $value); |
45
|
17 |
|
} |
46
|
|
|
|
47
|
41 |
|
$matchNumeric = (bool) preg_match('/^[0-9]+(\.[0-9]+)?$/', $value); |
48
|
41 |
|
if ($matchNumeric) { |
49
|
27 |
|
return doubleval($value); |
50
|
|
|
} |
51
|
|
|
|
52
|
15 |
|
return null; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (! function_exists('asDateTime')) { |
57
|
|
|
/** |
58
|
|
|
* Return a timestamp as DateTime object. |
59
|
|
|
* |
60
|
|
|
* @param mixed $value Mixed Value |
61
|
|
|
* |
62
|
|
|
* @return Carbon |
63
|
|
|
*/ |
64
|
|
|
function asDateTime($value) |
65
|
|
|
{ |
66
|
17 |
|
if (empty($value)) { |
67
|
2 |
|
return null; |
68
|
|
|
} |
69
|
|
|
|
70
|
16 |
|
if ($value instanceof DateTime) { |
71
|
5 |
|
return Carbon::instance($value); |
72
|
|
|
} |
73
|
|
|
|
74
|
12 |
|
if (is_numeric($value)) { |
75
|
1 |
|
return Carbon::createFromTimestamp($value); |
76
|
|
|
} |
77
|
|
|
|
78
|
12 |
|
if (is_string($value) && preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', $value)) { |
79
|
3 |
|
return Carbon::createFromFormat('Y-m-d', $value)->startOfDay(); |
80
|
|
|
} |
81
|
|
|
|
82
|
10 |
|
if (is_string($value)) { |
83
|
8 |
|
$formatDB = 'Y-m-d H:i:s'; |
84
|
8 |
|
$dateFormat = config('nwlaravel.date_format'); |
85
|
8 |
|
$formats = array_merge([$formatDB, $dateFormat], explode(" ", $dateFormat)); |
86
|
8 |
|
foreach ($formats as $format) { |
87
|
8 |
|
$date = date_parse_from_format($format, $value); |
88
|
8 |
|
if ($date['error_count'] == 0 && $date['warning_count'] == 0) { |
89
|
8 |
|
$value = Carbon::createFromFormat($format, $value); |
90
|
8 |
|
if ($date['hour'] === false) { |
91
|
7 |
|
$value->startOfDay(); |
92
|
7 |
|
} |
93
|
8 |
|
return $value; |
94
|
|
|
} |
95
|
7 |
|
} |
96
|
|
|
|
97
|
3 |
|
if (strtotime($value) !== false) { |
98
|
1 |
|
return (new Carbon($value)); |
99
|
|
|
} |
100
|
3 |
|
} |
101
|
|
|
|
102
|
5 |
|
return null; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (! function_exists('fromDateTime')) { |
107
|
|
|
/** |
108
|
|
|
* Convert a DateTime to a storable string. |
109
|
|
|
* |
110
|
|
|
* @param mixed $value Mixed Value |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
function fromDateTime($value) |
115
|
|
|
{ |
116
|
2 |
|
$formatDB = 'Y-m-d H:i:s'; |
117
|
|
|
|
118
|
2 |
|
if (is_numeric($value)) { |
119
|
1 |
|
$value = Carbon::createFromTimestamp($value); |
120
|
|
|
|
121
|
2 |
|
} elseif (is_string($value) && preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', $value)) { |
122
|
1 |
|
$value = Carbon::createFromFormat('Y-m-d', $value)->startOfDay(); |
123
|
|
|
|
124
|
2 |
|
} elseif (is_string($value)) { |
125
|
2 |
|
$dateFormat = config('nwlaravel.date_format'); |
126
|
2 |
|
$formats = array_merge([$dateFormat], explode(" ", $dateFormat)); |
127
|
2 |
|
$formats[] = $formatDB; |
128
|
2 |
|
foreach ($formats as $format) { |
129
|
2 |
|
$date = date_parse_from_format($format, $value); |
130
|
2 |
|
if ($date['error_count'] == 0 && $date['warning_count'] == 0) { |
131
|
2 |
|
$value = Carbon::createFromFormat($format, $value); |
132
|
2 |
|
if ($date['hour'] === false) { |
133
|
2 |
|
$value->startOfDay(); |
134
|
2 |
|
} |
135
|
2 |
|
break; |
136
|
|
|
} |
137
|
2 |
|
} |
138
|
|
|
|
139
|
2 |
|
if (strtotime($value) !== false) { |
140
|
2 |
|
$value = new Carbon($value); |
|
|
|
|
141
|
2 |
|
} |
142
|
2 |
|
} |
143
|
|
|
|
144
|
2 |
|
if ($value instanceof DateTime) { |
145
|
2 |
|
return $value->format($formatDB); |
146
|
|
|
} |
147
|
|
|
|
148
|
2 |
|
return null; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if (! function_exists('toFixed')) { |
153
|
|
|
/** |
154
|
|
|
* To Fixed |
155
|
|
|
* |
156
|
|
|
* @param int $number Integer Number |
157
|
|
|
* @param int $decimal Float Decimal |
158
|
|
|
* |
159
|
|
|
* @return float |
160
|
|
|
*/ |
161
|
|
|
function toFixed($number, $decimal = 0) |
162
|
|
|
{ |
163
|
21 |
|
$number = strval($number); |
164
|
21 |
|
$pos = strpos($number.'', "."); |
165
|
|
|
|
166
|
21 |
|
if ($pos > 0) { |
167
|
13 |
|
$int_str = substr($number, 0, $pos); |
168
|
13 |
|
$dec_str = substr($number, $pos+1); |
169
|
13 |
|
if (strlen($dec_str)>$decimal) { |
170
|
11 |
|
return floatval($int_str.($decimal>0?'.':'').substr($dec_str, 0, $decimal)); |
171
|
|
|
} else { |
172
|
3 |
|
return floatval($number); |
173
|
|
|
} |
174
|
|
|
} else { |
175
|
9 |
|
return floatval($number); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
if (! function_exists('numberHumans')) { |
181
|
|
|
/** |
182
|
|
|
* Number Humans |
183
|
|
|
* |
184
|
|
|
* @param float $number Number |
185
|
|
|
* |
186
|
|
|
* @return string |
187
|
|
|
*/ |
188
|
|
|
function numberHumans($number) |
189
|
|
|
{ |
190
|
1 |
|
$sufix = ''; |
191
|
1 |
|
if ($number >= 1000) { |
192
|
1 |
|
$number = $number / 1000; |
193
|
1 |
|
$sufix = 'K'; |
194
|
1 |
|
} |
195
|
|
|
|
196
|
1 |
|
if ($number >= 1000) { |
197
|
1 |
|
$number = $number / 1000; |
198
|
1 |
|
$sufix = 'M'; |
199
|
1 |
|
} |
200
|
|
|
|
201
|
1 |
|
if ($number >= 1000) { |
202
|
1 |
|
$number = $number / 1000; |
203
|
1 |
|
$sufix = 'B'; |
204
|
1 |
|
} |
205
|
|
|
|
206
|
1 |
|
if ($number >= 1000) { |
207
|
1 |
|
$number = $number / 1000; |
208
|
1 |
|
$sufix = 'T'; |
209
|
1 |
|
} |
210
|
|
|
|
211
|
1 |
|
return toFixed($number, 1).$sufix; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
if (! function_exists('storageFormat')) { |
216
|
|
|
/** |
217
|
|
|
* Storage Format |
218
|
|
|
* |
219
|
|
|
* @param float $storage Integer Storage |
220
|
|
|
* @param string $nivel Nivel Storage |
221
|
|
|
* |
222
|
|
|
* @return string |
223
|
|
|
*/ |
224
|
|
|
function storageFormat($storage, $nivel = null) |
225
|
|
|
{ |
226
|
22 |
|
$storage = trim($storage); |
227
|
22 |
|
if (!is_numeric($storage)) { |
228
|
2 |
|
return $storage; |
229
|
|
|
} |
230
|
|
|
|
231
|
20 |
|
$sizes = ['KB' => 1, 'MB' => 2, 'GB' => 3, 'TB' => 4, 'PB' => 5]; |
232
|
|
|
|
233
|
20 |
|
if (!is_null($nivel) && array_key_exists(strtoupper($nivel), $sizes)) { |
234
|
5 |
|
$multi = 1024*pow(1000, $sizes[strtoupper($nivel)]); |
235
|
5 |
|
$storage = $storage * $multi; |
236
|
5 |
|
if ($storage >= $multi) { |
237
|
5 |
|
$storage = $storage / 1024; |
238
|
5 |
|
} |
239
|
5 |
|
} |
240
|
|
|
|
241
|
20 |
|
$sufix = 'B'; |
242
|
20 |
|
foreach (array_keys($sizes) as $size) { |
243
|
20 |
|
if ($storage >= 1000) { |
244
|
18 |
|
$storage = $storage / 1000; |
245
|
18 |
|
$sufix = $size; |
246
|
18 |
|
} |
247
|
20 |
|
} |
248
|
|
|
|
249
|
20 |
|
return toFixed($storage, 1).$sufix; |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
if (! function_exists('dateFormatter')) { |
254
|
|
|
/** |
255
|
|
|
* Date Formatter |
256
|
|
|
* |
257
|
|
|
* @param DateTime $date Date Time |
258
|
|
|
* @param string $dateType String Date Type |
259
|
|
|
* @param string $timeType String Time Type |
260
|
|
|
* @param string $pattern String Pattern |
261
|
|
|
* |
262
|
|
|
* @return string |
263
|
|
|
*/ |
264
|
|
|
function dateFormatter($date, $dateType, $timeType, $pattern = "") |
265
|
|
|
{ |
266
|
2 |
|
if ($date instanceof \DateTime) { |
267
|
2 |
|
$fmt = new \IntlDateFormatter( |
268
|
2 |
|
config('app.locale'), |
269
|
2 |
|
$dateType, |
270
|
2 |
|
\IntlDateFormatter::NONE, |
271
|
2 |
|
config('app.timezone'), |
272
|
2 |
|
\IntlDateFormatter::GREGORIAN, |
273
|
|
|
$pattern |
274
|
2 |
|
); |
275
|
|
|
|
276
|
2 |
|
if (empty($pattern) && $dateType == \IntlDateFormatter::SHORT) { |
277
|
1 |
|
$fmt->setPattern(preg_replace("/y+/", "yyyy", $fmt->getPattern())); |
278
|
1 |
|
} |
279
|
|
|
|
280
|
2 |
|
$strDate = ($dateType != \IntlDateFormatter::NONE) ? $fmt->format($date) : ''; |
281
|
2 |
|
$strTime = ''; |
282
|
|
|
|
283
|
|
|
switch ($timeType) { |
284
|
2 |
|
case \IntlDateFormatter::SHORT: |
285
|
1 |
|
$strTime = $date->format('H:i'); |
286
|
1 |
|
break; |
287
|
2 |
|
case \IntlDateFormatter::MEDIUM: |
288
|
1 |
|
$strTime = $date->format('H:i:s'); |
289
|
1 |
|
break; |
290
|
|
|
} |
291
|
|
|
|
292
|
2 |
|
return trim(sprintf('%s %s', $strDate, $strTime)); |
293
|
|
|
} |
294
|
|
|
|
295
|
1 |
|
return $date; |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
if (! function_exists('formatPattern')) { |
300
|
|
|
/** |
301
|
|
|
* Format Pattern |
302
|
|
|
* |
303
|
|
|
* @param DateTime $date Date Time |
304
|
|
|
* @param string $pattern String Pattern |
305
|
|
|
* |
306
|
|
|
* @return string |
307
|
|
|
*/ |
308
|
|
|
function formatPattern($date, $pattern) |
309
|
|
|
{ |
310
|
|
|
if ($date instanceof \DateTime) { |
311
|
|
|
$fmt = new \IntlDateFormatter( |
312
|
|
|
config('app.locale'), |
313
|
|
|
IntlDateFormatter::NONE, |
314
|
|
|
IntlDateFormatter::NONE, |
315
|
|
|
config('app.timezone'), |
316
|
|
|
\IntlDateFormatter::GREGORIAN, |
317
|
|
|
$pattern |
318
|
|
|
); |
319
|
|
|
|
320
|
|
|
return $fmt->format($date); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
return ""; |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
if (! function_exists('nameWeek')) { |
328
|
|
|
/** |
329
|
|
|
* Return Name Week |
330
|
|
|
* |
331
|
|
|
* @param DateTime $date Date Time |
332
|
|
|
* |
333
|
|
|
* @return string |
334
|
|
|
* @example : Domingo |
335
|
|
|
*/ |
336
|
|
|
function nameWeek($date) |
337
|
|
|
{ |
338
|
|
|
return formatPattern($date, "EEEE"); |
339
|
|
|
} |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
if (! function_exists('formatDateTime')) { |
343
|
|
|
/** |
344
|
|
|
* Format Date Time |
345
|
|
|
* |
346
|
|
|
* @param DateTime $date Date Time |
347
|
|
|
* |
348
|
|
|
* @return string |
349
|
|
|
* @example : DD/MM/YYYY HH:MIN:SS |
350
|
|
|
*/ |
351
|
|
|
function formatDateTime($date) |
352
|
|
|
{ |
353
|
1 |
|
return dateFormatter($date, \IntlDateFormatter::SHORT, \IntlDateFormatter::MEDIUM); |
354
|
|
|
} |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
if (! function_exists('formatTimeShort')) { |
358
|
|
|
/** |
359
|
|
|
* Format Time Short |
360
|
|
|
* |
361
|
|
|
* @param DateTime $date Date Time |
362
|
|
|
* |
363
|
|
|
* @return string |
364
|
|
|
* @example : HH:MIN |
365
|
|
|
*/ |
366
|
|
|
function formatTimeShort($date) |
367
|
|
|
{ |
368
|
1 |
|
return dateFormatter($date, \IntlDateFormatter::NONE, \IntlDateFormatter::SHORT); |
369
|
|
|
} |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
if (! function_exists('formatDate')) { |
373
|
|
|
/** |
374
|
|
|
* Format Date |
375
|
|
|
* |
376
|
|
|
* @param DateTime $date Date Time |
377
|
|
|
* |
378
|
|
|
* @return string |
379
|
|
|
* @example : DD/MM/YYYY |
380
|
|
|
*/ |
381
|
|
|
function formatDate($date) |
382
|
|
|
{ |
383
|
1 |
|
return dateFormatter($date, \IntlDateFormatter::SHORT, \IntlDateFormatter::NONE); |
384
|
|
|
} |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
if (! function_exists('formatTime')) { |
388
|
|
|
/** |
389
|
|
|
* Format Time |
390
|
|
|
* |
391
|
|
|
* @param DateTime $date Date Time |
392
|
|
|
* |
393
|
|
|
* @return string |
394
|
|
|
* @example : HH:MIN:SS |
395
|
|
|
*/ |
396
|
|
|
function formatTime($date) |
397
|
|
|
{ |
398
|
1 |
|
return dateFormatter($date, \IntlDateFormatter::NONE, \IntlDateFormatter::MEDIUM); |
399
|
|
|
} |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
if (! function_exists('formatDateLong')) { |
403
|
|
|
/** |
404
|
|
|
* Format Date Long |
405
|
|
|
* |
406
|
|
|
* @param DateTime $date Date Time |
407
|
|
|
* |
408
|
|
|
* @return string |
409
|
|
|
* @example : [DIA] de [MES] de [ANO] |
410
|
|
|
*/ |
411
|
|
|
function formatDateLong($date) |
412
|
|
|
{ |
413
|
1 |
|
return dateFormatter($date, \IntlDateFormatter::LONG, \IntlDateFormatter::NONE); |
414
|
|
|
} |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
if (! function_exists('formatDateTimeLong')) { |
418
|
|
|
/** |
419
|
|
|
* Format Date Time Long |
420
|
|
|
* |
421
|
|
|
* @param DateTime $date Date Time |
422
|
|
|
* |
423
|
|
|
* @return string |
424
|
|
|
* @example : [DIA] de [MES] de [ANO] - HH:MIN:SS |
425
|
|
|
*/ |
426
|
|
|
function formatDateTimeLong($date) |
427
|
|
|
{ |
428
|
1 |
|
if ($date instanceof \DateTime) { |
429
|
1 |
|
$date = sprintf('%s - %s', formatDateLong($date), formatTime($date)); |
430
|
1 |
|
} |
431
|
|
|
|
432
|
1 |
|
return $date; |
433
|
|
|
} |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
if (! function_exists('formatDateFull')) { |
437
|
|
|
/** |
438
|
|
|
* Format Date Full |
439
|
|
|
* |
440
|
|
|
* @param DateTime $date Date Time |
441
|
|
|
* |
442
|
|
|
* @return string |
443
|
|
|
* @example : [SEMANA], [DIA] de [MES] de [ANO] |
444
|
|
|
*/ |
445
|
|
|
function formatDateFull($date) |
446
|
|
|
{ |
447
|
1 |
|
return dateFormatter($date, \IntlDateFormatter::FULL, \IntlDateFormatter::NONE); |
448
|
|
|
} |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
if (! function_exists('formatDateTimeFull')) { |
452
|
|
|
/** |
453
|
|
|
* Format Date Time Full |
454
|
|
|
* |
455
|
|
|
* @param DateTime $date Date Time |
456
|
|
|
* |
457
|
|
|
* @return string |
458
|
|
|
* @example : [SEMANA], [DIA] de [MES] de [ANO] - HH:MIN:SS |
459
|
|
|
*/ |
460
|
|
|
function formatDateTimeFull($date) |
461
|
|
|
{ |
462
|
1 |
|
if ($date instanceof \DateTime) { |
463
|
1 |
|
return sprintf( |
464
|
1 |
|
'%s - %s', |
465
|
1 |
|
dateFormatter($date, \IntlDateFormatter::FULL, \IntlDateFormatter::NONE), |
466
|
1 |
|
dateFormatter($date, \IntlDateFormatter::NONE, \IntlDateFormatter::MEDIUM) |
467
|
1 |
|
); |
468
|
|
|
} |
469
|
|
|
|
470
|
1 |
|
return $date; |
471
|
|
|
} |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
if (! function_exists('formatDateTimeShort')) { |
475
|
|
|
/** |
476
|
|
|
* Format Date Time Short |
477
|
|
|
* |
478
|
|
|
* @param DateTime $date Date Time |
479
|
|
|
* |
480
|
|
|
* @return string |
481
|
|
|
* @example : DD/MM/YYYY HH:MIN |
482
|
|
|
*/ |
483
|
|
|
function formatDateTimeShort($date) |
484
|
|
|
{ |
485
|
1 |
|
return dateFormatter($date, \IntlDateFormatter::SHORT, \IntlDateFormatter::SHORT); |
486
|
|
|
} |
487
|
|
|
} |
488
|
|
|
|
489
|
|
|
if (! function_exists('currencySymbol')) { |
490
|
|
|
/** |
491
|
|
|
* Return Currency Symbol |
492
|
|
|
* |
493
|
|
|
* @return string |
494
|
|
|
*/ |
495
|
|
|
function currencySymbol() |
496
|
|
|
{ |
497
|
1 |
|
$fmt = new \NumberFormatter(config('app.locale'), \NumberFormatter::CURRENCY); |
498
|
1 |
|
return $fmt->getSymbol(\NumberFormatter::CURRENCY_SYMBOL); |
499
|
|
|
} |
500
|
|
|
} |
501
|
|
|
|
502
|
|
|
if (! function_exists('diffForHumans')) { |
503
|
|
|
/** |
504
|
|
|
* Diff date for Humans |
505
|
|
|
* |
506
|
|
|
* @param string|DataTime $date String\DateTime Date |
507
|
|
|
* @param string|DataTime $other String\DateTime Other |
508
|
|
|
* @param boolean $absolute Boolean Absolute |
509
|
|
|
* |
510
|
|
|
* @return string |
511
|
|
|
* @example : 7 minutos atras |
512
|
|
|
*/ |
513
|
|
|
function diffForHumans($date, $other = null, $absolute = false) |
514
|
|
|
{ |
515
|
1 |
|
if ($date instanceof \DateTime) { |
516
|
1 |
|
$date = Carbon::instance($date); |
517
|
1 |
|
if (!$other instanceof Carbon) { |
518
|
1 |
|
$other = null; |
519
|
1 |
|
} |
520
|
1 |
|
return $date->diffForHumans($other, $absolute); |
521
|
|
|
} |
522
|
|
|
|
523
|
1 |
|
return ''; |
524
|
|
|
} |
525
|
|
|
} |
526
|
|
|
|
527
|
|
|
if (! function_exists('now')) { |
528
|
|
|
/** |
529
|
|
|
* Now Date Time |
530
|
|
|
* |
531
|
|
|
* @return Carbon |
532
|
|
|
*/ |
533
|
|
|
function now() |
534
|
|
|
{ |
535
|
2 |
|
return Carbon::now(); |
536
|
|
|
} |
537
|
|
|
} |
538
|
|
|
|
539
|
|
|
if (! function_exists('formatCurrency')) { |
540
|
|
|
/** |
541
|
|
|
* Formato moeda conforme locale |
542
|
|
|
* |
543
|
|
|
* @param float $valor Float Valor |
544
|
|
|
* |
545
|
|
|
* @return string |
546
|
|
|
* @example : formatCurrency(8712.335) = R$8.712,34 |
547
|
|
|
*/ |
548
|
|
|
function formatCurrency($valor) |
549
|
|
|
{ |
550
|
|
|
$fmt = new \NumberFormatter(config('app.locale'), \NumberFormatter::CURRENCY); |
551
|
|
|
return $fmt->format(floatval($valor)); |
552
|
|
|
} |
553
|
|
|
} |
554
|
|
|
|
555
|
|
|
if (! function_exists('formatNumber')) { |
556
|
|
|
/** |
557
|
|
|
* Formato numero conforme locale |
558
|
|
|
* |
559
|
|
|
* @param float $valor |
560
|
|
|
* @param int $decimal |
561
|
|
|
* |
562
|
|
|
* @return string |
563
|
|
|
* @example : formatNumber(8712.335) = 8.712,34 |
564
|
|
|
*/ |
565
|
|
|
function formatNumber($valor, $decimal = 2) |
566
|
|
|
{ |
567
|
1 |
|
$valor = floatval($valor); |
568
|
1 |
|
$decimal = intval($decimal); |
569
|
|
|
|
570
|
1 |
|
$pattern = sprintf('#,##0.%s', str_pad('', $decimal, '0')); |
571
|
|
|
|
572
|
1 |
|
$fmt = new \NumberFormatter(config('app.locale'), \NumberFormatter::DECIMAL); |
573
|
1 |
|
$fmt->setPattern($pattern); |
574
|
1 |
|
return $fmt->format($valor); |
575
|
|
|
} |
576
|
|
|
} |
577
|
|
|
|
578
|
|
|
if (! function_exists('maskCep')) { |
579
|
|
|
/** |
580
|
|
|
* Cria a mascara do cep |
581
|
|
|
* |
582
|
|
|
* @param string $value |
583
|
|
|
* |
584
|
|
|
* @return string |
585
|
|
|
* @example : maskCep(12345678) = 12345-678 |
586
|
|
|
*/ |
587
|
|
|
function maskCep($value) |
588
|
|
|
{ |
589
|
1 |
|
$capture = '/^([0-9]{5})([0-9]{3})$/'; |
590
|
1 |
|
$format = '$1-$2'; |
591
|
1 |
|
$value = preg_replace('[^0-9]', '', $value); |
592
|
1 |
|
$result = preg_replace($capture, $format, $value); |
593
|
1 |
|
if (!is_null($result)) { |
594
|
1 |
|
$value = $result; |
595
|
1 |
|
} |
596
|
1 |
|
return $value; |
597
|
|
|
} |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
if (! function_exists('maskCpf')) { |
601
|
|
|
/** |
602
|
|
|
* Cria a mascara do cpf |
603
|
|
|
* |
604
|
|
|
* @param string $value |
605
|
|
|
* |
606
|
|
|
* @return string |
607
|
|
|
* @example : maskCpf(12345678901) = 123.456.789-01 |
608
|
|
|
*/ |
609
|
|
|
function maskCpf($value) |
610
|
|
|
{ |
611
|
2 |
|
$capture = '/^([0-9]{3})([0-9]{3})([0-9]{3})([0-9]{2})$/'; |
612
|
2 |
|
$format = '$1.$2.$3-$4'; |
613
|
2 |
|
$value = preg_replace('[^0-9]', '', $value); |
614
|
2 |
|
$result = preg_replace($capture, $format, $value); |
615
|
2 |
|
if (!is_null($result)) { |
616
|
2 |
|
$value = $result; |
617
|
2 |
|
} |
618
|
2 |
|
return $value; |
619
|
|
|
} |
620
|
|
|
} |
621
|
|
|
|
622
|
|
|
if (! function_exists('maskCnpj')) { |
623
|
|
|
/** |
624
|
|
|
* Cria a mascara do cnpj |
625
|
|
|
* |
626
|
|
|
* @param string $value |
627
|
|
|
* |
628
|
|
|
* @return string |
629
|
|
|
* @example : maskCpf(00123456/0001-78) = 00.123.456/0001-78 |
630
|
|
|
*/ |
631
|
|
|
function maskCnpj($value) |
632
|
|
|
{ |
633
|
2 |
|
$capture = '/^([0-9]{2,3})([0-9]{3})([0-9]{3})([0-9]{4})([0-9]{2})$/'; |
634
|
2 |
|
$format = '$1.$2.$3/$4-$5'; |
635
|
2 |
|
$value = preg_replace('[^0-9]', '', $value); |
636
|
2 |
|
$result = preg_replace($capture, $format, $value); |
637
|
2 |
|
if (!is_null($result)) { |
638
|
2 |
|
$value = $result; |
639
|
2 |
|
} |
640
|
2 |
|
return $value; |
641
|
|
|
} |
642
|
|
|
} |
643
|
|
|
|
644
|
|
|
if (! function_exists('maskCpfOrCnpj')) { |
645
|
|
|
/** |
646
|
|
|
* Cria a mascara do cpf ou cnpj |
647
|
|
|
* |
648
|
|
|
* @param string $value |
649
|
|
|
* |
650
|
|
|
* @return string |
651
|
|
|
*/ |
652
|
|
|
function maskCpfOrCnpj($value) |
653
|
|
|
{ |
654
|
1 |
|
$value = preg_replace('[^0-9]', '', $value); |
655
|
1 |
|
if (strlen($value)==11) { |
656
|
1 |
|
return maskCpf($value); |
657
|
|
|
} else { |
658
|
1 |
|
return maskCnpj($value); |
659
|
|
|
} |
660
|
|
|
} |
661
|
|
|
} |
662
|
|
|
|
663
|
|
|
if (! function_exists('numberRoman')) { |
664
|
|
|
/** |
665
|
|
|
* Number integer to Roman |
666
|
|
|
* |
667
|
|
|
* @param integer $integer |
668
|
|
|
* |
669
|
|
|
* @return string |
670
|
|
|
*/ |
671
|
|
|
function numberRoman($integer) |
672
|
|
|
{ |
673
|
|
|
$table = array( |
674
|
1 |
|
'M' =>1000, |
675
|
1 |
|
'CM' =>900, |
676
|
1 |
|
'D' =>500, |
677
|
1 |
|
'CD' =>400, |
678
|
1 |
|
'C' =>100, |
679
|
1 |
|
'XC' =>90, |
680
|
1 |
|
'L' =>50, |
681
|
1 |
|
'XL' =>40, |
682
|
1 |
|
'X' =>10, |
683
|
1 |
|
'IX' =>9, |
684
|
1 |
|
'V' =>5, |
685
|
1 |
|
'IV' =>4, |
686
|
|
|
'I' =>1 |
687
|
1 |
|
); |
688
|
|
|
|
689
|
1 |
|
if ($integer < 1 || $integer > 3999) { |
690
|
1 |
|
return $integer; |
691
|
|
|
} |
692
|
|
|
|
693
|
1 |
|
$return = ''; |
694
|
1 |
|
while ($integer > 0) { |
695
|
1 |
|
foreach ($table as $rom => $arb) { |
696
|
1 |
|
if ($integer >= $arb) { |
697
|
1 |
|
$integer -= $arb; |
698
|
1 |
|
$return .= $rom; |
699
|
1 |
|
break; |
700
|
|
|
} |
701
|
1 |
|
} |
702
|
1 |
|
} |
703
|
|
|
|
704
|
1 |
|
return $return; |
705
|
|
|
} |
706
|
|
|
} |
707
|
|
|
|
708
|
|
|
if (! function_exists('numberLetra')) { |
709
|
|
|
/** |
710
|
|
|
* Number to Letra |
711
|
|
|
* |
712
|
|
|
* @param integer $number |
713
|
|
|
* @param array|null $letras |
714
|
|
|
* @param integer $nivel |
715
|
|
|
* |
716
|
|
|
* @return string |
717
|
|
|
*/ |
718
|
|
|
function numberLetra($number, array $letras = null, $nivel = -1) |
719
|
|
|
{ |
720
|
1 |
|
$number = trim($number); |
721
|
1 |
|
if (preg_match('/[^0-9]/', $number)) { |
722
|
1 |
|
return $number; |
723
|
|
|
} |
724
|
|
|
|
725
|
1 |
|
$num = intval($number); |
726
|
|
|
|
727
|
|
|
$letrasOrig = array( |
728
|
1 |
|
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', |
729
|
1 |
|
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' |
730
|
1 |
|
); |
731
|
|
|
|
732
|
1 |
|
if (is_null($letras)) { |
733
|
1 |
|
$letras = $letrasOrig; |
734
|
1 |
|
} |
735
|
|
|
|
736
|
1 |
|
$nivel++; |
737
|
1 |
|
if ($num > count($letras) && array_key_exists($nivel, $letras)) { |
738
|
1 |
|
$letraParent = $letras[$nivel]; |
739
|
1 |
|
foreach ($letrasOrig as $value) { |
740
|
1 |
|
$letras[] = $letraParent.$value; |
741
|
1 |
|
} |
742
|
|
|
|
743
|
1 |
|
return numberLetra($num, $letras, $nivel); |
744
|
|
|
} |
745
|
|
|
|
746
|
1 |
|
$index = $num-1; |
747
|
1 |
|
if (array_key_exists($index, $letras)) { |
748
|
1 |
|
$return = $letras[$index]; |
749
|
1 |
|
} else { |
750
|
1 |
|
$return = $number; |
751
|
|
|
} |
752
|
|
|
|
753
|
1 |
|
return $return; |
754
|
|
|
} |
755
|
|
|
} |
756
|
|
|
|
757
|
|
|
if (! function_exists('activePattern')) { |
758
|
|
|
/** |
759
|
|
|
* Return class $active, para url pattern, |
760
|
|
|
* caso contrario $inactive |
761
|
|
|
* |
762
|
|
|
* @param string $path String Path |
763
|
|
|
* @param string $active String Active |
764
|
|
|
* @param string $inactive String Inactive |
765
|
|
|
* |
766
|
|
|
* @return string |
767
|
|
|
*/ |
768
|
|
|
function activePattern($path, $active = 'active', $inactive = '') |
769
|
|
|
{ |
770
|
1 |
|
$method = '\Illuminate\Support\Facades\Request::is'; |
771
|
1 |
|
return call_user_func_array($method, (array) $path) ? $active : $inactive; |
772
|
|
|
} |
773
|
|
|
} |
774
|
|
|
|
775
|
|
|
if (! function_exists('activeRoute')) { |
776
|
|
|
/** |
777
|
|
|
* Return class $active, para route currentRoute |
778
|
|
|
* caso contrario $inactive |
779
|
|
|
* |
780
|
|
|
* @param string $route String Route |
781
|
|
|
* @param string $active String Active |
782
|
|
|
* @param string $inactive String Inactive |
783
|
|
|
* |
784
|
|
|
* @return string |
785
|
|
|
*/ |
786
|
|
|
function activeRoute($route, $active = 'active', $inactive = '') |
787
|
|
|
{ |
788
|
4 |
|
$method = '\Illuminate\Support\Facades\Route::currentRouteName'; |
789
|
4 |
|
return call_user_func_array($method, [])==$route ? $active : $inactive; |
790
|
|
|
} |
791
|
|
|
} |
792
|
|
|
|
793
|
|
|
if (! function_exists('linkRoute')) { |
794
|
|
|
/** |
795
|
|
|
* Cria o link com currentRoute |
796
|
|
|
* |
797
|
|
|
* @param string $route String Route |
798
|
|
|
* @param string $label String Label |
799
|
|
|
* @param string $class String Class |
800
|
|
|
* |
801
|
|
|
* @return string |
802
|
|
|
*/ |
803
|
|
|
function linkRoute($route, $label, $class = '') |
804
|
|
|
{ |
805
|
3 |
|
return sprintf('<a href="%s" class="%s">%s</a>', route($route), $class?:activeRoute($route), $label); |
806
|
|
|
} |
807
|
|
|
} |
808
|
|
|
|
809
|
|
|
if (! function_exists('activity')) { |
810
|
|
|
/** |
811
|
|
|
* Log activity |
812
|
|
|
* |
813
|
|
|
* @param string $action |
814
|
|
|
* @param string $description |
815
|
|
|
* @param \Eloquent $model |
816
|
|
|
* |
817
|
|
|
* @return bool |
818
|
|
|
*/ |
819
|
|
|
function activity($action, $description, $model = null) |
820
|
|
|
{ |
821
|
1 |
|
return app('nwlaravel.activity')->log($action, $description, $model); |
822
|
|
|
} |
823
|
|
|
} |
824
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.