-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNum.php
1144 lines (886 loc) · 31.3 KB
/
Num.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
declare(strict_types=1);
/*
* This file is part of the QuidPHP package <https://quidphp.com>
* Author: Pierre-Philippe Emond <[email protected]>
* License: https://github.com/quidphp/base/blob/master/LICENSE
*/
namespace Quid\Base;
// num
// class with static methods to work with strings, ints and floats numbers
final class Num extends Root
{
// config
protected static array $config = [
'intMaxLength'=>11, // longueur maximale d'une int pour le cast
'alias'=>[ // alias de méthode pour math
'avg'=>'average',
'average'=>'average',
'min'=>'min',
'max'=>'max'],
'symbol'=>[ // symbol pour méthode math
'+'=>'addition',
'-'=>'subtraction',
'*'=>'multiplication',
'**'=>'pow',
'/'=>'division',
'%'=>'modulo',
'>'=>'max',
'<'=>'min']
];
// typecast
// typecasts des valeurs par référence, utilise castMore donc les float sont cast
final public static function typecast(&...$values):void
{
foreach ($values as &$value)
{
$value = self::castMoreOrNull($value);
}
}
// cast
// retourne la valeur dans le type (int ou float) si celle ci est numérique
// castFloat permet de remplacer la virgule par un décimal et de cast les float, castFloat est false par défaut car risqué
final public static function cast($value,bool $more=false,bool $commaToDecimal=false,bool $cleanDecimal=true)
{
$return = null;
if(is_int($value))
$return = $value;
elseif(is_scalar($value))
{
$return = $value;
$string = Str::cast($value);
// commaToDecimal
if($commaToDecimal === true)
$string = Str::commaToDecimal($string);
// cleanDecimal
if($cleanDecimal === true)
$string = Str::cleanDecimal($string);
if(self::isReallyNumeric($string))
{
$stringLength = strlen($string);
$float = (float) $string;
$int = (int) $float;
$hasDot = (strpos($string,'.') !== false);
// string si longueur trop longue
if($hasDot === false && $stringLength > self::$config['intMaxLength'])
$return = $string;
// si premier caractère est zéro, contient plus d'un caractère et extra est false
elseif($hasDot === false && $more === false && $stringLength > 1 && $string[0] === '0')
$return = $string;
// si valeur égale à int
elseif($int == $string)
$return = $int;
// si valeur égale à int
elseif($float == $string)
$return = ($more === true || is_float($value))? $float:$string;
}
}
return $return;
}
// castMore
// comme cast mais castFloat est true
final public static function castMore($value,bool $commaToDecimal=true,?bool $cleanDecimal=true)
{
return static::cast($value,true,$commaToDecimal,$cleanDecimal);
}
// castMoreOrNull
// comme cast mais retourne null si la valeur de retour n'est pas numérique
final public static function castMoreOrNull($value,bool $commaToDecimal=true,?bool $cleanDecimal=true)
{
$return = self::castMore($value,$commaToDecimal,$cleanDecimal);
return (is_numeric($return))? $return:null;
}
// is
// retourne vrai si la valeur est numerique
final public static function is($value):bool
{
return is_numeric($value);
}
// isEmpty
// retourne vrai si la valeur est numerique et vide
final public static function isEmpty($value):bool
{
return is_numeric($value) && empty($value);
}
// isNotEmpty
// retourne vrai si la valeur est numerique et non vide
final public static function isNotEmpty($value):bool
{
return is_numeric($value) && !empty($value);
}
// isString
// retourne vrai si la valeur est numérique et string
final public static function isString($value):bool
{
return is_numeric($value) && is_string($value);
}
// isFinite
// retourne vrai si le nombre est fini
final public static function isFinite($value):bool
{
return is_numeric($value) && is_finite((float) $value);
}
// isInfinite
// retourne vrai si le nombre est infini
final public static function isInfinite($value):bool
{
return is_numeric($value) && is_infinite((float) $value);
}
// isNan
// retourne vrai si le nombre est nan
final public static function isNan($value):bool
{
return is_numeric($value) && is_nan((float) $value);
}
// isReallyNumeric
// retourne vrai si la valeur est réelement numérique, va retourner faux à quelque chose que comme 21E1
final public static function isReallyNumeric($value):bool
{
return is_numeric($value) && Str::keepNum((string) $value) === (string) $value;
}
// isPositive
// vérifie que la valeur est un chiffre positif
// si allowZero est true, retourne true si zero
final public static function isPositive($value,bool $allowZero=false):bool
{
$value = self::castMoreOrNull($value);
return $value !== null && ($value > 0 || ($allowZero === true && $value === 0));
}
// isNegative
// vérifie que la valeur est un chiffre négatif
// si allowZero est true, retourne true si zero
final public static function isNegative($value,bool $allowZero=false):bool
{
$value = self::castMoreOrNull($value);
return $value !== null && ($value < 0 || ($allowZero === true && $value === 0));
}
// isOdd
// vérifie que la valeur est un chiffre impair
final public static function isOdd($value):bool
{
$value = self::castMoreOrNull($value);
return is_int($value) && is_float($value / 2);
}
// isEven
// vérifie que la valeur est un chiffre pair
final public static function isEven($value):bool
{
$value = self::castMoreOrNull($value);
return is_int($value) && is_int($value / 2);
}
// isOverflow
// vérifie que la valeur est numérique et une string après cast
// peu signifé que le numéro est out of bounds (plus grand que php_int_max)
final public static function isOverflow($value):bool
{
$return = false;
if(is_numeric($value))
{
$value = self::castMore($value);
$return = is_string($value);
}
return $return;
}
// isLength
// retourne vrai si la length est celle spécifié
final public static function isLength(int $length,$value):bool
{
$value = self::castMoreOrNull($value);
return $value !== null && self::len($value) === $length;
}
// isMinLength
// retourne vrai si la length est plus grande ou égale que celle spécifié
final public static function isMinLength(int $length,$value):bool
{
$value = self::castMoreOrNull($value);
return $value !== null && self::len($value) >= $length;
}
// isMaxLength
// retourne vrai si la length est plus petite ou égale que celui spécifié
final public static function isMaxLength(int $length,$value):bool
{
$value = self::castMoreOrNull($value);
return $value !== null && self::len($value) <= $length;
}
// isCountable
// retourne vrai si la valeur est countable par la méthode count
// cette méthode est inutile en php 7.3
final public static function isCountable($value):bool
{
return is_countable($value);
}
// append
// ajoute des chaînes numériques une après l'autre
final public static function append(...$values)
{
$return = Str::append(...$values);
return self::castMoreOrNull($return);
}
// commaToDecimal
// transforme la virgule en decimal et cast
final public static function commaToDecimal($value)
{
$return = null;
if(is_scalar($value))
{
$return = Str::commaToDecimal((string) $value);
$return = self::castMoreOrNull($return);
}
return $return;
}
// len
// retourne la longeur de la chaîne numérique
final public static function len($value):?int
{
$value = self::castMoreOrNull($value);
return ($value !== null)? Str::len((string) $value):null;
}
// sub
// coupe un numéro avec un début et un length
final public static function sub(int $start,?int $length,$value)
{
$return = null;
$value = self::castMoreOrNull($value);
if($value !== null)
{
$return = Str::sub($start,$length,(string) $value);
$return = self::castMoreOrNull($return);
}
return $return;
}
// decimal
// similaire à round, mais n'arrondie rien (coupe seulement à la décimale spécifiée)
// de même, la valeur est retourne en string et non pas float
// possible de cleaner les decimal vide (0)
final public static function decimal($value,int $decimal=2,bool $clean=false):?string
{
$return = null;
$value = Floating::cast($value);
if($value !== null)
{
$return = number_format($value,$decimal,'.','');
if($clean === true)
$return = Str::cleanDecimal($return);
}
return $return;
}
// round
// arrondie un chiffre
final public static function round($value,int $round=0,int $mode=PHP_ROUND_HALF_UP)
{
$return = null;
$value = Floating::cast($value);
if($value !== null)
{
$return = round($value,$round,$mode);
$return = self::castMoreOrNull($return);
}
return $return;
}
// roundLower
// arrondie un chiffre
final public static function roundLower($value,int $round=0)
{
return self::round($value,$round,PHP_ROUND_HALF_DOWN);
}
// ceil
// amène le float au int plus grand
final public static function ceil($value):?int
{
$value = Floating::cast($value);
return ($value !== null)? (int) ceil($value):null;
}
// floor
// amène le float au int plus petit
final public static function floor($value):?int
{
$value = Floating::cast($value);
return ($value !== null)? (int) floor($value):null;
}
// positive
// retourne la version positive d'un chiffre
final public static function positive($value)
{
$value = self::castMoreOrNull($value);
return ($value !== null)? abs($value):null;
}
// negative
// retourne la version négative d'un chiffre
final public static function negative($value)
{
$value = self::castMoreOrNull($value);
return ($value !== null)? (abs($value) * -1):null;
}
// invert
// inverse un chiffre (négatif devient positif ou vice versa)
final public static function invert($value)
{
$return = null;
$value = self::castMoreOrNull($value);
if($value !== null)
{
$abs = abs($value);
$return = ($value < 0)? $abs:($abs * -1);
}
return $return;
}
// increment
// augment une valeur
final public static function increment($value,$amount=1)
{
$return = null;
self::typecast($value,$amount);
if($value !== null && $amount !== null)
$return = $value + $amount;
return $return;
}
// decrement
// réduit une valeur
final public static function decrement($value,$amount=1)
{
$return = null;
self::typecast($value,$amount);
if($value !== null && $amount !== null)
$return = $value - $amount;
return $return;
}
// in
// vérifie si un numéro est entre les valeurs incluses dans from et to
final public static function in($from,$value,$to,bool $inclusive=true):?bool
{
$return = null;
self::typecast($from,$value,$to);
if($value !== null && $from !== null && $to !== null)
{
if($inclusive === true)
$return = ($value >= $from && $value <= $to);
else
$return = ($value > $from && $value < $to);
}
return $return;
}
// inExclusive
// comme in, mais est inclusif
final public static function inExclusive($from,$value,$to):?bool
{
return self::in($from,$value,$to,false);
}
// pi
// retourne pi, peut être arrondi
final public static function pi(?int $round=null):float
{
$return = M_PI;
return (is_int($round))? self::round($return,$round):$return;
}
// math
// calcule mathématique entre plusieurs valeurs
// symbol sont + - * / avg et average
final public static function math(string $operation,array $values,?int $round=null)
{
$return = null;
if(!empty(self::$config['symbol'][$operation]))
$method = self::$config['symbol'][$operation];
elseif(!empty(self::$config['alias'][$operation]))
$method = self::$config['alias'][$operation];
if(is_string($method) && self::classHasMethod($method))
{
$values = array_values($values);
$return = self::$method(...$values);
if(is_numeric($round))
$return = self::round($return,$round);
}
return $return;
}
// mathCommon
// méthode commune utilisé par plusieurs fonctions de calculations
protected static function mathCommon(string $symbol,int $minArgs=1,...$args)
{
$return = null;
if(count($args) >= $minArgs)
{
$return = self::castMoreOrNull($args[0]);
if($return !== null)
{
unset($args[0]);
foreach ($args as $v)
{
$v = self::castMoreOrNull($v);
if($v !== null)
{
if($symbol === '+')
$return += $v;
elseif($symbol === '-')
$return -= $v;
elseif($symbol === '*')
$return *= $v;
elseif($symbol === '**')
$return **= $v;
elseif($symbol === '%')
$return %= $v;
elseif($symbol === '/')
{
if($v == 0)
{
$return = null;
break;
}
else
$return /= $v;
}
}
}
}
}
return $return;
}
// combine
// combine des tableaux en faisant une opération mathématique sur leurs valeurs numériques
// les valeurs non numériques ne sont pas conservés
final public static function combine(string $operation,array ...$values):array
{
$return = [];
foreach ($values as $key => $value)
{
foreach ($value as $k => $v)
{
if(is_numeric($v))
{
if(!array_key_exists($k,$return))
$return[$k] = $v;
else
$return[$k] = self::math($operation,[$return[$k],$v]);
}
}
}
return $return;
}
// addition
// performe une addition entre plusieurs valeurs
// peut y avoir une seule valeur
final public static function addition(...$args)
{
return self::mathCommon('+',1,...$args);
}
// subtraction
// performe une soustraction entre plusieurs valeurs
// peut y avoir une seule valeur
final public static function subtraction(...$args)
{
return self::mathCommon('-',1,...$args);
}
// multiplication
// performe une multiplication entre plusieurs valeurs
// doit au moins y avoir deux valeurs
final public static function multiplication(...$args)
{
return self::mathCommon('*',2,...$args);
}
// pow
// performe une expression exponentielle
// doit au moins y a voir deux valeurs
final public static function pow(...$args)
{
return self::mathCommon('**',2,...$args);
}
// division
// performe une division entre plusieurs valeurs
// doit au moins y avoir deux valeurs
final public static function division(...$args)
{
return self::mathCommon('/',2,...$args);
}
// modulo
// performe un calcul modulo, retourne le reste d'une division
// doit au moins y a voir deux valeurs
final public static function modulo(...$args)
{
return self::mathCommon('%',2,...$args);
}
// average
// performe une moyenne entre deux valeurs
// doit au moins y avoir deux valeurs
final public static function average(...$args)
{
$return = null;
$total = 0;
$count = 0;
if(count($args) > 1)
{
foreach ($args as $v)
{
$v = self::castMoreOrNull($v);
if($v !== null)
{
$total += $v;
$count++;
}
}
if($count > 0)
$return = $total / $count;
}
return $return;
}
// min
// retourne la valeur la plus petite
final public static function min(...$values)
{
self::typecast(...$values);
$values = Arr::filter($values,fn($value) => is_numeric($value));
return min(...$values);
}
// max
// retourne la valeur la plus grande
final public static function max(...$values)
{
self::typecast(...$values);
$values = Arr::filter($values,fn($value) => is_numeric($value));
return max(...$values);
}
// random
// génère un string random en utilisant mt_rand
final public static function random(?int $length=null,int $min=0,int $max=PHP_INT_MAX,bool $csprng=false):int
{
$return = 0;
if($csprng === true)
$return = Crypt::randomInt($length,$min,$max);
else
{
$rand = mt_rand($min,$max);
if(is_int($length))
$return = self::sub(0,$length,$rand);
}
return $return;
}
// zerofill
// retourne une string avec le int zérofill (à gauche)
final public static function zerofill(int $length,int $value):string
{
return Str::padLeft('0',$length,(string) $value);
}
// fromOctal
// transforme une chaîne octale en décimale
final public static function fromOctal($value):?int
{
$return = null;
if(is_numeric($value))
$return = octdec((string) $value);
return $return;
}
// toOctal
// transforme un chiffre en string octal
final public static function toOctal($value,$format=null)
{
$return = null;
$value = Integer::cast($value);
if($value !== null)
{
$return = decoct($value);
if(!empty($format))
{
if(is_int($format))
$return = substr($return,$format);
$return = (int) $return;
}
}
return $return;
}
// format
// format un numéro en string (via number_format)
// option decimal|separator|thousand
final public static function format($value,?string $lang=null,?array $option=null):?string
{
$return = null;
$value = self::castMoreOrNull($value);
$option = self::getFormat($lang,$option);
if(!empty($option) && $value !== null)
{
$return = $value;
if(!is_string($return))
$return = number_format($return,$option['decimal'],$option['separator'],$option['thousand']);
if(is_string($return) && !empty($option['output']) && is_string($option['output']))
$return = str_replace('%v%',$return,$option['output']);
}
return $return;
}
// formats
// permet de formater un tableau de valeur
// supporte format, percentFormat, moneyFormat, phoneFormat et sizeFormat
final public static function formats(string $type,array $return,?string $lang=null,?array $option=null):array
{
$method = self::formatsMethod($type);
if(is_string($method))
{
foreach ($return as $key => $value)
{
if($method === 'sizeFormat')
$return[$key] = self::sizeFormat($value,true,$lang,$option);
elseif($method === 'phoneFormat')
$return[$key] = self::phoneFormat($value,$lang,$option);
else
$return[$key] = self::$method($value,$lang,$option);
}
}
return $return;
}
// formatsMethod
// retourne la méthode à utiliser pour formats
final public static function formatsMethod(string $type):?string
{
$return = null;
if(in_array($type,['number','format'],true))
$return = 'format';
elseif(in_array($type,['%','percent','percentFormat'],true))
$return = 'percentFormat';
elseif(in_array($type,['$','money','moneyFormat'],true))
$return = 'moneyFormat';
elseif(in_array($type,['phone','phoneFormat'],true))
$return = 'phoneFormat';
elseif(in_array($type,['size','sizeFormat'],true))
$return = 'sizeFormat';
return $return;
}
// getFormat
// retourne le tableau de format pour la méthode format
final public static function getFormat(?string $lang=null,?array $option=null):array
{
return Arr::replace(Lang\En::getConfig('number/format'),Lang::numberFormat(null,$lang),$option);
}
// percentFormat
// formate un numéro, ajoute un pourcentage
final public static function percentFormat($value,?string $lang=null,?array $option=null)
{
return self::format($value,$lang,self::getPercentFormat($lang,$option));
}
// getPercentFormat
// retourne le tableau de format pour la méthode moneyFormat
final public static function getPercentFormat(?string $lang=null,?array $option=null):array
{
return Arr::replace(Lang\En::getConfig('number/percentFormat'),Lang::numberPercentFormat(null,$lang),$option);
}
// moneyFormat
// format un numéro en format monétaire (selon une langue défini dans static config)
// option decimal|separator|thousand
final public static function moneyFormat($value,?string $lang=null,?array $option=null):?string
{
return self::format($value,$lang,self::getMoneyFormat($lang,$option));
}
// getMoneyFormat
// retourne le tableau de format pour la méthode moneyFormat
final public static function getMoneyFormat(?string $lang=null,?array $option=null):array
{
return Arr::replace(Lang\En::getConfig('number/moneyFormat'),Lang::numberMoneyFormat(null,$lang),$option);
}
// phoneFormat
// format un numéro de téléphone
// ajoute de l'option areaDash, pour ajouter un tiret entre code régional et reste du numéro
final public static function phoneFormat($value,?string $lang=null,?array $option=null):?string
{
$return = null;
$option = self::getPhoneFormat($lang,$option);
if(is_scalar($value))
{
$value = (string) $value;
$value = Str::keepNumber($value);
$valueLength = strlen($value);
if($valueLength >= 10)
{
preg_match("/(\d{3})(\d{3})(\d{4})\.?(\d+)?/",$value,$match);
if(!empty($match[0]) && $match[0] === $value)
{
$return = '';
if($option['parenthesis'] === true)
$return .= '(';
$return .= $match[1];
if($option['parenthesis'] === true)
$return .= ') ';
elseif($option['areaDash'] === true)
$return .= '-';
else
$return .= ' ';
$return .= "{$match[2]}-{$match[3]}";
// extension
if(is_string($option['extension']) && !empty($match[4]))
{
$return .= ' ';
$return .= $option['extension'];
$return .= $match[4];
}
}
}
}
return $return;
}
// getPhoneFormat
// retourne le tableau de format pour la méthode phoneFormat
final public static function getPhoneFormat(?string $lang=null,?array $option=null):array
{
return Arrs::replace(Lang\En::getConfig('number/phoneFormat'),Lang::numberPhoneFormat(null,$lang),$option);
}
// sizeFormat
// transforme une entrée de taille numérique en bytes en une taille formatté
// si round est true, le niveau d'arrondissement est défini dans les config selon le niveau de taille
final public static function sizeFormat(float $size,$round=true,?string $lang=null,?array $option=null):string
{
$return = '';
$option = self::getSizeFormat($lang,$option);
$texts = $option['text'];
$rounds = $option['round'];
if(is_numeric($size) && $size >= 0 && !empty($option))
{
$log = log($size,1024);
$log = (int) floor($log);
$pow = pow(1024,$log);
if(array_key_exists($log,$texts) && is_string($texts[$log]))
{
$text = $texts[$log];
if($log === 0)
$text .= Str::s($size);
if($round === true && array_key_exists($log,$rounds) && is_int($rounds[$log]))
$round = $rounds[$log];
if(is_int($round))
$return = self::round(($size / $pow),$round);
$return = (string) $return;
$return .= ' '.$text;
}
}
return $return;
}
// getSizeFormat
// retourne le tableau de format pour la méthode sizeFormat
final public static function getSizeFormat(?string $lang=null,?array $option=null):array
{
return Arrs::replace(Lang\En::getConfig('number/sizeFormat'),Lang::numberSizeFormat(null,$lang),$option);
}
// fromSizeFormat
// retourne le nombre de byte à partir d'une string de size format
// supporte tous les formats décrits dans le tableau sizeFormat
// utilise le tableau de la langue courante ou le tableau original (anglais)
// si la string est m, alors considère que c'est mb
final public static function fromSizeFormat(string $value,?string $lang=null,?array $option=null):?int
{
$return = null;
$formatOriginal = Lang\En::getConfig('number/sizeFormat');
$format = self::getSizeFormat($lang,$option);
$alpha = Str::keepAlpha($value);
$alpha = strtolower($alpha);
$alpha = Str::stripEnd('s',$alpha,false);
$value = Str::toInt($value);
if(!empty($formatOriginal['text']) && strlen($alpha) && is_int($value))
{
if($alpha === 'm')
$key = 2;