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