77use Tamedevelopers \Support \Str ;
88use Tamedevelopers \Support \Server ;
99use Tamedevelopers \Support \Traits \TameTrait ;
10+ use Tamedevelopers \Support \Traits \NumberToWordsTraits ;
1011
1112/**
1213 * @see \Tamedevelopers\Support\Str
1516 */
1617class Tame {
1718
18- use TameTrait;
19+ use TameTrait,
20+ NumberToWordsTraits;
1921
2022 /**
2123 * Count
@@ -230,26 +232,34 @@ static public function requireOnce($path)
230232 /**
231233 * Convert Bytes to Units
232234 *
233- * @param float| int $bytes
234- * @param bool $format
235- * @param string|null $gb
236- * @param string|null $mb
237- * @param string|null $kb
235+ * @param int|float $bytes The size in bytes to be converted.
236+ * @param bool $format Whether to preserve case (default: lowercase).
237+ * @param string|null $gb Custom label for GB (default: 'GB').
238+ * @param string|null $mb Custom label for MB (default: 'MB').
239+ * @param string|null $kb Custom label for KB (default: 'KB').
238240 *
239241 * @return string
240242 */
241- static public function byteToUnit ($ bytes = 0 , $ format = true , $ gb = 'GB ' , $ mb = 'MB ' , $ kb = 'KB ' )
243+ static public function byteToUnit ($ bytes = 0 , $ format = false , $ gb = 'GB ' , $ mb = 'MB ' , $ kb = 'KB ' )
242244 {
243- $ bytes = (int ) $ bytes ;
244- if ($ bytes >= 1073741824 ){
245- $ bytes = round (($ bytes / 1073741824 )) . $ gb ;
246- } elseif ($ bytes >= 1048576 ){
247- $ bytes = round (($ bytes / 1048576 )) . $ mb ;
248- } elseif ($ bytes >= 1024 ){
249- $ bytes = round (($ bytes / 1024 )) . $ kb ;
245+ // Define byte thresholds.
246+ $ units = [
247+ 'GB ' => [1073741824 , $ gb ],
248+ 'MB ' => [1048576 , $ mb ],
249+ 'KB ' => [1024 , $ kb ],
250+ ];
251+
252+ if (!is_numeric ($ bytes ) || $ bytes < $ units ['KB ' ][0 ]) {
253+ return "{$ bytes } {$ kb }" ; // Handle invalid or negative input.
250254 }
251255
252- return $ format ? $ bytes : Str::lower ($ bytes );
256+ foreach ($ units as $ unit => [$ threshold , $ label ]) {
257+ if ($ bytes >= $ threshold ) {
258+ $ value = round ($ bytes / $ threshold ) . $ label ;
259+
260+ return $ format ? $ value : Str::lower ($ value );
261+ }
262+ }
253263 }
254264
255265 /**
@@ -417,7 +427,7 @@ static public function getBetweenBoxLengthAndWeightInKg($length = 0, $width = 0,
417427 {
418428 $ weight = (float ) $ weight ;
419429 $ dimensional_weight = self ::calculateVolumeWeight ($ length , $ width , $ height , $ format , $ decimal );
420- if ($ dimensional_weight > $ weight ){
430+ if ($ dimensional_weight >= $ weight ){
421431 return $ dimensional_weight ;
422432 }
423433
@@ -452,7 +462,7 @@ static public function getBetweenBoxLengthAndWeightInCMB($length = 0, $width = 0
452462 {
453463 $ weight = (float ) $ weight ;
454464 $ dimensional_weight = self ::calculateCubicMeterWeight ($ length , $ width , $ height , $ format , $ decimal );
455- if ($ dimensional_weight > $ weight ){
465+ if ($ dimensional_weight >= $ weight ){
456466 return $ dimensional_weight ;
457467 }
458468
@@ -497,26 +507,28 @@ static public function getBetweenDimensionalWeightAndWeightInCBM(mixed $dimensio
497507 }
498508
499509 /**
500- * Round to decimal point
510+ * Round a value to the nearest specified decimal.
501511 *
502- * @param mixed $value
503- * - float|int
504- *
505- * @param mixed $decimal
506- * - float|int|string
512+ * @param float|int|string $value The value to be rounded.
513+ * @param float|int|string $decimal The decimal value for rounding. Default is 0.5.
507514 *
508- * @return int
515+ * @return int|float
509516 */
510517 static public function roundToDecimal (mixed $ value = 0 , mixed $ decimal = 0.5 )
511518 {
519+ $ value = (float ) $ value ;
512520 $ decimal = (float ) $ decimal ;
513521
514- if ($ decimal == self ::COUNT ) $ decimal = 0.1 ;
515- if ($ value == self ::COUNT ) return $ value ;
522+ if ($ decimal == self ::COUNT ){
523+ $ decimal = 0.1 ;
524+ }
525+
526+ if ($ value == self ::COUNT ){
527+ return $ value ;
528+ }
516529
517- // Perform the rounding
530+ // Perform the rounding calculation
518531 $ result = ceil ($ value / $ decimal ) * $ decimal ;
519- // return round($value / $decimal, 1, PHP_ROUND_HALF_UP) * $decimal;
520532
521533 return round ($ result , 1 );
522534 }
@@ -571,20 +583,20 @@ static public function gramsToKg(float|int $weight = 0)
571583 /**
572584 * Calculation Percentage between numbers
573585 *
574- * @param float|int $total
586+ * @param float|int $number
575587 * @param float|int $newNumber
576588 * @return int
577589 */
578- static public function calPercentageBetweenNumbers (float |int $ total = 0 , float |int $ newNumber = 0 )
590+ static public function calPercentageBetweenNumbers (float |int $ number = 0 , float |int $ newNumber = 0 )
579591 {
580592 // default
581593 $ decreaseValue = self ::COUNT ;
582594
583- if ($ total > $ newNumber ){
584- $ decreaseValue = ($ newNumber / $ total ) * 100 ;
595+ if ($ number > $ newNumber ){
596+ $ decreaseValue = ($ newNumber / $ number ) * 100 ;
585597 } else {
586- if ($ total != self ::COUNT && $ newNumber != self ::COUNT ){
587- $ decreaseValue = ($ total / $ newNumber ) * 100 ;
598+ if ($ number != self ::COUNT && $ newNumber != self ::COUNT ){
599+ $ decreaseValue = ($ number / $ newNumber ) * 100 ;
588600 } elseif ($ newNumber != self ::COUNT ){
589601 $ decreaseValue = ($ newNumber * 100 ) / $ newNumber ;
590602 }
@@ -839,32 +851,29 @@ static public function filter_input($string = null)
839851
840852 /**
841853 * Format number to nearest thousand
842- * @link https://code.recuweb.com/2018/php-format-numbers-to-nearest-thousands/
843- *
844854 * @param float|int $number
845855 * @return void
846856 */
847857 static public function formatNumberToNearestThousand (float |int $ number = 0 )
848858 {
849- if ( $ number >= 1000 ) {
850- $ x = round ($ number );
851- $ x_number_format = number_format ($ x );
852- $ x_array = explode (', ' , $ x_number_format );
853-
854- //[t(trillion) - p(quadrillion) - e(quintillion) - z(sextillion) - y(septillion)]
855- $ x_parts = array ('k ' , 'm ' , 'b ' , 't ' , 'p ' , 'e ' , 'z ' , 'y ' );
856- $ x_count_parts = count ($ x_array ) - 1 ;
857- $ x_display = $ x ;
858- $ x_display = $ x_array [0 ] . ((int ) $ x_array [1 ][0 ] !== 0 ? '. ' . $ x_array [1 ][0 ] : '' );
859-
860- // if amount in array
861- if (in_array ($ x_count_parts - 1 , array_keys ($ x_parts ))){
862- $ x_display .= $ x_parts [$ x_count_parts - 1 ];
863- }
864- return $ x_display ;
859+ if ($ number < 1000 ) {
860+ return $ number ; // Return the number as is if it's less than 1000.
865861 }
866862
867- return $ number ;
863+ // Define suffixes for each magnitude
864+ $ suffixes = self ::$ suffixes ;
865+
866+ // Calculate the magnitude
867+ $ magnitude = floor (log ($ number , 1000 )); // Find the magnitude in powers of 1000
868+ $ suffix = $ suffixes [$ magnitude ] ?? '' ; // Use suffix if it exists, otherwise empty
869+
870+ // Scale the number down to the appropriate magnitude
871+ $ scaledNumber = $ number / pow (1000 , $ magnitude );
872+
873+ // Keep one decimal place only if the scaled number has significant decimals
874+ $ formattedNumber = floor ($ scaledNumber * 10 ) / 10 ;
875+
876+ return "{$ formattedNumber }{$ suffix }" ;
868877 }
869878
870879 /**
@@ -883,20 +892,20 @@ static public function exists($path = null)
883892 /**
884893 * Unlink File from Server
885894 *
886- * @param string $pathToFile
895+ * @param string $file
887896 * - [full path to file is required]
888897 *
889- * @param string|null $fileName
898+ * @param string|null $restrictedfileName
890899 * - [optional] file name. <avatar.png>
891900 *
892901 * @return void
893902 */
894- static public function unlink (string $ pathToFile , $ fileName = null )
903+ static public function unlink (string $ file , $ restrictedfileName = null )
895904 {
896- $ fullPath = self ::stringReplacer ($ pathToFile );
905+ $ fullPath = self ::stringReplacer ($ file );
897906
898907 if (self ::exists ($ fullPath )){
899- if (basename ($ fullPath ) != basename ((string ) $ fileName )){
908+ if (basename ($ fullPath ) != basename ((string ) $ restrictedfileName )){
900909 @unlink ($ fullPath );
901910 }
902911 }
0 commit comments