Skip to content

Commit 9aa383f

Browse files
committed
Add weight and length conversion functions
1 parent 626ccdc commit 9aa383f

File tree

2 files changed

+194
-0
lines changed

2 files changed

+194
-0
lines changed

Conversions/Lengthconversions.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
/**
3+
* Class for converting length between different units.
4+
*/
5+
class LengthConversions {
6+
/**
7+
* Validates input for conversion methods
8+
*
9+
* @param mixed $value The value to check
10+
* @param string $method The method name for error message
11+
* @throws InvalidArgumentException
12+
*/
13+
private static function validateInput($value, $method) {
14+
// Make sure we have a numeric value
15+
if (!is_numeric($value)) {
16+
throw new InvalidArgumentException("Invalid input for $method");
17+
}
18+
}
19+
20+
/**
21+
* Converts meters to kilometers.
22+
*
23+
* @param float $meters The length in meters.
24+
* @return float The equivalent length in kilometers.
25+
*/
26+
public static function mToKm($meters) {
27+
self::validateInput($meters, 'mToKm');
28+
return round($meters / 1000, 4);
29+
}
30+
31+
/**
32+
* Converts kilometers to meters.
33+
*
34+
* @param float $kilometers The length in kilometers.
35+
* @return float The equivalent length in meters.
36+
*/
37+
public static function kmToM($kilometers) {
38+
self::validateInput($kilometers, 'kmToM');
39+
return round($kilometers * 1000, 4);
40+
}
41+
42+
/**
43+
* Converts meters to miles.
44+
*
45+
* @param float $meters The length in meters.
46+
* @return float The equivalent length in miles.
47+
*/
48+
public static function mToMiles($meters) {
49+
self::validateInput($meters, 'mToMiles');
50+
return round($meters / 1609.34, 6);
51+
}
52+
53+
/**
54+
* Converts miles to meters.
55+
*
56+
* @param float $miles The length in miles.
57+
* @return float The equivalent length in meters.
58+
*/
59+
public static function milesToM($miles) {
60+
self::validateInput($miles, 'milesToM');
61+
return round($miles * 1609.34, 4);
62+
}
63+
64+
/**
65+
* Converts inches to centimeters.
66+
*
67+
* @param float $inches The length in inches.
68+
* @return float The equivalent length in centimeters.
69+
*/
70+
public static function inToCm($inches) {
71+
self::validateInput($inches, 'inToCm');
72+
return round($inches * 2.54, 4);
73+
}
74+
75+
/**
76+
* Converts centimeters to inches.
77+
*
78+
* @param float $centimeters The length in centimeters.
79+
* @return float The equivalent length in inches.
80+
*/
81+
public static function cmToIn($centimeters) {
82+
self::validateInput($centimeters, 'cmToIn');
83+
return round($centimeters / 2.54, 2);
84+
}
85+
86+
/**
87+
* Converts kilometers to miles.
88+
*
89+
* @param float $km The length in kilometers.
90+
* @return float The equivalent length in miles.
91+
*/
92+
public static function kmToMiles($km) {
93+
self::validateInput($km, 'kmToMiles');
94+
return round($km / 1.609, 5);
95+
}
96+
97+
/**
98+
* Converts miles to kilometers.
99+
*
100+
* @param float $miles The length in miles.
101+
* @return float The equivalent length in kilometers.
102+
*/
103+
public static function milesToKm($miles) {
104+
self::validateInput($miles, 'milesToKm');
105+
return round($miles * 1.609, 4);
106+
}
107+
}

Conversions/Weightconversions.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
/**
3+
* Class for converting weight between different units.
4+
*/
5+
class WeightConversions {
6+
/**
7+
* Validates input for conversion methods
8+
*
9+
* @param mixed $value The value to check
10+
* @param string $method The method name for error message
11+
* @throws InvalidArgumentException
12+
*/
13+
private static function validateInput($value, $method) {
14+
// Make sure we have a numeric value
15+
if (!is_numeric($value)) {
16+
throw new InvalidArgumentException("Invalid input for $method");
17+
}
18+
}
19+
20+
/**
21+
* Converts kilograms to pounds.
22+
*
23+
* @param float $kg The weight in kilograms.
24+
* @return float The equivalent weight in pounds.
25+
* @see https://en.wikipedia.org/wiki/Kilogram
26+
*/
27+
public static function kgToLbs($kg) {
28+
self::validateInput($kg, 'kgToLbs');
29+
return round($kg * 2.20462, 4);
30+
}
31+
32+
/**
33+
* Converts pounds to kilograms.
34+
*
35+
* @param float $lbs The weight in pounds.
36+
* @return float The equivalent weight in kilograms.
37+
* @see https://en.wikipedia.org/wiki/Pound_(mass)
38+
*/
39+
public static function lbsToKg($lbs) {
40+
self::validateInput($lbs, 'lbsToKg');
41+
return round($lbs / 2.20462, 4);
42+
}
43+
44+
/**
45+
* Converts grams to kilograms.
46+
*
47+
* @param float $grams The weight in grams.
48+
* @return float The equivalent weight in kilograms.
49+
*/
50+
public static function gToKg($grams) {
51+
self::validateInput($grams, 'gToKg');
52+
return round($grams / 1000, 4);
53+
}
54+
55+
/**
56+
* Converts kilograms to grams.
57+
*
58+
* @param float $kg The weight in kilograms.
59+
* @return float The equivalent weight in grams.
60+
*/
61+
public static function kgToG($kg) {
62+
self::validateInput($kg, 'kgToG');
63+
return round($kg * 1000, 4);
64+
}
65+
66+
/**
67+
* Converts ounces to pounds.
68+
*
69+
* @param float $oz The weight in ounces.
70+
* @return float The equivalent weight in pounds.
71+
*/
72+
public static function ozToLbs($oz) {
73+
self::validateInput($oz, 'ozToLbs');
74+
return round($oz / 16, 4);
75+
}
76+
77+
/**
78+
* Converts pounds to ounces.
79+
*
80+
* @param float $lbs The weight in pounds.
81+
* @return float The equivalent weight in ounces.
82+
*/
83+
public static function lbsToOz($lbs) {
84+
self::validateInput($lbs, 'lbsToOz');
85+
return round($lbs * 16, 4);
86+
}
87+
}

0 commit comments

Comments
 (0)