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 |
|
$timeType, |
271
|
2 |
|
config('app.timezone'), |
272
|
2 |
|
\IntlDateFormatter::GREGORIAN, |
273
|
|
|
$pattern |
274
|
2 |
|
); |
275
|
|
|
|
276
|
2 |
|
return $fmt->format($date); |
277
|
|
|
} |
278
|
|
|
|
279
|
1 |
|
return $date; |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
if (! function_exists('formatPattern')) { |
284
|
|
|
/** |
285
|
|
|
* Format Pattern |
286
|
|
|
* |
287
|
|
|
* @param DateTime $date Date Time |
288
|
|
|
* @param string $pattern String Pattern |
289
|
|
|
* |
290
|
|
|
* @return string |
291
|
|
|
*/ |
292
|
|
|
function formatPattern($date, $pattern) |
293
|
|
|
{ |
294
|
|
|
if ($date instanceof \DateTime) { |
295
|
|
|
$fmt = new \IntlDateFormatter( |
296
|
|
|
config('app.locale'), |
297
|
|
|
IntlDateFormatter::NONE, |
298
|
|
|
IntlDateFormatter::NONE, |
299
|
|
|
config('app.timezone'), |
300
|
|
|
\IntlDateFormatter::GREGORIAN, |
301
|
|
|
$pattern |
302
|
|
|
); |
303
|
|
|
|
304
|
|
|
return $fmt->format($date); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
return ""; |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
if (! function_exists('nameWeek')) { |
312
|
|
|
/** |
313
|
|
|
* Return Name Week |
314
|
|
|
* |
315
|
|
|
* @param DateTime $date Date Time |
316
|
|
|
* |
317
|
|
|
* @return string |
318
|
|
|
* @example : Domingo |
319
|
|
|
*/ |
320
|
|
|
function nameWeek($date) |
321
|
|
|
{ |
322
|
|
|
return formatPattern($date, "EEEE"); |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
if (! function_exists('formatDateTime')) { |
327
|
|
|
/** |
328
|
|
|
* Format Date Time |
329
|
|
|
* |
330
|
|
|
* @param DateTime $date Date Time |
331
|
|
|
* |
332
|
|
|
* @return string |
333
|
|
|
* @example : DD/MM/YYYY HH:MIN:SS |
334
|
|
|
*/ |
335
|
|
|
function formatDateTime($date) |
336
|
|
|
{ |
337
|
1 |
|
return dateFormatter($date, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::MEDIUM); |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
if (! function_exists('formatTimeShort')) { |
342
|
|
|
/** |
343
|
|
|
* Format Time Short |
344
|
|
|
* |
345
|
|
|
* @param DateTime $date Date Time |
346
|
|
|
* |
347
|
|
|
* @return string |
348
|
|
|
* @example : HH:MIN |
349
|
|
|
*/ |
350
|
|
|
function formatTimeShort($date) |
351
|
|
|
{ |
352
|
1 |
|
return dateFormatter($date, \IntlDateFormatter::NONE, \IntlDateFormatter::SHORT); |
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
if (! function_exists('formatDate')) { |
357
|
|
|
/** |
358
|
|
|
* Format Date |
359
|
|
|
* |
360
|
|
|
* @param DateTime $date Date Time |
361
|
|
|
* |
362
|
|
|
* @return string |
363
|
|
|
* @example : DD/MM/YYYY |
364
|
|
|
*/ |
365
|
|
|
function formatDate($date) |
366
|
|
|
{ |
367
|
1 |
|
return dateFormatter($date, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::NONE); |
368
|
|
|
} |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
if (! function_exists('formatTime')) { |
372
|
|
|
/** |
373
|
|
|
* Format Time |
374
|
|
|
* |
375
|
|
|
* @param DateTime $date Date Time |
376
|
|
|
* |
377
|
|
|
* @return string |
378
|
|
|
* @example : HH:MIN:SS |
379
|
|
|
*/ |
380
|
|
|
function formatTime($date) |
381
|
|
|
{ |
382
|
1 |
|
return dateFormatter($date, \IntlDateFormatter::NONE, \IntlDateFormatter::MEDIUM); |
383
|
|
|
} |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
if (! function_exists('formatDateLong')) { |
387
|
|
|
/** |
388
|
|
|
* Format Date Long |
389
|
|
|
* |
390
|
|
|
* @param DateTime $date Date Time |
391
|
|
|
* |
392
|
|
|
* @return string |
393
|
|
|
* @example : [DIA] de [MES] de [ANO] |
394
|
|
|
*/ |
395
|
|
|
function formatDateLong($date) |
396
|
|
|
{ |
397
|
1 |
|
return dateFormatter($date, \IntlDateFormatter::LONG, \IntlDateFormatter::NONE); |
398
|
|
|
} |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
if (! function_exists('formatDateTimeLong')) { |
402
|
|
|
/** |
403
|
|
|
* Format Date Time Long |
404
|
|
|
* |
405
|
|
|
* @param DateTime $date Date Time |
406
|
|
|
* |
407
|
|
|
* @return string |
408
|
|
|
* @example : [DIA] de [MES] de [ANO] - HH:MIN:SS |
409
|
|
|
*/ |
410
|
|
|
function formatDateTimeLong($date) |
411
|
|
|
{ |
412
|
1 |
|
if ($date instanceof \DateTime) { |
413
|
1 |
|
$date = sprintf('%s - %s', formatDateLong($date), formatTime($date)); |
414
|
1 |
|
} |
415
|
|
|
|
416
|
1 |
|
return $date; |
417
|
|
|
} |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
if (! function_exists('formatDateFull')) { |
421
|
|
|
/** |
422
|
|
|
* Format Date Full |
423
|
|
|
* |
424
|
|
|
* @param DateTime $date Date Time |
425
|
|
|
* |
426
|
|
|
* @return string |
427
|
|
|
* @example : [SEMANA], [DIA] de [MES] de [ANO] |
428
|
|
|
*/ |
429
|
|
|
function formatDateFull($date) |
430
|
|
|
{ |
431
|
1 |
|
return dateFormatter($date, \IntlDateFormatter::FULL, \IntlDateFormatter::NONE); |
432
|
|
|
} |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
if (! function_exists('formatDateTimeFull')) { |
436
|
|
|
/** |
437
|
|
|
* Format Date Time Full |
438
|
|
|
* |
439
|
|
|
* @param DateTime $date Date Time |
440
|
|
|
* |
441
|
|
|
* @return string |
442
|
|
|
* @example : [SEMANA], [DIA] de [MES] de [ANO] - HH:MIN:SS |
443
|
|
|
*/ |
444
|
|
|
function formatDateTimeFull($date) |
445
|
|
|
{ |
446
|
1 |
|
if ($date instanceof \DateTime) { |
447
|
1 |
|
return sprintf( |
448
|
1 |
|
'%s - %s', |
449
|
1 |
|
dateFormatter($date, \IntlDateFormatter::FULL, \IntlDateFormatter::NONE), |
450
|
1 |
|
dateFormatter($date, \IntlDateFormatter::NONE, \IntlDateFormatter::MEDIUM) |
451
|
1 |
|
); |
452
|
|
|
} |
453
|
|
|
|
454
|
1 |
|
return $date; |
455
|
|
|
} |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
if (! function_exists('formatDateTimeShort')) { |
459
|
|
|
/** |
460
|
|
|
* Format Date Time Short |
461
|
|
|
* |
462
|
|
|
* @param DateTime $date Date Time |
463
|
|
|
* |
464
|
|
|
* @return string |
465
|
|
|
* @example : DD/MM/YYYY HH:MIN |
466
|
|
|
*/ |
467
|
|
|
function formatDateTimeShort($date) |
468
|
|
|
{ |
469
|
1 |
|
return dateFormatter($date, \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT); |
470
|
|
|
} |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
if (! function_exists('currencySymbol')) { |
474
|
|
|
/** |
475
|
|
|
* Return Currency Symbol |
476
|
|
|
* |
477
|
|
|
* @return string |
478
|
|
|
*/ |
479
|
|
|
function currencySymbol() |
480
|
|
|
{ |
481
|
1 |
|
$fmt = new \NumberFormatter(config('app.locale'), \NumberFormatter::CURRENCY); |
482
|
1 |
|
return $fmt->getSymbol(\NumberFormatter::CURRENCY_SYMBOL); |
483
|
|
|
} |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
if (! function_exists('diffForHumans')) { |
487
|
|
|
/** |
488
|
|
|
* Diff date for Humans |
489
|
|
|
* |
490
|
|
|
* @param string|DataTime $date String\DateTime Date |
491
|
|
|
* @param string|DataTime $other String\DateTime Other |
492
|
|
|
* @param boolean $absolute Boolean Absolute |
493
|
|
|
* |
494
|
|
|
* @return string |
495
|
|
|
* @example : 7 minutos atras |
496
|
|
|
*/ |
497
|
|
|
function diffForHumans($date, $other = null, $absolute = false) |
498
|
|
|
{ |
499
|
1 |
|
if ($date instanceof \DateTime) { |
500
|
1 |
|
$date = Carbon::instance($date); |
501
|
1 |
|
if (!$other instanceof Carbon) { |
502
|
1 |
|
$other = null; |
503
|
1 |
|
} |
504
|
1 |
|
return $date->diffForHumans($other, $absolute); |
505
|
|
|
} |
506
|
|
|
|
507
|
1 |
|
return ''; |
508
|
|
|
} |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
if (! function_exists('now')) { |
512
|
|
|
/** |
513
|
|
|
* Now Date Time |
514
|
|
|
* |
515
|
|
|
* @return Carbon |
516
|
|
|
*/ |
517
|
|
|
function now() |
518
|
|
|
{ |
519
|
2 |
|
return Carbon::now(); |
520
|
|
|
} |
521
|
|
|
} |
522
|
|
|
|
523
|
|
|
if (! function_exists('formatCurrency')) { |
524
|
|
|
/** |
525
|
|
|
* Formato moeda conforme locale |
526
|
|
|
* |
527
|
|
|
* @param float $valor Float Valor |
528
|
|
|
* |
529
|
|
|
* @return string |
530
|
|
|
* @example : formatCurrency(8712.335) = R$8.712,34 |
531
|
|
|
*/ |
532
|
|
|
function formatCurrency($valor) |
533
|
|
|
{ |
534
|
1 |
|
$fmt = new \NumberFormatter(config('app.locale'), \NumberFormatter::CURRENCY); |
535
|
1 |
|
return $fmt->format(floatval($valor)); |
536
|
|
|
} |
537
|
|
|
} |
538
|
|
|
|
539
|
|
|
if (! function_exists('formatNumber')) { |
540
|
|
|
/** |
541
|
|
|
* Formato numero conforme locale |
542
|
|
|
* |
543
|
|
|
* @param float $valor |
544
|
|
|
* @param int $decimal |
545
|
|
|
* |
546
|
|
|
* @return string |
547
|
|
|
* @example : formatNumber(8712.335) = 8.712,34 |
548
|
|
|
*/ |
549
|
|
|
function formatNumber($valor, $decimal = 2) |
550
|
|
|
{ |
551
|
1 |
|
$valor = floatval($valor); |
552
|
1 |
|
$decimal = intval($decimal); |
553
|
|
|
|
554
|
1 |
|
$pattern = sprintf('#,##0.%s', str_pad('', $decimal, '0')); |
555
|
|
|
|
556
|
1 |
|
$fmt = new \NumberFormatter(config('app.locale'), \NumberFormatter::DECIMAL); |
557
|
1 |
|
$fmt->setPattern($pattern); |
558
|
1 |
|
return $fmt->format($valor); |
559
|
|
|
} |
560
|
|
|
} |
561
|
|
|
|
562
|
|
|
if (! function_exists('maskCep')) { |
563
|
|
|
/** |
564
|
|
|
* Cria a mascara do cep |
565
|
|
|
* |
566
|
|
|
* @param string $value |
567
|
|
|
* |
568
|
|
|
* @return string |
569
|
|
|
* @example : maskCep(12345678) = 12345-678 |
570
|
|
|
*/ |
571
|
|
|
function maskCep($value) |
572
|
|
|
{ |
573
|
1 |
|
$capture = '/^([0-9]{5})([0-9]{3})$/'; |
574
|
1 |
|
$format = '$1-$2'; |
575
|
1 |
|
$value = preg_replace('[^0-9]', '', $value); |
576
|
1 |
|
$result = preg_replace($capture, $format, $value); |
577
|
1 |
|
if (!is_null($result)) { |
578
|
1 |
|
$value = $result; |
579
|
1 |
|
} |
580
|
1 |
|
return $value; |
581
|
|
|
} |
582
|
|
|
} |
583
|
|
|
|
584
|
|
|
if (! function_exists('maskCpf')) { |
585
|
|
|
/** |
586
|
|
|
* Cria a mascara do cpf |
587
|
|
|
* |
588
|
|
|
* @param string $value |
589
|
|
|
* |
590
|
|
|
* @return string |
591
|
|
|
* @example : maskCpf(12345678901) = 123.456.789-01 |
592
|
|
|
*/ |
593
|
|
|
function maskCpf($value) |
594
|
|
|
{ |
595
|
2 |
|
$capture = '/^([0-9]{3})([0-9]{3})([0-9]{3})([0-9]{2})$/'; |
596
|
2 |
|
$format = '$1.$2.$3-$4'; |
597
|
2 |
|
$value = preg_replace('[^0-9]', '', $value); |
598
|
2 |
|
$result = preg_replace($capture, $format, $value); |
599
|
2 |
|
if (!is_null($result)) { |
600
|
2 |
|
$value = $result; |
601
|
2 |
|
} |
602
|
2 |
|
return $value; |
603
|
|
|
} |
604
|
|
|
} |
605
|
|
|
|
606
|
|
|
if (! function_exists('maskCnpj')) { |
607
|
|
|
/** |
608
|
|
|
* Cria a mascara do cnpj |
609
|
|
|
* |
610
|
|
|
* @param string $value |
611
|
|
|
* |
612
|
|
|
* @return string |
613
|
|
|
* @example : maskCpf(00123456/0001-78) = 00.123.456/0001-78 |
614
|
|
|
*/ |
615
|
|
|
function maskCnpj($value) |
616
|
|
|
{ |
617
|
2 |
|
$capture = '/^([0-9]{2,3})([0-9]{3})([0-9]{3})([0-9]{4})([0-9]{2})$/'; |
618
|
2 |
|
$format = '$1.$2.$3/$4-$5'; |
619
|
2 |
|
$value = preg_replace('[^0-9]', '', $value); |
620
|
2 |
|
$result = preg_replace($capture, $format, $value); |
621
|
2 |
|
if (!is_null($result)) { |
622
|
2 |
|
$value = $result; |
623
|
2 |
|
} |
624
|
2 |
|
return $value; |
625
|
|
|
} |
626
|
|
|
} |
627
|
|
|
|
628
|
|
|
if (! function_exists('maskCpfOrCnpj')) { |
629
|
|
|
/** |
630
|
|
|
* Cria a mascara do cpf ou cnpj |
631
|
|
|
* |
632
|
|
|
* @param string $value |
633
|
|
|
* |
634
|
|
|
* @return string |
635
|
|
|
*/ |
636
|
|
|
function maskCpfOrCnpj($value) |
637
|
|
|
{ |
638
|
1 |
|
$value = preg_replace('[^0-9]', '', $value); |
639
|
1 |
|
if (strlen($value)==11) { |
640
|
1 |
|
return maskCpf($value); |
641
|
|
|
} else { |
642
|
1 |
|
return maskCnpj($value); |
643
|
|
|
} |
644
|
|
|
} |
645
|
|
|
} |
646
|
|
|
|
647
|
|
|
if (! function_exists('numberRoman')) { |
648
|
|
|
/** |
649
|
|
|
* Number integer to Roman |
650
|
|
|
* |
651
|
|
|
* @param integer $integer |
652
|
|
|
* |
653
|
|
|
* @return string |
654
|
|
|
*/ |
655
|
|
|
function numberRoman($integer) |
656
|
|
|
{ |
657
|
|
|
$table = array( |
658
|
1 |
|
'M' =>1000, |
659
|
1 |
|
'CM' =>900, |
660
|
1 |
|
'D' =>500, |
661
|
1 |
|
'CD' =>400, |
662
|
1 |
|
'C' =>100, |
663
|
1 |
|
'XC' =>90, |
664
|
1 |
|
'L' =>50, |
665
|
1 |
|
'XL' =>40, |
666
|
1 |
|
'X' =>10, |
667
|
1 |
|
'IX' =>9, |
668
|
1 |
|
'V' =>5, |
669
|
1 |
|
'IV' =>4, |
670
|
|
|
'I' =>1 |
671
|
1 |
|
); |
672
|
|
|
|
673
|
1 |
|
if ($integer < 1 || $integer > 3999) { |
674
|
1 |
|
return $integer; |
675
|
|
|
} |
676
|
|
|
|
677
|
1 |
|
$return = ''; |
678
|
1 |
|
while ($integer > 0) { |
679
|
1 |
|
foreach ($table as $rom => $arb) { |
680
|
1 |
|
if ($integer >= $arb) { |
681
|
1 |
|
$integer -= $arb; |
682
|
1 |
|
$return .= $rom; |
683
|
1 |
|
break; |
684
|
|
|
} |
685
|
1 |
|
} |
686
|
1 |
|
} |
687
|
|
|
|
688
|
1 |
|
return $return; |
689
|
|
|
} |
690
|
|
|
} |
691
|
|
|
|
692
|
|
|
if (! function_exists('numberLetra')) { |
693
|
|
|
/** |
694
|
|
|
* Number to Letra |
695
|
|
|
* |
696
|
|
|
* @param integer $number |
697
|
|
|
* @param array|null $letras |
698
|
|
|
* @param integer $nivel |
699
|
|
|
* |
700
|
|
|
* @return string |
701
|
|
|
*/ |
702
|
|
|
function numberLetra($number, array $letras = null, $nivel = -1) |
703
|
|
|
{ |
704
|
1 |
|
$number = trim($number); |
705
|
1 |
|
if (preg_match('/[^0-9]/', $number)) { |
706
|
1 |
|
return $number; |
707
|
|
|
} |
708
|
|
|
|
709
|
1 |
|
$num = intval($number); |
710
|
|
|
|
711
|
|
|
$letrasOrig = array( |
712
|
1 |
|
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', |
713
|
1 |
|
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' |
714
|
1 |
|
); |
715
|
|
|
|
716
|
1 |
|
if (is_null($letras)) { |
717
|
1 |
|
$letras = $letrasOrig; |
718
|
1 |
|
} |
719
|
|
|
|
720
|
1 |
|
$nivel++; |
721
|
1 |
|
if ($num > count($letras) && array_key_exists($nivel, $letras)) { |
722
|
1 |
|
$letraParent = $letras[$nivel]; |
723
|
1 |
|
foreach ($letrasOrig as $value) { |
724
|
1 |
|
$letras[] = $letraParent.$value; |
725
|
1 |
|
} |
726
|
|
|
|
727
|
1 |
|
return numberLetra($num, $letras, $nivel); |
728
|
|
|
} |
729
|
|
|
|
730
|
1 |
|
$index = $num-1; |
731
|
1 |
|
if (array_key_exists($index, $letras)) { |
732
|
1 |
|
$return = $letras[$index]; |
733
|
1 |
|
} else { |
734
|
1 |
|
$return = $number; |
735
|
|
|
} |
736
|
|
|
|
737
|
1 |
|
return $return; |
738
|
|
|
} |
739
|
|
|
} |
740
|
|
|
|
741
|
|
|
if (! function_exists('activePattern')) { |
742
|
|
|
/** |
743
|
|
|
* Return class $active, para url pattern, |
744
|
|
|
* caso contrario $inactive |
745
|
|
|
* |
746
|
|
|
* @param string $path String Path |
747
|
|
|
* @param string $active String Active |
748
|
|
|
* @param string $inactive String Inactive |
749
|
|
|
* |
750
|
|
|
* @return string |
751
|
|
|
*/ |
752
|
|
|
function activePattern($path, $active = 'active', $inactive = '') |
753
|
|
|
{ |
754
|
1 |
|
$method = '\Illuminate\Support\Facades\Request::is'; |
755
|
1 |
|
return call_user_func_array($method, (array) $path) ? $active : $inactive; |
756
|
|
|
} |
757
|
|
|
} |
758
|
|
|
|
759
|
|
|
if (! function_exists('activeRoute')) { |
760
|
|
|
/** |
761
|
|
|
* Return class $active, para route currentRoute |
762
|
|
|
* caso contrario $inactive |
763
|
|
|
* |
764
|
|
|
* @param string $route String Route |
765
|
|
|
* @param string $active String Active |
766
|
|
|
* @param string $inactive String Inactive |
767
|
|
|
* |
768
|
|
|
* @return string |
769
|
|
|
*/ |
770
|
|
|
function activeRoute($route, $active = 'active', $inactive = '') |
771
|
|
|
{ |
772
|
4 |
|
$method = '\Illuminate\Support\Facades\Route::currentRouteName'; |
773
|
4 |
|
return call_user_func_array($method, [])==$route ? $active : $inactive; |
774
|
|
|
} |
775
|
|
|
} |
776
|
|
|
|
777
|
|
|
if (! function_exists('linkRoute')) { |
778
|
|
|
/** |
779
|
|
|
* Cria o link com currentRoute |
780
|
|
|
* |
781
|
|
|
* @param string $route String Route |
782
|
|
|
* @param string $label String Label |
783
|
|
|
* @param string $class String Class |
784
|
|
|
* |
785
|
|
|
* @return string |
786
|
|
|
*/ |
787
|
|
|
function linkRoute($route, $label, $class = '') |
788
|
|
|
{ |
789
|
3 |
|
return sprintf('<a href="%s" class="%s">%s</a>', route($route), $class?:activeRoute($route), $label); |
790
|
|
|
} |
791
|
|
|
} |
792
|
|
|
|
793
|
|
|
if (! function_exists('activity')) { |
794
|
|
|
/** |
795
|
|
|
* Log activity |
796
|
|
|
* |
797
|
|
|
* @param string $action |
798
|
|
|
* @param string $description |
799
|
|
|
* @param \Eloquent $model |
800
|
|
|
* |
801
|
|
|
* @return bool |
802
|
|
|
*/ |
803
|
|
|
function activity($action, $description, $model = null) |
804
|
|
|
{ |
805
|
1 |
|
return app('nwlaravel.activity')->log($action, $description, $model); |
806
|
|
|
} |
807
|
|
|
} |
808
|
|
|
|
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.