Skip to content

Commit ff070ea

Browse files
UPDATE
1 parent 8009af1 commit ff070ea

File tree

6 files changed

+232
-60
lines changed

6 files changed

+232
-60
lines changed

README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ Support Package For Tamedevelopers
77
* [Installation](#installation)
88
* [All Paths](#all-paths)
99
* [Number to Words](#number-to-words)
10+
* [Tame](#tame)
11+
* [byteToUnit](#byteToUnit)
12+
* [sizeToBytes](#sizeToBytes)
13+
* [fileTime](#fileTime)
14+
* [exists](#exists)
15+
* [unlink](#unlink)
16+
* [calPercentageBetweenNumbers](#calPercentageBetweenNumbers)
17+
* [formatNumberToNearestThousand](#formatNumberToNearestThousand)
18+
* [calculateVolumeWeight](#calculateVolumeWeight)
19+
* [calculateCubicMeterWeight](#calculateCubicMeterWeight)
20+
* [getBetweenBoxLengthAndWeightInKg](#getBetweenBoxLengthAndWeightInKg)
21+
* [getBetweenBoxLengthAndWeightInCMB](#getBetweenBoxLengthAndWeightInCMB)
1022
* [Zip](#zip)
1123
* [Unzip](#unzip)
1224
* [Zip Download](#zip-download)
@@ -120,6 +132,80 @@ NumberToWords::value('twelve million three hundred thousand, six hundred and nin
120132
// 12300000.698
121133
```
122134

135+
## Tame
136+
- The Core Class of Components
137+
- It's helper class can be called as -- `Tame()`
138+
139+
```
140+
Tamedevelopers\Support\Tame
141+
142+
Tame::fileTime('absolute_path_to_file');
143+
```
144+
145+
### byteToUnit
146+
- Accepts 5 param. first param alone is needed
147+
- All other params are [optional]
148+
149+
| Params | Description |
150+
|-----------|-----------------------------------------------|
151+
| bytes | The size in bytes to be converted |
152+
| format | Whether to preserve case (default: lowercase) |
153+
| gb | Custom label for GB (default: 'GB') |
154+
| mb | Custom label for MB (default: 'MB') |
155+
| kb | Custom label for KB (default: 'KB') |
156+
157+
```
158+
Tame()->byteToUnit(6880);
159+
160+
// 7kb
161+
```
162+
163+
### sizeToBytes
164+
165+
```
166+
Tame()->sizeToBytes('24mb');
167+
168+
// 25165824
169+
```
170+
171+
### fileTime
172+
- Returns last edited time of the file as an - `int|false`
173+
174+
```
175+
Tame()->fileTime(base_path('filepath.php'));
176+
```
177+
178+
### exists
179+
- Checks if a file exists and is not a directory - `bool`
180+
181+
```
182+
Tame()->exists(base_path('filepath.php'));
183+
// Output: true or false
184+
```
185+
186+
### unlink
187+
- Deletes a file from the server if it exists and does not match the restricted file name - `void`
188+
- [optional] second param <filename.extension>
189+
190+
```
191+
Tame()->unlink(base_path('path/to/directory/avatar.png'), 'default.png');
192+
```
193+
194+
### calPercentageBetweenNumbers
195+
- Calculates the percentage relationship between two numbers as an - `int`
196+
197+
```
198+
Tame()->calPercentageBetweenNumbers(100, 80);
199+
```
200+
201+
### formatNumberToNearestThousand
202+
- Formats a number to its nearest thousand, million, billion, or higher as a - `string|float|int`
203+
204+
```
205+
Tame()->formatNumberToNearestThousand(1500000);
206+
// Output: "1.5m"
207+
```
208+
123209
## Zip
124210
- Takes two param as `string`
125211
- [sourcePath] relative path of zip-file

Tame.php

Lines changed: 67 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Tamedevelopers\Support\Str;
88
use Tamedevelopers\Support\Server;
99
use Tamedevelopers\Support\Traits\TameTrait;
10+
use Tamedevelopers\Support\Traits\NumberToWordsTraits;
1011

1112
/**
1213
* @see \Tamedevelopers\Support\Str
@@ -15,7 +16,8 @@
1516
*/
1617
class 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
}

Tests/numberToWords.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
// NumberToWords::cents(true)->iso('nga')->value('4531232221205435349345443534.21')->toText(),
1010

11-
NumberToWords::iso('FRA')->cents(true)->value('1,000,000,000,000,000,057,857,959,942,726,969,827,393,378,689,175,040,438,172,647,424')->toText(),
11+
// NumberToWords::iso('FRA')->cents(true)->value('1000000000000000057857959942726969827393378689175040438172647424')->toText(),
1212

1313
NumberToWords::iso('FRA')->cents(true)->value(34590323.231)->toText(),
1414

Tests/tame.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
use Tamedevelopers\Support\Time;
4+
5+
require_once __DIR__ . '/../vendor/autoload.php';
6+
7+
8+
// Tame()->HeadersSent();
9+
10+
$volume = [
11+
1 => [
12+
'length' => 20,
13+
'width' => 10,
14+
'height' => 40,
15+
],
16+
2 => [
17+
'length' => 120,
18+
'width' => 87,
19+
'height' => 450,
20+
],
21+
];
22+
23+
$VolumeWeight = Tame()->calculateVolumeWeight($volume[1]['length'], $volume[1]['width'], $volume[1]['height'], false, 0.5);
24+
$CubicMeterWeight = Tame()->calculateCubicMeterWeight($volume[2]['length'], $volume[2]['width'], $volume[2]['height'], false, 0.1);
25+
26+
dd(
27+
Tame()->countDivisibleNumbers(100, 680),
28+
29+
Tame()->byteToUnit(6880),
30+
31+
Tame()->sizeToBytes('24mb'),
32+
33+
Tame()->fileTime(base_path('Tests/tame.php')),
34+
35+
'br',
36+
37+
$VolumeWeight,
38+
39+
$CubicMeterWeight,
40+
41+
Tame()->getBetweenBoxLengthAndWeightInKg(20, 10, 40, $VolumeWeight, true, 0.5),
42+
Tame()->getBetweenBoxLengthAndWeightInCMB(20, 10, 40, $CubicMeterWeight),
43+
44+
Tame()->kgToGrams(2.7),
45+
46+
Tame()->gramsToKg(2701.0),
47+
48+
Tame()->calPercentageBetweenNumbers(2701.0, 4320),
49+
50+
Tame()->formatNumberToNearestThousand(300196366636753),
51+
);

Tests/time.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
Time::getGlobalTimeZone(),
4545

4646
TameTime()->formatRange('1-14')->get(false, true),
47-
TameTime()->dateRange('0-40')->get(true, true),
47+
TameTime()->dateRange('0-40')->get(true),
4848

4949
// Time::allTimezone(),
5050

0 commit comments

Comments
 (0)